Skip to content

Commit

Permalink
[FIX] purchase_stock: vendor code
Browse files Browse the repository at this point in the history
- 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#62037

X-original-commit: 0b138ff
Signed-off-by: Nicolas Martinelli (nim) <[email protected]>
  • Loading branch information
nim-odoo committed Nov 19, 2020
1 parent 24071ff commit 0329217
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
29 changes: 25 additions & 4 deletions addons/purchase_stock/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand Down
18 changes: 15 additions & 3 deletions addons/purchase_stock/tests/test_purchase_lead_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion addons/purchase_stock/tests/test_reordering_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0329217

Please sign in to comment.