Skip to content

Commit

Permalink
feat(journal): Input Validation in Transaction Edit
Browse files Browse the repository at this point in the history
This commit introduces edit-state validation on the Posting Journal.  The validation happens on all new rows and checks for common cases before submitting to the server:
 1. Check single line transaction
 2. Check missing accounts
 3. Check missing dates
 4. Check dates in wrong period
 5. Check that dates are all the same.
  • Loading branch information
lomamech authored and jniles committed Apr 13, 2017
1 parent 269a020 commit 570b993
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 12 deletions.
4 changes: 4 additions & 0 deletions client/src/i18n/en/posting_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@
"ERRORS":{"DATE_IN_WRONG_PERIOD":"Dates in wrong period",
"MISSING_DESCRIPTION":"Descriptions omitted",
"LOCKED_ACCOUNT":"Locked account(s)",
"TRANSACTION_DIFF_DATES" : "Transactions with different dates",
"UNBALANCED_TRANSACTIONS":"Unbalanced Transaction(s)",
"MISSING_ACCOUNTS":"Accounts omitted",
"MISSING_DATES":"Missing Dates",
"DEB_CRED_NOT_NULL" : "Debit and Credit should not be Zero",
"CREDITED_DEBITED" : "On the same transaction line an account can not be debited and credited at the same time",
"MISSING_DOCUMENT_ID":"Records without document ID",
"MISSING_ENTITY":"Record without entity ID",
"MISSING_FISCAL_OR_PERIOD":"Fiscal year or period omitted",
Expand Down
4 changes: 4 additions & 0 deletions client/src/i18n/fr/posting_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@
"COLUMNS":"Colonnes"},
"ERRORS":{"DATE_IN_WRONG_PERIOD":"Dates dans la mauvaise periode",
"MISSING_DESCRIPTION":"Descriptions omises",
"TRANSACTION_DIFF_DATES" : "Transactions avec des dates differentes",
"LOCKED_ACCOUNT":"Compte(s) verrouille(s)",
"UNBALANCED_TRANSACTIONS":"Transaction(s) non balancee(s)",
"MISSING_ACCOUNTS":"Compte(s) omis",
"CREDITED_DEBITED": "Sur une meme ligne de transaction un compte ne peut pas être débité et crédité en même temps",
"DEB_CRED_NOT_NULL" : "Le débit et le crédit ne doivent pas être égale à Zero",
"MISSING_DATES":"Date(s) manquante(s)",
"MISSING_DOCUMENT_ID":"Ligne(s) sans ID Document",
"MISSING_ENTITY":"Ligne(s) sans ID entite",
"MISSING_FISCAL_OR_PERIOD":"Annee fiscale ou periode omise",
Expand Down
93 changes: 82 additions & 11 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', '$q'
'$timeout', 'util', 'uiGridConstants', 'bhConstants', 'NotifyService', 'uuid', 'JournalService', 'Store', '$q', 'DateService'
];

/**
Expand All @@ -21,7 +21,7 @@ TransactionService.$inject = [
* @requires util
* @requires uiGridConstants
*/
function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify, uuid, Journal, Store, $q) {
function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify, uuid, Journal, Store, $q, Dates) {
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 @@ -303,22 +303,93 @@ function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify
* This method is used to check if a transaction is balanced by their record
*/
function validateTransaction(entity) {
var transaction = entity.data.data;
var transaction = [];

// include transaction data
entity.data.data.forEach(function (row) {
transaction.push(row);
});

// include new rows
entity.newRows.data.forEach(function (row) {
transaction.push(row);
});

var numberOfLine = transaction.length;
var error;

var ERR_SINGLE_LINE_TRANSACTION = 'POSTING_JOURNAL.ERRORS.SINGLE_LINE_TRANSACTION',
ERR_MISSING_ACCOUNTS = 'POSTING_JOURNAL.ERRORS.MISSING_ACCOUNTS',
ERR_MISSING_DATES = 'POSTING_JOURNAL.ERRORS.MISSING_DATES',
ERR_DATE_IN_WRONG_PERIOD = 'POSTING_JOURNAL.ERRORS.DATE_IN_WRONG_PERIOD',
ERR_TRANSACTION_DIFF_DATES = 'POSTING_JOURNAL.ERRORS.TRANSACTION_DIFF_DATES',
ERR_UNBALANCED_TRANSACTIONS = 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS',
ERR_DEB_CRED_NOT_NULL = 'POSTING_JOURNAL.ERRORS.DEB_CRED_NOT_NULL',
ERR_CREDITED_DEBITED = 'POSTING_JOURNAL.ERRORS.CREDITED_DEBITED';

// If the transaction are single line transaction
if(numberOfLine === 1){
return ERR_SINGLE_LINE_TRANSACTION;
}


var debit = 0,
credit = 0;

credit = 0,
initialDate,
accountNull = false,
dateNull = false,
dateDifferent = false,
dateWrongPeriod = false,
debitCreditNull = false,
debitedCreditedNull = false;

if(transaction[0].trans_date){
initialDate = transaction[0].trans_date;
}


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

var ERR_UNBALANCED_TXN = 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS';
// Check if they are account number Null
accountNull = !row.account_number;

// later in validateTransaction()
var error;
// Check if they are trans_date Null
dateNull = !row.trans_date;

// Check if they are different Date
dateDifferent = Dates.util.str(row.trans_date) !== Dates.util.str(initialDate);

// Check if debit and credit are Null
debitCreditNull = (!Number(row.debit_equiv) && !Number(row.credit_equiv));

// Check if they are value on debit and Credit
debitedCreditedNull = (row.debit_equiv > 0 && row.credit_equiv > 0);

// check if trans_date is in bed period
if(new Date(row.trans_date) < new Date(row.period_start) || new Date(row.trans_date) > new Date(row.period_end)){
dateWrongPeriod = true;
}
});

if (debit !== credit) {
error = ERR_UNBALANCED_TXN;
if(accountNull) {
error = ERR_MISSING_ACCOUNTS;
} else if (dateNull){
error = ERR_MISSING_DATES;
} else if (dateWrongPeriod){
error = ERR_DATE_IN_WRONG_PERIOD;
} else if (dateDifferent){
error = ERR_TRANSACTION_DIFF_DATES;
} else if (debitCreditNull){
error = ERR_DEB_CRED_NOT_NULL;
} else if (debitedCreditedNull){
error = ERR_CREDITED_DEBITED;
} else {
// later in validateTransaction()
if (debit !== credit) {
error = ERR_UNBALANCED_TRANSACTIONS;
}
}

return error;
Expand Down
3 changes: 2 additions & 1 deletion client/src/modules/journal/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ function JournalController(Journal, Sorting, Grouping, Filtering, Columns, Confi
load(vm.filters);
})
.catch(function (error) {
if(error === 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS'){
if (!error.status){
Notify.warn(error);
} else {
Notify.handleError(error);
}

});
}

Expand Down

0 comments on commit 570b993

Please sign in to comment.