[IMP] barcodes_gs1_nomenclature: add support to search with gs1

Provide a preprocess method to add support to search record with gs1
barcode.

Task-2648993

closes odoo/odoo#83462

Related: odoo/enterprise#23781
Signed-off-by: Steve Van Essche <svs@odoo.com>
This commit is contained in:
yhu-odoo
2022-06-23 11:15:48 +02:00
parent bc3ce2f6da
commit 2dd9b0b77a
@@ -124,3 +124,43 @@ class BarcodeNomenclature(models.Model):
if self.is_gs1_nomenclature:
return self.gs1_decompose_extanded(barcode)
return super().parse_barcode(barcode)
@api.model
def _preprocess_gs1_search_args(self, args, barcode_types, field='barcode'):
"""Helper method to preprocess 'args' in _search method to add support to
search with GS1 barcode result.
Cut off the padding if using GS1 and searching on barcode. If the barcode
is only digits to keep the original barcode part only.
"""
nomenclature = self.env.company.nomenclature_id
if nomenclature.is_gs1_nomenclature:
for i, arg in enumerate(args):
if not isinstance(arg, (list, tuple)) or len(arg) != 3:
continue
field_name, operator, value = arg
if field_name != field or operator not in ['ilike', 'not ilike', '=', '!=']:
continue
parsed_data = []
try:
parsed_data += nomenclature.parse_barcode(value) or []
except (ValidationError, ValueError):
pass
replacing_operator = 'ilike' if operator in ['ilike', '='] else 'not ilike'
for data in parsed_data:
data_type = data['rule'].type
value = data['value']
if data_type in barcode_types:
match = re.match('0*([0-9]+)$', value)
if match:
unpadded_barcode = match.groups()[0]
args[i] = (field_name, replacing_operator, unpadded_barcode)
break
# The barcode isn't a valid GS1 barcode, checks if it can be unpadded.
if not parsed_data:
match = re.match('0+([0-9]+)$', value)
if match:
args[i] = (field_name, replacing_operator, match.groups()[0])
return args