From 0fecc02e1ac10ae99df6f300632d5e20b818646c Mon Sep 17 00:00:00 2001 From: tanshu Date: Sun, 4 Dec 2016 13:02:37 +0530 Subject: [PATCH] Feature: Math Solver for the Amounts in Journal, Payment and Receipt. --- brewman/static/base.html | 2 ++ brewman/static/scripts/angular_service.js | 19 +++++++++++++++++++ brewman/static/scripts/journal.js | 16 +++++++++++----- brewman/static/scripts/payment.js | 16 +++++++++++----- brewman/static/scripts/receipt.js | 16 +++++++++++----- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/brewman/static/base.html b/brewman/static/base.html index 94fd2592..cf0aaa7d 100644 --- a/brewman/static/base.html +++ b/brewman/static/base.html @@ -36,6 +36,8 @@ + + diff --git a/brewman/static/scripts/angular_service.js b/brewman/static/scripts/angular_service.js index 9ecac5a2..65e9d706 100644 --- a/brewman/static/scripts/angular_service.js +++ b/brewman/static/scripts/angular_service.js @@ -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; + } + }; +}); diff --git a/brewman/static/scripts/journal.js b/brewman/static/scripts/journal.js index da2e6684..7fbfea49 100644 --- a/brewman/static/scripts/journal.js +++ b/brewman/static/scripts/journal.js @@ -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; diff --git a/brewman/static/scripts/payment.js b/brewman/static/scripts/payment.js index ac3b52a8..83bc50b0 100644 --- a/brewman/static/scripts/payment.js +++ b/brewman/static/scripts/payment.js @@ -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; } diff --git a/brewman/static/scripts/receipt.js b/brewman/static/scripts/receipt.js index 790897c4..3c8624c7 100644 --- a/brewman/static/scripts/receipt.js +++ b/brewman/static/scripts/receipt.js @@ -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; }