Skip to content

Commit

Permalink
Updates for Aged Creditor Report
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcameron committed Jan 3, 2022
1 parent ad233a1 commit 0702abb
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ angular.module('bhima.controllers')
.controller('aged_creditorsController', AgedCreditorsConfigController);

AgedCreditorsConfigController.$inject = [
'$sce', 'NotifyService', 'BaseReportService', 'AppCache', 'reportData', '$state',
'$sce', 'NotifyService', 'BaseReportService', 'AppCache', 'reportData', '$state', 'SessionService',
];

function AgedCreditorsConfigController($sce, Notify, SavedReports, AppCache, reportData, $state) {
function AgedCreditorsConfigController($sce, Notify, SavedReports, AppCache, reportData, $state, Session) {
const vm = this;
const cache = new AppCache('configure_aged_creditors');
const reportUrl = 'reports/finance/creditors/aged';
Expand All @@ -20,6 +20,10 @@ function AgedCreditorsConfigController($sce, Notify, SavedReports, AppCache, rep
vm.previewResult = null;
};

vm.setCurrency = function setCurrency(currency) {
vm.reportDetails.currency_id = currency.id;
};

vm.onSelectFiscalYear = (fiscalYear) => {
vm.reportDetails.fiscal_id = fiscalYear.id;
};
Expand Down Expand Up @@ -60,5 +64,10 @@ function AgedCreditorsConfigController($sce, Notify, SavedReports, AppCache, rep
if (cache.reportDetails) {
vm.reportDetails = angular.copy(cache.reportDetails);
}

// Set the defaults
if (!angular.isDefined(vm.reportDetails.currencyId)) {
vm.reportDetails.currency_id = Session.enterprise.currency_id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ <h3 class="text-capitalize" translate>REPORT.AGED_CREDITORS.TITLE</h3>
</label>
</div>

<bh-currency-select
on-change="ReportConfigCtrl.setCurrency(currency)"
currency-id="ReportConfigCtrl.reportDetails.currency_id">
</bh-currency-select>

<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
</bh-loading-button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ <h3 class="text-capitalize" translate>REPORT.AGED_DEBTORS.TITLE</h3>
</label>
</div>

<bh-currency-select
on-change="ReportConfigCtrl.setCurrency(currency)"
currency-id="ReportConfigCtrl.reportDetails.currency_id">
</bh-currency-select>

<!-- included Total Zeros -->
<div class="checkbox">
<label>
Expand All @@ -57,6 +52,11 @@ <h3 class="text-capitalize" translate>REPORT.AGED_DEBTORS.TITLE</h3>
</label>
</div>

<bh-currency-select
on-change="ReportConfigCtrl.setCurrency(currency)"
currency-id="ReportConfigCtrl.reportDetails.currency_id">
</bh-currency-select>

<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
</bh-loading-button>
Expand Down
30 changes: 18 additions & 12 deletions server/controllers/finance/reports/creditors/aged.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
<strong>{{translate "REPORT.AGED_CREDITORS.TITLE"}}</strong>
</h3>

<h5 class="text-center">
<h4 class="text-center">
<span class="text-capitalize">{{translate "FORM.LABELS.UNTIL_PERIOD"}}</span> <span class="text-capitalize"> {{date dateUntil "MMMM YYYY"}} </span>
</h5>
</h4>

{{#unless previousFyClosed}}
<div class="alert alert-warning">
<p>{{translate "REPORT.WARN_PREV_FY_NOT_CLOSED"}}</p>
</div>
{{/unless}}

<!-- margin is the cell size -->
<section>
Expand Down Expand Up @@ -59,25 +65,25 @@
<td class="text-left">{{this.number}}</td>
<td class="text-right">
{{#if this.excess}}
{{debcred this.excess ../metadata.enterprise.currency_id}}
{{debcred this.excess ../currency_id}}
{{/if}}
</td>
<td class="text-right">
{{#if this.ninety}}
{{debcred this.ninety ../metadata.enterprise.currency_id}}
{{debcred this.ninety ../currency_id}}
{{/if}}
</td>
<td class="text-right">
{{#if this.sixty}}
{{debcred this.sixty ../metadata.enterprise.currency_id}}
{{debcred this.sixty ../currency_id}}
{{/if}}
</td>
<td class="text-right">
{{#if this.thirty}}
{{debcred this.thirty ../metadata.enterprise.currency_id}}
{{debcred this.thirty ../currency_id}}
{{/if}}
</td>
<td class="text-right">{{debcred this.total ../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{debcred this.total ../currency_id}}</td>
</tr>
{{else}}
{{> emptyTable columns=7}}
Expand All @@ -87,11 +93,11 @@
<tfoot>
<tr style="background-color: #ddd;">
<th colspan="2">{{translate "TABLE.COLUMNS.TOTAL"}}</th>
<th class="text-right">{{debcred aggregates.excess metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred aggregates.ninety metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred aggregates.sixty metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred aggregates.thirty metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred aggregates.total metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred aggregates.excess ./currency_id}}</th>
<th class="text-right">{{debcred aggregates.ninety ./currency_id}}</th>
<th class="text-right">{{debcred aggregates.sixty ./currency_id}}</th>
<th class="text-right">{{debcred aggregates.thirty ./currency_id}}</th>
<th class="text-right">{{debcred aggregates.total ./currency_id}}</th>
</tr>
</tfoot>
{{/if}}
Expand Down
47 changes: 36 additions & 11 deletions server/controllers/finance/reports/creditors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function agedCreditorReport(req, res, next) {
db.one(sql, [qs.period_id])
.then(period => {
qs.date = period.end_date;
qs.enterprise_id = metadata.enterprise.id;
// fire the SQL for the report
return queryContext(qs);
})
Expand Down Expand Up @@ -74,28 +75,49 @@ async function queryContext(params = {}) {
// format the dates for MySQL escape
const dates = _.fill(Array(5), params.date);
const data = {};
const currencyId = db.escape(params.currency_id);
const enterpriseId = db.escape(params.enterprise_id);

const fySql = `
SELECT locked FROM fiscal_year
WHERE ID = (SELECT previous_FISCAL_year_id from fiscal_year where id = ?)
`;

const previousFy = await db.one(fySql, [params.fiscal_id]);

const groupByMonthColumns = `
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 0, gl.debit_equiv - gl.credit_equiv, 0)) AS thirty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 1, gl.debit_equiv - gl.credit_equiv, 0)) AS sixty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 2, gl.debit_equiv - gl.credit_equiv, 0)) AS ninety,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) > 2, gl.debit_equiv - gl.credit_equiv, 0)) AS excess,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 0, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 1, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 2, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) > 2, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
`;

const groupByRangeColumns = `
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, gl.debit_equiv - gl.credit_equiv, 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, gl.debit_equiv - gl.credit_equiv, 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, gl.debit_equiv - gl.credit_equiv, 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, gl.debit_equiv - gl.credit_equiv, 0)) AS excess,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
`;

const columns = useMonthGrouping ? groupByMonthColumns : groupByRangeColumns;
// switch between grouping by month and grouping by period
const columns = useMonthGrouping
? groupByMonthColumns
: groupByRangeColumns;

// selects into columns of 30, 60, 90, and >90
const creditorSql = `
SELECT BUID(cg.uuid) AS id, cg.name, a.number,
${columns}
SUM(gl.debit_equiv - gl.credit_equiv) AS total
SUM((gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1)) AS total
FROM creditor_group AS cg
JOIN general_ledger AS gl ON gl.account_id = cg.account_id
JOIN account AS a ON a.id = cg.account_id
Expand All @@ -110,7 +132,8 @@ async function queryContext(params = {}) {
const aggregateSql = `
SELECT
${columns}
SUM(gl.debit_equiv - gl.credit_equiv) AS total
SUM((gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1)) AS total
FROM creditor_group AS cg
JOIN general_ledger AS gl ON gl.account_id = cg.account_id
WHERE DATE(gl.trans_date) <= DATE(?)
Expand All @@ -121,6 +144,7 @@ async function queryContext(params = {}) {
const creditors = await db.exec(creditorSql, [...dates, params.fiscal_id]);
data.creditors = creditors;
data.dateUntil = params.date;
data.previousFyClosed = previousFy.locked;

// this is specific to grouping by months
data.firstMonth = params.date;
Expand All @@ -131,6 +155,7 @@ async function queryContext(params = {}) {

const [aggregates] = await db.exec(aggregateSql, [...dates, params.fiscal_id]);
data.aggregates = aggregates;
data.currency_id = params.currency_id;

return data;
}
Expand Down
39 changes: 16 additions & 23 deletions server/controllers/finance/reports/debtors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const db = require('../../../../lib/db');
const TEMPLATE = './server/controllers/finance/reports/debtors/aged.handlebars';

const DEFAULT_OPTIONS = {
csvKey: 'debtors',
orientation: 'landscape',
csvKey : 'debtors',
orientation : 'landscape',
};

/**
Expand All @@ -40,7 +40,6 @@ function agedDebtorReport(req, res, next) {
const metadata = _.clone(req.session);

let report;
let previousFyLocked;

try {
report = new ReportManager(TEMPLATE, metadata, qs);
Expand Down Expand Up @@ -95,31 +94,25 @@ async function queryContext(params = {}) {
const previousFy = await db.one(fySql, [params.fiscal_id]);

const groupByMonthColumns = `
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 0, (gl.debit_equiv - gl.credit_equiv)*
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 1, (gl.debit_equiv - gl.credit_equiv)*
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 2, (gl.debit_equiv - gl.credit_equiv)*
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) > 2, (gl.debit_equiv - gl.credit_equiv)*
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 0, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 1, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 2, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) > 2, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
`;

const groupByRangeColumns = `
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, (gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1), 0)) AS excess,
`;

// switch between grouping by month and grouping by period
Expand All @@ -132,7 +125,7 @@ async function queryContext(params = {}) {
SELECT BUID(dg.uuid) AS id, dg.name, a.number,
${columns}
SUM((gl.debit_equiv - gl.credit_equiv) *
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1)) AS total
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1)) AS total
FROM debtor_group AS dg JOIN debtor AS d ON dg.uuid = d.group_uuid
LEFT JOIN general_ledger AS gl ON gl.entity_uuid = d.uuid
JOIN account AS a ON a.id = dg.account_id
Expand All @@ -148,7 +141,7 @@ async function queryContext(params = {}) {
SELECT
${columns}
SUM((gl.debit_equiv - gl.credit_equiv)*
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1))AS total
IFNULL(GetExchangeRate(${enterpriseId}, ${currencyId}, gl.trans_date), 1)) AS total
FROM debtor_group AS dg JOIN debtor AS d ON dg.uuid = d.group_uuid
LEFT JOIN general_ledger AS gl ON gl.entity_uuid = d.uuid
WHERE DATE(gl.trans_date) <= DATE(?)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/reports/finance/aged_creditors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const RenderingTests = require('../rendering');

const target = '/reports/finance/creditors/aged';
const options = {
fiscal_id : 4,
period_id : 201802,
currency_id : 2,
enterprise_id : 1,
};

describe(`(${target}) Aged Creditors`, RenderingTests(target, null, options));
3 changes: 3 additions & 0 deletions test/integration/reports/finance/aged_debtors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const RenderingTests = require('../rendering');

const target = '/reports/finance/debtors/aged';
const options = {
fiscal_id : 4,
period_id : 201802,
currency_id : 2,
enterprise_id : 1,
};

describe(`(${target}) Aged Debtors`, RenderingTests(target, null, options));

0 comments on commit 0702abb

Please sign in to comment.