Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Error while inheriting an existing function in purchase order line ?

By
Dimple
on 10/31/14, 2:39 AM 1,299 views

Hai,

I wanted to add a new field named as brand in purchase order line,so i created a new module and also i want to see this field in the corresponding invoice. 

class purchase_order_line(osv.osv):

       
    _inherit ='purchase.order.line'
    _columns = {

                 'brand' : fields.text('Brand'),
                 
      }
                

    def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name='', price_unit=False, context=None):
       
        value = {}
        if not product_id:
            return value
        names = self.pool.get('product.product').browse(cr, uid,product_id, context=context)
        value.update({
                 'brand' : names.product_brand_id and names.product_brand_id.name or False,
                 'name' : names.product_brand_id and names.product_brand_id.name or False,
                 
#                  'sitelocation': names.sitelocation and names.sitelocation.id or False,                
#                  
            
        })
        return {'value' : value}

purchase_order_line()

class purchase_order(osv.osv):
    _inherit ='purchase.order' 
    
    def _prepare_inv_line(self, cr, uid, account_id, order_line, context=None):
        """Collects require data from purchase order line that is used to create invoice line
        for that purchase order line
        :param account_id: Expense account of the product of PO line if any.
        :param browse_record order_line: Purchase order line browse record
        :return: Value for fields of invoice lines.
        :rtype: dict
        """
        print 'helloooooooooooooooooooooooooooooooooooo',order_line.brand
        return {
            'name': order_line.name,
            'account_id': account_id,
            'price_unit': order_line.price_unit or 0.0,
            'quantity': order_line.product_qty,
            'product_id': order_line.product_id.id or False,
            'brand':order_line.brand,
            
            'uos_id': order_line.product_uom.id or False,
            'invoice_line_tax_id': [(6, 0, [x.id for x in order_line.taxes_id])],
            'account_analytic_id': order_line.account_analytic_id.id or False,
        }

    def action_invoice_create(self, cr, uid, ids, context=None):
        print 'haiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii'
        if context is None:
            context = {}
        journal_obj = self.pool.get('account.journal')
        inv_obj = self.pool.get('account.invoice')
        inv_line_obj = self.pool.get('account.invoice.line')
 
        res = False
        uid_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
         
        for order in self.browse(cr, uid, ids, context=context):         
            context.pop('force_company', None)
            if order.company_id.id != uid_company_id:
                #if the company of the document is different than the current user company, force the company in the context
                #then re-do a browse to read the property fields for the good company.
                context['force_company'] = order.company_id.id
                order = self.browse(cr, uid, order.id, context=context)
            pay_acc_id = order.partner_id.property_account_payable.id
            journal_ids = journal_obj.search(cr, uid, [('type', '=', 'purchase'), ('company_id', '=', order.company_id.id)], limit=1)
            if not journal_ids:
                raise osv.except_osv(_('Error!'),
                    _('Define purchase journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
            # generate invoice line correspond to PO line and link that to created invoice (inv_id) and PO line
            inv_lines = []
            for po_line in order.order_line:
                acc_id = self._choose_account_from_po_line(cr, uid, po_line, context=context)
                inv_line_data = self._prepare_inv_line(cr, uid, acc_id, po_line, context=context)
                inv_line_id = inv_line_obj.create(cr, uid, inv_line_data, context=context)
                inv_lines.append(inv_line_id)

                po_line.write({'invoice_lines': [(4, inv_line_id)]}, context=context)

            #get invoice data and create invoice
            inv_data = {
                'name': order.partner_ref or order.name,
                'reference': order.partner_ref or order.name,
                'account_id': pay_acc_id,
                'type': 'in_invoice',
                'partner_id': order.partner_id.id,
                'currency_id': order.pricelist_id.currency_id.id,
                'journal_id': len(journal_ids) and journal_ids[0] or False,
                'invoice_line': [(6, 0, inv_lines)],
                'origin': order.name,
                'fiscal_position': order.fiscal_position.id or False,
                'payment_term': order.payment_term_id.id or False,
                'company_id': order.company_id.id,
            }
            inv_id = inv_obj.create(cr, uid, inv_data, context=context)
 
            # compute the invoice
            inv_obj.button_compute(cr, uid, [inv_id], context=context, set_total=True)
 
            # Link this new invoice to related purchase order
            order.write({'invoice_ids': [(4, inv_id)]}, context=context)
            res = inv_id
            
        return res


    
    
purchase_order()
       
class invoice_line(osv.osv):

       
    _inherit ='account.invoice.line'
    _columns = {

                 'brand' : fields.char('Brand'),
                 
      }
    
invoice_line()    

By doing this above code i get the wanted result,but while creating a purchase order i didn't getting the unit price and subtotal values. How can i solve this problem ?

 

 

 

Updated code:

from openerp.osv import fields, osv

class purchase_order_line(osv.osv):

       
    _inherit ='purchase.order.line'
    _columns = {

                 'brand' : fields.text('Brand'),
                 
      }
                

    def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name='', price_unit=False, context=None):
       
        value = {}
        if not product_id:
            return value
        value = super(purchase_order_line,self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned, name, price_unit, context )


        names = self.pool.get('product.product').browse(cr, uid,product_id, context=context)
        value.update({
                'brand' : names.product_brand_id and names.product_brand_id.name or False,

        })
        return {'value' : value}

purchase_order_line()

When I updated like the above code it doesn't .show .the .Description field , .Scheduled Date field.Then what is wrong in my code.

0
rocky990011
On 9/18/15, 11:59 AM

same error........ i am in the same situation and i need to calculate total subtotal on that particular field and i get the same issue

my code is and i want to calculate total sub total from item_seller_price

from openerp.osv import osv, fields

class purchase_order_line(osv.osv):

_inherit ='purchase.order.line'

_columns = {

'item_seller_price' : fields.float('Item Seller Price'),

}

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):

