Add ability to "transform" TD parent row from pack to unit item

to make "claiming" more straightforward
This commit is contained in:
Lance Edgar 2018-10-24 18:52:49 -05:00
parent 2bd107056c
commit 05c33a4b34
4 changed files with 164 additions and 0 deletions

View file

@ -694,6 +694,75 @@ class ReceivingBatchView(PurchasingBatchView):
g.hide_column('cases_ordered')
g.hide_column('units_ordered')
# add "Transform to Unit" action, if appropriate
if batch.is_truck_dump_parent():
permission_prefix = self.get_permission_prefix()
if self.request.has_perm('{}.edit_row'.format(permission_prefix)):
transform = grids.GridAction('transform',
icon='shuffle',
label="Transform to Unit",
url=self.transform_unit_url)
g.more_actions.append(transform)
if g.main_actions and g.main_actions[-1].key == 'delete':
delete = g.main_actions.pop()
g.more_actions.append(delete)
def transform_unit_url(self, row, i):
# grid action is shown only when we return a URL here
if self.row_editable(row):
if row.batch.is_truck_dump_parent():
if row.product and row.product.is_pack_item():
return self.get_row_action_url('transform_unit', row)
def transform_unit_row(self):
"""
View which transforms the given row, which is assumed to associate with
a "pack" item, such that it instead associates with the "unit" item,
with quantities adjusted accordingly.
"""
batch = self.get_instance()
row_uuid = self.request.params.get('row_uuid')
row = self.Session.query(model.PurchaseBatchRow).get(row_uuid) if row_uuid else None
if row and row.batch is batch and not row.removed:
pass # we're good
else:
if self.request.method == 'POST':
raise self.notfound()
return {'error': "Row not found."}
def normalize(product):
data = {
'upc': product.upc,
'item_id': product.item_id,
'description': product.description,
'size': product.size,
'case_quantity': None,
'cases_received': row.cases_received,
}
cost = product.cost_for_vendor(batch.vendor)
if cost:
data['case_quantity'] = cost.case_size
return data
if self.request.method == 'POST':
self.handler.transform_pack_to_unit(row)
self.request.session.flash("Transformed pack to unit item for: {}".format(row.product))
return self.redirect(self.get_action_url('view', batch))
pack_data = normalize(row.product)
pack_data['units_received'] = row.units_received
unit_data = normalize(row.product.unit)
unit_data['units_received'] = None
if row.units_received:
unit_data['units_received'] = row.units_received * row.product.pack_size
diff = self.make_diff(pack_data, unit_data, monospace=True)
return self.render_to_response('transform_unit_row', {
'batch': batch,
'row': row,
'diff': diff,
})
def configure_row_form(self, f):
super(ReceivingBatchView, self).configure_row_form(f)
batch = self.get_instance()
@ -1318,10 +1387,17 @@ class ReceivingBatchView(PurchasingBatchView):
permission_prefix = cls.get_permission_prefix()
if cls.allow_truck_dump:
# add TD child batch, from invoice file
config.add_route('{}.add_child_from_invoice'.format(route_prefix), '{}/{{{}}}/add-child-from-invoice'.format(url_prefix, model_key))
config.add_view(cls, attr='add_child_from_invoice', route_name='{}.add_child_from_invoice'.format(route_prefix),
permission='{}.create'.format(permission_prefix))
# transform TD parent row from "pack" to "unit" item
config.add_route('{}.transform_unit_row'.format(route_prefix), '{}/{{{}}}/transform-unit'.format(url_prefix, model_key))
config.add_view(cls, attr='transform_unit_row', route_name='{}.transform_unit_row'.format(route_prefix),
permission='{}.edit_row'.format(permission_prefix), renderer='json')
@classmethod
def defaults(cls, config):
cls._receiving_defaults(config)