Skip to content

Commit

Permalink
refactor: Add multi currency support
Browse files Browse the repository at this point in the history
  • Loading branch information
lomamech committed Jan 26, 2022
1 parent 4f6c561 commit 2b79fd7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ angular.module('bhima.controllers')

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

function recoveryCapacityController($sce, Notify, SavedReports, AppCache, reportData, $state) {
function recoveryCapacityController($sce, Notify, SavedReports, AppCache, reportData, $state, Session) {
const vm = this;
const cache = new AppCache('recovery_capacity');
const reportUrl = 'reports/finance/recovery_capacity';

vm.previewGenerated = false;
vm.reportDetails = { includeUnpostedValues : 0 };
vm.reportDetails = { includeUnpostedValues : 0, currencyId : Session.enterprise.currency_id };

vm.onChangeUnpostedValues = value => {
vm.reportDetails.includeUnpostedValues = value;
};

vm.onSelectCurrency = currency => {
vm.reportDetails.currencyId = currency.id;
};

vm.clearPreview = function clearPreview() {
vm.previewGenerated = false;
vm.previewResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ <h3 class="text-capitalize" translate>REPORT.RECOVERY_CAPACITY.TITLE</h3>
limit-min-fiscal>
</bh-date-interval>

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

<bh-yes-no-radios
value="ReportConfigCtrl.reportDetails.includeUnpostedValues"
name="includeUnpostedValues"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ <h3 class="text-capitalize" translate>REPORT.UNPAID_INVOICE_PAYMENTS_REPORT.TITL
<bh-clear on-clear="ReportConfigCtrl.onClear()"></bh-clear>
</bh-debtor-group-select>


<!-- service -->
<bh-service-select
service-uuid="ReportConfigCtrl.reportDetails.serviceUuid"
Expand All @@ -52,7 +51,6 @@ <h3 class="text-capitalize" translate>REPORT.UNPAID_INVOICE_PAYMENTS_REPORT.TITL
on-change="ReportConfigCtrl.onSelectCurrency(currency)">
</bh-currency-select>


<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
</bh-loading-button>
Expand Down
8 changes: 8 additions & 0 deletions server/controllers/finance/reports/recovery_capacity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const moment = require('moment');

const ReportManager = require('../../../../lib/ReportManager');
const db = require('../../../../lib/db');
const Exchange = require('../../exchange');

module.exports.report = report;

Expand All @@ -29,6 +30,9 @@ async function report(req, res, next) {
const { dateFrom, dateTo } = req.query;
const metadata = _.clone(req.session);

const { enterprise } = req.session;
const currencyId = Number(req.query.currencyId);

const rpt = new ReportManager(TEMPLATE, metadata, qs);

const CASH_PAYMENT_TRANSACTION_TYPE = 2;
Expand Down Expand Up @@ -158,10 +162,14 @@ async function report(req, res, next) {

const rows = await db.exec(query, parameters);
const totals = await db.one(queryTotals, parameters);
const getExchangeRateData = await Exchange.getExchangeRate(enterprise.id, currencyId, new Date(dateTo));
const exchangeRate = getExchangeRateData.rate || 1;

const result = await rpt.render({
dateFrom,
dateTo,
currencyId,
exchangeRate,
rows,
totals,
includeUnpostedValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
{{date dateFrom}} - {{date dateTo}}
</h4>

{{> exchangeRate rate=exchangeRate currencyId=currencyId date=dateTo}}

<table class="table table-condensed" style="margin-bottom:16px; border:0px;">
<tr>
<td style="border: 0px;">
Expand Down Expand Up @@ -52,9 +54,9 @@
<tr>
<td>{{date row.date}}</td>
<td style="width: 10em;" class="text-right">{{row.registrations}}</td>
<td class="text-right">{{debcred row.total_invoiced ../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{debcred row.avg_cost ../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{debcred row.total_paid ../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{debcred (multiply row.total_invoiced ../exchangeRate) ../currencyId}}</td>
<td class="text-right">{{debcred (multiply row.avg_cost ../exchangeRate) ../currencyId}}</td>
<td class="text-right">{{debcred (multiply row.total_paid ../exchangeRate) ../currencyId}}</td>
<td
style="width: 15em;"
{{#if row.has_data}}
Expand All @@ -73,9 +75,9 @@
<tr>
<th>{{translate 'TABLE.COLUMNS.TOTAL'}}</th>
<th style="width: 10em;" class="text-right">{{totals.registrations}}</th>
<th class="text-right">{{debcred totals.total_invoiced metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred totals.avg_cost metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred totals.total_paid metadata.enterprise.currency_id}}</th>
<th class="text-right">{{debcred (multiply totals.total_invoiced exchangeRate) currencyId}}</th>
<th class="text-right">{{debcred (multiply totals.avg_cost exchangeRate) currencyId}}</th>
<th class="text-right">{{debcred (multiply totals.total_paid exchangeRate) currencyId}}</th>
<th
style="width: 15em;"
{{#gt totals.recovery_capacity 0.71}} class="bg-success text-success text-right" {{/gt}}
Expand All @@ -86,9 +88,7 @@
</tr>
</tfoot>
</table>

</div>
</div>

</div>
</body>

0 comments on commit 2b79fd7

Please sign in to comment.