Feature: Math Solver for the Amounts in Journal, Payment and Receipt.

This commit is contained in:
tanshu 2016-12-04 13:02:37 +05:30
parent c90651aa8c
commit 0fecc02e1a
5 changed files with 54 additions and 15 deletions

View File

@ -36,6 +36,8 @@
<script src="/js/d3.v3.min.js"></script>
<script src="/js/nv.d3.js"></script>
<script src="/js/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.8.0/math.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
<script src="https://code.angularjs.org/1.5.0/i18n/angular-locale_en-in.js"></script>

View File

@ -469,3 +469,22 @@ overlordService.factory("uploadedImageResizer", ["$q", "ReaderPromise", function
return ProcessUpload;
}]);
overlordService.factory("mathSolver", function () {
return function Test(expr) {
var ans;
try {
if (typeof expr === 'number') {
ans = expr;
} else {
ans = math.eval(expr.trim().replace(',', ''));
}
}
catch (e) {
ans = Number.NaN;
}
finally {
return ans;
}
};
});

View File

@ -1,6 +1,6 @@
'use strict';
var JournalController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'voucher', 'Account', function ($scope, $location, asDate, $modal, uploadedImageResizer, voucher, Account) {
var JournalController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'mathSolver', 'voucher', 'Account', function ($scope, $location, asDate, $modal, uploadedImageResizer, mathSolver, voucher, Account) {
$scope.foAccount = true;
$scope.voucher = voucher;
@ -35,12 +35,18 @@ var JournalController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploa
};
$scope.add = function () {
if (!$scope.account || !$scope.account.LedgerID || !$scope.amount || Number(!$scope.amount)) {
var amount,
oldJournal;
if (!$scope.account || !$scope.account.LedgerID) {
return;
}
var oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
amount = mathSolver($scope.amount);
if (Number.isNaN(amount) || amount <= 0) {
return;
}
oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
if (!angular.isUndefined(oldJournal)) {
var amount = (oldJournal.Debit * oldJournal.Amount) + (parseInt($scope.debit) * Number($scope.amount));
amount = (oldJournal.Debit * oldJournal.Amount) + (parseInt($scope.debit) * amount);
if (amount < 0) {
oldJournal.Debit = -1;
oldJournal.Amount = amount * -1;
@ -49,7 +55,7 @@ var JournalController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploa
oldJournal.Amount = amount;
}
} else {
$scope.voucher.Journals.push({Debit: parseInt($scope.debit), Amount: Number($scope.amount), Ledger: $scope.account});
$scope.voucher.Journals.push({Debit: parseInt($scope.debit), Amount: amount, Ledger: $scope.account});
}
delete $scope.account;
delete $scope.amount;

View File

@ -1,6 +1,6 @@
'use strict';
var PaymentController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'voucher', 'ledgers', 'Account', function ($scope, $location, asDate, $modal, uploadedImageResizer, voucher, ledgers, Account) {
var PaymentController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'mathSolver', 'voucher', 'ledgers', 'Account', function ($scope, $location, asDate, $modal, uploadedImageResizer, mathSolver, voucher, ledgers, Account) {
$scope.foAccount = true;
$scope.voucher = voucher;
@ -35,15 +35,21 @@ var PaymentController = ['$scope', '$location', 'asDateFilter', '$modal', 'uploa
};
$scope.add = function () {
if (!$scope.account || !$scope.account.LedgerID || !$scope.amount || Number(!$scope.amount)) {
var amount,
oldJournal;
if (!$scope.account || !$scope.account.LedgerID) {
return;
}
var oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
amount = mathSolver($scope.amount);
if (Number.isNaN(amount) || amount <= 0) {
return;
}
oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
if (angular.isUndefined(oldJournal)) {
$scope.voucher.Journals.push({Debit: 1, Amount: Number($scope.amount), Ledger: $scope.account});
$scope.voucher.Journals.push({Debit: 1, Amount: amount, Ledger: $scope.account});
} else {
if (oldJournal.Debit === 1) {
oldJournal.Amount = oldJournal.Amount + Number($scope.amount);
oldJournal.Amount += amount;
} else {
return;
}

View File

@ -1,6 +1,6 @@
'use strict';
var ReceiptController = ['$scope', '$routeParams', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'voucher', 'ledgers', 'Voucher', 'Account', function ($scope, $routeParams, $location, asDate, $modal, uploadedImageResizer, voucher, ledgers, Voucher, Account) {
var ReceiptController = ['$scope', '$routeParams', '$location', 'asDateFilter', '$modal', 'uploadedImageResizer', 'mathSolver', 'voucher', 'ledgers', 'Voucher', 'Account', function ($scope, $routeParams, $location, asDate, $modal, uploadedImageResizer, mathSolver, voucher, ledgers, Voucher, Account) {
$scope.foAccount = true;
$scope.voucher = voucher;
$scope.ledgers = ledgers;
@ -34,15 +34,21 @@ var ReceiptController = ['$scope', '$routeParams', '$location', 'asDateFilter',
};
$scope.add = function () {
if (!$scope.account || !$scope.account.LedgerID || !$scope.amount || Number(!$scope.amount)) {
var amount,
oldJournal;
if (!$scope.account || !$scope.account.LedgerID) {
return;
}
var oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
amount = mathSolver($scope.amount);
if (Number.isNaN(amount) || amount <= 0) {
return;
}
oldJournal = getOld($scope.account.LedgerID, $scope.voucher.Journals);
if (angular.isUndefined(oldJournal)) {
$scope.voucher.Journals.push({Debit: -1, Amount: Number($scope.amount), Ledger: $scope.account});
$scope.voucher.Journals.push({Debit: -1, Amount: amount, Ledger: $scope.account});
} else {
if (oldJournal.Debit === -1) {
oldJournal.Amount = oldJournal.Amount + Number($scope.amount);
oldJournal.Amount += amount;
} else {
return;
}