Add initial support for 'costing' mode for purchase batches

This commit is contained in:
Lance Edgar 2016-11-21 03:37:45 -06:00
parent 67f6c11307
commit 8399eb8f91
3 changed files with 60 additions and 14 deletions

View file

@ -12,18 +12,30 @@
$('.field-wrapper.buyer_uuid').show(); $('.field-wrapper.buyer_uuid').show();
$('.field-wrapper.date_ordered').show(); $('.field-wrapper.date_ordered').show();
$('.field-wrapper.date_received').hide(); $('.field-wrapper.date_received').hide();
$('.field-wrapper.invoice_date').hide();
$('.field-wrapper.invoice_number').hide();
} else if (mode == ${enum.PURCHASE_BATCH_MODE_RECEIVING}) { } else if (mode == ${enum.PURCHASE_BATCH_MODE_RECEIVING}) {
$('.field-wrapper.store_uuid').hide(); $('.field-wrapper.store_uuid').hide();
$('.field-wrapper.purchase_uuid').show(); $('.field-wrapper.purchase_uuid').show();
$('.field-wrapper.buyer_uuid').hide(); $('.field-wrapper.buyer_uuid').hide();
$('.field-wrapper.date_ordered').hide(); $('.field-wrapper.date_ordered').hide();
$('.field-wrapper.date_received').show(); $('.field-wrapper.date_received').show();
$('.field-wrapper.invoice_date').show();
$('.field-wrapper.invoice_number').show();
} else if (mode == ${enum.PURCHASE_BATCH_MODE_COSTING}) {
$('.field-wrapper.store_uuid').hide();
$('.field-wrapper.purchase_uuid').show();
$('.field-wrapper.buyer_uuid').hide();
$('.field-wrapper.date_ordered').hide();
$('.field-wrapper.date_received').hide();
$('.field-wrapper.invoice_date').show();
$('.field-wrapper.invoice_number').show();
} }
} }
function vendor_selected(uuid, name) { function vendor_selected(uuid, name) {
var mode = $('.mode select').val(); var mode = $('.mode select').val();
if (mode == ${enum.PURCHASE_BATCH_MODE_RECEIVING}) { if (mode == ${enum.PURCHASE_BATCH_MODE_RECEIVING} || mode == ${enum.PURCHASE_BATCH_MODE_COSTING}) {
var purchases = $('.purchase_uuid select'); var purchases = $('.purchase_uuid select');
purchases.empty(); purchases.empty();

View file

@ -143,6 +143,8 @@ class PurchaseBatchView(BatchMasterView):
fs.date_received, fs.date_received,
fs.po_number, fs.po_number,
fs.po_total, fs.po_total,
fs.invoice_date,
fs.invoice_number,
fs.invoice_total, fs.invoice_total,
fs.created, fs.created,
fs.created_by, fs.created_by,
@ -179,9 +181,8 @@ class PurchaseBatchView(BatchMasterView):
fs.model.date_ordered = today fs.model.date_ordered = today
fs.model.date_received = today fs.model.date_received = today
# TODO: temp hack until we support more modes # TODO: restrict via permission per batch mode?
modes = dict(self.enum.PURCHASE_BATCH_MODE) modes = dict(self.enum.PURCHASE_BATCH_MODE)
del modes[self.enum.PURCHASE_BATCH_MODE_COSTING]
fs.mode.set(renderer=forms.renderers.EnumFieldRenderer(modes)) fs.mode.set(renderer=forms.renderers.EnumFieldRenderer(modes))
elif self.editing: elif self.editing:
@ -206,12 +207,21 @@ class PurchaseBatchView(BatchMasterView):
if mode == enum.PURCHASE_BATCH_MODE_RECEIVING: if mode == enum.PURCHASE_BATCH_MODE_RECEIVING:
purchases = purchases.filter(model.Purchase.status == self.enum.PURCHASE_STATUS_ORDERED)\ purchases = purchases.filter(model.Purchase.status == self.enum.PURCHASE_STATUS_ORDERED)\
.order_by(model.Purchase.date_ordered, model.Purchase.created) .order_by(model.Purchase.date_ordered, model.Purchase.created)
elif mode == enum.PURCHASE_BATCH_MODE_COSTING:
purchases = purchases.filter(model.Purchase.status == self.enum.PURCHASE_STATUS_RECEIVED)\
.order_by(model.Purchase.date_received, model.Purchase.created)
return {'purchases': [{'uuid': p.uuid, 'display': self.render_eligible_purchase(p)} return {'purchases': [{'uuid': p.uuid, 'display': self.render_eligible_purchase(p)}
for p in purchases]} for p in purchases]}
def render_eligible_purchase(self, purchase): def render_eligible_purchase(self, purchase):
return '{} for ${:0,.2f} ({})'.format(purchase.date_ordered, purchase.po_total, purchase.buyer) if purchase.status == self.enum.PURCHASE_STATUS_ORDERED:
date = purchase.date_ordered
total = purchase.po_total
elif purchase.status == self.enum.PURCHASE_STATUS_RECEIVED:
date = purchase.date_received
total = purchase.invoice_total
return '{} for ${:0,.2f} ({})'.format(date, total, purchase.buyer)
def get_batch_kwargs(self, batch): def get_batch_kwargs(self, batch):
kwargs = super(PurchaseBatchView, self).get_batch_kwargs(batch) kwargs = super(PurchaseBatchView, self).get_batch_kwargs(batch)
@ -235,6 +245,12 @@ class PurchaseBatchView(BatchMasterView):
elif batch.mode == self.enum.PURCHASE_BATCH_MODE_RECEIVING: elif batch.mode == self.enum.PURCHASE_BATCH_MODE_RECEIVING:
kwargs['date_received'] = batch.date_received kwargs['date_received'] = batch.date_received
elif batch.mode == self.enum.PURCHASE_BATCH_MODE_COSTING:
kwargs['invoice_date'] = batch.invoice_date
if batch.mode in (self.enum.PURCHASE_BATCH_MODE_RECEIVING,
self.enum.PURCHASE_BATCH_MODE_COSTING):
if batch.purchase_uuid: if batch.purchase_uuid:
purchase = Session.query(model.Purchase).get(batch.purchase_uuid) purchase = Session.query(model.Purchase).get(batch.purchase_uuid)
assert purchase assert purchase
@ -293,7 +309,8 @@ class PurchaseBatchView(BatchMasterView):
del g.cases_received del g.cases_received
del g.units_received del g.units_received
del g.invoice_total del g.invoice_total
elif batch.mode == self.enum.PURCHASE_BATCH_MODE_RECEIVING: elif batch.mode in (self.enum.PURCHASE_BATCH_MODE_RECEIVING,
self.enum.PURCHASE_BATCH_MODE_COSTING):
del g.po_total del g.po_total
def make_row_grid_tools(self, batch): def make_row_grid_tools(self, batch):

View file

@ -26,7 +26,6 @@ Views for "true" purchase orders
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from rattail import enum
from rattail.db import model from rattail.db import model
import formalchemy as fa import formalchemy as fa
@ -44,12 +43,20 @@ class BatchesFieldRenderer(fa.FieldRenderer):
if not batches: if not batches:
return '' return ''
enum = self.request.rattail_config.get_enum()
def render(batch): def render(batch):
display = '{} ({}){}'.format(batch.id_str, enum.PURCHASE_BATCH_MODE[batch.mode], if batch.executed:
'' if batch.executed else ' (pending)') actor = batch.executed_by
pending = ''
else:
actor = batch.created_by
pending = ' (pending)'
display = '{} ({} by {}){}'.format(batch.id_str,
enum.PURCHASE_BATCH_MODE[batch.mode],
actor, pending)
return tags.link_to(display, self.request.route_url('purchases.batch.view', uuid=batch.uuid)) return tags.link_to(display, self.request.route_url('purchases.batch.view', uuid=batch.uuid))
enum = self.request.rattail_config.get_enum()
items = [HTML.tag('li', c=render(batch)) for batch in batches] items = [HTML.tag('li', c=render(batch)) for batch in batches]
return HTML.tag('ul', c=items) return HTML.tag('ul', c=items)
@ -67,7 +74,13 @@ class PurchaseView(MasterView):
row_model_title = 'Purchase Item' row_model_title = 'Purchase Item'
def get_instance_title(self, purchase): def get_instance_title(self, purchase):
if purchase.status >= self.enum.PURCHASE_STATUS_RECEIVED: if purchase.status >= self.enum.PURCHASE_STATUS_COSTED:
if purchase.invoice_date:
return "{} (invoiced {})".format(purchase.vendor, purchase.invoice_date.strftime('%Y-%m-%d'))
if purchase.date_received:
return "{} (invoiced {})".format(purchase.vendor, purchase.date_received.strftime('%Y-%m-%d'))
return "{} (invoiced)".format(purchase.vendor)
elif purchase.status >= self.enum.PURCHASE_STATUS_RECEIVED:
if purchase.date_received: if purchase.date_received:
return "{} (received {})".format(purchase.vendor, purchase.date_received.strftime('%Y-%m-%d')) return "{} (received {})".format(purchase.vendor, purchase.date_received.strftime('%Y-%m-%d'))
return "{} (received)".format(purchase.vendor) return "{} (received)".format(purchase.vendor)
@ -100,7 +113,8 @@ class PurchaseView(MasterView):
g.default_sortdir = 'desc' g.default_sortdir = 'desc'
g.date_ordered.set(label="Ordered") g.date_ordered.set(label="Ordered")
g.status.set(renderer=forms.renderers.EnumFieldRenderer(enum.PURCHASE_STATUS)) g.date_received.set(label="Received")
g.status.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_STATUS))
def configure_grid(self, g): def configure_grid(self, g):
g.configure( g.configure(
@ -109,13 +123,14 @@ class PurchaseView(MasterView):
g.vendor, g.vendor,
g.buyer, g.buyer,
g.date_ordered, g.date_ordered,
g.date_received,
g.status, g.status,
], ],
readonly=True) readonly=True)
def _preconfigure_fieldset(self, fs): def _preconfigure_fieldset(self, fs):
fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer) fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer)
fs.status.set(renderer=forms.renderers.EnumFieldRenderer(enum.PURCHASE_STATUS), fs.status.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_STATUS),
readonly=True) readonly=True)
fs.po_number.set(label="PO Number") fs.po_number.set(label="PO Number")
fs.po_total.set(label="PO Total", renderer=forms.renderers.CurrencyFieldRenderer) fs.po_total.set(label="PO Total", renderer=forms.renderers.CurrencyFieldRenderer)
@ -133,6 +148,7 @@ class PurchaseView(MasterView):
fs.date_received, fs.date_received,
fs.po_number, fs.po_number,
fs.po_total, fs.po_total,
fs.invoice_date,
fs.invoice_number, fs.invoice_number,
fs.invoice_total, fs.invoice_total,
fs.created, fs.created,
@ -182,11 +198,12 @@ class PurchaseView(MasterView):
g.invoice_total, g.invoice_total,
], ],
readonly=True) readonly=True)
if purchase.status == enum.PURCHASE_STATUS_ORDERED: if purchase.status == self.enum.PURCHASE_STATUS_ORDERED:
del g.cases_received del g.cases_received
del g.units_received del g.units_received
del g.invoice_total del g.invoice_total
elif purchase.status == enum.PURCHASE_STATUS_RECEIVED: elif purchase.status in (self.enum.PURCHASE_STATUS_RECEIVED,
self.enum.PURCHASE_STATUS_COSTED):
del g.po_total del g.po_total
def _preconfigure_row_fieldset(self, fs): def _preconfigure_row_fieldset(self, fs):