Repository navigation
Expand file tree
/
Copy pathProduct_Template_Validation.py
More file actions
57 lines (46 loc) · 2.34 KB
/
Copy pathProduct_Template_Validation.py
File metadata and controls
57 lines (46 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Automation Rule for product.template
# Model: product.template
# Trigger: On Creation & Update
# Trigger Fields: barcode, default_code
#
# Description:
# Prevents duplicate barcodes and internal references by searching across product.template
# and the custom barcode line model product.template.barcode.
# Ensures that barcode and default_code are unique across all models.
# Check barcode field
if record.barcode:
barcode_to_check = record.barcode
# 1. Check for duplicates in product.template (other records)
domain = [
('id', '!=', record.id),
'|', ('barcode', '=', barcode_to_check), ('default_code', '=', barcode_to_check)
]
duplicate_product = env['product.template'].search(domain, limit=1)
if duplicate_product:
raise UserError("This barcode/reference is already used on product: %s" % duplicate_product.display_name)
# 2. Check for duplicates in product.template.barcode
duplicate_line = env['product.template.barcode'].search([('name', '=', barcode_to_check)], limit=1)
if duplicate_line:
p_name = "another product"
if 'product_tmpl_id' in duplicate_line._fields and duplicate_line.product_tmpl_id:
p_name = duplicate_line.product_tmpl_id.display_name
raise UserError("This barcode is already used as a barcode line on: %s" % p_name)
# Check default_code field (Internal Reference)
if record.default_code:
ref_to_check = record.default_code
# 1. Check for duplicates in product.template (other records)
domain = [
('id', '!=', record.id),
'|', ('barcode', '=', ref_to_check), ('default_code', '=', ref_to_check)
]
duplicate_product = env['product.template'].search(domain, limit=1)
if duplicate_product:
raise UserError("This reference/barcode is already used on product: %s" % duplicate_product.display_name)
# 2. Check for duplicates in product.template.barcode
duplicate_line = env['product.template.barcode'].search([('name', '=', ref_to_check)], limit=1)
if duplicate_line:
p_name = "another product"
if 'product_tmpl_id' in duplicate_line._fields and duplicate_line.product_tmpl_id:
p_name = duplicate_line.product_tmpl_id.display_name
raise UserError("This reference is already used as a barcode line on: %s" % p_name)
## Powered By HSx Tech