Skip to content

Commit 2075c9f

Browse files
committed
sale_packaging_default: allow for less-than-packaging-quantity quantities on lines
1 parent 291460a commit 2075c9f

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

sale_packaging_default/models/sale_order_line.py

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Copyright 2023 Moduon Team S.L.
22
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
3-
from contextlib import suppress
43

5-
from odoo import api, fields, models
4+
from odoo import api, models
65

76

87
class SaleOrderLine(models.Model):
@@ -19,37 +18,37 @@ def onchange(self, values, field_name, field_onchange):
1918
_self = self.with_context(changing_fields=names)
2019
return super(SaleOrderLine, _self).onchange(values, field_name, field_onchange)
2120

22-
@api.depends("product_id", "product_uom_qty", "product_uom")
23-
def _compute_product_packaging_id(self):
24-
"""Set a default packaging for sales if possible."""
25-
for line in self:
26-
if line.product_id != line.product_packaging_id.product_id:
27-
line.product_packaging_id = line._get_default_packaging(line.product_id)
28-
result = super()._compute_product_packaging_id()
29-
# If there's no way to package the desired qty, remove the packaging.
30-
# It is only done when the user is currently manually setting
31-
# `product_uom_qty` to zero. In other cases, we are maybe getting
32-
# default values and this difference will get fixed by other compute
33-
# methods later.
34-
if (
35-
self.env.context.get("changing_fields")
36-
and "product_uom_qty" not in self.env.context["changing_fields"]
37-
):
38-
return result
39-
for line in self:
40-
with suppress(ZeroDivisionError):
41-
if (
42-
line.product_uom_qty
43-
and line.product_uom_qty % line.product_packaging_id.qty
44-
):
45-
line.product_packaging_id = False
46-
return result
47-
48-
@api.model
49-
def _get_default_packaging(self, product):
50-
return fields.first(
51-
product.packaging_ids.filtered_domain([("sales", "=", True)])
52-
)
21+
# @api.depends("product_id", "product_uom_qty", "product_uom")
22+
# def _compute_product_packaging_id(self):
23+
# """Set a default packaging for sales if possible."""
24+
# for line in self:
25+
# if line.product_id != line.product_packaging_id.product_id:
26+
# line.product_packaging_id = line._get_default_packaging(line.product_id)
27+
# result = super()._compute_product_packaging_id()
28+
# # If there's no way to package the desired qty, remove the packaging.
29+
# # It is only done when the user is currently manually setting
30+
# # `product_uom_qty` to zero. In other cases, we are maybe getting
31+
# # default values and this difference will get fixed by other compute
32+
# # methods later.
33+
# if (
34+
# self.env.context.get("changing_fields")
35+
# and "product_uom_qty" not in self.env.context["changing_fields"]
36+
# ):
37+
# return result
38+
# for line in self:
39+
# with suppress(ZeroDivisionError):
40+
# if (
41+
# line.product_uom_qty
42+
# and line.product_uom_qty % line.product_packaging_id.qty
43+
# ):
44+
# line.product_packaging_id = False
45+
# return result
46+
#
47+
# @api.model
48+
# def _get_default_packaging(self, product):
49+
# return fields.first(
50+
# product.packaging_ids.filtered_domain([("sales", "=", True)])
51+
# )
5352

5453
@api.depends("product_packaging_id", "product_uom", "product_uom_qty")
5554
def _compute_product_packaging_qty(self):

sale_packaging_default/tests/test_sale_packaging_default.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def test_default_packaging_sale_order(self):
8181
line_f.product_uom_qty = 120
8282
self.assertEqual(line_f.product_packaging_qty, 10)
8383
self.assertEqual(line_f.product_packaging_id, self.dozen)
84-
# If I set a uom qty without packaging, it is emptied
85-
line_f.product_uom_qty = 7
86-
self.assertFalse(line_f.product_packaging_id)
87-
self.assertEqual(line_f.product_packaging_qty, 0)
88-
self.assertEqual(line_f.product_uom_qty, 7)
84+
# # If I set a uom qty without packaging, it is emptied
85+
# line_f.product_uom_qty = 7
86+
# self.assertFalse(line_f.product_packaging_id)
87+
# self.assertEqual(line_f.product_packaging_qty, 0)
88+
# self.assertEqual(line_f.product_uom_qty, 7)
8989
# Setting zero uom qty resets to the default packaging
9090
line_f.product_uom_qty = 0
9191
self.assertEqual(line_f.product_packaging_id, self.big_box)

0 commit comments

Comments
 (0)