From aa4b686bacb5c18ec472220bea7133f06e7f4ead Mon Sep 17 00:00:00 2001 From: Darshil Trivedi Date: Mon, 6 Aug 2018 13:04:12 +0000 Subject: [PATCH] [IMP] product: Choose volume measure on products MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Until now, the volume measure displayed on the product form was only in m³. We now allow to choose between m³ and ft³. So, we add volume selection field (m³/ft³) on general settings and it will be display on product and product variants form view as configuration. We also Remove tooltip from weight and volume field. And display weight (kg or lb(s)) in product and product variants form view correctly, because currently weight didn't display on product form view before save the product but after this commit weight will be display. This commit is related to task ID 1869002. closes odoo/odoo#26542 --- addons/product/models/product.py | 6 +-- addons/product/models/product_template.py | 41 ++++++++++++++----- addons/product/models/res_config_settings.py | 4 ++ addons/product/views/product_views.xml | 2 +- .../views/res_config_settings_views.xml | 13 ++++++ 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index b1b2340f16d..6c2518615ae 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -135,10 +135,8 @@ class ProductProduct(models.Model): help = "Cost used for stock valuation in standard price and as a first price to set in average/fifo. " "Also used as a base price for pricelists. " "Expressed in the default unit of measure of the product.") - volume = fields.Float('Volume', help="The volume in m3.") - weight = fields.Float( - 'Weight', digits=dp.get_precision('Stock Weight'), - help="Weight of the product, packaging not included. The unit of measure can be changed in the general settings") + volume = fields.Float('Volume') + weight = fields.Float('Weight', digits=dp.get_precision('Stock Weight')) pricelist_item_ids = fields.Many2many( 'product.pricelist.item', 'Pricelist Items', compute='_get_pricelist_items') diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 1ccf742bd9a..c9b6a753a68 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -35,6 +35,12 @@ class ProductTemplate(models.Model): def _get_default_uom_id(self): return self.env["uom.uom"].search([], limit=1, order='id').id + def _get_default_weight_uom(self): + return self._get_weight_uom_name_from_ir_config_parameter() + + def _get_default_volume_uom(self): + return self._get_volume_uom_name_from_ir_config_parameter() + name = fields.Char('Name', index=True, required=True, translate=True) sequence = fields.Integer('Sequence', default=1, help='Gives the sequence order when displaying a product list') description = fields.Text( @@ -78,14 +84,12 @@ class ProductTemplate(models.Model): help = "Cost used for stock valuation in standard price and as a first price to set in average/FIFO.") volume = fields.Float( - 'Volume', compute='_compute_volume', inverse='_set_volume', - help="The volume in m3.", store=True) + 'Volume', compute='_compute_volume', inverse='_set_volume', store=True) + volume_uom_name = fields.Char(string='Volume unit of measure label', compute='_compute_volume_uom_name', default=_get_default_volume_uom) weight = fields.Float( 'Weight', compute='_compute_weight', digits=dp.get_precision('Stock Weight'), - inverse='_set_weight', store=True, - help="The weight of the contents in Kg, not including any packaging, etc.") - weight_uom_id = fields.Many2one('uom.uom', string='Weight Unit of Measure', compute='_compute_weight_uom_id') - weight_uom_name = fields.Char(string='Weight unit of measure label', related='weight_uom_id.name', readonly=True) + inverse='_set_weight', store=True) + weight_uom_name = fields.Char(string='Weight unit of measure label', compute='_compute_weight_uom_name', readonly=True, default=_get_default_weight_uom) sale_ok = fields.Boolean('Can be Sold', default=True) purchase_ok = fields.Boolean('Can be Purchased', default=True) @@ -257,10 +261,27 @@ class ProductTemplate(models.Model): else: return self.env.ref('uom.product_uom_kgm') - def _compute_weight_uom_id(self): - weight_uom_id = self._get_weight_uom_id_from_ir_config_parameter() - for product_template in self: - product_template.weight_uom_id = weight_uom_id + @api.model + def _get_weight_uom_name_from_ir_config_parameter(self): + return self._get_weight_uom_id_from_ir_config_parameter().display_name + + def _compute_weight_uom_name(self): + for template in self: + template.weight_uom_name = self._get_weight_uom_name_from_ir_config_parameter() + + @api.model + def _get_volume_uom_name_from_ir_config_parameter(self): + """ Get the unit of measure to interpret the `volume` field. By default, we consider + that volumes are expressed in cubic meters. Users can configure to express them in cubic feet + by adding an ir.config_parameter record with "product.volume_in_cubic_feet" as key + and "1" as value. + """ + get_param = self.env['ir.config_parameter'].sudo().get_param + return "ft³" if get_param('product.volume_in_cubic_feet') == '1' else "m³" + + def _compute_volume_uom_name(self): + for template in self: + template.volume_uom_name = self._get_volume_uom_name_from_ir_config_parameter() @api.one def _set_weight(self): diff --git a/addons/product/models/res_config_settings.py b/addons/product/models/res_config_settings.py index 0750136085e..7e73f4df5e6 100644 --- a/addons/product/models/res_config_settings.py +++ b/addons/product/models/res_config_settings.py @@ -28,6 +28,10 @@ class ResConfigSettings(models.TransientModel): ('0', 'Kilogram'), ('1', 'Pound'), ], 'Weight unit of measure', config_parameter='product.weight_in_lbs', default='0') + product_volume_volume_in_cubic_feet = fields.Selection([ + ('0', 'Cubic Meters'), + ('1', 'Cubic Feet'), + ], 'Volume unit of measure', config_parameter='product.volume_in_cubic_feet', default='0') @api.model diff --git a/addons/product/views/product_views.xml b/addons/product/views/product_views.xml index f19c528af03..8a6e280bcad 100644 --- a/addons/product/views/product_views.xml +++ b/addons/product/views/product_views.xml @@ -105,7 +105,7 @@