From f3dbaf9a7c1ccbd38b44a6d511cf8f2ff963eee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 18 May 2021 22:19:09 +0200 Subject: [PATCH] Remove unique product constraint on BOM The constraint is needed only for compute_factor which can be written to not rely on it. issue10363 review336091002 --- CHANGELOG | 2 ++ bom.py | 27 ++++++++++++++++----------- message.xml | 3 --- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 97de929da3..c46dc5c410 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Remove unique product constraint on BOM + Version 6.0.0 - 2021-05-03 * Bug fixes (see mercurial logs for details) * Allow productions to be automatically assigned diff --git a/bom.py b/bom.py index dc3faeb001..3f9c369de9 100644 --- a/bom.py +++ b/bom.py @@ -1,6 +1,6 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. -from trytond.model import ModelView, ModelSQL, DeactivableMixin, fields, Unique +from trytond.model import ModelView, ModelSQL, DeactivableMixin, fields from trytond.wizard import Wizard, StateView, Button from trytond.pyson import Eval from trytond.pool import Pool @@ -21,13 +21,15 @@ class BOM(DeactivableMixin, ModelSQL, ModelView): Compute factor for an output product ''' Uom = Pool().get('product.uom') + output_quantity = 0 for output in self.outputs: if output.product == product: - if not output.quantity: - return 0.0 - quantity = Uom.compute_qty(uom, quantity, - output.uom, round=False) - return quantity / output.quantity + output_quantity += Uom.compute_qty( + output.uom, output.quantity, uom, round=False) + if output_quantity: + return quantity / output_quantity + else: + return 0 @classmethod def copy(cls, records, default=None): @@ -67,11 +69,14 @@ class BOMInput(ModelSQL, ModelView): super(BOMInput, cls).__setup__() cls.product.domain = [('type', 'in', cls.get_product_types())] cls.__access__.add('bom') - t = cls.__table__() - cls._sql_constraints = [ - ('product_bom_uniq', Unique(t, t.product, t.bom), - 'production.msg_product_bom_unique'), - ] + + @classmethod + def __register__(cls, module): + super().__register__(module) + table_h = cls.__table_handler__(module) + + # Migration from 6.0: remove unique constraint + table_h.drop_constraint('product_bom_uniq') @classmethod def get_product_types(cls): diff --git a/message.xml b/message.xml index f97efb1310..b12c6e4511 100644 --- a/message.xml +++ b/message.xml @@ -6,8 +6,5 @@ this repository contains the full copyright notices and license terms. --> You cannot create a recursive BOM for product "%(product)s". - - Products can only appear once on each BOM. -