changeset 79cee9968695 in modules/production:default
details: 
https://hg.tryton.org/modules/production?cmd=changeset&node=79cee9968695
description:
        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
diffstat:

 CHANGELOG   |   2 ++
 bom.py      |  27 ++++++++++++++++-----------
 message.xml |   3 ---
 3 files changed, 18 insertions(+), 14 deletions(-)

diffs (73 lines):

diff -r 365a4393a441 -r 79cee9968695 CHANGELOG
--- a/CHANGELOG Mon May 03 15:44:52 2021 +0200
+++ b/CHANGELOG Tue May 18 22:19:09 2021 +0200
@@ -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 -r 365a4393a441 -r 79cee9968695 bom.py
--- a/bom.py    Mon May 03 15:44:52 2021 +0200
+++ b/bom.py    Tue May 18 22:19:09 2021 +0200
@@ -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 @@
         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 @@
         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 -r 365a4393a441 -r 79cee9968695 message.xml
--- a/message.xml       Mon May 03 15:44:52 2021 +0200
+++ b/message.xml       Tue May 18 22:19:09 2021 +0200
@@ -6,8 +6,5 @@
         <record model="ir.message" id="msg_recursive_bom">
             <field name="text">You cannot create a recursive BOM for product 
"%(product)s".</field>
         </record>
-        <record model="ir.message" id="msg_product_bom_unique">
-            <field name="text">Products can only appear once on each 
BOM.</field>
-        </record>
     </data>
 </tryton>

Reply via email to