changeset 288d5eff357e in modules/sale_product_customer:5.2
details: 
https://hg.tryton.org/modules/sale_product_customer?cmd=changeset;node=288d5eff357e
description:
        Prevent domain error when copy products with product customers on 
variants

        issue9017
        review315221002
        (grafted from 3bef7a63ee9cdc937a05e650181f08c2917651ea)
diffstat:

 product.py |  56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 1 deletions(-)

diffs (77 lines):

diff -r 67bd71cbcfce -r 288d5eff357e product.py
--- a/product.py        Sat Apr 04 17:15:49 2020 +0200
+++ b/product.py        Fri May 01 20:32:37 2020 +0200
@@ -3,7 +3,7 @@
 
 from trytond.model import (fields, ModelSQL, ModelView, sequence_ordered,
     MatchMixin)
-from trytond.pool import PoolMeta
+from trytond.pool import PoolMeta, Pool
 from trytond.pyson import If, Eval, Bool
 from trytond.tools import lstrip_wildcard
 
@@ -85,6 +85,31 @@
             if product_customer.match(pattern):
                 yield product_customer
 
+    @classmethod
+    def copy(cls, templates, default=None):
+        pool = Pool()
+        ProductCustomer = pool.get('sale.product_customer')
+        if default is None:
+            default = {}
+        else:
+            default = default.copy()
+
+        copy_customers = 'product_customers' not in default
+        default.setdefault('product_customers', None)
+        new_templates = super().copy(templates, default)
+        if copy_customers:
+            old2new = {}
+            to_copy = []
+            for template, new_template in zip(templates, new_templates):
+                to_copy.extend(
+                    pc for pc in template.product_customers if not pc.product)
+                old2new[template.id] = new_template.id
+            if to_copy:
+                ProductCustomer.copy(to_copy, {
+                        'template': lambda d: old2new[d['template']],
+                        })
+        return new_templates
+
 
 class Product(metaclass=PoolMeta):
     __name__ = 'product.product'
@@ -105,3 +130,32 @@
                 yield product_customer
         pattern['product'] = None
         yield from self.template.product_customer_used(**pattern)
+
+    @classmethod
+    def copy(cls, products, default=None):
+        pool = Pool()
+        ProductCustomer = pool.get('sale.product_customer')
+        if default is None:
+            default = {}
+        else:
+            default = default.copy()
+
+        copy_customers = 'product_customers' not in default
+        if 'template' in default:
+            default.setdefault('product_customers', None)
+        new_products = super().copy(products, default)
+        if 'template' in default and copy_customers:
+            template2new = {}
+            product2new = {}
+            to_copy = []
+            for product, new_product in zip(products, new_products):
+                if product.product_customers:
+                    to_copy.extend(product.product_customers)
+                    template2new[product.template.id] = new_product.template.id
+                    product2new[product.id] = new_product.id
+            if to_copy:
+                ProductCustomer.copy(to_copy, {
+                        'product': lambda d: product2new[d['product']],
+                        'template': lambda d: template2new[d['template']],
+                        })
+        return new_products

Reply via email to