Let custom inventory batch view override logic for mobile UPC scanning

This commit is contained in:
Lance Edgar 2018-01-23 19:00:33 -06:00
parent eefc3b33d7
commit 04d1e303be
2 changed files with 39 additions and 21 deletions

View file

@ -11,7 +11,10 @@
if row.cases: if row.cases:
uom = 'CS' uom = 'CS'
elif row.units: elif row.units:
uom = 'EA' if row.product and row.product.weighed:
uom = 'LB'
else:
uom = 'EA'
elif row.case_quantity: elif row.case_quantity:
uom = 'CS' uom = 'CS'
else: else:

View file

@ -27,6 +27,7 @@ Views for inventory batches
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import re import re
import decimal
import six import six
@ -239,24 +240,8 @@ class InventoryBatchView(BatchMasterView):
if upc: if upc:
if len(upc) <= 14: if len(upc) <= 14:
row = self.add_row_for_upc(batch, upc)
# try to locate general product by UPC; add to batch either way if not row:
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)
if product or self.unknown_product_creates_row:
row = model.InventoryBatchRow()
if product:
row.product = product
row.upc = product.upc
else:
row.upc = provided # TODO: why not 'checked' instead? how to choose?
row.description = "(unknown product)"
self.handler.add_row(batch, row)
else:
self.request.session.flash("Product not found: {}".format(upc), 'error') self.request.session.flash("Product not found: {}".format(upc), 'error')
return self.redirect(self.get_action_url('view', batch, mobile=True)) return self.redirect(self.get_action_url('view', batch, mobile=True))
@ -267,6 +252,27 @@ class InventoryBatchView(BatchMasterView):
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))
def add_row_for_upc(self, batch, upc):
"""
Add a row to the batch for the given UPC, if applicable.
"""
# try to locate general product by UPC; add to batch either way
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)
if product or self.unknown_product_creates_row:
row = model.InventoryBatchRow()
if product:
row.product = product
row.upc = product.upc
else:
row.upc = provided # TODO: why not 'checked' instead? how to choose?
row.description = "(unknown product)"
self.handler.add_row(batch, row)
return row
def template_kwargs_view_row(self, **kwargs): def template_kwargs_view_row(self, **kwargs):
row = kwargs['instance'] row = kwargs['instance']
kwargs['product_image_url'] = pod.get_image_url(self.rattail_config, row.upc) kwargs['product_image_url'] = pod.get_image_url(self.rattail_config, row.upc)
@ -425,12 +431,21 @@ class ValidBatchRow(forms.validators.ModelValidator):
return row return row
class Decimal(fe.validators.Number):
def _to_python(self, value, state):
try:
return decimal.Decimal(value)
except ValueError:
raise Invalid(self.message('number', state), value, state)
class InventoryForm(forms.Schema): class InventoryForm(forms.Schema):
allow_extra_fields = True allow_extra_fields = True
filter_extra_fields = True filter_extra_fields = True
row = ValidBatchRow() row = ValidBatchRow()
cases = fe.validators.Number() cases = Decimal()
units = fe.validators.Number() units = Decimal()
def includeme(config): def includeme(config):