Skip to content

Commit

Permalink
fix(trialbalance): transition out of TB on post
Browse files Browse the repository at this point in the history
This commit resolves conflicting $state transitions by eliminating the
transition on failure.  This means that when $uibModalStack.dismissAll()
takes affect, the $state is not triggered to transition.  This is what
you expect.
  • Loading branch information
Jonathan Niles authored and jniles committed Feb 19, 2017
1 parent 08d9eb9 commit 6839172
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 63 deletions.
5 changes: 2 additions & 3 deletions client/src/js/services/TrialBalanceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ function TrialBalanceService(util, $http, $translate) {
*
* This function is called only when every test are passed without a fatal error
**/
function postToGeneralLedger (records) {

function postToGeneralLedger(records) {
var transactions = getTransactionList(records);
var url = baseUrl.concat('post_transactions/');

/** posting a list of transactions to the server to be stored to the general ledger **/
return $http.post(url, {transactions : transactions});
return $http.post(url, { transactions : transactions });
}

/**
Expand Down
23 changes: 10 additions & 13 deletions client/src/partials/journal/journal.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@ angular.module('bhima.routes')

$stateProvider
.state('journal', {
url : '/journal',
controller : 'JournalController as JournalCtrl',
url : '/journal',
controller : 'JournalController as JournalCtrl',
templateUrl : 'partials/journal/journal.html'
})
.state('journalPrint', {
controller : 'journal.print',
controller : 'journal.print',
templateUrl : 'partials/journal/print.html'
})
.state('journalVoucher', {
controller: 'JournalVoucherController as JournalVoucherCtrl',
templateUrl: 'partials/journal/voucher/voucher.html'
controller : 'JournalVoucherController as JournalVoucherCtrl',
templateUrl : 'partials/journal/voucher/voucher.html'
})
.state('journalModal', {
parent : 'journal',
parent : 'journal',
onEnter : ['$state', '$uibModal', function ($state, Modal) {
Modal.open({
size : 'lg',
templateUrl : 'partials/journal/modals/trialBalanceStructure.html'
}).result.then(function () {
// go to the parent state (with refresh)
$state.go('journal', null, { reload : true });
})
.catch(function () {
$state.go('journal', null, { notify: false });
}).result
.then(function () {
$state.go('journal', null, { reload: true });
});
}],
onExit : ['$uibModalStack', function (ModalStack) {
ModalStack.dismissAll();
}]
}],
})
.state('trialBalanceMain',{
parent : 'journalModal',
Expand Down
31 changes: 17 additions & 14 deletions client/src/partials/journal/modals/trialBalanceMain.body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ angular.module('bhima.controllers')

TrialBalanceMainBodyController.$inject = [
'SessionService', 'TrialBalanceService', 'GridGroupingService', 'GridColumnService',
'NotifyService', '$state', '$timeout', 'uiGridConstants'
'NotifyService', '$state', '$timeout', 'uiGridConstants',
];

/**
Expand Down Expand Up @@ -58,11 +58,11 @@ function TrialBalanceMainBodyController(Session, trialBalanceService, Grouping,
};

vm.gridOptions = {
enableColumnMenus: false,
treeRowHeaderAlwaysVisible: false,
appScopeProvider: vm,
columnDefs : columns,
onRegisterApi: function(gridApi){ vm.gridApi = gridApi;}
enableColumnMenus : false,
treeRowHeaderAlwaysVisible : false,
appScopeProvider : vm,
columnDefs : columns,
onRegisterApi : function(gridApi) { vm.gridApi = gridApi;}
};

vm.grouping = new Grouping(vm.gridOptions, false);
Expand All @@ -75,7 +75,7 @@ function TrialBalanceMainBodyController(Session, trialBalanceService, Grouping,
* This function fetch data by account from the server
* through the trial balance service module
**/
function fetchDataByAccount(){
function fetchDataByAccount() {
return trialBalanceService.getDataByAccount(vm.dataByTrans);
}

Expand Down Expand Up @@ -184,19 +184,20 @@ function TrialBalanceMainBodyController(Session, trialBalanceService, Grouping,
* The target view is not the main view, because you can not post to the general ledger from this view,
* you have to reset the view to the main view first, this view is just giving complementary information to the user.
**/
function viewErrorList () {
function viewErrorList() {
var lines = trialBalanceService.parseErrorRecord(errorList);
//FIX ME : what is the good way of keeping records? using appcache?
$state.go('trialBalanceErrors', {lines : lines, feedBack : vm.feedBack, records : records}, {reload : false});
// FIX ME : what is the good way of keeping records? using appcache?
$state.go('trialBalanceErrors', { lines: lines, feedBack: vm.feedBack, records: records }, { reload: false });
}


/**
* @function switchView
* @function switchView
* @description
* This method can change the way data are filled in the grid, from transaction grouping to account grouping vice versa
**/
function switchView (){
* This method can change the way data are filled in the grid, from
* transaction grouping to account grouping vice versa
*/
function switchView() {
var newView = trialBalanceService.switchView(vm.viewDetail.selected);
vm.viewDetail[newView]();
}
Expand All @@ -219,12 +220,14 @@ function TrialBalanceMainBodyController(Session, trialBalanceService, Grouping,
vm.feedBack = trialBalanceService.getFeedBack(errorList); //getting a feedback object to customize the grid
vm.isInvalid = vm.feedBack.hasError || vm.feedBack.hasWarning;
cssClass = trialBalanceService.getCSSClass(vm.feedBack);

$state.current.data.checkingData = {errors : errorList, feedBack : vm.feedBack, cssClass : cssClass};
$state.current.data.checked = true;

columns.forEach(function (col) {
col.headerCellClass = cssClass;
});

vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
})
.catch(errorHandler)
Expand Down
8 changes: 3 additions & 5 deletions client/src/partials/journal/modals/trialBalanceMain.footer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

angular.module('bhima.controllers')
.controller('TrialBalanceMainFooterController', TrialBalanceMainFooterController);

TrialBalanceMainFooterController.$inject = [
'$state', 'TrialBalanceService', 'NotifyService'
'$state', 'TrialBalanceService', 'NotifyService',
];

/**
Expand All @@ -14,7 +13,6 @@ TrialBalanceMainFooterController.$inject = [
*/
function TrialBalanceMainFooterController($state, trialBalanceService, Notify) {
var vm = this;

vm.state = $state;

/**
Expand All @@ -23,7 +21,7 @@ function TrialBalanceMainFooterController($state, trialBalanceService, Notify) {
* closes the modal and stop the posting process
**/
function cancel() {
$state.transitionTo('journal');
$state.go('journal');
}

/**
Expand All @@ -33,7 +31,7 @@ function TrialBalanceMainFooterController($state, trialBalanceService, Notify) {
function submit() {
trialBalanceService.postToGeneralLedger($state.params.records)
.then(function () {
$state.go('generalLedger', null, {reload : true});
$state.go('generalLedger');
})
.catch(Notify.handleError);
}
Expand Down
52 changes: 24 additions & 28 deletions server/controllers/finance/trialBalance/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@

/**
* The trial balance provides a description of what the general
* ledger would look like after posting data from the
* posting journal to the general ledger.
* It also submit errors back to the client.
*/
const q = require('q');
const uuid = require('node-uuid');
const _ = require('lodash');
const db = require('../../../lib/db');
const util = require('../../../lib/util');
const NotFound = require('../../../lib/errors/NotFound');
const BadRequest = require('../../../lib/errors/BadRequest');

// creates an error report for a given code
function createErrorReport(code, isFatal, rows) {
return {
code : code,
fatal : isFatal,
transactions : rows.map((row) => row.trans_id),
affectedRows : _.sumBy(rows, 'count')
code,
fatal : isFatal,
transactions : rows.map(row => row.trans_id),
affectedRows : _.sumBy(rows, 'count'),
};
}

// make sure that a entity_uuid exists for each deb_cred_type
function checkDescriptionExists(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id, pj.description FROM posting_journal AS pj
WHERE pj.trans_id IN (?) GROUP BY trans_id HAVING pj.description IS NULL;`;

Expand All @@ -41,7 +37,7 @@ function checkDescriptionExists(transactions) {

// make sure that the record Id exist in each line of the transaction
function checkRecordUuidExists(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.record_uuid, pj.trans_id FROM posting_journal AS pj
WHERE pj.trans_id IN (?) GROUP BY pj.trans_id HAVING pj.record_uuid IS NULL;`;

Expand All @@ -57,7 +53,7 @@ function checkRecordUuidExists(transactions) {

// make sure dates are in their correct period
function checkDateInPeriod(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id, pj.trans_date, p.start_date, p.end_date
FROM posting_journal AS pj JOIN period as p ON pj.period_id = p.id
WHERE DATE(pj.trans_date) NOT BETWEEN DATE(p.start_date) AND DATE(p.end_date) AND
Expand All @@ -76,7 +72,7 @@ function checkDateInPeriod(transactions) {

// make sure fiscal years and periods exist for all transactions
function checkPeriodAndFiscalYearExists(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id
FROM posting_journal AS pj
WHERE pj.trans_id IN (?) AND (pj.period_id IS NULL OR pj.fiscal_year_id IS NULL)
Expand All @@ -94,7 +90,7 @@ function checkPeriodAndFiscalYearExists(transactions) {

// make sure there are no missing accounts in the transactions
function checkMissingAccounts(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id
FROM posting_journal AS pj LEFT JOIN account ON
pj.account_id = account.id
Expand All @@ -113,7 +109,7 @@ function checkMissingAccounts(transactions) {

// Ensure no accounts are locked in the transactions
function checkAccountsLocked(transactions) {
let sql =
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id
FROM posting_journal AS pj LEFT JOIN account
ON pj.account_id = account.id
Expand All @@ -132,7 +128,7 @@ function checkAccountsLocked(transactions) {

// make sure the debit_equiv, credit_equiv are balanced
function checkTransactionsBalanced(transactions) {
let sql = `
const sql = `
SELECT COUNT(pj.uuid) AS count, pj.trans_id, SUM(pj.debit_equiv - pj.credit_equiv) AS balance
FROM posting_journal AS pj
WHERE pj.trans_id IN (?)
Expand All @@ -149,9 +145,9 @@ function checkTransactionsBalanced(transactions) {
});
}

//Check if there is no transaction with one line to avoid single line with ero in debit and credit which is valuable
function checkSingleLineTransaction (transactions){
let sql =
// Check if there is no transaction with one line to avoid single line with ero in debit and credit which is valuable
function checkSingleLineTransaction(transactions) {
const sql =
`SELECT COUNT(pj.uuid) AS count, pj.trans_id FROM posting_journal AS pj
WHERE pj.trans_id IN (?)
GROUP BY trans_id HAVING count = 1;`;
Expand All @@ -167,10 +163,10 @@ function checkSingleLineTransaction (transactions){
}

exports.getDataPerAccount = function (req, res, next) {
let transactions = req.body.transactions;
const transactions = req.body.transactions;

// @FIXME This could use a lot of comments
let requestString =
const requestString =
`SELECT pt.debit_equiv, pt.credit_equiv,
pt.account_id, pt.balance_before, account.number AS account_number,
(pt.balance_before + (pt.debit_equiv - pt.credit_equiv)) as balance_final
Expand Down Expand Up @@ -222,7 +218,7 @@ exports.getDataPerAccount = function (req, res, next) {
* }]
**/
exports.checkTransactions = function (req, res, next) {
let transactions = req.body.transactions;
const transactions = req.body.transactions;

if (!transactions) {
return next(new BadRequest('The transaction list is null or undefined'));
Expand All @@ -235,10 +231,10 @@ exports.checkTransactions = function (req, res, next) {
return q.all([
checkSingleLineTransaction(transactions), checkTransactionsBalanced(transactions), checkAccountsLocked(transactions),
checkMissingAccounts(transactions), checkPeriodAndFiscalYearExists(transactions), checkDateInPeriod(transactions),
checkRecordUuidExists(transactions), checkDescriptionExists(transactions)
checkRecordUuidExists(transactions), checkDescriptionExists(transactions),
])
.then(function (errorReports){
let errors = errorReports.filter(function (errorReport) {
.then(function (errorReports) {
const errors = errorReports.filter(function (errorReport) {
return errorReport;
});
res.status(201).json(errors);
Expand All @@ -253,15 +249,15 @@ exports.checkTransactions = function (req, res, next) {
* It posts data to the general ledger.
**/
exports.postToGeneralLedger = function (req, res, next) {
let transaction = db.transaction();
let transactions = req.body.transactions;
const transaction = db.transaction();
const transactions = req.body.transactions;

if (!transactions || !Array.isArray(transactions)) {
return next(new BadRequest('The transaction list is null or undefined otherwise The query is bad formatted'));
}

//Just a workaround because mysql does not have a type for array
let transactionString =
// Just a workaround because mysql does not have a type for array
const transactionString =
transactions.map((trans_id) => `"${trans_id}"`).join(',');

transaction.addQuery('CALL postToGeneralLedger(?)', [transactionString]);
Expand Down

0 comments on commit 6839172

Please sign in to comment.