Allow update of row unit cost directly from receiving batch view

This commit is contained in:
Lance Edgar 2019-11-21 17:41:26 -06:00
parent b7c710cddd
commit c14cf3022c
4 changed files with 196 additions and 5 deletions

View file

@ -694,13 +694,12 @@ class BatchMasterView(MasterView):
if not batch.executed and not batch.complete:
# edit action
if self.rows_editable:
if self.rows_editable and self.has_perm('edit_row'):
icon = 'edit' if use_buefy else 'pencil'
actions.append(self.make_action('edit', icon=icon, url=self.row_edit_action_url))
# delete action
permission_prefix = self.get_permission_prefix()
if self.rows_deletable and self.request.has_perm('{}.delete_row'.format(permission_prefix)):
if self.rows_deletable and self.has_perm('delete_row'):
actions.append(self.make_action('delete', icon='trash', url=self.row_delete_action_url))
kwargs.setdefault('delete_speedbump', self.rows_deletable_speedbump)

View file

@ -489,7 +489,7 @@ class MasterView(View):
actions.append(self.make_action('view', icon=icon, url=view))
# edit action
if self.rows_editable:
if self.rows_editable and self.has_perm('edit_row'):
icon = 'edit' if use_buefy else 'pencil'
actions.append(self.make_action('edit', icon=icon, url=self.row_edit_action_url))

View file

@ -27,6 +27,7 @@ Views for 'receiving' (purchasing) batches
from __future__ import unicode_literals, absolute_import
import re
import decimal
import logging
import six
@ -222,6 +223,7 @@ class ReceivingBatchView(PurchasingBatchView):
'cases_received',
'units_received',
# 'po_total',
'invoice_unit_cost',
'invoice_total_calculated',
'credits',
'status_code',
@ -254,6 +256,7 @@ class ReceivingBatchView(PurchasingBatchView):
'po_total',
'invoice_line_number',
'invoice_unit_cost',
'invoice_cost_confirmed',
'invoice_total',
'invoice_total_calculated',
'status_code',
@ -780,6 +783,11 @@ class ReceivingBatchView(PurchasingBatchView):
super(ReceivingBatchView, self).configure_row_grid(g)
g.set_label('department_name', "Department")
# invoice_unit_cost
g.set_renderer('invoice_unit_cost', self.render_row_grid_unit_cost)
g.set_label('invoice_unit_cost', "Unit Cost")
g.filters['invoice_unit_cost'].label = "Invoice Unit Cost"
# credits
# note that sorting by credits involves a subquery with group by clause.
# seems likely there may be a better way? but this seems to work fine
@ -816,6 +824,20 @@ class ReceivingBatchView(PurchasingBatchView):
else:
g.set_enum('truck_dump_status', model.PurchaseBatchRow.STATUS)
def row_grid_extra_class(self, row, i):
css_class = super(ReceivingBatchView, self).row_grid_extra_class(row, i)
if row.invoice_cost_confirmed:
css_class = '{} invoice_cost_confirmed'.format(css_class or '')
return css_class
def render_row_grid_unit_cost(self, row, field):
cost = getattr(row, field)
if cost is None:
return ""
return "{:0,.3f}".format(cost)
def transform_unit_url(self, row, i):
# grid action is shown only when we return a URL here
if self.row_editable(row):
@ -1383,6 +1405,42 @@ class ReceivingBatchView(PurchasingBatchView):
def redirect_after_edit_row(self, row, mobile=False):
return self.redirect(self.get_row_action_url('view', row, mobile=mobile))
def update_row_cost(self):
"""
AJAX view for updating the invoice (actual) unit cost for a row.
"""
batch = self.get_instance()
data = self.request.POST
# validate row
uuid = data.get('row_uuid')
row = self.Session.query(model.PurchaseBatchRow).get(uuid) if uuid else None
if not row or row.batch is not batch:
return {'error': "Row not found"}
# validate cost
cost = data.get('invoice_unit_cost')
if cost is None:
return {'error': "You must specify a cost"}
try:
cost = decimal.Decimal(six.text_type(cost))
except decimal.InvalidOperation:
return {'error': "Cost is not valid!"}
# okay, update our row
self.handler.update_row_cost(row, invoice_unit_cost=cost)
return {
'row': {
'invoice_unit_cost': '{:0.3f}'.format(row.invoice_unit_cost),
'invoice_cost_confirmed': row.invoice_cost_confirmed,
'invoice_total_calculated': '{:0.2f}'.format(row.invoice_total_calculated),
},
'batch': {
'invoice_total_calculated': '{:0.2f}'.format(batch.invoice_total_calculated),
},
}
def render_mobile_row_listitem(self, row, i):
key = self.render_product_key_value(row)
description = row.product.full_description if row.product else row.description
@ -1721,6 +1779,7 @@ class ReceivingBatchView(PurchasingBatchView):
rattail_config = config.registry.settings.get('rattail_config')
route_prefix = cls.get_route_prefix()
url_prefix = cls.get_url_prefix()
instance_url_prefix = cls.get_instance_url_prefix()
model_key = cls.get_model_key()
permission_prefix = cls.get_permission_prefix()
@ -1737,6 +1796,12 @@ class ReceivingBatchView(PurchasingBatchView):
config.add_view(cls, attr='declare_credit', route_name='{}.declare_credit'.format(route_prefix),
permission='{}.edit_row'.format(permission_prefix))
# update row cost
config.add_route('{}.update_row_cost'.format(route_prefix), '{}/update-row-cost'.format(instance_url_prefix))
config.add_view(cls, attr='update_row_cost', route_name='{}.update_row_cost'.format(route_prefix),
permission='{}.edit_row'.format(permission_prefix),
renderer='json')
if cls.allow_truck_dump:
# add TD child batch, from invoice file