Skip to content

Commit

Permalink
Merge PR #2741 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by CarlosRoca13
  • Loading branch information
OCA-git-bot committed Feb 5, 2025
2 parents e0352c9 + 17b70a5 commit 977be07
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
32 changes: 31 additions & 1 deletion sale_order_product_assortment/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2020 Tecnativa - Carlos Roca
# Copyright 2023 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.osv import expression


Expand Down Expand Up @@ -36,3 +38,31 @@ def _compute_product_assortment_ids(self):
product_domain
)
self.has_allowed_products = True

@api.onchange("partner_id", "partner_shipping_id", "partner_invoice_id")
def _onchange_partner_id(self):
"""
Check if all the products in the order lines
contains products that are allowed for the partner
"""
for order in self:
if order.has_allowed_products:
product_ids = order.order_line.mapped("product_id")
products_set = set(product_ids.ids)
allowed_products_set = set(order.allowed_product_ids.ids)
if not products_set <= allowed_products_set:
products_not_allowed_set = products_set - allowed_products_set
raise UserError(
_(
"This SO contains one or more products "
"that are not allowed for partner %(partner)s:\n- %(products)s"
)
% {
"partner": order.partner_id.name,
"products": "\n- ".join(
self.env["product.product"]
.browse(products_not_allowed_set)
.mapped("name")
),
}
)
2 changes: 2 additions & 0 deletions sale_order_product_assortment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
* `Ooops404 <https://www.ooops404.com>`_:

* Ilyas

* PyTech SRL <[email protected]>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2020 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError
from odoo.tests.common import Form, TransactionCase


class TestProductAssortment(TransactionCase):
Expand All @@ -27,7 +28,21 @@ def test_sale_order_product_assortment(self):
"whitelist_product_ids": [(4, product_1.id)],
}
)
sale_order_1 = self.sale_order_obj.create({"partner_id": self.partner_1.id})
sale_order_1 = self.sale_order_obj.create(
{
"partner_id": self.partner_1.id,
"order_line": [
(
0,
0,
{
"product_id": product_1.id,
},
)
],
}
)

self.assertEqual(
sale_order_1.allowed_product_ids,
assortment_with_whitelist.whitelist_product_ids,
Expand All @@ -46,10 +61,33 @@ def test_sale_order_product_assortment(self):
"black_list_product_domain": [("id", "=", product_3.id)],
}
)
sale_order_2 = self.sale_order_obj.create({"partner_id": self.partner_1.id})
sale_order_2 = self.sale_order_obj.create(
{
"partner_id": self.partner_1.id,
"order_line": [
(
0,
0,
{
"product_id": product_1.id,
},
)
],
}
)
self.assertNotIn(product_2, sale_order_2.allowed_product_ids)
self.assertTrue(sale_order_2.has_allowed_products)
sale_order_3 = self.sale_order_obj.create({"partner_id": self.partner_2.id})
self.assertTrue(sale_order_3.has_allowed_products)
self.assertNotIn(product_2, sale_order_3.allowed_product_ids)
self.assertNotIn(product_3, sale_order_3.allowed_product_ids)

so = Form(self.env["sale.order"])
with so.order_line.new() as line:
line.product_id = product_2

# Changing the partner while having an order line
# with a product that doesn't belong to the partner
# should raise an error
with self.assertRaises(UserError):
so.partner_id = self.partner_1

0 comments on commit 977be07

Please sign in to comment.