Updated statistics

* Added "total positive" and "total negative" credits.
* Fixed unicode for "by revenue"
* Fixed "no purchases" and "anti-purchases" cases for "by revenue"
This commit is contained in:
Christoffer Viken 2017-06-05 13:14:21 +00:00
parent 078ccf6b8a
commit de24644a51
1 changed files with 14 additions and 8 deletions

View File

@ -44,8 +44,8 @@ class ProductRevenueMenu(Menu):
text = '' text = ''
sub = \ sub = \
self.session.query(PurchaseEntry.product_id, self.session.query(PurchaseEntry.product_id,
func.count('*').label('purchase_count')) \ func.sum(PurchaseEntry.amount).label('purchase_count')) \
.group_by(PurchaseEntry.product_id) \ .filter(PurchaseEntry.amount > 0).group_by(PurchaseEntry.product_id) \
.subquery() .subquery()
product_list = \ product_list = \
self.session.query(Product, sub.c.purchase_count) \ self.session.query(Product, sub.c.purchase_count) \
@ -53,11 +53,14 @@ class ProductRevenueMenu(Menu):
.order_by(desc(sub.c.purchase_count * Product.price)) \ .order_by(desc(sub.c.purchase_count * Product.price)) \
.filter(sub.c.purchase_count is not None) \ .filter(sub.c.purchase_count is not None) \
.all() .all()
line_format = '%7s | %10s | %5s | %-' + str(Product.name_length) + 's\n' line_format = u'{0:7s} | {1:10s} | {2:6s} | {3:>45s}\n'
text += line_format % ('revenue', 'items sold', 'price', 'product') text += line_format.format('revenue', 'items sold', 'price', 'product')
text += '-' * (31 + Product.name_length) + '\n' text += '-' * (31 + Product.name_length) + '\n'
for product, number in product_list: for product, number in product_list:
text += line_format % (number * product.price, number, product.price, product.name) print(product.name)
if number is None:
continue
text += line_format.format(str(number * product.price), str(number), str(product.price), product.name)
less(text) less(text)
@ -73,12 +76,17 @@ class BalanceMenu(Menu):
for p in product_list: for p in product_list:
total_value += p.stock * p.price total_value += p.stock * p.price
total_credit = self.session.query(sqlalchemy.func.sum(User.credit)).first()[0] total_positive_credit = self.session.query(sqlalchemy.func.sum(User.credit)).filter(User.credit > 0).first()[0]
total_negative_credit = self.session.query(sqlalchemy.func.sum(User.credit)).filter(User.credit < 0).first()[0]
total_credit = total_positive_credit + total_negative_credit
total_balance = total_value - total_credit total_balance = total_value - total_credit
line_format = '%15s | %5d \n' line_format = '%15s | %5d \n'
text += line_format % ('Total value', total_value) text += line_format % ('Total value', total_value)
text += 24 * '-' + '\n' text += 24 * '-' + '\n'
text += line_format % ('Positive credit', total_positive_credit)
text += line_format % ('Negative credit', total_negative_credit)
text += line_format % ('Total credit', total_credit) text += line_format % ('Total credit', total_credit)
text += 24 * '-' + '\n' text += 24 * '-' + '\n'
text += line_format % ('Total balance', total_balance) text += line_format % ('Total balance', total_balance)
@ -91,5 +99,3 @@ class LoggedStatisticsMenu(Menu):
def _execute(self): def _execute(self):
statisticsTextOnly() statisticsTextOnly()