From 05c33a4b347095b1244c5ef8ddfe46f5ce7bd8af Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 24 Oct 2018 18:52:49 -0500 Subject: [PATCH] Add ability to "transform" TD parent row from pack to unit item to make "claiming" more straightforward --- tailbone/static/css/diffs.css | 4 + .../receiving/transform_unit_row.mako | 16 ++++ tailbone/templates/receiving/view.mako | 68 +++++++++++++++++ tailbone/views/purchasing/receiving.py | 76 +++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 tailbone/templates/receiving/transform_unit_row.mako create mode 100644 tailbone/templates/receiving/view.mako diff --git a/tailbone/static/css/diffs.css b/tailbone/static/css/diffs.css index 9af7710c..662aae07 100644 --- a/tailbone/static/css/diffs.css +++ b/tailbone/static/css/diffs.css @@ -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; diff --git a/tailbone/templates/receiving/transform_unit_row.mako b/tailbone/templates/receiving/transform_unit_row.mako new file mode 100644 index 00000000..d003228c --- /dev/null +++ b/tailbone/templates/receiving/transform_unit_row.mako @@ -0,0 +1,16 @@ +## -*- coding: utf-8; -*- + +

+ This row is associated with a "pack" item, but you may transform it, so it + associates with the "unit" item instead: +

+ +
+ +${diff.render_html()} + +
+

+ Transforming to the unit item may help with "claiming" between Truck Dump + parent and child rows. +

diff --git a/tailbone/templates/receiving/view.mako b/tailbone/templates/receiving/view.mako new file mode 100644 index 00000000..95750bda --- /dev/null +++ b/tailbone/templates/receiving/view.mako @@ -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)): + + % endif + + +${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()} + + +% endif diff --git a/tailbone/views/purchasing/receiving.py b/tailbone/views/purchasing/receiving.py index 19aab266..b5332f2d 100644 --- a/tailbone/views/purchasing/receiving.py +++ b/tailbone/views/purchasing/receiving.py @@ -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)