From 412704a73eaedf0e9e1080d71d2e5a66b9a656b7 Mon Sep 17 00:00:00 2001 From: Tanshu Date: Mon, 29 Oct 2012 15:44:41 +0530 Subject: [PATCH] Converted most routes to use resolve. Issue fixed (hopefully) :). Error in payment / receipt resolved fixed. --- brewman/brewman/static/partial/issue.html | 2 +- .../brewman/static/scripts/angular_service.js | 2 +- brewman/brewman/static/scripts/issue.js | 169 +++++++++++------- brewman/brewman/static/scripts/overlord.js | 16 +- brewman/brewman/static/scripts/payment.js | 40 ++++- brewman/brewman/static/scripts/purchase.js | 27 ++- brewman/brewman/static/scripts/receipt.js | 40 ++++- brewman/brewman/views/services/cost_center.py | 40 +++-- .../views/services/voucher/__init__.py | 11 +- .../views/services/voucher/blank_voucher.py | 12 +- .../brewman/views/services/voucher/issue.py | 2 +- 11 files changed, 250 insertions(+), 111 deletions(-) diff --git a/brewman/brewman/static/partial/issue.html b/brewman/brewman/static/partial/issue.html index a11ec941..b4c81dd2 100644 --- a/brewman/brewman/static/partial/issue.html +++ b/brewman/brewman/static/partial/issue.html @@ -16,7 +16,7 @@ {{item.Code}} {{item.Amount}} - + Load diff --git a/brewman/brewman/static/scripts/angular_service.js b/brewman/brewman/static/scripts/angular_service.js index 5ffd5ec7..730ca0f3 100644 --- a/brewman/brewman/static/scripts/angular_service.js +++ b/brewman/brewman/static/scripts/angular_service.js @@ -1,7 +1,7 @@ var overlord_service = angular.module('overlord.service', ['ngResource']); // TODO: Replace hardcoded url with route_url -overlord_service.factory('issue_grid', ['$resource', function ($resource) { +overlord_service.factory('IssueGrid', ['$resource', function ($resource) { return $resource('/Issues/Services/:date'); }]); diff --git a/brewman/brewman/static/scripts/issue.js b/brewman/brewman/static/scripts/issue.js index ee0d087e..69bf9f7c 100644 --- a/brewman/brewman/static/scripts/issue.js +++ b/brewman/brewman/static/scripts/issue.js @@ -1,11 +1,15 @@ -function IssueCtrl($scope, $routeParams, $location, Voucher, CostCenter, issue_grid) { - if (typeof $routeParams.id === 'undefined') { -// $scope.voucher = Voucher.get({type:'Issue', date:$routeParams.Date, source:$routeParams.Source, destination:$routeParams.Destination}); - $scope.voucher = Voucher.get({type:'Issue'}); - } else { - $scope.voucher = Voucher.get({id:$routeParams.id}); +function IssueCtrl($scope, $routeParams, $location, voucher, cost_centers, issue_grid, IssueGrid, Voucher) { + $scope.voucher = voucher; + $scope.cost_centers = cost_centers; + $scope.smallGrid = issue_grid; + + if (typeof $routeParams.Source === 'undefined' && typeof $routeParams.Destination === 'undefined' && typeof $routeParams.id === 'undefined') { + console.log('Changing route on load'); + var date = $scope.voucher.Date; + var source = getDebitCredit($scope.voucher.Journals, -1); + var destination = getDebitCredit($scope.voucher.Journals, 1); + $location.path('/Issue/' + date).search({Source:source, Destination:destination}); } - $scope.cost_centers = CostCenter.query(); $scope.addInventory = function () { for (var i = 0, l = this.voucher.Inventories.length; i < l; i++) { @@ -37,59 +41,40 @@ this.voucher.Inventories.splice(index, 1); }; - $scope.updateGrid = function () { - var voucher = $scope.voucher; - for (var i = 0, l = voucher.Journals.length; i < l; i++) { - if (voucher.Journals[i].Debit === -1) { - var creditJournal = voucher.Journals[i].CostCenter.CostCenterID; - } else { - var debitJournal = voucher.Journals[i].CostCenter.CostCenterID; - } - } - $scope.smallGrid = issue_grid.query({date:voucher.Date, source:creditJournal, destination:debitJournal }); - }; - $scope.$watch('voucher.Date', function (newDate, oldDate) { if (newDate !== oldDate && typeof newDate !== 'undefined') { - $scope.updateGrid(); + if (typeof $routeParams.Source !== 'undefined' && typeof $routeParams.Destination !== 'undefined' && typeof $routeParams.id !== 'undefined') { + $location.path('/Issue/' + newDate).search({Source:$routeParams.Source, Destination:$routeParams.Destination}); + } else { + $scope.smallGrid = IssueGrid.query({date:newDate, Source:$routeParams.Source, Destination:$routeParams.Destination}); + } } }, true); + function getDebitCredit(journals, type) { + for (var i = 0, l = journals.length; i < l; i++) { + if (journals[i].Debit === type) { + return journals[i].CostCenter.CostCenterID; + } + } + } + $scope.$watch('voucher.Journals', function (newJournals, oldJournals) { - if (typeof newJournals === 'undefined') { + if (typeof newJournals === 'undefined' || angular.equals(newJournals, oldJournals)) { return; } - var doUpdate = false; - - if (typeof oldJournals !== 'undefined' && typeof $scope.source === 'undefined') { - for (var i = 0, l = oldJournals.length; i < l; i++) { - if (oldJournals[i].Debit === -1) { - $scope.source = oldJournals[i].CostCenter.CostCenterID; - } else { - $scope.destination = oldJournals[i].CostCenter.CostCenterID; - } - } - doUpdate = true; - } - - if (!doUpdate) { - for (var i = 0, l = newJournals.length; i < l; i++) { - if (newJournals[i].Debit === -1) { - if (newJournals[i].CostCenter.CostCenterID != $scope.source) { - $scope.source = newJournals[i].CostCenter.CostCenterID; - doUpdate = true; - } - - } else { - if (newJournals[i].CostCenter.CostCenterID != $scope.destination) { - $scope.destination = newJournals[i].CostCenter.CostCenterID; - doUpdate = true; - } - } + if (typeof oldJournals !== 'undefined') { + if (getDebitCredit(oldJournals, -1) === getDebitCredit(newJournals, -1) && getDebitCredit(oldJournals, 1) === getDebitCredit(newJournals, 1)) { + return; } } - if (doUpdate) { - $scope.updateGrid(); + + if (typeof $routeParams.Source !== 'undefined' && typeof $routeParams.Destination !== 'undefined' && typeof $routeParams.id !== 'undefined') { + console.log('journal watch location'); + $location.path('/Issue/' + $scope.voucher.Date).search({Source:getDebitCredit(newJournals, -1), Destination:getDebitCredit(newJournals, 1)}); + } else { + console.log('journal watch grid'); + $scope.smallGrid = IssueGrid.query({date:$scope.voucher.Date, Source:getDebitCredit(newJournals, -1), Destination:getDebitCredit(newJournals, 1)}); } }, true); @@ -107,12 +92,10 @@ }, true); $scope.resetVoucher = function (voucherid) { -// $location.path('/Issue').search({Date:voucher.Date, Source:'', Destination:''}); - $scope.voucher = Voucher.get({id:voucherid, type:'Issue'}, function (u, putResponseHeaders) { - $location.path('/Issue'); - }, function (data, status) { - $scope.toasts.push({Type:'Error', Message:data.data}); - }); + var date = $scope.voucher.Date; + var source = getDebitCredit($scope.voucher.Journals, -1); + var destination = getDebitCredit($scope.voucher.Journals, 1); + $location.path('/Issue/' + date).search({Source:source, Destination:destination}); }; $scope.preventAlteration = function (voucher) { @@ -137,9 +120,8 @@ $scope.save = function () { $scope.voucher.$save({type:'Issue'}, function (u, putResponseHeaders) { - $location.path('/Issue/' + u.VoucherID); $scope.toasts.push({Type:'Success', Message:u.Code}); - $scope.updateGrid(); + $scope.redirectOnSave(u); }, function (data, status) { $scope.toasts.push({Type:'Error', Message:data.data}); }); @@ -147,11 +129,78 @@ $scope.delete = function () { $scope.voucher.$delete(function (u, putResponseHeaders) { - $location.path('/Issue').replace(); $scope.toasts.push({Type:'Success', Message:''}); - $scope.updateGrid(); + var date = u.Date; + var source = getDebitCredit(u.Journals, -1); + var destination = getDebitCredit(u.Journals, 1); + $location.path('/Issue/' + date).search({Source:source, Destination:destination}).replace(); }, function (data, status) { $scope.toasts.push({Type:'Error', Message:data.data}); }); }; + $scope.redirectOnSave = function (voucher) { + if (typeof $routeParams.Source !== 'undefined' && typeof $routeParams.Destination !== 'undefined' && typeof $routeParams.id !== 'undefined') { + Voucher.get({type:'Issue', Date:$routeParams.id, Source:$routeParams.Source, Destination:$routeParams.Destination}, function (u, putResponseHeaders) { + $scope.voucher = u; + }); + $scope.smallGrid = IssueGrid.query({date:$routeParams.id, Source:$routeParams.Source, Destination:$routeParams.Destination}); + } else { + var date = voucher.Date; + var source = getDebitCredit(voucher.Journals, -1); + var destination = getDebitCredit(voucher.Journals, 1); + $location.path('/Issue/' + date).search({Source:source, Destination:destination}); + } + }; } + +IssueCtrl.resolve = { + voucher:function ($q, $route, Voucher) { + var deferred = $q.defer(); + + var id = $route.current.params.id; + var source = $route.current.params.Source; + var destination = $route.current.params.Destination; + + var successCb = function (result) { + deferred.resolve(result); + }; + + if (typeof source !== 'undefined' && typeof destination !== 'undefined' && typeof id !== 'undefined') { + Voucher.get({type:'Issue', Date:id, Source:source, Destination:destination}, successCb); + } else if (typeof source === 'undefined' && typeof destination === 'undefined' && typeof id !== 'undefined') { + Voucher.get({id:id}, successCb); + } else { + Voucher.get({type:'Issue'}, successCb); + } + return deferred.promise; + }, + issue_grid:function ($q, $route, IssueGrid) { + var deferred = $q.defer(); + + var id = $route.current.params.id; + var source = $route.current.params.Source; + var destination = $route.current.params.Destination; + + var successCb = function (result) { + deferred.resolve(result); + }; + + if (typeof source !== 'undefined' && typeof destination !== 'undefined' && typeof id !== 'undefined') { + IssueGrid.query({date:id, Source:source, Destination:destination}, successCb); + } else if (typeof source === 'undefined' && typeof destination === 'undefined' && typeof id !== 'undefined') { + IssueGrid.query({date:id}, successCb); + } else { + deferred.resolve([]); + } + return deferred.promise; + }, + cost_centers:function ($q, CostCenter) { + var deferred = $q.defer(); + + var successCb = function (result) { + deferred.resolve(result); + }; + + CostCenter.query({}, successCb); + return deferred.promise; + }} \ No newline at end of file diff --git a/brewman/brewman/static/scripts/overlord.js b/brewman/brewman/static/scripts/overlord.js index 9c807e1a..ec96ebc5 100644 --- a/brewman/brewman/static/scripts/overlord.js +++ b/brewman/brewman/static/scripts/overlord.js @@ -10,17 +10,17 @@ var overlord = angular.module('overlord', ['overlord.directive', 'overlord.filte when('/Journal', {templateUrl:'/partial/journal.html', controller:JournalCtrl, resolve:JournalCtrl.resolve}). when('/Journal/:id', {templateUrl:'/partial/journal.html', controller:JournalCtrl, resolve:JournalCtrl.resolve}). - when('/Payment', {templateUrl:'/partial/payment.html', controller:PaymentCtrl}). - when('/Payment/:id', {templateUrl:'/partial/payment.html', controller:PaymentCtrl}). + when('/Payment', {templateUrl:'/partial/payment.html', controller:PaymentCtrl, resolve:PaymentCtrl.resolve}). + when('/Payment/:id', {templateUrl:'/partial/payment.html', controller:PaymentCtrl, resolve:PaymentCtrl.resolve}). - when('/Receipt', {templateUrl:'/partial/receipt.html', controller:ReceiptCtrl}). - when('/Receipt/:id', {templateUrl:'/partial/receipt.html', controller:ReceiptCtrl}). + when('/Receipt', {templateUrl:'/partial/receipt.html', controller:ReceiptCtrl, resolve:ReceiptCtrl.resolve}). + when('/Receipt/:id', {templateUrl:'/partial/receipt.html', controller:ReceiptCtrl, resolve:ReceiptCtrl.resolve}). - when('/Purchase', {templateUrl:'/partial/purchase.html', controller:PurchaseCtrl}). - when('/Purchase/:id', {templateUrl:'/partial/purchase.html', controller:PurchaseCtrl}). + when('/Purchase', {templateUrl:'/partial/purchase.html', controller:PurchaseCtrl, resolve:PurchaseCtrl.resolve}). + when('/Purchase/:id', {templateUrl:'/partial/purchase.html', controller:PurchaseCtrl, resolve:PurchaseCtrl.resolve}). - when('/Issue', {templateUrl:'/partial/issue.html', controller:IssueCtrl}). - when('/Issue/:id', {templateUrl:'/partial/issue.html', controller:IssueCtrl}). + when('/Issue', {templateUrl:'/partial/issue.html', controller:IssueCtrl, resolve:IssueCtrl.resolve}). + when('/Issue/:id', {templateUrl:'/partial/issue.html', controller:IssueCtrl, resolve:IssueCtrl.resolve}). 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/payment.js b/brewman/brewman/static/scripts/payment.js index cab9af63..eb7833f6 100644 --- a/brewman/brewman/static/scripts/payment.js +++ b/brewman/brewman/static/scripts/payment.js @@ -1,11 +1,6 @@ -function PaymentCtrl($scope, $routeParams, $location, Voucher, LedgerService) { - if (typeof $routeParams.id === 'undefined') { - $scope.voucher = Voucher.get({type:'Payment'}); - } else { - $scope.voucher = Voucher.get({id:$routeParams.id}); - } - - $scope.ledgers = LedgerService.query({type:1}); +function PaymentCtrl($scope, $location, voucher, ledgers) { + $scop.voucher = voucher + $scope.ledgers = ledgers; $scope.addJournal = function () { for (var i = 0, l = this.voucher.Journals.length; i < l; i++) { @@ -97,3 +92,32 @@ }); }; } + +PaymentCtrl.resolve = { + voucher:function ($q, $route, Voucher) { + var deferred = $q.defer(); + + var id = $route.current.params.id; + + var successCb = function (result) { + deferred.resolve(result); + }; + + if (typeof id === 'undefined') { + Voucher.get({type:'Payment'}, successCb); + } else { + Voucher.get({id:id}, successCb); + } + return deferred.promise; + }, + ledgers:function ($q, LedgerService) { + var deferred = $q.defer(); + + var successCb = function (result) { + deferred.resolve(result); + } + + LedgerService.query({type:1}, successCb); + return deferred.promise; + } +} \ No newline at end of file diff --git a/brewman/brewman/static/scripts/purchase.js b/brewman/brewman/static/scripts/purchase.js index ad8f490c..2193ef88 100644 --- a/brewman/brewman/static/scripts/purchase.js +++ b/brewman/brewman/static/scripts/purchase.js @@ -1,9 +1,5 @@ -function PurchaseCtrl($scope, $routeParams, $location, Voucher) { - if (typeof $routeParams.id === 'undefined') { - $scope.voucher = Voucher.get({type:'Purchase'}); - } else { - $scope.voucher = Voucher.get({id:$routeParams.id}); - } +function PurchaseCtrl($scope, $routeParams, $location, voucher) { + $scop.voucher = voucher $scope.rate = 0; $scope.addInventory = function () { @@ -108,3 +104,22 @@ }); }; } + +PurchaseCtrl.resolve = { + voucher:function ($q, $route, Voucher) { + var deferred = $q.defer(); + + var id = $route.current.params.id; + + var successCb = function (result) { + deferred.resolve(result); + }; + + if (typeof id === 'undefined') { + Voucher.get({type:'Purchase'}, successCb); + } else { + Voucher.get({id:id}, successCb); + } + return deferred.promise; + } +} \ No newline at end of file diff --git a/brewman/brewman/static/scripts/receipt.js b/brewman/brewman/static/scripts/receipt.js index a43f8299..96c0c3ff 100644 --- a/brewman/brewman/static/scripts/receipt.js +++ b/brewman/brewman/static/scripts/receipt.js @@ -1,11 +1,6 @@ -function ReceiptCtrl($scope, $routeParams, $location, Voucher, LedgerService) { - if (typeof $routeParams.id === 'undefined') { - $scope.voucher = Voucher.get({type:'Receipt'}); - } else { - $scope.voucher = Voucher.get({id:$routeParams.id}); - } - - $scope.ledgers = LedgerService.query({type:1}); +function ReceiptCtrl($scope, $routeParams, $location, voucher, ledgers) { + $scop.voucher = voucher + $scope.ledgers = ledgers; $scope.addJournal = function () { for (var i = 0, l = this.voucher.Journals.length; i < l; i++) { @@ -97,3 +92,32 @@ }); }; } + +ReceiptCtrl.resolve = { + voucher:function ($q, $route, Voucher) { + var deferred = $q.defer(); + + var id = $route.current.params.id; + + var successCb = function (result) { + deferred.resolve(result); + }; + + if (typeof id === 'undefined') { + Voucher.get({type:'Receipt'}, successCb); + } else { + Voucher.get({id:id}, successCb); + } + return deferred.promise; + }, + ledgers:function ($q, LedgerService) { + var deferred = $q.defer(); + + var successCb = function (result) { + deferred.resolve(result); + } + + LedgerService.query({type:1}, successCb); + return deferred.promise; + } +} \ No newline at end of file diff --git a/brewman/brewman/views/services/cost_center.py b/brewman/brewman/views/services/cost_center.py index ed142d34..dab7ac11 100644 --- a/brewman/brewman/views/services/cost_center.py +++ b/brewman/brewman/views/services/cost_center.py @@ -10,30 +10,40 @@ from brewman.models import DBSession from brewman.models.voucher import Voucher, Journal, VoucherType +@view_config(route_name='issues_grid', request_param='Source', renderer='json', xhr=True, permission='Issue') +def grid_date(request): + date = datetime.datetime.strptime(request.matchdict['date'], '%d-%b-%Y') + return get_grid(date, uuid.UUID(request.GET['Source']), uuid.UUID(request.GET['Destination'])) + + @view_config(route_name='issues_grid', renderer='json', xhr=True, permission='Issue') -def grid(request): - date = request.matchdict.get('date', None) - if date is None: - return {} - date = datetime.datetime.strptime(date, '%d-%b-%Y') +def grid_voucher(request): + id = uuid.UUID(request.matchdict['date']) + voucher = Voucher.by_id(id) + source = [j.cost_center_id for j in voucher.journals if j.debit == -1] + source = source[0] + destination = [j.cost_center_id for j in voucher.journals if j.debit == 1] + destination = destination[0] + return get_grid(voucher.date, source, destination) + + +def get_grid(date, source, destination): list = [] - source = uuid.UUID(request.GET['source']) - destination = uuid.UUID(request.GET['destination']) source_journal = aliased(Journal) destination_journal = aliased(Journal) source_or = (and_(source_journal.cost_center_id == source, source_journal.debit == -1)) destination_or = (and_(destination_journal.cost_center_id == destination, destination_journal.debit == 1)) query = DBSession.query(Voucher, source_journal.amount)\ - .join(source_journal, Voucher.journals)\ - .join(destination_journal, Voucher.journals)\ - .filter(Voucher.date == date)\ - .filter(Voucher.type == VoucherType.by_name('Issue').id)\ - .filter(and_(source_or, destination_or))\ - .order_by(Voucher.creation_date)\ - .all() + .join(source_journal, Voucher.journals)\ + .join(destination_journal, Voucher.journals)\ + .filter(Voucher.date == date)\ + .filter(Voucher.type == VoucherType.by_name('Issue').id)\ + .filter(and_(source_or, destination_or))\ + .order_by(Voucher.creation_date)\ + .all() for voucher, amount in query: list.append( - {'VoucherID': str(voucher.id), 'Code': str(voucher.code), 'Amount': "\u20B9 {0:.2f}".format(amount)}) + {'VoucherID': str(voucher.id), 'Code': str(voucher.code), 'Amount': "\u20B9 {0:.2f}".format(amount)}) return list diff --git a/brewman/brewman/views/services/voucher/__init__.py b/brewman/brewman/views/services/voucher/__init__.py index b8c228d2..53581552 100644 --- a/brewman/brewman/views/services/voucher/__init__.py +++ b/brewman/brewman/views/services/voucher/__init__.py @@ -126,7 +126,7 @@ def blank_voucher(type=None, date=None, additionalInfo=None): raise ValidationError('Voucher Type is null and no information is provided in additional information') elif type is None: type = additionalInfo['Type'] - if date is None and additionalInfo is None: + if date is None and 'Date' not in additionalInfo: raise ValidationError('Both Date and Additional Information cannot be null') elif date is None: date = additionalInfo['Date'] @@ -150,6 +150,15 @@ def blank_voucher(type=None, date=None, additionalInfo=None): 'Amount': 0, 'Debit': 1, 'CostCenter': {'CostCenterID': CostCenter.cost_center_kitchen()}}) + elif 'Source' in additionalInfo and 'Destination' in additionalInfo: + json_voucher['Journals'].append({'Ledger': {'LedgerID': LedgerBase.all_purchases()}, + 'Amount': 0, + 'Debit': -1, + 'CostCenter': {'CostCenterID': additionalInfo['Source']}}) + json_voucher['Journals'].append({'Ledger': {'LedgerID': LedgerBase.all_purchases()}, + 'Amount': 0, + 'Debit': 1, + 'CostCenter': {'CostCenterID': additionalInfo['Destination']}}) else: json_voucher['Date'] = additionalInfo['Date'] for item in additionalInfo['Journals']: diff --git a/brewman/brewman/views/services/voucher/blank_voucher.py b/brewman/brewman/views/services/voucher/blank_voucher.py index 9c0ebb54..fd4b628a 100644 --- a/brewman/brewman/views/services/voucher/blank_voucher.py +++ b/brewman/brewman/views/services/voucher/blank_voucher.py @@ -9,7 +9,7 @@ __author__ = 'tanshu' class blank_voucher_view(object): def __init__(self, request): self.request = request - + @view_config(request_param='type=Journal', permission='Journal') def journal(self): return self.get_blank() @@ -32,7 +32,15 @@ class blank_voucher_view(object): @view_config(request_param='type=Issue', permission='Issue') def issue(self): - return self.get_blank() + voucher_type = self.request.GET.get('type', None) + date = self.request.GET.get('Date', None) + source = self.request.GET.get('Source', None) + destination = self.request.GET.get('Destination', None) + if date is not None and source is not None and destination is not None: + return blank_voucher(type=voucher_type, date=None, + additionalInfo={'Date': date, 'Source': source, 'Destination': destination}) + else: + return self.get_blank() def get_blank(self): voucher_type = self.request.GET.get('type', None) diff --git a/brewman/brewman/views/services/voucher/issue.py b/brewman/brewman/views/services/voucher/issue.py index 85636e0d..80d1b899 100644 --- a/brewman/brewman/views/services/voucher/issue.py +++ b/brewman/brewman/views/services/voucher/issue.py @@ -41,7 +41,7 @@ def issue_create_inventory(voucher, item, batch_consumed): if quantity <= 0: raise ValidationError("Quantity of {0} cannot be zero".format(item.product.name)) if batch_consumed == True and quantity > batch.quantity_remaining: - raise ValidationError("Quantity available is {0} only".format(item.batch.quantity_remaining)) + raise ValidationError("Quantity available is {0} only".format(batch.quantity_remaining)) if batch_consumed is None: pass