[FIX] purchase: trigger onchange when po is import.

purpose of this commit is when we import po with po lines
system expects from user to import all those requried fields manually.

Using sale's hack 'prepare_add_missing_fields' fix this issue.

Now we can import po lines in purchase too.

Task- 2093205
closes- #41681
This commit is contained in:
nje-odoo
2020-02-06 10:18:34 +00:00
committed by Simon Lejeune
parent c98579d25a
commit b883041377
+26 -14
View File
@@ -563,21 +563,20 @@ class PurchaseOrderLine(models.Model):
else:
line.qty_received_manual = 0.0
@api.model
def create(self, values):
if values.get('display_type', self.default_get(['display_type'])['display_type']):
values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, date_planned=False)
@api.model_create_multi
def create(self, vals_list):
for values in vals_list:
if values.get('display_type', self.default_get(['display_type'])['display_type']):
values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, date_planned=False)
else:
values.update(self._prepare_add_missing_fields(values))
order_id = values.get('order_id')
if 'date_planned' not in values:
order = self.env['purchase.order'].browse(order_id)
if order.date_planned:
values['date_planned'] = order.date_planned
line = super(PurchaseOrderLine, self).create(values)
if line.order_id.state == 'purchase':
msg = _("Extra line with %s ") % (line.product_id.display_name,)
line.order_id.message_post(body=msg)
return line
lines = super().create(vals_list)
for line in lines:
if line.product_id and line.order_id.state == 'purchase':
msg = _("Extra line with %s ") % (line.product_id.display_name,)
line.order_id.message_post(body=msg)
return lines
def write(self, values):
if 'display_type' in values and self.filtered(lambda line: line.display_type != values.get('display_type')):
@@ -756,6 +755,19 @@ class PurchaseOrderLine(models.Model):
'display_type': self.display_type,
}
@api.model
def _prepare_add_missing_fields(self, values):
""" Deduce missing required fields from the onchange """
res = {}
onchange_fields = ['name', 'price_unit', 'product_qty', 'product_uom', 'taxes_id', 'date_planned']
if values.get('order_id') and values.get('product_id') and any(f not in values for f in onchange_fields):
line = self.new(values)
line.onchange_product_id()
for field in onchange_fields:
if field not in values:
res[field] = line._fields[field].convert_to_write(line[field], line)
return res
@api.model
def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, supplier, po):
partner = supplier.name