From c4ccee31c7be6d74b281905a3830083c8288c45e Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Thu, 19 Aug 2021 10:00:19 +0000 Subject: [PATCH] [FIX] purchase_stock : prevent wrong destination in PO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - create 2 warehouses A & B - create a new product and add a seller - Create an Automated replenishment order of the created product for warehouse A - click on “Automate orders” - open PO - in “other information” tab > change the picking type to the “warehouse B” picking type - confirm order - click on the receipt Problem: The destination location is “Warehouse A” instead of “Warehouse B” The problem also appears after the duplication. Solution: Use the parent_path to check if the location that the user wants to use is below the original warehouse. opw-2588082 closes odoo/odoo#75501 X-original-commit: bed546486aa667d735df015de8153d42a9af9a41 Signed-off-by: William Henrotin Signed-off-by: Djamel Touati Co-authored-by: fmdl --- addons/purchase_stock/i18n/purchase_stock.pot | 9 +++++++++ addons/purchase_stock/models/purchase.py | 9 ++++++++- addons/purchase_stock/tests/test_reordering_rule.py | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/addons/purchase_stock/i18n/purchase_stock.pot b/addons/purchase_stock/i18n/purchase_stock.pot index 9dfa2a2a7fe..d2fb4ea04ea 100644 --- a/addons/purchase_stock/i18n/purchase_stock.pot +++ b/addons/purchase_stock/i18n/purchase_stock.pot @@ -228,6 +228,15 @@ msgstr "" msgid "Exception(s):" msgstr "" +#. module: purchase_stock +#: code:addons/purchase_stock/models/purchase.py:0 +#, python-format +msgid "" +"For the product %s, the warehouse of the operation type (%s) is inconsistent" +" with the location (%s) of the reordering rule (%s). Change the operation " +"type or cancel the request for quotation." +msgstr "" + #. module: purchase_stock #: model:ir.model.fields,field_description:purchase_stock.field_purchase_order_line__forecasted_issue msgid "Forecasted Issue" diff --git a/addons/purchase_stock/models/purchase.py b/addons/purchase_stock/models/purchase.py index 15aa91778ae..23737273c4a 100644 --- a/addons/purchase_stock/models/purchase.py +++ b/addons/purchase_stock/models/purchase.py @@ -274,7 +274,7 @@ class PurchaseOrderLine(models.Model): qty_received_method = fields.Selection(selection_add=[('stock_moves', 'Stock Moves')]) move_ids = fields.One2many('stock.move', 'purchase_line_id', string='Reservation', readonly=True, copy=False) - orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Orderpoint') + orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Orderpoint', copy=False) move_dest_ids = fields.One2many('stock.move', 'created_purchase_line_id', 'Downstream Moves') product_description_variants = fields.Char('Custom Description') propagate_cancel = fields.Boolean('Propagate cancellation', default=True) @@ -468,8 +468,15 @@ class PurchaseOrderLine(models.Model): res.append(extra_move_vals) return res + def _check_orderpoint_picking_type(self): + warehouse_loc = self.order_id.picking_type_id.warehouse_id.view_location_id + if self.orderpoint_id and not warehouse_loc.parent_path in self.orderpoint_id.location_id.parent_path: + raise UserError(_('For the product %s, the warehouse of the operation type (%s) is inconsistent with the location (%s) of the reordering rule (%s). Change the operation type or cancel the request for quotation.', + self.product_id.display_name, self.order_id.picking_type_id.display_name, self.orderpoint_id.location_id.display_name, self.orderpoint_id.display_name)) + def _prepare_stock_move_vals(self, picking, price_unit, product_uom_qty, product_uom): self.ensure_one() + self._check_orderpoint_picking_type() product = self.product_id.with_context(lang=self.order_id.dest_address_id.lang or self.env.user.lang) date_planned = self.date_planned or self.order_id.date_planned return { diff --git a/addons/purchase_stock/tests/test_reordering_rule.py b/addons/purchase_stock/tests/test_reordering_rule.py index b5f7d40bb4b..e7c404f51db 100644 --- a/addons/purchase_stock/tests/test_reordering_rule.py +++ b/addons/purchase_stock/tests/test_reordering_rule.py @@ -7,6 +7,7 @@ from datetime import timedelta as td from odoo import SUPERUSER_ID from odoo.tests import Form from odoo.tests.common import TransactionCase +from odoo.exceptions import UserError class TestReorderingRule(TransactionCase): @@ -38,6 +39,7 @@ class TestReorderingRule(TransactionCase): """ warehouse_1 = self.env['stock.warehouse'].search([('company_id', '=', self.env.user.id)], limit=1) warehouse_1.write({'reception_steps': 'two_steps'}) + warehouse_2 = self.env['stock.warehouse'].create({'name': 'WH 2', 'code': 'WH2', 'company_id': self.env.company.id, 'partner_id': self.env.company.partner_id.id, 'reception_steps': 'one_step'}) # create reordering rule orderpoint_form = Form(self.env['stock.warehouse.orderpoint']) @@ -64,6 +66,12 @@ class TestReorderingRule(TransactionCase): purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner.id)]) self.assertTrue(purchase_order, 'No purchase order created.') + # Check the picking type on the purchase order + purchase_order.picking_type_id = warehouse_2.in_type_id + with self.assertRaises(UserError): + purchase_order.button_confirm() + purchase_order.picking_type_id = warehouse_1.in_type_id + # On the po generated, the source document should be the name of the reordering rule self.assertEqual(order_point.name, purchase_order.origin, 'Source document on purchase order should be the name of the reordering rule.') self.assertEqual(purchase_order.order_line.product_qty, 10)