From d096909a95debec5606cf3563737a0a24fc52cee Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 8 Mar 2019 14:33:57 -0600 Subject: [PATCH] Expose "true cost" and "true margin" columns for products grid --- tailbone/views/products.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tailbone/views/products.py b/tailbone/views/products.py index 3369e3e2..eb34839a 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -97,6 +97,8 @@ class ProductsView(MasterView): 'department', 'vendor', 'cost', + 'true_cost', + 'true_margin', 'regular_price', 'current_price', ] @@ -234,6 +236,21 @@ class ProductsView(MasterView): g.sorters['subdepartment'] = g.make_sorter(model.Subdepartment.name) g.sorters['vendor'] = g.make_sorter(model.Vendor.name) + ProductTrueCost = orm.aliased(model.ProductVolatile) + ProductTrueMargin = orm.aliased(model.ProductVolatile) + + # true_cost + g.set_joiner('true_cost', lambda q: q.outerjoin(ProductTrueCost)) + g.set_filter('true_cost', ProductTrueCost.true_cost) + g.set_sorter('true_cost', ProductTrueCost.true_cost) + g.set_renderer('true_cost', self.render_true_cost) + + # true_margin + g.set_joiner('true_margin', lambda q: q.outerjoin(ProductTrueMargin)) + g.set_filter('true_margin', ProductTrueMargin.true_margin) + g.set_sorter('true_margin', ProductTrueMargin.true_margin) + g.set_renderer('true_margin', self.render_true_margin) + # on_hand g.set_sorter('on_hand', model.ProductInventory.on_hand) g.set_filter('on_hand', model.ProductInventory.on_hand) @@ -455,6 +472,20 @@ class ProductsView(MasterView): return "" return "${:0.2f}".format(cost.unit_cost) + def render_true_cost(self, product, field): + if not product.volatile: + return "" + if product.volatile.true_cost is None: + return "" + return "${:0.3f}".format(product.volatile.true_cost) + + def render_true_margin(self, product, field): + if not product.volatile: + return "" + if product.volatile.true_margin is None: + return "" + return "{:0.3f} %".format(product.volatile.true_margin * 100) + def render_on_hand(self, product, column): inventory = product.inventory if not inventory: