From 2dd9b0b77ae52108a1013a50d8e72f81d69e4d00 Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Wed, 8 Jun 2022 15:46:36 +0000 Subject: [PATCH] [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 --- .../models/barcode_nomenclature.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/addons/barcodes_gs1_nomenclature/models/barcode_nomenclature.py b/addons/barcodes_gs1_nomenclature/models/barcode_nomenclature.py index b7a5a5e8f63..99c42f80d1a 100644 --- a/addons/barcodes_gs1_nomenclature/models/barcode_nomenclature.py +++ b/addons/barcodes_gs1_nomenclature/models/barcode_nomenclature.py @@ -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