From d221a4f4e9d2577c6857c210a98b56527263b6aa Mon Sep 17 00:00:00 2001
From: Tanshu
Date: Wed, 17 Oct 2012 00:21:43 +0530
Subject: [PATCH] Cash flow done.
---
brewman/brewman/__init__.py | 4 +-
brewman/brewman/static/partial/cash-flow.html | 41 ++++++
.../brewman/static/scripts/angular_service.js | 5 +
brewman/brewman/static/scripts/cash-flow.js | 17 +++
brewman/brewman/static/scripts/cash_flow.js | 28 ----
brewman/brewman/static/scripts/overlord.js | 3 +
.../{trial_balance.js => trial-balance.js} | 0
brewman/brewman/templates/angular_base.mako | 3 +-
.../brewman/templates/reports/cash_flow.pt | 58 --------
brewman/brewman/views/pages.py | 3 +-
brewman/brewman/views/reports/cash_flow.py | 139 ++++++------------
11 files changed, 117 insertions(+), 184 deletions(-)
create mode 100644 brewman/brewman/static/partial/cash-flow.html
create mode 100644 brewman/brewman/static/scripts/cash-flow.js
delete mode 100644 brewman/brewman/static/scripts/cash_flow.js
rename brewman/brewman/static/scripts/{trial_balance.js => trial-balance.js} (100%)
delete mode 100644 brewman/brewman/templates/reports/cash_flow.pt
diff --git a/brewman/brewman/__init__.py b/brewman/brewman/__init__.py
index e406ba76..0bfa2597 100644
--- a/brewman/brewman/__init__.py
+++ b/brewman/brewman/__init__.py
@@ -106,8 +106,8 @@ def main(global_config, **settings):
config.add_route('trial_balance_date', '/TrialBalance/{date}')
config.add_route('trial_balance', '/TrialBalance')
- config.add_route('cash_flow_id', '/Reports/CashFlow/{id}')
- config.add_route('cash_flow', '/Reports/CashFlow')
+ config.add_route('cash_flow_id', '/CashFlow/{id}')
+ config.add_route('cash_flow', '/CashFlow')
config.add_route('profit_loss', '/Reports/ProfitLoss')
diff --git a/brewman/brewman/static/partial/cash-flow.html b/brewman/brewman/static/partial/cash-flow.html
new file mode 100644
index 00000000..99dee542
--- /dev/null
+++ b/brewman/brewman/static/partial/cash-flow.html
@@ -0,0 +1,41 @@
+
diff --git a/brewman/brewman/static/scripts/angular_service.js b/brewman/brewman/static/scripts/angular_service.js
index c7f3e518..6250c96f 100644
--- a/brewman/brewman/static/scripts/angular_service.js
+++ b/brewman/brewman/static/scripts/angular_service.js
@@ -87,6 +87,11 @@ overlord_service.factory('TrialBalance', ['$resource', function ($resource) {
return $resource('/TrialBalance/:date');
}]);
+// TODO: Replace hardcoded url with route_url
+overlord_service.factory('CashFlow', ['$resource', function ($resource) {
+ return $resource('/CashFlow/:id');
+}]);
+
// TODO: Replace hardcoded url with route_url
overlord_service.factory('LedgerService', ['$resource', function ($resource) {
return $resource('/Services/Accounts/:type');
diff --git a/brewman/brewman/static/scripts/cash-flow.js b/brewman/brewman/static/scripts/cash-flow.js
new file mode 100644
index 00000000..bb64e041
--- /dev/null
+++ b/brewman/brewman/static/scripts/cash-flow.js
@@ -0,0 +1,17 @@
+function CashFlowCtrl($scope, $routeParams, $location, CashFlow) {
+ if (typeof $routeParams.StartDate === 'undefined') {
+ $scope.info = CashFlow.get({});
+ } else if (typeof $routeParams.id === 'undefined') {
+ $scope.info = CashFlow.get({StartDate:$routeParams.StartDate, FinishDate:$routeParams.FinishDate});
+ } else {
+ $scope.info = CashFlow.get({id:$routeParams.id, StartDate:$routeParams.StartDate, FinishDate:$routeParams.FinishDate});
+ }
+ $scope.show = function () {
+ $scope.info = CashFlow.get({StartDate:$scope.info.StartDate, FinishDate:$scope.info.FinishDate}, function (u, putResponseHeaders) {
+ $location.path('/CashFlow').search({StartDate:u.StartDate, FinishDate:u.FinishDate});
+ }, function (data, status) {
+ $scope.toasts.push({Type:'Error', Message:data.data});
+ });
+ };
+ $('#txtStartDate').focus();
+}
diff --git a/brewman/brewman/static/scripts/cash_flow.js b/brewman/brewman/static/scripts/cash_flow.js
deleted file mode 100644
index e538f630..00000000
--- a/brewman/brewman/static/scripts/cash_flow.js
+++ /dev/null
@@ -1,28 +0,0 @@
-function Populate(response) {
- if (response != null) {
- var stateObj = { 'StartDate': response.start_date, 'FinishDate': response.finish_date };
- history.pushState(stateObj, 'Display', response.url);
- var $table = $('#tbodyMain')
- $table.html(response.body);
- var $footer = $('#tfootMain')
- $footer.html(response.footer);
- }
-}
-
-function Show(startDate, finishDate) {
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- data: JSON.stringify({ startDate: startDate, finishDate: finishDate }),
- dataType: "json",
- success: function(response) {
- Populate(response);
- },
- error: function(jqXHR, textStatus) {
- $("#ctl00_statusDiv").removeClass().addClass("error");
- var msg = $.parseJSON(jqXHR.responseText).Message;
- $("#ctl00_statusDiv").html(msg);
- }
- });
- return false;
-}
diff --git a/brewman/brewman/static/scripts/overlord.js b/brewman/brewman/static/scripts/overlord.js
index adf5c212..992552b2 100644
--- a/brewman/brewman/static/scripts/overlord.js
+++ b/brewman/brewman/static/scripts/overlord.js
@@ -26,6 +26,9 @@ var overlord = angular.module('overlord', ['overlord.directive', 'overlord.filte
when('/ProductLedger', {templateUrl:'/partial/product-ledger.html', controller:ProductLedgerCtrl}).
when('/ProductLedger/:id', {templateUrl:'/partial/product-ledger.html', controller:ProductLedgerCtrl}).
+ when('/CashFlow', {templateUrl:'/partial/cash-flow.html', controller:CashFlowCtrl}).
+ when('/CashFlow/:id', {templateUrl:'/partial/cash-flow.html', controller:CashFlowCtrl}).
+
// when('/Ledger', {templateUrl:'/partial/ledger.html', controller:LedgerCtrl, resolve: LedgerCtrl.resolve}).
// when('/Ledger/:id', {templateUrl:'/partial/ledger.html', controller:LedgerCtrl, resolve: LedgerCtrl.resolve}).
diff --git a/brewman/brewman/static/scripts/trial_balance.js b/brewman/brewman/static/scripts/trial-balance.js
similarity index 100%
rename from brewman/brewman/static/scripts/trial_balance.js
rename to brewman/brewman/static/scripts/trial-balance.js
diff --git a/brewman/brewman/templates/angular_base.mako b/brewman/brewman/templates/angular_base.mako
index d414a59e..fbf3b2ca 100644
--- a/brewman/brewman/templates/angular_base.mako
+++ b/brewman/brewman/templates/angular_base.mako
@@ -40,7 +40,8 @@
${h.ScriptLink(request, 'ledger.js')}
${h.ScriptLink(request, 'product-ledger.js')}
- ${h.ScriptLink(request, 'trial_balance.js')}
+ ${h.ScriptLink(request, 'trial-balance.js')}
+ ${h.ScriptLink(request, 'cash-flow.js')}
${h.ScriptLink(request, 'account.js')}
${h.ScriptLink(request, 'user.js')}
diff --git a/brewman/brewman/templates/reports/cash_flow.pt b/brewman/brewman/templates/reports/cash_flow.pt
deleted file mode 100644
index 13147107..00000000
--- a/brewman/brewman/templates/reports/cash_flow.pt
+++ /dev/null
@@ -1,58 +0,0 @@
-
-
- ${h.JsLink(request, 'cash_flow.js')}
-
-
-
- ${h.EndForm()}
-
-
-
-
\ No newline at end of file
diff --git a/brewman/brewman/views/pages.py b/brewman/brewman/views/pages.py
index 19af8f6d..82acb852 100644
--- a/brewman/brewman/views/pages.py
+++ b/brewman/brewman/views/pages.py
@@ -8,8 +8,6 @@ from pyramid.view import view_config
from brewman.models.auth import User
-
-
@view_config(context=HTTPNotFound, renderer='brewman:templates/404.mako')
def not_found(request):
return {'title': '404',
@@ -69,6 +67,7 @@ def login_ajax(request):
else:
return Response('name ' + username + ' Login Failure ' + password)
+
@view_config(route_name='favicon')
def favicon(request):
here = os.path.dirname(__file__)
diff --git a/brewman/brewman/views/reports/cash_flow.py b/brewman/brewman/views/reports/cash_flow.py
index 8bd4ac97..ac97cde8 100644
--- a/brewman/brewman/views/reports/cash_flow.py
+++ b/brewman/brewman/views/reports/cash_flow.py
@@ -3,75 +3,42 @@ from sqlalchemy.orm.util import aliased
from sqlalchemy.sql.expression import func, desc
from pyramid.view import view_config
-from brewman.helpers import Literal
from brewman.models import DBSession
from brewman.models.master import Ledger, LedgerType
from brewman.models.voucher import Voucher, Journal
+from brewman.views.services.session import services_session_period_start, services_session_period_finish
-@view_config(request_method='GET', route_name='cash_flow_id', renderer='brewman:templates/reports/cash_flow.pt')
-@view_config(request_method='GET', route_name='cash_flow', renderer='brewman:templates/reports/cash_flow.pt')
-def cash_flow(request):
+@view_config(request_method='GET', route_name='cash_flow', renderer='brewman:templates/angular_base.mako',
+ xhr=False)
+@view_config(request_method='GET', route_name='cash_flow_id', renderer='brewman:templates/angular_base.mako',
+ xhr=False)
+def get_html(request):
+ return {}
+
+
+@view_config(request_method='GET', route_name='cash_flow', renderer='json', xhr=True)
+def get_cash_flow(request):
+ start_date = request.GET.get('StartDate', None)
+ finish_date = request.GET.get('FinishDate', None)
+ if start_date and finish_date:
+ return build_report(request, start_date, finish_date)
+ else:
+ return {'StartDate': services_session_period_start(request),
+ 'FinishDate': services_session_period_finish(request), 'Body': [], 'Footer': {}}
+
+
+@view_config(request_method='GET', route_name='cash_flow_id', renderer='json', xhr=True)
+def get_cash_flow_id(request):
id = request.matchdict.get('id', None)
- startDate = request.GET.get('startDate', None)
- finishDate = request.GET.get('finishDate', None)
- should_build_report = False if startDate is None else True
-
- if not should_build_report:
- body = ""
- footer = ""
- elif id is None:
- report = build_report(request, datetime.datetime.strptime(startDate, '%d-%b-%Y'),
- datetime.datetime.strptime(finishDate, '%d-%b-%Y'))
- body = Literal(report['body'])
- footer = Literal(report['footer'])
- else:
- report = build_report_id(request, int(id), datetime.datetime.strptime(startDate, '%d-%b-%Y'),
- datetime.datetime.strptime(finishDate, '%d-%b-%Y'))
- body = Literal(report['body'])
- footer = Literal(report['footer'])
-
- if startDate == None:
- startDate = Literal(
- "StartDate('#txtStartDate', '{0}');".format(request.route_url('services_session_period_start')))
- finishDate = Literal(
- "FinishDate('#txtFinishDate', '{0}');".format(request.route_url('services_session_period_finish')))
- else:
- startDate = Literal("$('#txtStartDate').val('{0}');".format(startDate))
- finishDate = Literal("$('#txtFinishDate').val('{0}');".format(finishDate))
-
- return {'title': 'Cash Flow - Hops n Grains',
- 'pageclass': "page-blogpost page-sidebar-right",
- 'pagecontentclass': "page-content grid_12",
- 'page_header': '',
- 'body': body,
- 'footer': footer,
- 'startDate': startDate,
- 'finishDate': finishDate}
+ start_date = request.GET.get('StartDate', None)
+ finish_date = request.GET.get('FinishDate', None)
+ return build_report_id(request, int(id), start_date, finish_date)
-@view_config(request_method='POST', route_name='cash_flow_id', renderer='json', xhr=True)
-@view_config(request_method='POST', route_name='cash_flow', renderer='json', xhr=True)
-def cash_flow_main(request):
- id = request.matchdict.get('id', None)
- startDate = datetime.datetime.strptime(request.json_body['startDate'], '%d-%b-%Y')
- finishDate = datetime.datetime.strptime(request.json_body['finishDate'], '%d-%b-%Y')
- if id is None:
- result = build_report(request, startDate, finishDate)
- result['url'] = request.route_url('cash_flow',
- _query={'startDate': request.json_body['startDate'], 'finishDate': request.json_body['finishDate']})
- else:
- result = build_report_id(request, int(id), startDate, finishDate)
- result['url'] = request.route_url('cash_flow', id=id,
- _query={'startDate': request.json_body['startDate'], 'finishDate': request.json_body['finishDate']})
- result['start_date'] = request.json_body['startDate']
- result['finish_date'] = request.json_body['finishDate']
- return result
-
-
-def build_report(request, startDate, finishDate):
- body = ''
+def build_report(request, start_date, finish_date):
+ report = {'StartDate': start_date, 'FinishDate': finish_date, 'Body': [], 'Footer': []}
sub_voucher = aliased(Voucher)
sub_journal = aliased(Journal)
sub_ledger = aliased(Ledger)
@@ -80,8 +47,8 @@ def build_report(request, startDate, finishDate):
.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 >= startDate)\
- .filter(sub_voucher.date <= finishDate).subquery()
+ .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 = DBSession.query(Ledger.type, func.sum(Journal.signed_amount))\
.join(Journal, Voucher.journals)\
@@ -99,29 +66,20 @@ def build_report(request, startDate, finishDate):
outflow = "\u20B9 {0:.2f}".format(amount) if amount >= 0 else ""
totalInflow += (amount * -1) if amount < 0 else 0
totalOutflow += amount if amount >= 0 else 0
- body += '' + str(lt.id) + ' | ' + lt.name +\
- ' | ' + inflow + ' | ' + outflow +\
- ' |
'
+ 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 = LedgerType.by_name('Total')
- body += '' + str(total.id) + ' | ' + total.name +\
- ' | ' + "\u20B9 {0:.2f}".format(totalInflow) +\
- ' | ' + "\u20B9 {0:.2f}".format(totalOutflow) + ' |
'
+ report['Footer'].append({'Name': 'Total', 'Inflow': "\u20B9 {0:.2f}".format(totalInflow),
+ 'Outflow': "\u20B9 {0:.2f}".format(totalOutflow)})
- net = LedgerType.by_name('Net')
netInflow = totalInflow - totalOutflow
- footer = '' + str(net.id) + ' | ' + net.name +\
- ' | ' + ("\u20B9 {0:.2f}".format(netInflow) if netInflow >= 0 else "") +\
- ' | ' + ("\u20B9 {0:.2f}".format(netInflow * -1) if netInflow < 0 else "") +\
- ' |
'
- return {'body': body, 'footer': footer}
+ report['Footer'].append({'Name': 'Net', 'Inflow': ("\u20B9 {0:.2f}".format(netInflow) if netInflow >= 0 else ""),
+ 'Outflow': ("\u20B9 {0:.2f}".format(netInflow * -1) if netInflow < 0 else "")})
+ return report
-def build_report_id(request, ledgerType, startDate, finishDate):
- body = ''
+def build_report_id(request, ledger_type, start_date, finish_date):
+ report = {'StartDate': start_date, 'FinishDate': finish_date, 'Body': [], 'Footer': []}
sub_voucher = aliased(Voucher)
sub_journal = aliased(Journal)
sub_ledger = aliased(Ledger)
@@ -130,14 +88,14 @@ def build_report_id(request, ledgerType, startDate, finishDate):
.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 >= startDate)\
- .filter(sub_voucher.date <= finishDate).subquery()
+ .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 = DBSession.query(Ledger, func.sum(Journal.signed_amount))\
.join(Journal, Voucher.journals)\
.join(Ledger, Journal.ledger)\
.filter(Voucher.id.in_(sub_query))\
- .filter(Ledger.type == ledgerType)\
+ .filter(Ledger.type == ledger_type)\
.group_by(Ledger)\
.order_by(desc(func.sum(Journal.amount))).all()
@@ -148,14 +106,9 @@ def build_report_id(request, ledgerType, startDate, finishDate):
outflow = "\u20B9 {0:.2f}".format(amount) if amount >= 0 else ""
totalInflow += (amount * -1) if amount < 0 else 0
totalOutflow += amount if amount >= 0 else 0
- body += '' + str(ledger.id) + ' | ' + ledger.name +\
- ' | ' + inflow + ' | ' + outflow +\
- ' |
'
+ 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})
- footer = ' | ' + 'Total' +\
- ' | ' + "\u20B9 {0:.2f}".format(totalInflow) +\
- ' | ' + "\u20B9 {0:.2f}".format(totalOutflow) + ' |
'
- return {'body': body, 'footer': footer}
+ report['Footer'].append({'Name': 'Total', 'Inflow': "\u20B9 {0:.2f}".format(totalInflow),
+ 'Outflow': "\u20B9 {0:.2f}".format(totalOutflow)})
+ return report