|
2 | 2 | # Copyright 2016 Camptocamp SA
|
3 | 3 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
4 | 4 |
|
5 |
| -import logging |
6 | 5 |
|
7 | 6 | from odoo import fields, models
|
8 |
| - |
9 |
| -_logger = logging.getLogger(__name__) |
| 7 | +from odoo.tools import float_round |
10 | 8 |
|
11 | 9 |
|
12 | 10 | class StockMoveLine(models.Model):
|
13 | 11 | _inherit = "stock.move.line"
|
14 | 12 |
|
15 |
| - weight = fields.Float(digits="Stock Weight", help="Weight of the pack_operation") |
| 13 | + weight = fields.Float(digits="Stock Weight") |
16 | 14 |
|
17 | 15 | 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 |
37 | 25 | )
|
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