diff --git a/tailbone/templates/mobile/batch/inventory/view_row.mako b/tailbone/templates/mobile/batch/inventory/view_row.mako index 5ef6165a..46f3abfe 100644 --- a/tailbone/templates/mobile/batch/inventory/view_row.mako +++ b/tailbone/templates/mobile/batch/inventory/view_row.mako @@ -11,7 +11,10 @@ if row.cases: uom = 'CS' elif row.units: - uom = 'EA' + if row.product and row.product.weighed: + uom = 'LB' + else: + uom = 'EA' elif row.case_quantity: uom = 'CS' else: diff --git a/tailbone/views/inventory.py b/tailbone/views/inventory.py index 83cc77a3..215322ca 100644 --- a/tailbone/views/inventory.py +++ b/tailbone/views/inventory.py @@ -27,6 +27,7 @@ Views for inventory batches from __future__ import unicode_literals, absolute_import import re +import decimal import six @@ -239,24 +240,8 @@ class InventoryBatchView(BatchMasterView): if upc: if len(upc) <= 14: - - # 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) - - else: + row = self.add_row_for_upc(batch, upc) + if not row: self.request.session.flash("Product not found: {}".format(upc), 'error') return self.redirect(self.get_action_url('view', batch, mobile=True)) @@ -267,6 +252,27 @@ class InventoryBatchView(BatchMasterView): self.Session.flush() 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): row = kwargs['instance'] kwargs['product_image_url'] = pod.get_image_url(self.rattail_config, row.upc) @@ -425,12 +431,21 @@ class ValidBatchRow(forms.validators.ModelValidator): 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): allow_extra_fields = True filter_extra_fields = True row = ValidBatchRow() - cases = fe.validators.Number() - units = fe.validators.Number() + cases = Decimal() + units = Decimal() def includeme(config):