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

[15.0] [FW-PORT] sale_order_product_assortment: Check products on write and create #2741

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
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
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 <info@pytech.it>
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
Loading