Converted most routes to use resolve.

Issue fixed (hopefully) :).
Error in payment / receipt resolved fixed.
This commit is contained in:
Tanshu 2012-10-29 15:44:41 +05:30
parent 7ab945464b
commit 412704a73e
11 changed files with 250 additions and 111 deletions

View File

@ -16,7 +16,7 @@
<td>{{item.Code}}</td>
<td>{{item.Amount}}</td>
<td>
<button class="btn" ng-click="get(item.VoucherID)">Load</button>
<a class="btn" href="/Issue/{{item.VoucherID}}">Load</a>
</td>
</tr>
</table>

View File

@ -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');
}]);

View File

@ -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;
}}

View File

@ -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}).

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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']:

View File

@ -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)

View File

@ -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