changeset 03f64fe15398 in modules/product:default
details: https://hg.tryton.org/modules/product?cmd=changeset;node=03f64fe15398
description:
        Allow to set the product prefix code by a sequence

        issue9512
        review307981005
diffstat:

 CHANGELOG                   |   1 +
 configuration.py            |   9 +++++++--
 doc/index.rst               |   5 +++--
 product.py                  |  31 ++++++++++++++++++++++++++++++-
 product.xml                 |  16 +++++++++++++++-
 view/configuration_form.xml |   3 +++
 6 files changed, 59 insertions(+), 6 deletions(-)

diffs (154 lines):

diff -r cf07f6239827 -r 03f64fe15398 CHANGELOG
--- a/CHANGELOG Sat Jul 25 12:54:47 2020 +0200
+++ b/CHANGELOG Fri Aug 14 14:26:59 2020 +0200
@@ -1,3 +1,4 @@
+* Allow the product template code to be set from a sequence
 * Set code column first in template tree view
 * Add default order on product and template
 * Check price_decimal configuration
diff -r cf07f6239827 -r 03f64fe15398 configuration.py
--- a/configuration.py  Sat Jul 25 12:54:47 2020 +0200
+++ b/configuration.py  Fri Aug 14 14:26:59 2020 +0200
@@ -26,11 +26,16 @@
 
     default_cost_price_method = fields.MultiValue(default_cost_price_method)
     get_cost_price_methods = get_cost_price_methods
-    product_sequence = fields.Many2One('ir.sequence', 'Product Sequence',
+    product_sequence = fields.Many2One('ir.sequence', "Variant Sequence",
         domain=[
             ('code', '=', 'product.product'),
             ],
-        help="Used to generate the product code.")
+        help="Used to generate the last part of the product code.")
+    template_sequence = fields.Many2One('ir.sequence', "Product Sequence",
+        domain=[
+            ('code', '=', 'product.template'),
+            ],
+        help="Used to generate the first part of the product code.")
 
     @classmethod
     def default_default_cost_price_method(cls, **pattern):
diff -r cf07f6239827 -r 03f64fe15398 doc/index.rst
--- a/doc/index.rst     Sat Jul 25 12:54:47 2020 +0200
+++ b/doc/index.rst     Fri Aug 14 14:26:59 2020 +0200
@@ -39,10 +39,11 @@
 The product concept in Tryton is composed of two models: Product
 Template and Product.
 
-The Product Template model contains the following fields: 
+The Product Template model contains the following fields:
 
 - Name.
-- Code, a common prefix for all products.
+- Code, a common prefix for all products. If a sequence is set on product
+  configuration the code will be read-only and filled using the sequence.
 - Type, whose value can be *Goods*, *Assets*, *Service*.
 - Category.
 - List Price, the default sale price expressed in the List Price UOM.
diff -r cf07f6239827 -r 03f64fe15398 product.py
--- a/product.py        Sat Jul 25 12:54:47 2020 +0200
+++ b/product.py        Fri Aug 14 14:26:59 2020 +0200
@@ -55,7 +55,14 @@
     _order_name = 'rec_name'
     name = fields.Char(
         "Name", size=None, required=True, translate=True, select=True)
-    code = fields.Char("Code", select=True)
+    code_readonly = fields.Function(
+        fields.Boolean("Code Readonly"), 'get_code_readonly')
+    code = fields.Char(
+        "Code", select=True,
+        states={
+            'readonly': Eval('code_readonly', False),
+            },
+        depends=['code_readonly'])
     type = fields.Selection(TYPES, "Type", required=True)
     consumable = fields.Boolean('Consumable',
         states={
@@ -181,6 +188,16 @@
             return []
         return [{}]
 
+    @classmethod
+    def default_code_readonly(cls):
+        pool = Pool()
+        Configuration = pool.get('product.configuration')
+        config = Configuration(1)
+        return bool(config.template_sequence)
+
+    def get_code_readonly(self, name):
+        return self.default_code_readonly()
+
     @fields.depends('type', 'cost_price_method')
     def on_change_type(self):
         if self.type == 'service':
@@ -197,12 +214,24 @@
             + tuple(clause[1:])]
 
     @classmethod
+    def _new_code(cls):
+        pool = Pool()
+        Sequence = pool.get('ir.sequence')
+        Configuration = pool.get('product.configuration')
+        config = Configuration(1)
+        sequence = config.template_sequence
+        if sequence:
+            return Sequence.get_id(sequence.id)
+
+    @classmethod
     def create(cls, vlist):
         pool = Pool()
         Product = pool.get('product.product')
         vlist = [v.copy() for v in vlist]
         for values in vlist:
             values.setdefault('products', None)
+            if not values.get('code'):
+                values['code'] = cls._new_code()
         templates = super(Template, cls).create(vlist)
         products = sum((t.products for t in templates), ())
         Product.sync_code(products)
diff -r cf07f6239827 -r 03f64fe15398 product.xml
--- a/product.xml       Sat Jul 25 12:54:47 2020 +0200
+++ b/product.xml       Fri Aug 14 14:26:59 2020 +0200
@@ -221,7 +221,7 @@
         </record>
 
         <record model="ir.sequence.type" id="sequence_type_product">
-            <field name="name">Product</field>
+            <field name="name">Variant</field>
             <field name="code">product.product</field>
         </record>
         <record model="ir.sequence.type-res.group"
@@ -234,6 +234,20 @@
             <field name="sequence_type" ref="sequence_type_product"/>
             <field name="group" ref="group_product_admin"/>
         </record>
+        <record model="ir.sequence.type" id="sequence_type_template">
+            <field name="name">Product</field>
+            <field name="code">product.template</field>
+        </record>
+        <record model="ir.sequence.type-res.group"
+            id="sequence_type_template_group_admin">
+            <field name="sequence_type" ref="sequence_type_template"/>
+            <field name="group" ref="res.group_admin"/>
+        </record>
+        <record model="ir.sequence.type-res.group"
+            id="sequence_type_template_group_template_admin">
+            <field name="sequence_type" ref="sequence_type_template"/>
+            <field name="group" ref="group_product_admin"/>
+        </record>
 
     </data>
 </tryton>
diff -r cf07f6239827 -r 03f64fe15398 view/configuration_form.xml
--- a/view/configuration_form.xml       Sat Jul 25 12:54:47 2020 +0200
+++ b/view/configuration_form.xml       Fri Aug 14 14:26:59 2020 +0200
@@ -4,6 +4,9 @@
 <form>
     <label name="default_cost_price_method"/>
     <field name="default_cost_price_method"/>
+    <separator id="sequences" string="Sequences" colspan="4"/>
+    <label name="template_sequence"/>
+    <field name="template_sequence"/>
     <label name="product_sequence"/>
     <field name="product_sequence"/>
 </form>

Reply via email to