diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-01-29 13:45:45 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-01-29 13:45:45 +0700 |
| commit | a142137887530c904c158859889f340c42fe78e1 (patch) | |
| tree | 171aa6acfc9a940b7b4a196ef1edda225a5da6c3 | |
| parent | bbbb683a01e316633cdbe8ef82303d05562ae0bb (diff) | |
refactor kode validation duplicate product
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index bdbc391d..30d89356 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -74,32 +74,34 @@ class ProductTemplate(models.Model): @api.constrains('name', 'default_code') def _check_duplicate_product(self): for product in self: - if not self.env.user.is_purchasing_manager and not self.env.user.is_editor_product and self.env.user.id not in [1, 25]: - domain = [('default_code', '!=', False)] - - 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.append(('id', '!=', product.id)) - - 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 = "Hanya Pak Tyas yang dapat merubah data produk" - existing_purchase = self.search(domain, limit=1) - if existing_purchase: - raise UserError(message) + 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)] + + 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.append(('id', '!=', product.id)) + + 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) @api.constrains('name') def _validate_name(self): @@ -379,24 +381,26 @@ class ProductProduct(models.Model): @api.constrains('name','default_code') def _check_duplicate_product(self): for product in self: - if not self.env.user.is_purchasing_manager and not self.env.user.is_editor_product and self.env.user.id not in [1, 25]: - 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('Hanya Pak Tyas Yang Dapat Merubah Data Product') + 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): |
