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

Update a field in model product.template when PO is confirmed

By
José
on 12/1/14, 3:58 PM 1,811 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!

1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/1/14, 7:41 PM

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.

0
José
On 12/1/14, 8:16 PM

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)

 

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.

Ivan
on 12/1/14, 8:52 PM

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

José
on 12/1/14, 9:50 PM

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.

Ivan
on 12/1/14, 10:16 PM

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

José
on 12/2/14, 8:31 AM

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.

Ivan
on 12/2/14, 9:09 AM

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!

José
on 12/2/14, 3:28 PM

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: 12/1/14, 3:58 PM
Seen: 1811 times
Last updated: 3/16/15, 8:10 AM