From 0c10e083ec9459b4d07c795f1cc7577d5974e8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Thu, 22 Aug 2024 13:21:54 +0200 Subject: [PATCH] [IMP] sale_purchase_force_vendor: Child vendor compatibility TT46573 --- .../models/sale_order_line.py | 24 ++++++++++-------- sale_purchase_force_vendor/tests/common.py | 18 ++++++++++--- .../tests/test_sale_purchase_force_vendor.py | 25 +++++++++++++------ 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/sale_purchase_force_vendor/models/sale_order_line.py b/sale_purchase_force_vendor/models/sale_order_line.py index 529228eb62d..c7603475745 100644 --- a/sale_purchase_force_vendor/models/sale_order_line.py +++ b/sale_purchase_force_vendor/models/sale_order_line.py @@ -1,4 +1,4 @@ -# Copyright 2022 Tecnativa - Víctor Martínez +# Copyright 2022-2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import api, fields, models @@ -35,6 +35,17 @@ def _compute_vendor_id_domain(self): ) item.vendor_id_domain = domain + def _prepare_force_vendor_product_supplierinfo_vals(self): + """We use this method so that we can overwrite it if we need to modify or + add a value. + """ + return { + "product_tmpl_id": self.product_id.product_tmpl_id.id, + "partner_id": self.vendor_id.commercial_partner_id.id, + "min_qty": 0, + "company_id": self.company_id.id, + } + def _prepare_procurement_values(self, group_id=False): """Inject in the procurement values the preferred vendor if any, and create supplierinfo record for it if it doesn't exist. @@ -43,7 +54,7 @@ def _prepare_procurement_values(self, group_id=False): if self.vendor_id: product = self.product_id suppinfo = product.with_company(self.company_id.id)._select_seller( - partner_id=self.vendor_id, + partner_id=self.vendor_id.commercial_partner_id, quantity=self.product_uom_qty, uom_id=self.product_uom, ) @@ -53,14 +64,7 @@ def _prepare_procurement_values(self, group_id=False): suppinfo = ( self.env["product.supplierinfo"] .sudo() - .create( - { - "product_tmpl_id": product.product_tmpl_id.id, - "partner_id": self.vendor_id.id, - "min_qty": 0, - "company_id": self.company_id.id, - } - ) + .create(self._prepare_force_vendor_product_supplierinfo_vals()) ) res["supplierinfo_id"] = suppinfo return res diff --git a/sale_purchase_force_vendor/tests/common.py b/sale_purchase_force_vendor/tests/common.py index 54f64f2b124..8658130b0db 100644 --- a/sale_purchase_force_vendor/tests/common.py +++ b/sale_purchase_force_vendor/tests/common.py @@ -1,15 +1,24 @@ -# Copyright 2022 Tecnativa - Víctor Martínez +# Copyright 2022-2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.tests import Form, common +from odoo.tests import Form +from odoo.addons.base.tests.common import BaseCommon -class TestSalePurchaseForceVendorBase(common.TransactionCase): + +class TestSalePurchaseForceVendorBase(BaseCommon): @classmethod def setUpClass(cls): super().setUpClass() cls.partner = cls.env["res.partner"].create({"name": "Test partner"}) cls.vendor_a = cls.env["res.partner"].create({"name": "Test vendor A"}) + cls.vendor_child_a = cls.env["res.partner"].create( + { + "name": "Test vendor child A", + "type": "contact", + "parent_id": cls.vendor_a.id, + } + ) cls.vendor_b = cls.env["res.partner"].create({"name": "Test vendor B"}) cls.mto = cls.env.ref("stock.route_warehouse0_mto") cls.mto.active = True @@ -32,6 +41,9 @@ def setUpClass(cls): } ) cls.sale_order = cls._create_sale_order(cls) + order_lines = cls.sale_order.order_line + cls.sol_a = order_lines.filtered(lambda x: x.product_id == cls.product_a) + cls.sol_b = order_lines.filtered(lambda x: x.product_id == cls.product_b) def _create_sale_order(self): order_form = Form(self.env["sale.order"]) diff --git a/sale_purchase_force_vendor/tests/test_sale_purchase_force_vendor.py b/sale_purchase_force_vendor/tests/test_sale_purchase_force_vendor.py index 098b2f2312a..b401e129b59 100644 --- a/sale_purchase_force_vendor/tests/test_sale_purchase_force_vendor.py +++ b/sale_purchase_force_vendor/tests/test_sale_purchase_force_vendor.py @@ -14,17 +14,28 @@ def test_misc(self): self.assertNotIn(self.vendor_a, self.product_b.seller_ids.mapped("partner_id")) self.assertIn(self.vendor_b, self.product_b.seller_ids.mapped("partner_id")) + def test_misc_child_vendor(self): + self.sale_order.order_line.vendor_id = self.vendor_child_a + self.sale_order.action_confirm() + self.assertNotIn( + self.vendor_child_a, self.product_a.seller_ids.mapped("partner_id") + ) + self.assertEqual(self.product_b.seller_ids.partner_id, self.vendor_a) + def test_misc_force_vendor_restrict(self): self.env.company.sale_purchase_force_vendor_restrict = True self.sale_order.action_confirm() - line_0 = self.sale_order.order_line[0] - partners = self.env["res.partner"].search(line_0.vendor_id_domain) - self.assertNotIn(self.partner, partners) - self.assertIn(self.vendor_a, partners) - self.assertIn(self.vendor_b, partners) + partners_sol_a = self.env["res.partner"].search(self.sol_a.vendor_id_domain) + self.assertNotIn(self.partner, partners_sol_a) + self.assertIn(self.vendor_a, partners_sol_a) + self.assertIn(self.vendor_b, partners_sol_a) + partners_sol_b = self.env["res.partner"].search(self.sol_b.vendor_id_domain) + self.assertNotIn(self.partner, partners_sol_b) + self.assertNotIn(self.vendor_a, partners_sol_b) + self.assertIn(self.vendor_b, partners_sol_b) def test_misc_not_force_vendor_restrict(self): self.env.company.sale_purchase_force_vendor_restrict = False self.sale_order.action_confirm() - line_0 = self.sale_order.order_line[0] - self.assertEqual(line_0.vendor_id_domain, []) + self.assertEqual(self.sol_a.vendor_id_domain, []) + self.assertEqual(self.sol_b.vendor_id_domain, [])