From b88304137722f0f3794c4f7a91cecce09c4052ae Mon Sep 17 00:00:00 2001 From: nje-odoo Date: Wed, 11 Dec 2019 04:42:04 +0000 Subject: [PATCH] [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 --- addons/purchase/models/purchase.py | 40 +++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index a05edfedbf1..ffb917359d7 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -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