diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2024-02-09 14:58:28 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2024-02-09 14:58:28 +0700 |
| commit | ba871e1ff11e160823e3c816c707b642eedea200 (patch) | |
| tree | 51f16f1df5145f1101240d6572297a65e4193352 | |
| parent | 1418dbbb581228383df151f1f8df6fc48a0fb9cc (diff) | |
Fix check duplicate product function
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 67 |
1 files changed, 18 insertions, 49 deletions
diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 30d89356..ba8fac55 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -74,34 +74,27 @@ class ProductTemplate(models.Model): @api.constrains('name', 'default_code') def _check_duplicate_product(self): for product in self: - if self.env.user.is_purchasing_manager or self.env.user.is_editor_product or self.env.user.id in [1, 25]: - continue - - domain = [('default_code', '!=', False)] + variants = product.product_variant_ids + names = [x.name for x in variants] if variants else [product.name] + default_codes = [x.default_code for x in variants] if variants else [product.default_code] - if product.product_variant_ids: - domain.extend([ - '|', - ('name', 'in', [variants.name for variants in product.product_variant_ids]), - ('default_code', 'in', [variants.default_code for variants in product.product_variant_ids]) - ]) - else: - domain.extend([ - '|', - ('name', 'in', [product.name]), - ('default_code', 'in', [product.default_code]) - ]) + domain = [ + ('default_code', '!=', False), + ('id', '!=', product.id), + '|', + ('name', 'in', names), + ('default_code', 'in', default_codes) + ] + + product_exist = self.search(domain, limit=1) + if len(product_exist) > 0: + raise UserError('Name atau Internal Reference sudah digunakan pada produk lain') - domain.append(('id', '!=', product.id)) + if self.env.user.is_purchasing_manager or self.env.user.is_editor_product or self.env.user.id in [1, 25]: + continue - if product.write_date == product.create_date: - message = "SKU atau Name yang Anda gunakan sudah digunakan di produk lain" - elif all(day_product > 0 for day_product in product.day_product_to_edit()): - domain = [('id', '=', product.id)] - message = "Anda tidak berhak merubah data produk ini" - existing_purchase = self.search(domain, limit=1) - if existing_purchase: - raise UserError(message) + if sum(product.day_product_to_edit()) > 0: + raise UserError('Produk ini tidak dapat diubah') @api.constrains('name') def _validate_name(self): @@ -377,30 +370,6 @@ class ProductProduct(models.Model): day_products.append(day_product) return day_products - - @api.constrains('name','default_code') - def _check_duplicate_product(self): - for product in self: - if self.env.user.is_purchasing_manager or self.env.user.is_editor_product or self.env.user.id in [1, 25]: - continue - - if product.write_date == product.create_date: - domain = [ - ('default_code', '!=', False), - '|', - ('name', 'in', [template.name for template in product.product_tmpl_id] or [product.name]), - ('default_code', 'in', [template.default_code for template in product.product_tmpl_id] or [product.default_code])] - - domain.append(('id', '!=', product.id)) - massage="SKU atau Name yang anda pakai sudah digunakan di product lain" - existing_purchase = self.search(domain, limit=1) - if existing_purchase: - raise UserError(massage) - elif all(day_product > 0 for day_product in product.day_product_to_edit()): - domain = [('id', '=', product.id)] - existing_purchase = self.search(domain) - if existing_purchase: - raise UserError('Anda tidak berhak merubah data product ini') @api.constrains('name') def _validate_name(self): |
