From a934b9e1ba4031e1bf0689d5cee6feea09424e8b Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Tue, 8 Sep 2015 18:22:36 +0200 Subject: [PATCH] [IMP] delivery: add fields for retrieving the bulk weight and the weight of the packages of a picking As the transporter interfaces will put a label on every package, it is in handy to have a field with the list of the final packages (and their weights) treated in the picking. We give a weight for the products that are not in a package also(bulk). We only put the weight on the package in the views. The weights in the product form views should appear entirely or not in product / product templates (only when only 1 variant) (attrs was only on the field, not label, ...) The digits= for the weights are not needed as there is a decimal precision on these fields. --- addons/delivery/models/stock_picking.py | 40 +++++++++++++++++++++++++ addons/delivery/views/delivery_view.xml | 11 +++++++ addons/product/product_view.xml | 22 +++++++------- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/addons/delivery/models/stock_picking.py b/addons/delivery/models/stock_picking.py index b6b9f291d71..344828b27a2 100644 --- a/addons/delivery/models/stock_picking.py +++ b/addons/delivery/models/stock_picking.py @@ -6,6 +6,22 @@ from openerp.exceptions import UserError import openerp.addons.decimal_precision as dp +class StockQuantPackage(models.Model): + _inherit = "stock.quant.package" + + @api.one + @api.depends('quant_ids', 'children_ids') + def _compute_weight(self): + weight = 0 + for quant in self.quant_ids: + weight += quant.qty * quant.product_id.weight + for pack in self.children_ids: + pack._compute_weight() + weight += pack.weight + self.weight = weight + + weight = fields.Float(compute='_compute_weight') + class StockPicking(models.Model): _inherit = 'stock.picking' @@ -14,6 +30,28 @@ class StockPicking(models.Model): uom_categ_id = self.env.ref('product.product_uom_categ_kgm').id return self.env['product.uom'].search([('category_id', '=', uom_categ_id), ('factor', '=', 1)], limit=1) + @api.one + @api.depends('pack_operation_ids') + def _compute_packages(self): + self.ensure_one() + packs = set() + for packop in self.pack_operation_ids: + if packop.result_package_id: + packs.add(packop.result_package_id.id) + elif packop.package_id and not packop.product_id: + packs.add(packop.package_id.id) + self.package_ids = list(packs) + + @api.one + @api.depends('pack_operation_ids') + def _compute_bulk_weight(self): + weight = 0.0 + uom_obj = self.env['product.uom'] + for packop in self.pack_operation_ids: + if packop.product_id and not packop.result_package_id: + weight += uom_obj._compute_qty_obj(packop.product_uom_id , packop.product_qty, packop.product_id.uom_id) * packop.product_id.weight + self.weight_bulk = weight + carrier_price = fields.Float(string="Shipping Cost", readonly=True) delivery_type = fields.Selection(related='carrier_id.delivery_type', readonly=True) carrier_id = fields.Many2one("delivery.carrier", string="Carrier") @@ -22,6 +60,8 @@ class StockPicking(models.Model): carrier_tracking_ref = fields.Char(string='Carrier Tracking Ref', copy=False) number_of_packages = fields.Integer(string='Number of Packages', copy=False) weight_uom_id = fields.Many2one('product.uom', string='Unit of Measure', required=True, readonly="1", help="Unit of measurement for Weight", default=_default_uom) + package_ids = fields.Many2many('stock.quant.package', compute='_compute_packages', string='Packages') + weight_bulk = fields.Float('Bulk Weight', compute='_compute_bulk_weight') @api.depends('product_id', 'move_lines') def _cal_weight(self): diff --git a/addons/delivery/views/delivery_view.xml b/addons/delivery/views/delivery_view.xml index 6d7525be2b8..2b6fbae5902 100644 --- a/addons/delivery/views/delivery_view.xml +++ b/addons/delivery/views/delivery_view.xml @@ -195,6 +195,17 @@ + + stock.quant.package.weight.form + stock.quant.package + + + + + + + + stock.move.form.weight stock.move diff --git a/addons/product/product_view.xml b/addons/product/product_view.xml index ea959d44c15..617c471dddc 100644 --- a/addons/product/product_view.xml +++ b/addons/product/product_view.xml @@ -125,11 +125,9 @@ - @@ -482,12 +480,12 @@ - - - - + +
+ {'invisible':[('type', 'not in', ['product', 'consu'])]} +