diff --git a/sale_purchase_force_vendor/models/sale_order_line.py b/sale_purchase_force_vendor/models/sale_order_line.py index 529228eb62d..3a44c4b6aef 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.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. @@ -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..f4d535c12da 100644 --- a/sale_purchase_force_vendor/tests/common.py +++ b/sale_purchase_force_vendor/tests/common.py @@ -1,10 +1,12 @@ -# 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() @@ -32,6 +34,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..9e414f0b96d 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 @@ -17,14 +17,17 @@ def test_misc(self): 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, [])