Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] sale_purchase_force_vendor: Add method for values to be used in product.supplierinfo record #2395

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions sale_purchase_force_vendor/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
11 changes: 8 additions & 3 deletions sale_purchase_force_vendor/tests/common.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Loading