Expose "true cost" and "true margin" columns for products grid

This commit is contained in:
Lance Edgar 2019-03-08 14:33:57 -06:00
parent 15c47fb593
commit d096909a95

View file

@ -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: