diff --git a/tailbone/views/purchasing/receiving.py b/tailbone/views/purchasing/receiving.py index 3e269d7b..723b51fc 100644 --- a/tailbone/views/purchasing/receiving.py +++ b/tailbone/views/purchasing/receiving.py @@ -223,49 +223,50 @@ class ReceivingBatchView(PurchasingBatchView): row = None upc = self.request.GET.get('upc', '').strip() upc = re.sub(r'\D', '', upc) - if upc: + if not upc: + self.request.session.flash("Invalid UPC: {}".format(self.request.GET.get('upc')), 'error') + return self.redirect(self.get_action_url('view', batch, mobile=True)) - # first try to locate existing batch row by UPC match - provided = GPC(upc, calc_check_digit=False) - checked = GPC(upc, calc_check_digit='upc') - rows = self.Session.query(model.PurchaseBatchRow)\ - .filter(model.PurchaseBatchRow.batch == batch)\ - .filter(model.PurchaseBatchRow.upc.in_((provided, checked)))\ - .filter(model.PurchaseBatchRow.removed == False)\ - .all() + # first try to locate existing batch row by UPC match + provided = GPC(upc, calc_check_digit=False) + checked = GPC(upc, calc_check_digit='upc') + rows = self.Session.query(model.PurchaseBatchRow)\ + .filter(model.PurchaseBatchRow.batch == batch)\ + .filter(model.PurchaseBatchRow.upc.in_((provided, checked)))\ + .filter(model.PurchaseBatchRow.removed == False)\ + .all() - if rows: - if len(rows) > 1: - log.warning("found multiple UPC matches for {} in batch {}: {}".format( - upc, batch.id_str, batch)) - row = rows[0] + if rows: + if len(rows) > 1: + log.warning("found multiple UPC matches for {} in batch {}: {}".format( + upc, batch.id_str, batch)) + row = rows[0] + else: + + # try to locate general product by UPC; add to batch if found + product = api.get_product_by_upc(self.Session(), provided) + if not product: + product = api.get_product_by_upc(self.Session(), checked) + if product: + row = model.PurchaseBatchRow() + row.product = product + batch.add_row(row) + self.handler.refresh_row(row) + + # check for "bad" upc + elif len(upc) > 14: + self.request.session.flash("Invalid UPC: {}".format(upc), 'error') + return self.redirect(self.get_action_url('view', batch, mobile=True)) + + # product in system, but sane upc, so add to batch anyway else: - - # try to locate general product by UPC; add to batch if found - product = api.get_product_by_upc(self.Session(), provided) - if not product: - product = api.get_product_by_upc(self.Session(), checked) - if product: - row = model.PurchaseBatchRow() - row.product = product - batch.add_row(row) - self.handler.refresh_row(row) - - else: - - # if product not even in system, add to batch anyway.. - # but only if it was a "sane" UPC - if len(upc) <= 14: - row = model.PurchaseBatchRow() - row.upc = provided # TODO: why not checked? how to know? - row.description = "(unknown product)" - batch.add_row(row) - self.handler.refresh_row(row) - self.handler.refresh_batch_status(batch) - else: - self.request.session.flash("Invalid UPC: {}".format(upc), 'error') - return self.redirect(self.get_action_url('view', batch, mobile=True)) + row = model.PurchaseBatchRow() + row.upc = provided # TODO: why not checked? how to know? + row.description = "(unknown product)" + batch.add_row(row) + self.handler.refresh_row(row) + self.handler.refresh_batch_status(batch) self.Session.flush() return self.redirect(self.mobile_row_route_url('view', uuid=row.batch_uuid, row_uuid=row.uuid))