res = {}

cur_obj=self.pool.get('res.currency')

for order in self.browse(cr, uid, ids, context=context):

res[order.id] = {

'amount_untaxed': 0.0,

'amount_tax': 0.0,

'amount_total': 0.0,

}

val = val1 = 0.0

cur = order.pricelist_id.currency_id

for line in order.order_line:

price_unit = line.price_unit

if line.item_seller_price != 0:

price_unit = line.item_seller_price

val1 += line.price_subtotal

for c in self.pool.get('account.tax').compute_all(cr, uid, line.taxes_id, line.price_unit, line.product_qty, line.product_id, order.partner_id)['taxes']:

val += c.get('amount', 0.0)

res[order.id]['amount_tax']=cur_obj.round(cr, uid, cur, val)

res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1)

res[order.id]['amount_total']=res[order.id]['amount_untaxed'] + res[order.id]['amount_tax']

return res

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,

partner_id, date_order=False, fiscal_position_id=False, date_planned=False,

name=False, price_unit=False, state='draft', context=None):

value = {}

if not product_id:

return value

value = super(purchase_order_line,self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned, name, price_unit, state, context )

names = self.pool.get('product.product').browse(cr, uid,product_id, context=context)

value.update({

'item_seller_price' : names.product_item_seller_price_id and names.product_item_seller_price_id.name or False,

'name' : names.product_item_seller_price_id and names.product_item_seller_price_id.name or False,

# 'sitelocation': names.sitelocation and names.sitelocation.id or False,

#

})

return {'value' : value}

#purchase_order_line()

class purchase_order(osv.osv):

_inherit ='purchase.order'

def _prepare_inv_line(self, cr, uid, account_id, order_line, context=None):

"""Collects require data from purchase order line that is used to create invoice line

for that purchase order line

:param account_id: Expense account of the product of PO line if any.

:param browse_record order_line: Purchase order line browse record

:return: Value for fields of invoice lines.

:rtype: dict

"""

return {

'name': order_line.name,

'account_id': account_id,

'price_unit': order_line.price_unit or 0.0,

'quantity': order_line.product_qty,

'product_id': order_line.product_id.id or False,

'item_seller_price':order_line.item_seller_price,

'uos_id': order_line.product_uom.id or False,

'invoice_line_tax_id': [(6, 0, [x.id for x in order_line.taxes_id])],

'account_analytic_id': order_line.account_analytic_id.id or False,

'purchase_line_id': order_line.id,

}

