Add "cost history" dialog for product view

older jquery theme only, for now
This commit is contained in:
Lance Edgar 2020-01-16 11:56:33 -06:00
parent 91c1c1c5c8
commit 0e4b33be96
2 changed files with 103 additions and 2 deletions

View file

@ -1030,6 +1030,24 @@ class ProductsView(MasterView):
grid.set_type('changed', 'datetime')
kwargs['suggested_price_history_grid'] = grid
# cost history
data = self.get_cost_history(product)
grid = grids.Grid('products.cost_history', data,
request=self.request,
columns=[
'cost',
'vendor',
'since',
'changed',
'changed_by',
],
labels={
'price_type': "Type",
})
grid.set_type('cost', 'currency')
grid.set_type('changed', 'datetime')
kwargs['cost_history_grid'] = grid
kwargs['costs_label_preferred'] = "Pref."
kwargs['costs_label_vendor'] = "Vendor"
kwargs['costs_label_code'] = "Order Code"
@ -1289,6 +1307,58 @@ class ProductsView(MasterView):
return list(final_history.values())
def get_cost_history(self, product):
"""
Returns a sequence of "records" which corresponds to the given
product's cost history.
"""
Transaction = continuum.transaction_class(model.Product)
ProductVersion = continuum.version_class(model.Product)
ProductCostVersion = continuum.version_class(model.ProductCost)
now = make_utc()
history = []
# we just find all relevant (preferred!) ProductCostVersion records
versions = self.Session.query(ProductCostVersion)\
.join(Transaction,
Transaction.id == ProductCostVersion.transaction_id)\
.filter(ProductCostVersion.product_uuid == product.uuid)\
.filter(ProductCostVersion.preference == 1)\
.order_by(Transaction.issued_at,
Transaction.id)\
.all()
last_cost = None
last_vendor_uuid = None
for version in versions:
changed = False
if version.unit_cost != last_cost:
changed = True
elif version.vendor_uuid != last_vendor_uuid:
changed = True
if changed:
changed = version.transaction.issued_at
history.append({
'transaction_id': version.transaction.id,
'cost': version.unit_cost,
'vendor': version.vendor.name,
'since': humanize.naturaltime(now - changed),
'changed': changed,
'changed_by': version.transaction.user,
})
last_cost = version.unit_cost
last_vendor_uuid = version.vendor_uuid
final_history = OrderedDict()
for hist in sorted(history, key=lambda h: h['changed'], reverse=True):
if hist['transaction_id'] not in final_history:
final_history[hist['transaction_id']] = hist
return list(final_history.values())
def edit(self):
# TODO: Should add some more/better hooks, so don't have to duplicate
# so much code here.