From e868a213b1d92c26be5e9f3a5d719adf77dcb007 Mon Sep 17 00:00:00 2001 From: tanshu Date: Tue, 12 Apr 2016 13:57:24 +0530 Subject: [PATCH] Update: Changed the Reconcile report so that it shows all the unreconciled entries and also shows the balance according to the reconcile date. --- brewman/static/partial/reconcile.html | 16 +++---- brewman/static/scripts/ledger.js | 10 +---- brewman/static/scripts/reconcile.js | 60 ++++++++++++++++++++++----- brewman/views/Management/rebase.py | 1 - brewman/views/product.py | 2 +- brewman/views/reports/reconcile.py | 54 ++++++++++++------------ 6 files changed, 87 insertions(+), 56 deletions(-) diff --git a/brewman/static/partial/reconcile.html b/brewman/static/partial/reconcile.html index 4dff2370..3941b0a1 100644 --- a/brewman/static/partial/reconcile.html +++ b/brewman/static/partial/reconcile.html @@ -49,8 +49,8 @@ Reconcile Date - - + + {{item.Date}} {{item.Name}} {{item.Debit | currency | clr}} @@ -70,7 +70,7 @@ - +

{{item.Narration}}

{{item.Type}} @@ -80,11 +80,11 @@ - {{info.Footer.Date}} - {{info.Footer.Name}} - {{info.Footer.Debit | currency}} - {{info.Footer.Credit | currency}} - {{info.Footer.Running | accounting}} + + + {{footer.Debit | currency}} + {{footer.Credit | currency}} + {{footer.Running | accounting}} diff --git a/brewman/static/scripts/ledger.js b/brewman/static/scripts/ledger.js index 7ae22923..4d0b8ff2 100644 --- a/brewman/static/scripts/ledger.js +++ b/brewman/static/scripts/ledger.js @@ -35,15 +35,7 @@ var LedgerController = ['$scope', '$routeParams', '$location', 'asDateFilter', ' $scope.selected = index; }; - - // Replace with $watchGroup in AngularJS 1.3+ - $scope.$watch('hidden', function () { - var filtered = $scope.doFilter($scope.hidden, $scope.info.Body); - $scope.ledger = filtered.Body; - $scope.footer = filtered.Footer; - }, true); - - $scope.$watch('info', function () { + $scope.$watch(['info', 'hidden'], function () { var filtered = $scope.doFilter($scope.hidden, $scope.info.Body); $scope.ledger = filtered.Body; $scope.footer = filtered.Footer; diff --git a/brewman/static/scripts/reconcile.js b/brewman/static/scripts/reconcile.js index 3fc98a7a..cbcb7eda 100644 --- a/brewman/static/scripts/reconcile.js +++ b/brewman/static/scripts/reconcile.js @@ -14,15 +14,21 @@ var ReconcileController = ['$scope', '$routeParams', '$location', 'asDateFilter' $location.path('/Reconcile/' + id).search('StartDate', startDate).search('FinishDate', finishDate); } }; + $scope.save = function () { - var i, l, - len = $scope.info.Body.length; - for (i = 0, l = len; i < l; i++) { + var i, + len = $scope.body.length; + $scope.info.Body = $scope.body; + for (i = 0; i < len; i++) { $scope.info.Body[i].ReconcileDate = asDate($scope.info.Body[i].ReconcileDate); } - $scope.info.$save({StartDate: $routeParams.StartDate, FinishDate: $routeParams.FinishDate}, function (u, putResponseHeaders) { + $scope.info.$save({ + StartDate: $routeParams.StartDate, + FinishDate: $routeParams.FinishDate + }, function (u, putResponseHeaders) { $scope.toasts.push({Type: 'Success', Message: ''}); $scope.info = u; + $scope.doFilter(); }, function (data, status) { $scope.toasts.push({Type: 'Danger', Message: data.data}); }); @@ -46,22 +52,57 @@ var ReconcileController = ['$scope', '$routeParams', '$location', 'asDateFilter' $scope.selected = index; }; + $scope.doFilter = function () { + var data = angular.copy($scope.info.Body), + debit = 0, credit = 0, running = 0; + + data = data.sort(function (a, b) { + if (a.IsReconciled !== b.IsReconciled) { + return b.IsReconciled - a.IsReconciled; + } + var aDate = moment(a.ReconcileDate, 'DD-MMM-YYYY'); + var bDate = moment(b.ReconcileDate, 'DD-MMM-YYYY'); + return aDate - bDate; + }); + + _.forEach(data, function (item) { + if (item.Type !== 'Opening Balance') { + debit += item.Debit; + credit += item.Credit; + if (item.IsReconciled) { + running += item.Debit - item.Credit; + } + } else { + running += item.Debit - item.Credit; + } + item.Running = running; + }); + $scope.body = data; + $scope.footer = { + Debit: debit, + Credit: credit, + Running: running + } + }; + + $scope.doFilter(); + $scope.shortcuts = { 'up': function (e) { if ($scope.selected > 0) { $scope.$apply(function () { - $scope.selected = $scope.selected -= 1; + $scope.selected = Math.min(Math.max(0, $scope.selected - 1), $scope.ledger.length - 1); }); - $("#" + $scope.selected).scrollintoview({duration: 'fast'}); + $("#" + $scope.selected).scrollintoview(); e.preventDefault(); } }, 'down': function (e) { if ($scope.selected < $scope.info.Body.length - 1) { $scope.$apply(function () { - $scope.selected = $scope.selected += 1; + $scope.selected = Math.min(Math.max(0, $scope.selected + 1), $scope.ledger.length - 1); }); - $("#" + $scope.selected).scrollintoview({duration: 'fast'}); + $("#" + $scope.selected).scrollintoview(); e.preventDefault(); } }, @@ -70,6 +111,7 @@ var ReconcileController = ['$scope', '$routeParams', '$location', 'asDateFilter' $scope.$apply(function () { $location.path(path).search('StartDate', null).search('FinishDate', null); }); + e.preventDefault(); } }; @@ -91,5 +133,3 @@ ReconcileController.resolve = { } }] }; - - diff --git a/brewman/views/Management/rebase.py b/brewman/views/Management/rebase.py index 2af5b65d..7c7b38ef 100644 --- a/brewman/views/Management/rebase.py +++ b/brewman/views/Management/rebase.py @@ -13,7 +13,6 @@ from brewman.models.validation_exception import TryCatchFunction from brewman.models.voucher import Journal, Voucher, VoucherType, Batch, Inventory, SalaryDeduction, Fingerprint, \ Attendance, DbImage - __author__ = 'tanshu' diff --git a/brewman/views/product.py b/brewman/views/product.py index 7217e1e3..8f0e92e0 100644 --- a/brewman/views/product.py +++ b/brewman/views/product.py @@ -223,7 +223,7 @@ def product_info(id): def delete_with_data(product): suspense_product = Product.by_id(Product.suspense()) suspense_batch = Batch.by_id(Batch.suspense()) - query = Voucher.query().options(joinedload_all(Voucher.inventories, Inventory.product, innerjoin=True)) \ + query = Voucher.query().options(joinedload_all(Voucher.inventories, Inventory.product, innerjoin=True) ) \ .filter(Voucher.inventories.any(Inventory.product_id == product.id)) \ .all() diff --git a/brewman/views/reports/reconcile.py b/brewman/views/reports/reconcile.py index b177fed1..655a88fb 100644 --- a/brewman/views/reports/reconcile.py +++ b/brewman/views/reports/reconcile.py @@ -2,7 +2,7 @@ import datetime import pkg_resources from pyramid.response import FileResponse from sqlalchemy.orm import joinedload_all -from sqlalchemy.sql.expression import func +from sqlalchemy.sql.expression import func, or_, and_ import uuid from pyramid.view import view_config @@ -28,7 +28,7 @@ def html(request): @view_config(request_method='GET', route_name='api_reconcile', renderer='json', permission='Reconcile') def show_blank(request): return {'StartDate': session_period_start(request), - 'FinishDate': session_period_finish(request), 'Ledger': None, 'Body': [], 'Footer': {}} + 'FinishDate': session_period_finish(request), 'Ledger': None, 'Body': []} @view_config(request_method='GET', route_name='api_reconcile_id', renderer='json', permission='Reconcile') @@ -39,8 +39,7 @@ def show_data(request): start_date = request.GET.get('StartDate', session_period_start(request)) finish_date = request.GET.get('FinishDate', session_period_finish(request)) info = {'StartDate': start_date, 'FinishDate': finish_date, - 'Account': {'LedgerID': account.id, 'Name': account.name}, - 'Body': [], 'Footer': {}} + 'Account': {'LedgerID': account.id, 'Name': account.name}, 'Body': []} build_report(request, info) return info @@ -49,15 +48,24 @@ def build_report(request, info): ledger_id = info['Account']['LedgerID'] start_date = info['StartDate'] finish_date = info['FinishDate'] - total_debit, total_credit, running_total, opening = opening_balance(ledger_id, start_date) + opening = opening_balance(ledger_id, start_date) info['Body'].append(opening) - query = Voucher.query().options(joinedload_all(Voucher.journals, Journal.ledger, innerjoin=True)) \ - .filter(Voucher.journals.any(Journal.ledger_id == ledger_id)) \ - .filter(Voucher.date >= datetime.datetime.strptime(start_date, '%d-%b-%Y')) \ - .filter(Voucher.date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y')) \ - .filter(Voucher.type != VoucherType.by_name('Issue').id) \ - .order_by(Voucher.reconcile_date).order_by(Voucher.last_edit_date).all() + query = Voucher.query().options( + joinedload_all(Voucher.journals, Journal.ledger, innerjoin=True) + ).filter( + Voucher.journals.any(Journal.ledger_id == ledger_id) + ).filter( + or_( + Voucher.is_reconciled == False, + and_( + Voucher.reconcile_date >= datetime.datetime.strptime(start_date, '%d-%b-%Y'), + Voucher.reconcile_date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y') + ) + ) + ).filter( + Voucher.type != VoucherType.by_name('Issue').id + ).order_by(Voucher.is_reconciled).order_by(Voucher.reconcile_date).order_by(Voucher.last_edit_date).all() for voucher in query: debit = 0 @@ -66,17 +74,13 @@ def build_report(request, info): name = "" for journal in voucher.journals: if journal.ledger_id == ledger_id: - if voucher.posted: - running_total += (journal.amount * journal.debit) journal_debit = journal.debit if journal.debit == 1: debit = journal.amount - total_debit += journal.amount - credit = "" + credit = 0 else: credit = journal.amount - total_credit += journal.amount - debit = "" + debit = 0 for journal in voucher.journals: if journal.debit != journal_debit: name += "{0} / ".format(journal.ledger.name) @@ -84,18 +88,15 @@ def build_report(request, info): info['Body'].append( {'VoucherID': voucher.id, 'Date': voucher.date.strftime('%d-%b-%Y'), 'Name': name, 'Url': get_edit_url(request, voucher), 'Type': VoucherType.by_id(voucher.type).name, - 'Narration': voucher.narration, 'Debit': debit, 'Credit': credit, 'Running': running_total, - 'ReconcileDate': voucher.reconcile_date.strftime('%d-%b-%Y'), 'IsReconciled': voucher.is_reconciled}) - - info['Footer'] = {'Date': finish_date, 'Name': 'Closing Balance', 'Type': 'Closing Balance', - 'Narration': '', 'Debit': total_debit, 'Credit': total_credit, - 'Running': running_total, 'Posted': True} + 'Narration': voucher.narration, 'Debit': debit, 'Credit': credit, 'IsReconciled': voucher.is_reconciled, + 'ReconcileDate': voucher.reconcile_date.strftime('%d-%b-%Y')}) def opening_balance(ledgerID, start_date): opening = DBSession.query(func.sum(Journal.amount * Journal.debit)) \ .join(Journal.voucher) \ - .filter(Voucher.date < datetime.datetime.strptime(start_date, '%d-%b-%Y')) \ + .filter(Voucher.reconcile_date < datetime.datetime.strptime(start_date, '%d-%b-%Y')) \ + .filter(Voucher.is_reconciled == True) \ .filter(Voucher.type != VoucherType.by_name('Issue').id) \ .filter(Journal.ledger_id == ledgerID) \ .scalar() @@ -106,8 +107,8 @@ def opening_balance(ledgerID, start_date): else: debit = opening credit = 0 - return 0, 0, opening, {'Date': start_date, 'Name': 'Opening Balance', 'Type': 'Opening Balance', - 'Narration': '', 'Debit': debit, 'Credit': credit, 'Running': opening, 'Posted': True} + return {'Date': start_date, 'ID': 'OB', 'Name': 'Opening Balance', 'Type': 'Opening Balance', + 'Narration': '', 'Debit': debit, 'Credit': credit, 'Running': opening, 'Posted': True} @view_config(request_method='POST', route_name='api_reconcile_id', renderer='json', permission='Reconcile') @@ -131,4 +132,3 @@ def save(request): 'Body': [], 'Footer': {}} build_report(request, info) return info -