From 3a18c0803ec93fd45c4611db463b3a07aefe1ac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 23 Jan 2022 10:25:32 +0100 Subject: [PATCH] Retrieve unit for product margin from table joins Using a function field triggers a new read to round the quantity with the default uom. issue11097 review379931002 --- stock_reporting_margin.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/stock_reporting_margin.py b/stock_reporting_margin.py index 1dbc696417..0f967d4d1a 100644 --- a/stock_reporting_margin.py +++ b/stock_reporting_margin.py @@ -366,13 +366,34 @@ class ProductMixin: internal_quantity = fields.Float("Internal Quantity") quantity = fields.Function(fields.Float( "Quantity", digits='unit'), 'get_quantity') - unit = fields.Function(fields.Many2One('product.uom', "Unit"), 'get_unit') + unit = fields.Many2One('product.uom', "Unit") + + @classmethod + def _joins(cls): + pool = Pool() + Product = pool.get('product.product') + Template = pool.get('product.template') + from_item, tables, withs = super()._joins() + if 'move.product' not in tables: + product = Product.__table__() + tables['move.product'] = product + move = tables['move'] + from_item = (from_item + .join(product, condition=move.product == product.id)) + if 'move.product.template' not in tables: + template = Template.__table__() + tables['move.product.template'] = template + product = tables['move.product'] + from_item = (from_item + .join(template, condition=product.template == template.id)) + return from_item, tables, withs @classmethod def _columns(cls, tables, withs): move = tables['move'] from_location = tables['move.from_location'] to_location = tables['move.to_location'] + template = tables['move.product.template'] sign = Case( (from_location.type.in_(cls._to_location_types()) & to_location.type.in_(cls._from_location_types()), @@ -381,12 +402,15 @@ class ProductMixin: return super()._columns(tables, withs) + [ move.product.as_('product'), Sum(sign * move.internal_quantity).as_('internal_quantity'), + template.default_uom.as_('unit'), ] @classmethod def _group_by(cls, tables, withs): move = tables['move'] - return super()._group_by(tables, withs) + [move.product] + template = tables['move.product.template'] + return super()._group_by(tables, withs) + [ + move.product, template.default_uom] def get_rec_name(self, name): return self.product.rec_name @@ -394,9 +418,6 @@ class ProductMixin: def get_quantity(self, name): return self.unit.round(self.internal_quantity) - def get_unit(self, name): - return self.product.default_uom.id - class Product(ProductMixin, Abstract, ModelView): "Stock Reporting Margin per Product"