This question has been flagged
2 Replies
6364 Views

Hello, I have Odoo 8 running over Win 7 x64.

I am developing a new module, I have created a class that inherit from product.template:

from openerp import fields, models

class Product(models.Model):
    _inherit = 'product.template'

    last_cost_price = fields.Float(digits=(8,2), string="Last cost price", readonly=True)

When the user confirms a PO, I want to read every line order and set the unit_price of this order_line in last_cost_price of the corresponding product.template.

Have I to override wkf_confirm_order() method of purchase_order class? In this case, how can I access to product.last_cost_price attribute to edit it?

Thanks!

Avatar
Discard
Best Answer

Odoo has average product calculation method that will update the cost price when there is incoming shipment completed.  But if you really want to fix the price by PO price, yes you have to inherit and override wkf_confirm_order of purchase.order.  You need to loop through all ids (which is purchase.order model) that is passed to wkf_confirm_order, then within that loop through all it's order_line (which is purchase.order.line model) and then from the line you can access the product_id field (which will be product.product model).  Then you can write to that product record.

Avatar
Discard
Author Best Answer

Thanks for your reply John. So have I to inherit purchase.order class and override wkf_confirm_order method? Can you help me with some example python code to search the corresponding product and edit them? Thanks!

EDIT:

I have overrided the method, but I get an error when confirm the PO:

ValueError: "can't adapt type 'product.product'" while evaluating u'wkf_confirm_order()'

in the line marked with bold.

class purchase_order(models.Model):
    _inherit = 'purchase.order'
    
    def wkf_confirm_order(self, cr, uid, ids, context=None):
        for po in self.browse(cr, uid, ids, context=context):
            for line in po.order_line:
                product_tmpl_id = self.pool.get('product.product').browse(cr, uid, line.product_id, context).product_tmpl_id
                product_tmpl = self.pool.get('product.template').browse(cr, uid, product_tmpl_id, context)
                #... write the last_price_cost in the product.template object
                
        return super(purchase_order, self).wkf_confirm_order(cr, uid, ids, context)

 

Avatar
Discard

Not sure whether you have stock module installed or not, but there are some samples there: addons/stock/wizard/stock_change_standard_price.py which call another method in product provided by stock module do_change_standard_price addons/stock/product.py. The update happens in do_change_standard_price.

Author

I have the module installed, but I don't find the stock_change_standard_price.py file. In that folder, I have 6 .py files: make_procurement_product, orderpoint_procurement, stock_change_product_qty, stock_move, stock_return_picking, stock_transfer_details

Sorry, the module has been moved to stock_account in 8.0. So it is addons/stock_account/wizard/stock_change_standard_price.py and addons/stock_account/product.py.

Author

I edited my previous answer, please check that. Thanks!

It's because line.product_id is a browse_record object whlist the browse needed ids which is integer (or list of integers). But in fact you don't need to do that because product_tmpl = line.product_id.product_tmpl_id. In fact product_tmpl_id = line.product_id.product_tmpl_id.id.

Author

Thank you, my question is solved! The instruction for writing is: product_tmpl_obj = self.pool.get('product.template') // product_tmpl_obj.write(cr, uid, [product_tmpl_id], {'ultimo_precio_costo':line.price_unit}, context=None). Bye!