From b515331e483b8aa04db48a9368c6b3193bca480e Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 9 May 2018 15:58:09 -0500 Subject: [PATCH] Allow lookup of inventory item by alternate code i.e. in addition to UPC. but only if so configured --- tailbone/views/inventory.py | 38 +++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/tailbone/views/inventory.py b/tailbone/views/inventory.py index bff6c048..b10ade6f 100644 --- a/tailbone/views/inventory.py +++ b/tailbone/views/inventory.py @@ -324,25 +324,39 @@ class InventoryBatchView(BatchMasterView): 'redirect': self.get_action_url('view', batch), } data = {} - upc = self.request.GET.get('upc', '').strip() - upc = re.sub(r'\D', '', upc) + entry = self.request.GET.get('upc', '') + product = self.find_product(entry) + data = self.product_info(product) + + result = {'product': data or None, 'upc_raw': entry, 'upc': None} + if not data: + upc = re.sub(r'\D', '', entry.strip()) + if upc: + upc = GPC(upc) + result['upc'] = six.text_type(upc) + result['upc_pretty'] = upc.pretty() + result['image_url'] = pod.get_image_url(self.rattail_config, upc) + return result + + def find_product(self, entry): + upc = re.sub(r'\D', '', entry.strip()) if upc: # first try to locate existing batch row by UPC match provided = GPC(upc, calc_check_digit=False) checked = GPC(upc, calc_check_digit='upc') product = api.get_product_by_upc(self.Session(), provided) - if not product: - product = api.get_product_by_upc(self.Session(), checked) - data = self.product_info(product) + if product: + return product + product = api.get_product_by_upc(self.Session(), checked) + if product: + return product - result = {'product': data or None, 'upc_raw': upc, 'upc': None} - if not data and upc: - upc = GPC(upc) - result['upc'] = six.text_type(upc) - result['upc_pretty'] = upc.pretty() - result['image_url'] = pod.get_image_url(self.rattail_config, upc) - return result + # maybe try to locate product by alternate code + if self.rattail_config.getbool('tailbone', 'inventory.lookup_by_code', default=False): + product = api.get_product_by_code(self.Session(), entry) + if product: + return product def product_info(self, product): data = {}