Add initial support for mispicks / attaching credits for receiving batch

This commit is contained in:
Lance Edgar 2016-12-10 09:08:37 -06:00
parent b3010465b6
commit 839d3957dd
2 changed files with 259 additions and 49 deletions

View file

@ -53,13 +53,12 @@ log = logging.getLogger(__name__)
class ReceivingForm(forms.Schema):
allow_extra_fields = True
filter_extra_fields = True
mode = fe.validators.OneOf([
'received', 'damaged', 'expired',
# 'mispick',
])
mode = fe.validators.OneOf(['received', 'damaged', 'expired', 'mispick'])
product = forms.validators.ValidProduct()
cases = fe.validators.Int()
units = fe.validators.Int()
expiration_date = fe.validators.DateValidator()
ordered_product = forms.validators.ValidProduct()
class PurchaseBatchView(BatchMasterView):
@ -307,6 +306,10 @@ class PurchaseBatchView(BatchMasterView):
'tailbone', 'purchases.order_form.vendor_cost_warning_threshold', default=699)
return kwargs
def get_row_data(self, batch):
query = super(PurchaseBatchView, self).get_row_data(batch)
return query.options(orm.joinedload(model.PurchaseBatchRow.credits))
def _preconfigure_row_grid(self, g):
super(PurchaseBatchView, self)._preconfigure_row_grid(g)
@ -321,6 +324,8 @@ class PurchaseBatchView(BatchMasterView):
g.units_received.set(label="Units Rec.")
g.po_total.set(label="Total", renderer=forms.renderers.CurrencyFieldRenderer)
g.invoice_total.set(label="Total", renderer=forms.renderers.CurrencyFieldRenderer)
g.append(fa.Field('has_credits', type=fa.types.Boolean, label="Credits?",
value=lambda row: bool(row.credits)))
def configure_row_grid(self, g):
batch = self.get_instance()
@ -338,6 +343,7 @@ class PurchaseBatchView(BatchMasterView):
g.units_received,
g.po_total,
g.invoice_total,
g.has_credits,
g.status_code,
],
readonly=True)
@ -368,6 +374,7 @@ class PurchaseBatchView(BatchMasterView):
fs.po_unit_cost.set(label="PO Unit Cost")
fs.po_total.set(label="PO Total", renderer=forms.renderers.CurrencyFieldRenderer)
fs.invoice_total.set(renderer=forms.renderers.CurrencyFieldRenderer)
fs.credits.set(readonly=True)
fs.append(fa.Field('item_lookup', label="Item Lookup Code", required=True,
validate=self.item_lookup))
@ -410,9 +417,12 @@ class PurchaseBatchView(BatchMasterView):
fs.units_damaged,
fs.cases_expired,
fs.units_expired,
fs.cases_mispick,
fs.units_mispick,
fs.po_total,
fs.invoice_total,
fs.status_code,
fs.credits,
])
if self.creating:
@ -472,6 +482,8 @@ class PurchaseBatchView(BatchMasterView):
raise httpexceptions.HTTPNotFound()
if row.po_total:
row.batch.po_total -= row.po_total
if row.invoice_total:
row.batch.invoice_total -= row.invoice_total
row.removed = True
return self.redirect(self.get_action_url('view', row.batch))
@ -623,6 +635,43 @@ class PurchaseBatchView(BatchMasterView):
'batch_po_total': '${:0,.2f}'.format(batch.po_total),
}
def attach_credit(self, row, credit_type, cases, units, expiration_date=None, mispick_product=None):
batch = row.batch
credit = model.PurchaseBatchCredit()
credit.credit_type = credit_type
credit.store = batch.store
credit.vendor = batch.vendor
credit.date_ordered = batch.date_ordered
credit.date_shipped = batch.date_shipped
credit.date_received = batch.date_received
credit.invoice_number = batch.invoice_number
credit.invoice_date = batch.invoice_date
credit.product = row.product
credit.upc = row.upc
credit.brand_name = row.brand_name
credit.description = row.description
credit.size = row.size
credit.department_number = row.department_number
credit.department_name = row.department_name
credit.case_quantity = row.case_quantity
credit.cases_shorted = cases
credit.units_shorted = units
credit.invoice_line_number = row.invoice_line_number
credit.invoice_case_cost = row.invoice_case_cost
credit.invoice_unit_cost = row.invoice_unit_cost
credit.invoice_total = row.invoice_total
if credit_type == 'expired':
credit.expiration_date = expiration_date
elif credit_type == 'mispick' and mispick_product:
credit.mispick_product = mispick_product
credit.mispick_upc = mispick_product.upc
if mispick_product.brand:
credit.mispick_brand_name = mispick_product.brand.name
credit.mispick_description = mispick_product.description
credit.mispick_size = mispick_product.size
row.credits.append(credit)
return credit
def receiving_form(self):
"""
Workflow view for receiving items on a purchase batch.
@ -634,7 +683,9 @@ class PurchaseBatchView(BatchMasterView):
form = forms.SimpleForm(self.request, schema=ReceivingForm)
if form.validate():
product = form.data['product']
mode = form.data['mode']
shipped_product = form.data['product']
product = form.data['ordered_product'] if mode == 'mispick' else shipped_product
rows = [row for row in batch.active_rows() if row.product is product]
if rows:
if len(rows) > 1:
@ -644,20 +695,22 @@ class PurchaseBatchView(BatchMasterView):
else:
row = model.PurchaseBatchRow()
row.product = product
mode = form.data['mode']
if mode in ('received', 'damaged', 'expired'):
if form.data['cases']:
setattr(row, 'cases_{}'.format(mode),
(getattr(row, 'cases_{}'.format(mode)) or 0) + form.data['cases'])
if form.data['units']:
setattr(row, 'units_{}'.format(mode),
(getattr(row, 'units_{}'.format(mode)) or 0) + form.data['units'])
else:
assert False # TODO (mispick)
if not row.uuid:
batch.add_row(row)
cases = form.data['cases']
units = form.data['units']
if cases:
setattr(row, 'cases_{}'.format(mode),
(getattr(row, 'cases_{}'.format(mode)) or 0) + cases)
if units:
setattr(row, 'units_{}'.format(mode),
(getattr(row, 'units_{}'.format(mode)) or 0) + units)
if mode in ('damaged', 'expired', 'mispick'):
self.attach_credit(row, mode, cases, units,
expiration_date=form.data['expiration_date'],
mispick_product=shipped_product)
self.handler.refresh_row(row)
self.request.session.flash("({}) {} cases, {} units: {} {}".format(