Refactor Order Form a bit to allow custom history data

This commit is contained in:
Lance Edgar 2016-12-29 13:23:21 -06:00
parent feed93eb61
commit 814fd12e9e
2 changed files with 32 additions and 27 deletions

View file

@ -155,14 +155,14 @@
<th>Vend. Code</th>
<th>Pref.</th>
<th>Unit Cost</th>
% for data in history.itervalues():
% for data in history:
<th>
% if data:
% if data['purchase'].date_ordered:
${data['purchase'].date_ordered.strftime('%m/%d') if data else ''}
% elif data['purchase'].date_received:
% if data['purchase']['date_ordered']:
${data['purchase']['date_ordered'].strftime('%m/%d')}
% elif data['purchase']['date_received']:
Rec.<br />
${data['purchase'].date_received.strftime('%m/%d') if data else ''}
${data['purchase']['date_received'].strftime('%m/%d')}
% else:
??
% endif
@ -191,15 +191,15 @@
<td class="code">${cost.code or ''}</td>
<td class="preferred">${'X' if cost.preference == 1 else ''}</td>
<td class="unit-cost">$${'{:0.2f}'.format(cost.unit_cost)}</td>
% for data in history.itervalues():
% for data in history:
<td class="scratch_pad">
% if data:
<% item = data['items'].get(cost.product_uuid) %>
% if item:
% if data['purchase'].date_ordered and (item.cases_ordered is not None or item.units_ordered is not None):
${'{} / {}'.format(int(item.cases_ordered or 0), int(item.units_ordered or 0))}
% elif item.cases_received is not None or item.units_received is not None:
${'{} / {}'.format(int(item.cases_received or 0), int(item.units_received or 0))}
% if data['purchase']['date_ordered'] and (item['cases_ordered'] is not None or item['units_ordered'] is not None):
${'{} / {}'.format(int(item['cases_ordered'] or 0), int(item['units_ordered'] or 0))}
% elif item['cases_received'] is not None or item['units_received'] is not None:
${'{} / {}'.format(int(item['cases_received'] or 0), int(item['units_received'] or 0))}
% endif
% endif
% endif

View file

@ -37,7 +37,7 @@ from rattail.db.util import make_full_description
from rattail.gpc import GPC
from rattail.time import localtime
from rattail.core import Object
from rattail.util import OrderedDict, pretty_quantity
from rattail.util import pretty_quantity
import formalchemy as fa
import formencode as fe
@ -584,23 +584,11 @@ class PurchaseBatchView(BatchMasterView):
# do anything else needed to satisfy template display requirements etc.
self.decorate_order_form_cost(cost)
# fetch last 6 purchases for this vendor, organize line items by product
history = OrderedDict()
purchases = Session.query(model.Purchase)\
.filter(model.Purchase.vendor == batch.vendor)\
.filter(model.Purchase.status >= self.enum.PURCHASE_STATUS_ORDERED)\
.order_by(model.Purchase.date_ordered.desc(), model.Purchase.created.desc())\
.options(orm.joinedload(model.Purchase.items))[:6]
for purchase in purchases[:6]:
items = {}
for item in purchase.items:
items[item.product_uuid] = item
history[purchase.uuid] = {'purchase': purchase, 'items': items}
# reverse sorting and pad history as needed, for template convenience
# fetch recent purchase history, sort/pad for template convenience
history = self.get_order_form_history(batch, costs, 6)
for i in range(6 - len(history)):
history[i] = None
history = OrderedDict([(i, v) for i, v in enumerate(reversed(list(history.itervalues())))])
history.append(None)
history = list(reversed(history))
title = self.get_instance_title(batch)
return self.render_to_response('order_form', {
@ -615,6 +603,23 @@ class PurchaseBatchView(BatchMasterView):
'get_upc': lambda p: p.upc.pretty() if p.upc else '',
})
def get_order_form_history(self, batch, costs, count):
# fetch last 6 purchases for this vendor, organize line items by product
history = []
purchases = Session.query(model.Purchase)\
.filter(model.Purchase.vendor == batch.vendor)\
.filter(model.Purchase.status >= self.enum.PURCHASE_STATUS_ORDERED)\
.order_by(model.Purchase.date_ordered.desc(), model.Purchase.created.desc())\
.options(orm.joinedload(model.Purchase.items))
for purchase in purchases[:count]:
items = {}
for item in purchase.items:
items[item.product_uuid] = item
history.append({'purchase': purchase, 'items': items})
return history
def get_order_form_costs(self, vendor):
return Session.query(model.ProductCost)\
.join(model.Product)\