Add even better UPC validation for mobile receiving

This commit is contained in:
Lance Edgar 2018-02-06 12:57:27 -06:00
parent 44dec830e5
commit 9ad8e5b546

View file

@ -223,49 +223,50 @@ class ReceivingBatchView(PurchasingBatchView):
row = None row = None
upc = self.request.GET.get('upc', '').strip() upc = self.request.GET.get('upc', '').strip()
upc = re.sub(r'\D', '', upc) 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 # first try to locate existing batch row by UPC match
provided = GPC(upc, calc_check_digit=False) provided = GPC(upc, calc_check_digit=False)
checked = GPC(upc, calc_check_digit='upc') checked = GPC(upc, calc_check_digit='upc')
rows = self.Session.query(model.PurchaseBatchRow)\ rows = self.Session.query(model.PurchaseBatchRow)\
.filter(model.PurchaseBatchRow.batch == batch)\ .filter(model.PurchaseBatchRow.batch == batch)\
.filter(model.PurchaseBatchRow.upc.in_((provided, checked)))\ .filter(model.PurchaseBatchRow.upc.in_((provided, checked)))\
.filter(model.PurchaseBatchRow.removed == False)\ .filter(model.PurchaseBatchRow.removed == False)\
.all() .all()
if rows: if rows:
if len(rows) > 1: if len(rows) > 1:
log.warning("found multiple UPC matches for {} in batch {}: {}".format( log.warning("found multiple UPC matches for {} in batch {}: {}".format(
upc, batch.id_str, batch)) upc, batch.id_str, batch))
row = rows[0] 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: else:
row = model.PurchaseBatchRow()
# try to locate general product by UPC; add to batch if found row.upc = provided # TODO: why not checked? how to know?
product = api.get_product_by_upc(self.Session(), provided) row.description = "(unknown product)"
if not product: batch.add_row(row)
product = api.get_product_by_upc(self.Session(), checked) self.handler.refresh_row(row)
if product: self.handler.refresh_batch_status(batch)
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))
self.Session.flush() self.Session.flush()
return self.redirect(self.mobile_row_route_url('view', uuid=row.batch_uuid, row_uuid=row.uuid)) return self.redirect(self.mobile_row_route_url('view', uuid=row.batch_uuid, row_uuid=row.uuid))