Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Imbalanced transaction in Journal #1465

Merged
merged 3 commits into from
Apr 6, 2017

Conversation

lomamech
Copy link
Contributor

@lomamech lomamech commented Apr 4, 2017

Prevent Imbalanced transaction in Journal …

  • Adding function validate transaction who check if debit is equal to credit
  • Restructured catching error in controller of journal

closes #1256

@lomamech lomamech requested a review from jniles April 4, 2017 12:39
Copy link
Collaborator

@jniles jniles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start. If we want to add more checks later, we will need to move all the logic into validateTransacation(). Why don't you do that from the beginning?

credit += Number(journal.credit_equiv);
});

return debit !== credit ? true : false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, you can just return debit !== credit. It's probably easier to read.

@@ -532,6 +552,12 @@ function TransactionService($timeout, util, uiGridConstants, bhConstants, Notify
* This function saves all transactions by
*/
Transactions.prototype.save = function save() {
var clientErrors = validateTransaction(this._entity);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better way than returning (and rejecting) a boolean would be to return the error code. So, instead of returning true or false, return the string POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS.

That way, anyone else who makes an error condition (like missing dates, missing values, etc) can simply add to the validate function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best way to do this would be to add a validation key at the top of this service, like:

var ERR_IMBALANCED_TXN = 'POSTING_JOURNAL.ERRORS.UNBALANCED_TRANSACTIONS';

// later in validateTransaction()
var error;

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

// another condition

// another condition ..

return error;

* This method is used to check if a transaction is balanced by their record
*/
function validateTransaction(entity) {
var dataEntity = entity.data.data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name for this is transaction, since we are editing a transaction and the function is called validateTransaction().

var debit = 0,
credit = 0;

dataEntity.forEach(function (journal) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A transaction is made of rows, not journals (a journal contains transactions), so a better name for this variable is row. Or line. Or item. But journal is confusing.

* @description
* This method is used to check if a transaction is balanced by their record
*/
function transaction(entity) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lomamech, the name validateTransaction() was fine. It's dataEntity that should be called transaction. Because it is a transaction, right?

- Adding function validate transaction who check if debit is equal to
  credit
- Restructured catching error in controller of journal
-Using the best name for function who check the balance
-Using not confusing key word
-Return the String for the function transaction
* This method is used to check if a transaction is balanced by their record
*/
function validateTransaction(entity) {
var transaction = entity.data.data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This is much clearer!

@jniles
Copy link
Collaborator

jniles commented Apr 6, 2017

@lomamech, can you rebase this branch to master so that we can be sure everything will work properly?

@jniles jniles merged commit 6cdd9d1 into IMA-WorldHealth:master Apr 6, 2017
@jniles
Copy link
Collaborator

jniles commented Apr 6, 2017

@lomamech, thanks! 👍

@jniles jniles mentioned this pull request Apr 11, 2017
6 tasks
@lomamech lomamech deleted the preventImbalancedInJournal branch May 15, 2017 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent any transaction imbalance in editing of Posting Journal
2 participants