diff --git a/tailbone/forms/renderers/__init__.py b/tailbone/forms/renderers/__init__.py index 87d4dc4b..c238f537 100644 --- a/tailbone/forms/renderers/__init__.py +++ b/tailbone/forms/renderers/__init__.py @@ -47,6 +47,6 @@ from .products import (ProductFieldRenderer, GPCFieldRenderer, BrandFieldRendere from .stores import StoreFieldRenderer -from .vendors import VendorFieldRenderer +from .vendors import VendorFieldRenderer, PurchaseFieldRenderer from .batch import BatchIDFieldRenderer, HandheldBatchFieldRenderer diff --git a/tailbone/forms/renderers/vendors.py b/tailbone/forms/renderers/vendors.py index 372ac8bb..b0ce30e8 100644 --- a/tailbone/forms/renderers/vendors.py +++ b/tailbone/forms/renderers/vendors.py @@ -26,6 +26,7 @@ Vendor Field Renderers from __future__ import unicode_literals, absolute_import +import formalchemy as fa from webhelpers.html import tags from tailbone.forms.renderers.common import AutocompleteFieldRenderer @@ -42,3 +43,15 @@ class VendorFieldRenderer(AutocompleteFieldRenderer): if not vendor: return '' return tags.link_to(vendor, self.request.route_url('vendors.view', uuid=vendor.uuid)) + + +class PurchaseFieldRenderer(fa.FieldRenderer): + """ + Renderer for :class:`rattail.db.model.Purchase` relation fields. + """ + + def render_readonly(self, **kwargs): + purchase = self.raw_value + if not purchase: + return '' + return tags.link_to(purchase, self.request.route_url('purchases.view', uuid=purchase.uuid)) diff --git a/tailbone/templates/purchases/batches/create.mako b/tailbone/templates/purchases/batches/create.mako new file mode 100644 index 00000000..22a206d0 --- /dev/null +++ b/tailbone/templates/purchases/batches/create.mako @@ -0,0 +1,15 @@ +## -*- coding: utf-8 -*- +<%inherit file="/newbatch/create.mako" /> + +<%def name="head_tags()"> + ${parent.head_tags()} + + + +${parent.body()} diff --git a/tailbone/views/purchases/batch.py b/tailbone/views/purchases/batch.py index b7ba9be7..5132012b 100644 --- a/tailbone/views/purchases/batch.py +++ b/tailbone/views/purchases/batch.py @@ -74,11 +74,13 @@ class PurchaseBatchView(BatchMasterView): g.filters['complete'].default_verb = 'is_true' g.date_ordered.set(label="Ordered") + g.mode.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_BATCH_MODE)) def configure_grid(self, g): g.configure( include=[ g.id, + g.mode, g.vendor, g.buyer, g.date_ordered, @@ -90,6 +92,8 @@ class PurchaseBatchView(BatchMasterView): def _preconfigure_fieldset(self, fs): super(PurchaseBatchView, self)._preconfigure_fieldset(fs) + fs.mode.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.PURCHASE_BATCH_MODE)) + fs.purchase.set(renderer=forms.renderers.PurchaseFieldRenderer) fs.vendor.set(renderer=forms.renderers.VendorFieldRenderer) fs.buyer.set(renderer=forms.renderers.EmployeeFieldRenderer) fs.po_number.set(label="PO Number") @@ -118,6 +122,8 @@ class PurchaseBatchView(BatchMasterView): fs.configure( include=[ fs.id, + fs.mode, + fs.purchase, fs.store, fs.vendor, fs.vendor_email, @@ -136,6 +142,7 @@ class PurchaseBatchView(BatchMasterView): ]) if self.creating: + del fs.purchase del fs.po_total del fs.complete del fs.vendor_email @@ -159,10 +166,36 @@ class PurchaseBatchView(BatchMasterView): # default order date is today fs.model.date_ordered = localtime(self.rattail_config).date() + # TODO: temp hack until we support more modes + modes = dict(self.enum.PURCHASE_BATCH_MODE) + del modes[self.enum.PURCHASE_BATCH_MODE_RECEIVING] + del modes[self.enum.PURCHASE_BATCH_MODE_COSTING] + fs.mode.set(renderer=forms.renderers.EnumFieldRenderer(modes)) + elif self.editing: + fs.mode.set(readonly=True) fs.store.set(readonly=True) fs.vendor.set(readonly=True) + def get_batch_kwargs(self, batch): + kwargs = super(PurchaseBatchView, self).get_batch_kwargs(batch) + kwargs['mode'] = batch.mode + if batch.store: + kwargs['store'] = batch.store + elif batch.store_uuid: + kwargs['store_uuid'] = batch.store_uuid + if batch.vendor: + kwargs['vendor'] = batch.vendor + elif batch.vendor_uuid: + kwargs['vendor_uuid'] = batch.vendor_uuid + if batch.buyer: + kwargs['buyer'] = batch.buyer + elif batch.buyer_uuid: + kwargs['buyer_uuid'] = batch.buyer_uuid + kwargs['date_ordered'] = batch.date_ordered + kwargs['po_number'] = batch.po_number + return kwargs + def template_kwargs_view(self, **kwargs): kwargs = super(PurchaseBatchView, self).template_kwargs_view(**kwargs) vendor = kwargs['batch'].vendor @@ -289,10 +322,9 @@ class PurchaseBatchView(BatchMasterView): row.removed = True return self.redirect(self.get_action_url('view', row.batch)) - # TODO: redirect to new purchase... - # def get_execute_success_url(self, batch, result, **kwargs): - # # return self.get_action_url('view', batch) - # return + def get_execute_success_url(self, batch, result, **kwargs): + # redirect to associated purchase + return self.request.route_url('purchases.view', uuid=batch.purchase_uuid) def order_form(self): """ diff --git a/tailbone/views/purchases/core.py b/tailbone/views/purchases/core.py index 6c568a43..4716f8a9 100644 --- a/tailbone/views/purchases/core.py +++ b/tailbone/views/purchases/core.py @@ -30,12 +30,29 @@ from rattail import enum from rattail.db import model import formalchemy as fa +from webhelpers.html import HTML, tags from tailbone import forms from tailbone.db import Session from tailbone.views import MasterView +class BatchesFieldRenderer(fa.FieldRenderer): + + def render_readonly(self, **kwargs): + batches = self.raw_value + if not batches: + return '' + + def render(batch): + return tags.link_to('{} ({})'.format(batch.id_str, enum.PURCHASE_BATCH_MODE[batch.mode]), + 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] + return HTML.tag('ul', c=items) + + class PurchaseView(MasterView): """ Master view for purchase orders. @@ -90,6 +107,7 @@ class PurchaseView(MasterView): readonly=True) fs.po_number.set(label="PO Number") fs.po_total.set(label="PO Total") + fs.batches.set(renderer=BatchesFieldRenderer) def configure_fieldset(self, fs): fs.configure( @@ -103,6 +121,7 @@ class PurchaseView(MasterView): fs.status, fs.created, fs.created_by, + fs.batches, ]) def get_parent(self, item):