Skip to content

Commit

Permalink
fix(journal): Prevent Imbalanced transactions via client validation (#…
Browse files Browse the repository at this point in the history
…1465)

* Prevent Imbalanced transaction in Journal
- Adding function validate transaction who check if debit is equal to
  credit
- Restructured catching error in controller of journal

* Review for prevent imbalanced transaction
-Using the best name for function who check the balance
-Using not confusing key word
-Return the String for the function transaction

* Improve the label of function and variable
  • Loading branch information
lomamech authored and jniles committed Apr 6, 2017
1 parent c7cecb8 commit 6cdd9d1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
39 changes: 37 additions & 2 deletions client/src/js/services/grid/TransactionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('bhima.services')
.service('TransactionService', TransactionService);

TransactionService.$inject = [
'$timeout', 'util', 'uiGridConstants', 'bhConstants', 'NotifyService', 'uuid', 'JournalService', 'Store',
'$timeout', 'util', 'uiGridConstants', 'bhConstants', 'NotifyService', 'uuid', 'JournalService', 'Store', '$q'
];

/**
Expand All @@ -21,7 +21,7 @@ TransactionService.$inject = [
* @requires util
* @requires uiGridConstants
*/
function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify, uuid, Journal, Store) {
function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify, uuid, Journal, Store, $q) {
var ROW_EDIT_FLAG = bhConstants.transactions.ROW_EDIT_FLAG;
var ROW_HIGHLIGHT_FLAG = bhConstants.transactions.ROW_HIGHLIGHT_FLAG;
var ROW_INVALID_FLAG = bhConstants.transactions.ROW_INVALID_FLAG;
Expand Down Expand Up @@ -295,6 +295,35 @@ function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify
return true;
};


/**
* @method transaction
*
* @description
* This method is used to check if a transaction is balanced by their record
*/
function validateTransaction(entity) {
var transaction = entity.data.data;
var debit = 0,
credit = 0;

transaction.forEach(function (row) {
debit += Number(row.debit_equiv);
credit += Number(row.credit_equiv);
});

var ERR_UNBALANCED_TXN = 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS';

// later in validateTransaction()
var error;

if (debit !== credit) {
error = ERR_UNBALANCED_TXN;
}

return error;
}

// helper functions to get parent row (group header) from a child row
function getParentNode(row) {
return row.treeNode.parentRow;
Expand Down Expand Up @@ -532,6 +561,12 @@ function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify
* This function saves all transactions by
*/
Transactions.prototype.save = function save() {
var clientErrors = validateTransaction(this._entity);

if(clientErrors){
return $q.reject(clientErrors);
}

return Journal.saveChanges(this._entity, this._changes)
.then(function (results) {
this.disableCellNavigation();
Expand Down
8 changes: 7 additions & 1 deletion client/src/partials/journal/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,13 @@ function JournalController(Journal, Sorting, Grouping, Filtering, Columns, Confi
// ensure that all of the data now respects the current filter
load(vm.filters);
})
.catch(Notify.handleError);
.catch(function (error) {
if(error === 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS'){
Notify.warn(error);
} else {
Notify.handleError(error);
}
});
}

vm.toggleTransactionGroup = function toggleTransactionGroup() {
Expand Down

0 comments on commit 6cdd9d1

Please sign in to comment.