From 0329217db5528341f73c349edc595c9ab5a30a7a Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Wed, 18 Nov 2020 10:22:20 +0000 Subject: [PATCH] [FIX] purchase_stock: vendor code - Create a product P with a MTO Reordering Rule (Buy Route) - Add a supplier to P with a specific vendor code - Create a SO for 1 unit of P, validate A PO is created for the supplier but without using the vendor code. It happens because the vendor code is overridden by the product description. To avoid losing information and redundancy, we add the line description only if different from the product name. opw-2383418 closes odoo/odoo#62037 X-original-commit: 0b138ffa5a1b2b03f03fb5dc51994a3bfc4e5dea Signed-off-by: Nicolas Martinelli (nim) --- addons/purchase_stock/models/purchase.py | 29 ++++++++++++++++--- .../tests/test_purchase_lead_time.py | 18 ++++++++++-- .../tests/test_reordering_rule.py | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/addons/purchase_stock/models/purchase.py b/addons/purchase_stock/models/purchase.py index 01971b208466c..ae3e32c169f8e 100644 --- a/addons/purchase_stock/models/purchase.py +++ b/addons/purchase_stock/models/purchase.py @@ -475,7 +475,11 @@ def _prepare_purchase_order_line_from_procurement(self, product_id, product_qty, line_description += values['product_description_variants'] supplier = values.get('supplier') res = self._prepare_purchase_order_line(product_id, product_qty, product_uom, company_id, supplier, po) - res['name'] = line_description + # We need to keep the vendor name set in _prepare_purchase_order_line. To avoid redundancy + # in the line name, we add the line_description only if different from the product name. + # This way, we shoud not lose any valuable information. + if product_id.name != line_description: + res['name'] += '\n' + line_description res['move_dest_ids'] = [(4, x.id) for x in values.get('move_dest_ids', [])] res['orderpoint_id'] = values.get('orderpoint_id', False) and values.get('orderpoint_id').id res['propagate_cancel'] = values.get('propagate_cancel') @@ -500,9 +504,26 @@ def _find_candidate(self, product_id, product_qty, product_uom, location_id, nam if values.get('product_description_variants'): description_picking += values['product_description_variants'] lines = self.filtered( - lambda l: l.propagate_cancel == values['propagate_cancel'] and - ((values['orderpoint_id'] and not values['move_dest_ids']) and l.orderpoint_id == values['orderpoint_id'] or True) and - (not values.get('product_description_variants') or l.name == description_picking)) + lambda l: l.propagate_cancel == values['propagate_cancel'] + and ((values['orderpoint_id'] and not values['move_dest_ids']) and l.orderpoint_id == values['orderpoint_id'] or True) + ) + + # In case 'product_description_variants' is in the values, we also filter on the PO line + # name. This way, we can merge lines with the same description. To do so, we need the + # product name in the context of the PO partner. + if lines and values.get('product_description_variants'): + partner = self.mapped('order_id.partner_id')[:1] + product_lang = product_id.with_context( + lang=partner.lang, + partner_id=partner.id, + ) + name = product_lang.display_name + if product_lang.description_purchase: + name += '\n' + product_lang.description_purchase + lines = lines.filtered(lambda l: l.name == name + '\n' + description_picking) + if lines: + return lines[0] + return lines and lines[0] or self.env['purchase.order.line'] def _get_outgoing_incoming_moves(self): diff --git a/addons/purchase_stock/tests/test_purchase_lead_time.py b/addons/purchase_stock/tests/test_purchase_lead_time.py index b15bb9741ea36..01b936ef17954 100644 --- a/addons/purchase_stock/tests/test_purchase_lead_time.py +++ b/addons/purchase_stock/tests/test_purchase_lead_time.py @@ -213,6 +213,18 @@ def test_merge_po_line_3(self): """Change merging po line if same procurement is done depending on custom values.""" company = self.env.ref('base.main_company') company.write({'po_lead': 0.00}) + + # The seller has a specific product name and code which must be kept in the PO line + self.t_shirt.seller_ids.write({ + 'product_name': 'Vendor Name', + 'product_code': 'Vendor Code', + }) + partner = self.t_shirt.seller_ids[:1].name + t_shirt = self.t_shirt.with_context( + lang=partner.lang, + partner_id=partner.id, + ) + # Create procurement order of product_1 ProcurementGroup = self.env['procurement.group'] procurement_values = { @@ -232,7 +244,7 @@ def test_merge_po_line_3(self): purchase_order = self.env['purchase.order.line'].search([('product_id', '=', self.t_shirt.id)], limit=1).order_id order_line_description = purchase_order.order_line.product_id._get_description(purchase_order.picking_type_id) self.assertEqual(len(purchase_order.order_line), 1, 'wrong number of order line is created') - self.assertEqual(purchase_order.order_line.name, order_line_description + "Color (Red)", 'wrong description in po lines') + self.assertEqual(purchase_order.order_line.name, t_shirt.display_name + "\n" + order_line_description + "Color (Red)", 'wrong description in po lines') procurement_values['product_description_variants'] = 'Color (Red)' order_2_values = procurement_values @@ -252,8 +264,8 @@ def test_merge_po_line_3(self): self.t_shirt.name, '/', self.env.company, order_3_values) ]) self.assertEqual(len(purchase_order.order_line), 2, 'line with different custom value should not be merged') - self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 15).name, order_line_description + "Color (Red)", 'wrong description in po lines') - self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 10).name, order_line_description + "Color (Green)", 'wrong description in po lines') + self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 15).name, t_shirt.display_name + "\n" + order_line_description + "Color (Red)", 'wrong description in po lines') + self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 10).name, t_shirt.display_name + "\n" + order_line_description + "Color (Green)", 'wrong description in po lines') purchase_order.button_confirm() self.assertEqual(purchase_order.picking_ids[0].move_ids_without_package.filtered(lambda x: x.product_uom_qty == 15).description_picking, order_line_description + "Color (Red)", 'wrong description in picking') diff --git a/addons/purchase_stock/tests/test_reordering_rule.py b/addons/purchase_stock/tests/test_reordering_rule.py index dd1176e5016c5..74406ea8deb94 100644 --- a/addons/purchase_stock/tests/test_reordering_rule.py +++ b/addons/purchase_stock/tests/test_reordering_rule.py @@ -515,4 +515,4 @@ def test_procure_multi_lingual(self): po_line = self.env["purchase.order.line"].search( [("product_id", "=", product.id)]) self.assertTrue(po_line) - self.assertEqual("product TEST", po_line.name) + self.assertEqual("[A] product TEST", po_line.name)