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

@ -10,6 +10,10 @@ table.diff {
min-width: 80%;
}
.ui-dialog-content table.diff {
color: black;
}
table.diff th,
table.diff td {
border-bottom: 1px solid Black;

View file

@ -0,0 +1,16 @@
## -*- coding: utf-8; -*-
<p>
This row is associated with a "pack" item, but you may transform it, so it
associates with the "unit" item instead:
</p>
<br />
${diff.render_html()}
<br />
<p>
Transforming to the unit item may help with "claiming" between Truck Dump
parent and child rows.
</p>

View file

@ -0,0 +1,68 @@
## -*- coding: utf-8; -*-
<%inherit file="/batch/view.mako" />
<%def name="extra_javascript()">
${parent.extra_javascript()}
% if request.has_perm('{}.edit_row'.format(permission_prefix)):
<script type="text/javascript">
$(function() {
$('.grid-wrapper').on('click', '.grid .actions a.transform', function() {
var form = $('form[name="transform-unit-form"]');
var row_uuid = $(this).parents('tr:first').data('uuid');
form.find('[name="row_uuid"]').val(row_uuid);
$.get(form.attr('action'), {row_uuid: row_uuid}, function(data) {
if (typeof(data) == 'object') {
alert(data.error);
} else {
$('#transform-unit-dialog').html(data);
$('#transform-unit-dialog').dialog({
title: "Transform Pack to Unit Item",
width: 800,
height: 450,
modal: true,
buttons: [
{
text: "Transform",
click: function(event) {
disable_button(dialog_button(event));
form.submit();
}
},
{
text: "Cancel",
click: function() {
$(this).dialog('close');
}
}
]
});
}
});
return false;
});
});
</script>
% endif
</%def>
${parent.body()}
% if request.has_perm('{}.edit_row'.format(permission_prefix)):
${h.form(url('{}.transform_unit_row'.format(route_prefix), uuid=batch.uuid), name='transform-unit-form')}
${h.csrf_token(request)}
${h.hidden('row_uuid')}
${h.end_form()}
<div id="transform-unit-dialog" style="display: none;">
<p>hello world</p>
</div>
% endif

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)