Skip to content

Commit 5c73c98

Browse files
henrybackmansimahawk
authored andcommitted
sale_by_packaging: fix can_be_sold update on pkg type change
This shouldn't be required due to the compute method. However, we experienced some cases where nested form inside the product form fails to update the flag without this onchange.
1 parent da166f9 commit 5c73c98

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

sale_by_packaging/models/product_packaging.py

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def _compute_can_be_sold(self):
2626
for record in self:
2727
record.can_be_sold = record.packaging_type_id.can_be_sold
2828

29+
# This shouldn't be required due to the compute method.
30+
# However, we experienced some cases where nested form inside the product
31+
# form fails to update the flag without this onchange.
32+
@api.onchange("packaging_type_id")
33+
def _onchange_packaging_type(self):
34+
for record in self:
35+
record.can_be_sold = record.packaging_type_id.can_be_sold
36+
2937
def write(self, vals):
3038
if "active" in vals and vals["active"] is False:
3139
vals["can_be_sold"] = False

sale_by_packaging/tests/test_packaging_type_can_be_sold.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2020 Camptocamp SA
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
3+
34
from odoo.exceptions import ValidationError
5+
from odoo.tests.common import Form
46

57
from .common import Common
68

@@ -43,3 +45,18 @@ def test_product_packaging_can_be_sold(self):
4345
# Changing the can_be_sold on the packaging_type does not update the packaging
4446
self.packaging_type_cannot_be_sold.can_be_sold = True
4547
self.assertEqual(self.packaging_cannot_be_sold.can_be_sold, False)
48+
49+
50+
class TestPackagingForm(Common):
51+
def test_packaging_can_be_sold_default_product_form(self):
52+
pkg_type_cannot_be_sold = self.packaging_type_cannot_be_sold.copy()
53+
pkg_type_can_be_sold = self.packaging_type_pl.copy()
54+
with Form(
55+
self.env["product.packaging"],
56+
view="product_packaging_type.view_product_packaging_add_type_form",
57+
) as pkg:
58+
pkg.packaging_type_id = pkg_type_can_be_sold
59+
self.assertTrue(pkg.can_be_sold)
60+
pkg.packaging_type_id = pkg_type_cannot_be_sold
61+
self.assertFalse(pkg.can_be_sold)
62+
pkg.product_id = self.product

0 commit comments

Comments
 (0)