Reviewers: ,

Description:
stock: Check if the cost method of the product can be changed

Please review this at http://codereview.tryton.org/896002/

Affected files:
  M product.py


Index: product.py
diff --git a/product.py b/product.py
index 0344367c491ccddb8b466801a26a981eb036c162..d526c2126861ab1cbad91da03eec234a95b9168c 100644
--- a/product.py
+++ b/product.py
@@ -38,6 +38,9 @@ class Template:
         cls._error_messages.update({
'change_default_uom': ('You cannot change the default uom for '
                     'a product which is associated to stock moves.'),
+                'change_cost_price_method': ('You cannot change the cost '
+                    'method for a product which is associated to stock '
+                    'moves.'),
                 })
         cls.cost_price.states['required'] = Or(
             cls.cost_price.states.get('required', True),
@@ -48,23 +51,28 @@ class Template:
     def write(cls, templates, vals):
         Move = Pool().get('stock.move')
         cursor = Transaction().cursor
-        if not vals.get("default_uom"):
+ if not vals.get('default_uom') and not vals.get('cost_price_method'):
             super(Template, cls).write(templates, vals)
             return

-        for i in range(0, len(templates), cursor.IN_MAX):
-            sub_ids = [t.id for t in templates[i:i + cursor.IN_MAX]]
+        def validate(field, error):
             templates_to_check = cls.search([
                     ('id', 'in', sub_ids),
-                    ('default_uom', '!=', vals['default_uom']),
+                    (field, '!=', vals[field]),
                     ])
-
             if templates_to_check:
                 if Move.search([
                             ('product.template', 'in',
                                 [t.id for t in templates_to_check]),
                             ], limit=1):
-                    cls.raise_user_error('change_default_uom')
+                    cls.raise_user_error(error)
+
+        for i in range(0, len(templates), cursor.IN_MAX):
+            sub_ids = [t.id for t in templates[i:i + cursor.IN_MAX]]
+            if vals.get('default_uom'):
+                validate('default_uom', 'change_default_uom')
+            if vals.get('cost_price_method'):
+                validate('cost_price_method', 'change_cost_price_method')

         super(Template, cls).write(templates, vals)



Reply via email to