def action_invoice_create(self, cr, uid, ids, context=None):

"""Generates invoice for given ids of purchase orders and links that invoice ID to purchase order.

:param ids: list of ids of purchase orders.

:return: ID of created invoice.

:rtype: int

"""

context = dict(context or {})

inv_obj = self.pool.get('account.invoice')

inv_line_obj = self.pool.get('account.invoice.line')

res = False

uid_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id

for order in self.browse(cr, uid, ids, context=context):

context.pop('force_company', None)

if order.company_id.id != uid_company_id:

#if the company of the document is different than the current user company, force the company in the context

#then re-do a browse to read the property fields for the good company.

context['force_company'] = order.company_id.id

order = self.browse(cr, uid, order.id, context=context)

# generate invoice line correspond to PO line and link that to created invoice (inv_id) and PO line

inv_lines = []

for po_line in order.order_line:

if po_line.state == 'cancel':

continue

acc_id = self._choose_account_from_po_line(cr, uid, po_line, context=context)

inv_line_data = self._prepare_inv_line(cr, uid, acc_id, po_line, context=context)

inv_line_id = inv_line_obj.create(cr, uid, inv_line_data, context=context)

inv_lines.append(inv_line_id)

po_line.write({'invoice_lines': [(4, inv_line_id)]})

# get invoice data and create invoice

inv_data = self._prepare_invoice(cr, uid, order, inv_lines, context=context)

inv_id = inv_obj.create(cr, uid, inv_data, context=context)

# compute the invoice

inv_obj.button_compute(cr, uid, [inv_id], context=context, set_total=True)

# Link this new invoice to related purchase order

order.write({'invoice_ids': [(4, inv_id)]})

res = inv_id

return res

class invoice_line(osv.osv):

_inherit ='account.invoice.line'

_columns = {

'item_seller_price' : fields.float('item seller price'),

}

0

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 10/31/14, 3:16 AM

Hi,

your current on change method for product is :

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name='', price_unit=False, context=None):
       
        value = {}
        if not product_id:
            return value
        names = self.pool.get('product.product').browse(cr, uid,product_id, context=context)
        value.update({
                 'brand' : names.product_brand_id and names.product_brand_id.name or False,
                 'name' : names.product_brand_id and names.product_brand_id.name or False,
                 
#                  'sitelocation': names.sitelocation and names.sitelocation.id or False,                
#                  
            
        })
        return {'value' : value}

Now, here you left the calling of super method. method "onchange_product_id" is already define into default purchase module. So, in your code it is inherited from that module. You have to call super method to get all the values from it. So, your method should like below.

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name='', price_unit=False, context=None):
       
        value = {}
        if not product_id:
            return value
        value = super(purchase_order_line,self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned, name, price_unit, context )

        names = self.pool.get('product.product').browse(cr, uid,product_id, context=context)
        value.update({
                 'brand' : names.product_brand_id and names.product_brand_id.name or False,
                 'name' : names.product_brand_id and names.product_brand_id.name or False,
                 
#                  'sitelocation': names.sitelocation and names.sitelocation.id or False,                
#                  
            
        })
        return {'value' : value}

can u please check my updated question ?

Dimple
on 10/31/14, 4:15 AM

May I know that your module is depends on ? purchase or any other module ?

Emipro Technologies Pvt. Ltd.
on 10/31/14, 5:16 AM

only purchase

Dimple
on 10/31/14, 5:41 AM

Description : It is comes from the product -> Purchase Description. Schedule Date : It is comes from the passed argument or by calculated one function. So, you have to see on purchase description for description and on date_planned parameter for your schedule date. And also look on the onchange_product_id into purchase module. purchase => purchase.py

Emipro Technologies Pvt. Ltd.
on 10/31/14, 5:56 AM

did i want to give the value.update for this description and schedule date in this.py file and also i didn't getting the sub total value

Dimple
on 10/31/14, 6:10 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/31/14, 2:39 AM
Seen: 1299 times
Last updated: 9/18/15, 12:02 PM