Debian Codegraph @

addons/barcodes_gs1_nomenclature/models/barcode_rule.py

raw
1 import re
2
3 from odoo import api, fields, models, _
4 from odoo.exceptions import ValidationError
5
6
7 class BarcodeRule(models.Model):
8 _inherit = 'barcode.rule'
9
10 def _default_encoding(self):
11 return 'gs1-128' if self.env.context.get('is_gs1') else 'any'
12
13 encoding = fields.Selection(
14 selection_add=[('gs1-128', 'GS1-128')], default=_default_encoding,
15 ondelete={'gs1-128': 'set default'})
16 type = fields.Selection(
17 selection_add=[
18 ('quantity', 'Quantity'),
19 ('location', 'Location'),
20 ('location_dest', 'Destination location'),
21 ('lot', 'Lot number'),
22 ('package', 'Package'),
23 ('use_date', 'Best before Date'),
24 ('expiration_date', 'Expiration Date'),
25 ('package_type', 'Package Type'),
26 ('pack_date', 'Pack Date'),
27 ], ondelete={
28 'quantity': 'set default',
29 'location': 'set default',
30 'location_dest': 'set default',
31 'lot': 'set default',
32 'package': 'set default',
33 'use_date': 'set default',
34 'expiration_date': 'set default',
35 'package_type': 'set default',
36 'pack_date': 'set default',
37 })
38 is_gs1_nomenclature = fields.Boolean(related="barcode_nomenclature_id.is_gs1_nomenclature")
39 gs1_content_type = fields.Selection([
40 ('date', 'Date'),
41 ('measure', 'Measure'),
42 ('identifier', 'Numeric Identifier'),
43 ('alpha', 'Alpha-Numeric Name'),
44 ], string="GS1 Content Type",
45 help="The GS1 content type defines what kind of data the rule will process the barcode as:\
46 * Date: the barcode will be converted into a Odoo datetime;\
47 * Measure: the barcode's value is related to a specific unit;\
48 * Numeric Identifier: fixed length barcode following a specific encoding;\
49 * Alpha-Numeric Name: variable length barcode.")
50 gs1_decimal_usage = fields.Boolean('Decimal', help="If True, use the last digit of AI to determine where the first decimal is")
51 associated_uom_id = fields.Many2one('uom.uom')
52
53 @api.constrains('pattern')
54 def _check_pattern(self):
55 gs1_rules = self.filtered(lambda rule: rule.encoding == 'gs1-128')
56 for rule in gs1_rules:
57 try:
58 re.compile(rule.pattern)
59 except re.error as error:
60 raise ValidationError(_("The rule pattern '%(rule)s' is not a valid Regex: %(error)s", rule=rule.name, error=error))
61 groups = re.findall(r'\([^)]*\)', rule.pattern)
62 if len(groups) != 2:
63 raise ValidationError(_(
64 "The rule pattern \"%s\" is not valid, it needs two groups:"
65 "\n\t- A first one for the Application Identifier (usually 2 to 4 digits);"
66 "\n\t- A second one to catch the value.",
67 rule.name))
68
69 super(BarcodeRule, (self - gs1_rules))._check_pattern()