From 5a2278add5c13d3c0962f57a23652ef3911d9835 Mon Sep 17 00:00:00 2001 From: tanshu Date: Mon, 6 Feb 2017 13:09:15 +0530 Subject: [PATCH] Added: Tax LedgerType Change: Changed unused is_current LedgerType to CashFlowClassification so that cash flow can be properly classified Fix: Account update (missing get_code function) Chore: Upgrade to angular 1.6 Feature: Change Cash Flow to new format to match international guidelines. Still a work in progress. Fix: Removed memoizing doFilter in the Tokenizer Service as it was giving errors. There is not that much processing, so it may also not be required. Will judge the impact later. --- brewman/models/master.py | 39 ++++++--- brewman/static/base.html | 14 +-- brewman/static/partial/cash-flow.html | 9 +- brewman/static/scripts/angular_service.js | 4 +- brewman/views/account.py | 2 +- brewman/views/reports/cash_flow.py | 101 +++++++++++++--------- 6 files changed, 100 insertions(+), 69 deletions(-) diff --git a/brewman/models/master.py b/brewman/models/master.py index 5de193fa..e461d743 100644 --- a/brewman/models/master.py +++ b/brewman/models/master.py @@ -274,6 +274,11 @@ class LedgerBase(Base): return False, 'Account has journal entries' return True, '' + @classmethod + def get_code(cls, type, dbsession): + code = dbsession.query(func.max(LedgerBase.code)).filter(LedgerBase.type == type).one()[0] + return 1 if code is None else code + 1 + @classmethod def all_purchases(cls): return uuid.UUID('240dd899-c413-854c-a7eb-67a29d154490') @@ -389,30 +394,36 @@ class AttendanceType: class LedgerType: - def __init__(self, id, name, balance_sheet=None, debit=None, is_current=None, order=None, show_in_list=None): + def __init__(self, id, name, balance_sheet=None, debit=None, cash_flow_classification=None, order=None, + show_in_list=None): self.id = id self.name = name self.balance_sheet = balance_sheet self.debit = debit - self.is_current = is_current + self.cash_flow_classification = cash_flow_classification + # Cash flow Classifications are: + # Cash + # Operating + # Investing + # Financing self.order = order self.show_in_list = show_in_list @classmethod def list(cls): - list = [LedgerType(1, 'Cash', True, True, True, 10, True), - LedgerType(2, 'Purchase', False, True, True, 20, True), - LedgerType(3, 'Sale', False, False, True, 10, True), - LedgerType(4, 'Assets', True, True, False, 20, True), - LedgerType(5, 'Capital', True, False, False, 70, True), - LedgerType(6, 'Debtors', True, True, True, 30, True), - LedgerType(7, 'Expenses', False, True, True, 40, True), - LedgerType(9, 'Creditors', True, False, True, 60, True), - LedgerType(10, 'Salary', True, True, True, 40, False), - LedgerType(11, 'Liabilities', True, False, True, 50, True), - LedgerType(12, 'Revenue', False, False, True, 30, True)] + list = [LedgerType(1, 'Cash', True, True, 'Cash', 10, True), + LedgerType(2, 'Purchase', False, True, 'Operating', 20, True), + LedgerType(3, 'Sale', False, False, 'Operating', 10, True), + LedgerType(4, 'Assets', True, True, 'Investing', 20, True), + LedgerType(5, 'Capital', True, False, 'Financing', 70, True), + LedgerType(6, 'Debtors', True, True, 'Operating', 30, True), + LedgerType(7, 'Expenses', False, True, 'Operating', 40, True), + LedgerType(9, 'Creditors', True, False, 'Operating', 60, True), + LedgerType(10, 'Salary', True, True, 'Operating', 40, False), + LedgerType(11, 'Liabilities', True, False, 'Operating', 50, True), + LedgerType(12, 'Revenue', False, False, 'Operating', 30, True), + LedgerType(13, 'Tax', True, False, 'Operating', 80, True)] # list.append(LedgerType(8, 'Discount', False, False, True, 30, True)) - # list.append(LedgerType(13, 'Tax', True, False, True, 80, True)) # list.append(LedgerType(14, 'Total', False, False, False, 900, False)) # list.append(LedgerType(15, 'Net', False, False, False, 1000, False)) return list diff --git a/brewman/static/base.html b/brewman/static/base.html index 060025ec..baf51923 100644 --- a/brewman/static/base.html +++ b/brewman/static/base.html @@ -35,13 +35,13 @@ - - - - - - - + + + + + + + diff --git a/brewman/static/partial/cash-flow.html b/brewman/static/partial/cash-flow.html index cababe05..3f5095d6 100644 --- a/brewman/static/partial/cash-flow.html +++ b/brewman/static/partial/cash-flow.html @@ -31,22 +31,19 @@ Name - Inflow - Outflow + Flow {{item.Name}} - {{item.Inflow | currency | clr}} - {{item.Outflow | currency | clr}} + {{item.Amount | currency | clr}} {{item.Name}} - {{item.Inflow | currency | clr}} - {{item.Outflow | currency | clr}} + {{item.Amount | currency | clr}} diff --git a/brewman/static/scripts/angular_service.js b/brewman/static/scripts/angular_service.js index 6c7cdd37..2b2b5e79 100644 --- a/brewman/static/scripts/angular_service.js +++ b/brewman/static/scripts/angular_service.js @@ -340,7 +340,7 @@ overlordService.factory('Tokenizer', ['$filter', function ($filter) { }); return {'q': matches, 'o': sorting, 'f': flags}; }; - var doFilter = _.memoize(function (q, array, matches) { + var doFilter = function (q, array, matches) { var filterExpressions = matches.q, sortPredicates = matches.o, filterCount = filterExpressions.length; @@ -351,7 +351,7 @@ overlordService.factory('Tokenizer', ['$filter', function ($filter) { }); arrayCopy = $filter('orderBy')(arrayCopy, sortPredicates); return arrayCopy; - }); + }; return {parseFilterString: parseFilterString, doFilter: doFilter}; }]); diff --git a/brewman/views/account.py b/brewman/views/account.py index b381b468..35bb63f9 100644 --- a/brewman/views/account.py +++ b/brewman/views/account.py @@ -38,7 +38,7 @@ def update(request): raise ValidationError("{0} is a fixture and cannot be edited or deleted.".format(item.name)) new_type = int(request.json_body['Type']) if not item.type == new_type: - item.code = Ledger.get_code(new_type) + item.code = Ledger.get_code(new_type, request.dbsession) item.type = new_type item.name = request.json_body['Name'] item.is_active = request.json_body['IsActive'] diff --git a/brewman/views/reports/cash_flow.py b/brewman/views/reports/cash_flow.py index fc9a0703..a35ee7bc 100644 --- a/brewman/views/reports/cash_flow.py +++ b/brewman/views/reports/cash_flow.py @@ -52,32 +52,46 @@ def build_report(request, start_date, finish_date): .filter(sub_voucher.date >= datetime.datetime.strptime(start_date, '%d-%b-%Y')) \ .filter(sub_voucher.date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y')).subquery() - query = request.dbsession.query(LedgerBase.type, func.sum(Journal.signed_amount)) \ - .join(Journal, Voucher.journals) \ - .join(LedgerBase, Journal.ledger) \ - .filter(Voucher.id.in_(sub_query)) \ - .filter(LedgerBase.type != LedgerType.by_name('Cash').id) \ - .group_by(LedgerBase.type) \ - .order_by(func.sum(Journal.signed_amount)).all() + query = request.dbsession.query( + LedgerBase.type, func.sum(Journal.signed_amount) + ).join( + Journal, Voucher.journals + ).join( + LedgerBase, Journal.ledger + ).filter( + Voucher.id.in_(sub_query) + ).filter( + LedgerBase.type != LedgerType.by_name('Cash').id + ).group_by( + LedgerBase.type + ).order_by( + func.sum(Journal.signed_amount) + ).all() - total_inflow = 0 - total_outflow = 0 + total_amount = 0 + cf = {'Operating': [], 'Investing': [], 'Financing': []} for ledgerType, amount in query: lt = LedgerType.by_id(ledgerType) - inflow = amount * -1 if amount < 0 else 0 - outflow = amount if amount >= 0 else 0 - total_inflow += (amount * -1) if amount < 0 else 0 - total_outflow += amount if amount >= 0 else 0 - report['Body'].append({'Name': lt.name, 'Url': request.route_url('cash_flow_id', id=str(lt.id), - _query={'StartDate': start_date, - 'FinishDate': finish_date}), - 'Inflow': inflow, 'Outflow': outflow}) + total_amount += (amount * -1) + cf[lt.cash_flow_classification].append( + { + 'Name': lt.name, + 'Url': request.route_url( + 'cash_flow_id', + id=str(lt.id), + _query={'StartDate': start_date, 'FinishDate': finish_date} + ), + 'Amount': amount * -1 + } + ) + for key, value in cf.items(): + if len(value) == 0: + continue + report['Body'].append({'Name': 'Cash flow from ' + key + ' activities'}) + for item in value: + report['Body'].append(item) - report['Footer'].append({'Name': 'Total', 'Inflow': total_inflow, 'Outflow': total_outflow}) - - net_inflow = total_inflow - total_outflow - report['Footer'].append({'Name': 'Net', 'Inflow': (net_inflow if net_inflow >= 0 else 0), - 'Outflow': (net_inflow * -1 if net_inflow < 0 else 0)}) + report['Footer'].append({'Name': 'Net Cash Flow', 'Amount': total_amount}) return report @@ -87,12 +101,19 @@ def build_report_id(request, ledger_type, start_date, finish_date): sub_journal = aliased(Journal) sub_ledger = aliased(LedgerBase) - sub_query = request.dbsession.query(sub_voucher.id) \ - .join(sub_journal, sub_voucher.journals) \ - .join(sub_ledger, sub_journal.ledger) \ - .filter(sub_ledger.type == LedgerType.by_name('Cash').id) \ - .filter(sub_voucher.date >= datetime.datetime.strptime(start_date, '%d-%b-%Y')) \ - .filter(sub_voucher.date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y')).subquery() + sub_query = request.dbsession.query( + sub_voucher.id + ).join( + sub_journal, sub_voucher.journals + ).join( + sub_ledger, sub_journal.ledger + ).filter( + sub_ledger.type == LedgerType.by_name('Cash').id + ).filter( + sub_voucher.date >= datetime.datetime.strptime(start_date, '%d-%b-%Y') + ).filter( + sub_voucher.date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y') + ).subquery() query = request.dbsession.query(LedgerBase, func.sum(Journal.signed_amount)) \ .join(Journal, Voucher.journals) \ @@ -102,17 +123,19 @@ def build_report_id(request, ledger_type, start_date, finish_date): .group_by(LedgerBase) \ .order_by(desc(func.sum(Journal.amount))).all() - total_inflow = 0 - total_outflow = 0 + total_amount = 0 for ledger, amount in query: - inflow = amount * -1 if amount < 0 else 0 - outflow = amount if amount >= 0 else 0 - total_inflow += (amount * -1) if amount < 0 else 0 - total_outflow += amount if amount >= 0 else 0 - report['Body'].append({'Name': ledger.name, 'Url': request.route_url('ledger_id', id=ledger.id, - _query={'StartDate': start_date, - 'FinishDate': finish_date}), - 'Inflow': inflow, 'Outflow': outflow}) + total_amount += (amount * -1) + report['Body'].append( + { + 'Name': ledger.name, + 'Url': request.route_url( + 'ledger_id', id=ledger.id, + _query={'StartDate': start_date, 'FinishDate': finish_date} + ), + 'Amount': amount * -1 + } + ) - report['Footer'].append({'Name': 'Total', 'Inflow': total_inflow, 'Outflow': total_outflow}) + report['Footer'].append({'Name': 'Total', 'Amount': total_amount}) return report