diff --git a/delivery_usability/__manifest__.py b/delivery_usability/__manifest__.py index 1ec0678..d19e83e 100644 --- a/delivery_usability/__manifest__.py +++ b/delivery_usability/__manifest__.py @@ -22,6 +22,7 @@ This module has been written by Alexis de Lattre from Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductPackaging(models.Model): + _inherit = 'product.packaging' + + # product.packaging is defined in the 'product' module and enhanced in the 'delivery' module + # I used to make the improvements on the datamodel of product.packaging in the OCA module + # 'stock_packaging_usability_pp' from OCA/stock-logistics-tracking, + # but I eventually figured out that the feature provided by 'stock_packaging_usability_pp' + # was native in the 'delivery' module via the wizard choose.delivery.package. + # So I stopped using 'stock_packaging_usability_pp' and I moved the datamodel changes + # here in the module 'delivery_usability' + name = fields.Char(translate=True) + weight = fields.Float(digits="Stock Weight", string="Empty Package Weight") + active = fields.Boolean(default=True) + # packaging_type is important, in particular for pallets for which + # we need a special implementation to enter the height + packaging_type = fields.Selection( + [ + ("unit", "Unit"), + ("pack", "Pack"), + ("box", "Box"), + ("pallet", "Pallet"), + ], + string="Type", + ) diff --git a/delivery_usability/models/stock_move.py b/delivery_usability/models/stock_move.py new file mode 100644 index 0000000..ef753a8 --- /dev/null +++ b/delivery_usability/models/stock_move.py @@ -0,0 +1,23 @@ +# Copyright 2019 Akretion France (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo import api, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + # Fixing bug https://github.com/odoo/odoo/issues/34702 + @api.depends("product_id", "product_uom_qty", "product_uom") + def _cal_move_weight(self): + weight_uom_categ = self.env.ref("uom.product_uom_categ_kgm") + kg_uom = self.env.ref("uom.product_uom_kgm") + for move in self: + if move.product_id.uom_id.category_id == weight_uom_categ: + move.weight = move.product_id.uom_id._compute_quantity( + move.product_qty, kg_uom + ) + else: + move.weight = move.product_qty * move.product_id.weight diff --git a/delivery_usability/models/stock_quant_package.py b/delivery_usability/models/stock_quant_package.py new file mode 100644 index 0000000..8f788de --- /dev/null +++ b/delivery_usability/models/stock_quant_package.py @@ -0,0 +1,68 @@ +# Copyright 2019-2024 Akretion France (https://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo import fields, models +from odoo.tools import float_is_zero + + +class StockQuantPackage(models.Model): + _inherit = "stock.quant.package" + + # These 2 fields are defined in the 'delivery' module but they forgot + # the decimal precision + shipping_weight = fields.Float(digits="Stock Weight") + weight = fields.Float(digits="Stock Weight") + + # Fixing bug https://github.com/odoo/odoo/issues/34702 + # and take into account the weight of the packaging + # WARNING: this method _compute_weight() is also inherited by the OCA module + # base_delivery_carrier_label so if you use that module, you should copy + # that piece of code in a custom module that depend on delivery_usability + # and base_delivery_carrier_label + def _compute_weight(self): + smlo = self.env["stock.move.line"] + weight_uom_categ = self.env.ref("uom.product_uom_categ_kgm") + kg_uom = self.env.ref("uom.product_uom_kgm") + weight_prec = self.env['decimal.precision'].precision_get('Stock Weight') + for package in self: + # if the weight of the package has been measured, + # it is written in shipping_weight + if not float_is_zero(package.shipping_weight, precision_digits=weight_prec): + weight = package.shipping_weight + # otherwise, we compute the theorical weight from the weight of the products + # and the weight of the packaging + # Since Odoo v11, consu products don't create quants, so I can't loop + # on pack.quant_ids to get all the items inside a package: I have to + # get the picking, then loop on the stock.move.line of that picking + # linked to that package + else: + weight = 0.0 + # the package can be seen in a return + # So I get the picking of it's first appearance + domain = [ + ("result_package_id", "=", package.id), + ("product_id", "!=", False), + ] + first_move_line = smlo.search( + domain + [('picking_id', '!=', False)], limit=1, order='id') + if first_move_line: + picking_id = first_move_line.picking_id.id + current_picking_move_line_ids = smlo.search( + domain + [("picking_id", "=", picking_id)]) + for ml in current_picking_move_line_ids: + if ml.product_uom_id.category_id == weight_uom_categ: + weight += ml.product_uom_id._compute_quantity( + ml.qty_done, kg_uom + ) + else: + weight += ( + ml.product_uom_id._compute_quantity( + ml.qty_done, ml.product_id.uom_id + ) + * ml.product_id.weight + ) + if package.packaging_id: + weight += package.packaging_id.weight + package.weight = weight diff --git a/delivery_usability/views/product_packaging.xml b/delivery_usability/views/product_packaging.xml new file mode 100644 index 0000000..ec18bd3 --- /dev/null +++ b/delivery_usability/views/product_packaging.xml @@ -0,0 +1,79 @@ + + + + + + + + stock_packaging_usability_pp.product.packaging.form + product.packaging + + + + + + + + + + + stock_packaging_usability_pp.product.packaging.tree + product.packaging + + + + + + + + + + + + + + product.packaging.search + product.packaging + + + + + + + + + + + + + +