Skip to content

Commit f1b2be8

Browse files
committed
[FIX] base_delivery_carrier_label: fix weight computation logic
Re-write tests for get_weight()
1 parent 2406552 commit f1b2be8

File tree

2 files changed

+138
-249
lines changed

2 files changed

+138
-249
lines changed

base_delivery_carrier_label/models/stock_move_line.py

+19-34
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,32 @@
22
# Copyright 2016 Camptocamp SA
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44

5-
import logging
65

76
from odoo import fields, models
8-
9-
_logger = logging.getLogger(__name__)
7+
from odoo.tools import float_round
108

119

1210
class StockMoveLine(models.Model):
1311
_inherit = "stock.move.line"
1412

15-
weight = fields.Float(digits="Stock Weight", help="Weight of the pack_operation")
13+
weight = fields.Float(digits="Stock Weight")
1614

1715
def get_weight(self):
18-
"""Calc and save weight of pack.operations.
19-
20-
Warning: Type conversion not implemented
21-
it will return False if at least one uom or uos not in kg
22-
return:
23-
the sum of the weight of [self]
24-
"""
25-
total_weight = 0
26-
kg = self.env.ref("uom.product_uom_kgm").id
27-
units = self.env.ref("uom.product_uom_unit").id
28-
allowed = (False, kg, units)
29-
cant_calc_total = False
30-
for operation in self:
31-
product = operation.product_id
32-
33-
# if not defined we assume it's in kg
34-
if product.uom_id.id not in allowed:
35-
_logger.warning(
36-
"Type conversion not implemented for product %s" % product.id
16+
prec = self.env["decimal.precision"].precision_get("Stock Weight")
17+
weight_uom_categ_id = self.env.ref("uom.product_uom_categ_kgm").id
18+
ref_weight_uom = self.env['product.template']._get_weight_uom_id_from_ir_config_parameter()
19+
total_weight = 0.0
20+
for line in self:
21+
product = line.product_id
22+
if line.product_uom_id.category_id.id == weight_uom_categ_id:
23+
total_weight += line.product_uom_id._compute_quantity(
24+
line.qty_done, ref_weight_uom, round=False
3725
)
38-
cant_calc_total = True
39-
# product_qty may be 0 if you don't set move line
40-
# individually but directly validate the picking
41-
qty = operation.qty_done or operation.product_qty
42-
operation.weight = product.weight * qty
43-
44-
total_weight += operation.weight
45-
46-
if cant_calc_total:
47-
return False
48-
return total_weight
26+
else:
27+
total_weight += (
28+
line.product_uom_id._compute_quantity(
29+
line.qty_done, product.uom_id, round=False
30+
)
31+
* product.weight
32+
)
33+
return float_round(total_weight, precision_digits=prec)

0 commit comments

Comments
 (0)