[FIX] sale_margin, delivery: margin at creation

The `purchase_price` is filled in automatically thanks to the onchange
method `product_id_change_margin`. However, when a SO is created
programatically, it is necessary to call manually the method in order to
fill in the field. This is not convenient since the module sale_margin
might not be installed, and crappy workaround (such as the use of
`hasattr`) are necessary.

To avoid this, this fix automatically computes the purchase price at
creation if no value is provided.
This commit is contained in:
Nicolas Martinelli
2016-02-25 15:46:09 +01:00
parent 67edccbc7f
commit 16f698f6be
3 changed files with 29 additions and 19 deletions
-5
View File
@@ -93,11 +93,6 @@ class SaleOrder(models.Model):
if self.order_line:
values['sequence'] = self.order_line[-1].sequence + 1
sol = SaleOrderLine.create(values)
if hasattr(sol, 'product_id_change_margin'):
# if `sale_margin` module is installed, force computation of field `purchase_price`
# As there is not dependency between modules (nor a bridge module), this is the only
# place we can get the value for this field
sol.product_id_change_margin()
return sol
+27 -12
View File
@@ -7,20 +7,35 @@ import openerp.addons.decimal_precision as dp
class sale_order_line(osv.osv):
_inherit = "sale.order.line"
@api.multi
def _compute_margin(self, order_id, product_id, product_uom_id):
frm_cur = self.env.user.company_id.currency_id
to_cur = order_id.pricelist_id.currency_id
purchase_price = product_id.standard_price
if product_uom_id != product_id.uom_id:
purchase_price = self.env['product.uom']._compute_price(product_id.uom_id.id, purchase_price, to_uom_id=product_uom_id.id)
ctx = self.env.context.copy()
ctx['date'] = order_id.date_order
price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False)
return price
@api.onchange('product_id', 'product_uom')
def product_id_change_margin(self):
for line in self:
if line.order_id.pricelist_id:
frm_cur = self.env.user.company_id.currency_id
to_cur = line.order_id.pricelist_id.currency_id
purchase_price = line.product_id.standard_price
if line.product_uom != line.product_id.uom_id:
purchase_price = self.env['product.uom']._compute_price(line.product_id.uom_id.id, purchase_price, to_uom_id=line.product_uom.id)
ctx = self.env.context.copy()
ctx['date'] = line.order_id.date_order
price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False)
line.purchase_price = price
if not self.order_id.pricelist_id or not self.product_id or not self.product_uom:
return
self.purchase_price = self._compute_margin(self.order_id, self.product_id, self.product_uom)
@api.model
def create(self, vals):
# Calculation of the margin for programmatic creation of a SO line. It is therefore not
# necessary to call product_id_change_margin manually
if 'purchase_price' not in vals:
order_id = self.env['sale.order'].browse(vals['order_id'])
product_id = self.env['product.product'].browse(vals['product_id'])
product_uom_id = self.env['product.uom'].browse(vals['product_uom'])
vals['purchase_price'] = self._compute_margin(order_id, product_id, product_uom_id)
return super(sale_order_line, self).create(vals)
def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
cur_obj = self.pool.get('res.currency')
+2 -2
View File
@@ -48,7 +48,7 @@ class TestMoveExplode(common.TransactionCase):
'partner_shipping_id': self.partner.id,
'pricelist_id': self.pricelist.id,
}
self.so = self.SaleOrder.create(vals=so_vals)
self.so = self.SaleOrder.create(so_vals)
sol_vals = {
'order_id': self.so.id,
'name': self.product_bom.name,
@@ -56,7 +56,7 @@ class TestMoveExplode(common.TransactionCase):
'product_uom': self.product_bom.uom_id.id,
'product_uom_qty': 1.0,
}
self.SaleOrderLine.create(values=sol_vals)
self.SaleOrderLine.create(sol_vals)
#confirm sale order
self.so.action_confirm()
#get all move associated to that sale_order