-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ae875f
commit e28c3e2
Showing
10 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
client/src/modules/reports/generate/debtorSummary/debtorSummary.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
angular.module('bhima.controllers') | ||
.controller('debtorSummaryController', DebtorSummaryController); | ||
|
||
DebtorSummaryController.$inject = [ | ||
'$sce', 'NotifyService', 'BaseReportService', | ||
'AppCache', 'reportData', '$state', | ||
]; | ||
|
||
function DebtorSummaryController($sce, Notify, SavedReports, AppCache, reportData, $state) { | ||
const vm = this; | ||
const cache = new AppCache('debtorSummary'); | ||
const reportUrl = 'reports/finance/debtorSummary'; | ||
|
||
vm.previewGenerated = false; | ||
vm.reportDetails = {}; | ||
|
||
vm.clearPreview = function clearPreview() { | ||
vm.previewGenerated = false; | ||
vm.previewResult = null; | ||
}; | ||
|
||
vm.onSelectDebtor = (debtorGroup) => { | ||
vm.reportDetails.group_uuid = debtorGroup.uuid; | ||
}; | ||
|
||
vm.preview = function preview(form) { | ||
if (form.$invalid) { | ||
Notify.danger('FORM.ERRORS.RECORD_ERROR'); | ||
return 0; | ||
} | ||
|
||
|
||
// update cached configuration | ||
cache.reportDetails = angular.copy(vm.reportDetails); | ||
|
||
return SavedReports.requestPreview(reportUrl, reportData.id, angular.copy(vm.reportDetails)) | ||
.then(result => { | ||
vm.previewGenerated = true; | ||
vm.previewResult = $sce.trustAsHtml(result); | ||
}) | ||
.catch(Notify.handleError); | ||
}; | ||
|
||
vm.requestSaveAs = function requestSaveAs() { | ||
const options = { | ||
url : reportUrl, | ||
report : reportData, | ||
reportOptions : angular.copy(vm.reportDetails), | ||
}; | ||
|
||
return SavedReports.saveAsModal(options) | ||
.then(() => { | ||
$state.go('reportsBase.reportsArchive', { key : options.report.report_key }); | ||
}) | ||
.catch(Notify.handleError); | ||
}; | ||
|
||
checkCachedConfiguration(); | ||
|
||
function checkCachedConfiguration() { | ||
if (cache.reportDetails) { | ||
vm.reportDetails = angular.copy(cache.reportDetails); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
client/src/modules/reports/generate/debtorSummary/debtorSummary.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<bh-report-preview | ||
ng-if="ReportConfigCtrl.previewGenerated" | ||
source-document="ReportConfigCtrl.previewResult" | ||
on-clear-callback="ReportConfigCtrl.clearPreview()" | ||
on-save-callback="ReportConfigCtrl.requestSaveAs()"> | ||
</bh-report-preview> | ||
|
||
<div ng-show="!ReportConfigCtrl.previewGenerated"> | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<h3 class="text-capitalize" translate>REPORT.DEBTOR_SUMMARY.TITLE</h3> | ||
<p class="text-info" translate>REPORT.DEBTOR_SUMMARY.DESCRIPTION</p> | ||
</div> | ||
</div> | ||
|
||
<div class="row" style="margin-top : 10px"> | ||
<div class="col-md-6"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<span translate>REPORT.UTIL.OPTIONS</span> | ||
</div> | ||
<div class="panel-body"> | ||
<form name="ConfigForm" bh-submit="ReportConfigCtrl.preview(ConfigForm)" novalidate> | ||
|
||
<!-- debtor group --> | ||
<bh-debtor-group-select | ||
required="true" | ||
debtor-group-uuid = "ReportConfigCtrl.reportDetails.group_uuid" | ||
on-select-callback = "ReportConfigCtrl.onSelectDebtor(debtorGroup)"> | ||
</bh-debtor-group-select> | ||
|
||
|
||
<!-- Date interval --> | ||
<bh-date-interval | ||
date-from="ReportConfigCtrl.reportDetails.dateFrom" | ||
date-to="ReportConfigCtrl.reportDetails.dateTo" | ||
required="true" | ||
limit-min-fiscal> | ||
</bh-date-interval> | ||
|
||
<bh-loading-button loading-state="ConfigForm.$loading"> | ||
<span translate>REPORT.UTIL.PREVIEW</span> | ||
</bh-loading-button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
server/controllers/finance/reports/debtors/summaryReport.handlebars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{{> head title="REPORT.CLIENTS.TITLE"}} | ||
|
||
<body> | ||
<main class="container"> | ||
{{> header }} | ||
|
||
<h3 class="text-center text-capitalize"> | ||
<strong>{{translate "REPORT.DEBTOR_SUMMARY.TITLE"}}</strong> | ||
</h3> | ||
|
||
<h4 class="text-center text-capitalize"> | ||
{{ debtorGroup.name }} | ||
</h4> | ||
<h5 class="text-center"> | ||
<strong class="text-capitalize">{{date dateFrom "MMMM YYYY"}}</strong> | ||
- | ||
<strong class="text-capitalize">{{date dateTo "MMMM YYYY"}}</strong> | ||
</h4> | ||
|
||
<section> | ||
<table style="font-size:11px !important" class="table table-condensed table-report table-header-rotated table-bordered"> | ||
<thead> | ||
<tr class="text-capitalize text-center" style="background: #ddd"> | ||
<th>{{translate "FORM.LABELS.NO"}}</th> | ||
<th>{{translate "TABLE.COLUMNS.REFERENCE"}}</th> | ||
<th>{{translate "TABLE.COLUMNS.NAME"}}</th> | ||
{{#each ./services as | s |}} | ||
<th class="rotate-45" > | ||
<div><span style="padding-left:2px;">{{ s.name }}</span></div></th> | ||
|
||
{{/each}} | ||
<th class="rotate-45"><div><span style="padding-left:2px;">{{translate "FORM.LABELS.TOTAL"}}</span></div></th> | ||
|
||
</tr> | ||
</thead> | ||
<tbody> | ||
{{#each ./debtorMap as |debtor| }} | ||
<tr> | ||
<td>{{ add @index 1}}</td> | ||
<td>{{ debtor.ref}}</td> | ||
<td>{{ debtor.name}}</td> | ||
|
||
{{#each debtor.services as | amount |}} | ||
<th class="text-right">{{amount}}</th> | ||
{{/each}} | ||
<th class="text-right">{{currency debtor.total ../metadata.enterprise.currency_id}}</th> | ||
</tr> | ||
|
||
{{/each}} | ||
|
||
</tbody> | ||
|
||
<tfoot> | ||
<td colspan="3" class="text-right">{{ translate 'FORM.LABELS.TOTAL'}}</td> | ||
{{#each services as | s |}} | ||
<td class="text-right">{{ currency s.total ../metadata.enterprise.currency_id}}</td> | ||
{{/each}} | ||
<td>{{currency gobalSum ../metadata.enterprise.currency_id}}</td> | ||
</tfoot> | ||
</table> | ||
</section> | ||
</main> | ||
</body> |
100 changes: 100 additions & 0 deletions
100
server/controllers/finance/reports/debtors/summaryReport.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @overview ./finance/reports/debtors/summaryReport.js | ||
* | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
const ReportManager = require('../../../../lib/ReportManager'); | ||
const db = require('../../../../lib/db'); | ||
const util = require('../../../../lib/util'); | ||
|
||
module.exports.summaryReport = summaryReport; | ||
|
||
// path to the template to render | ||
const TEMPLATE = './server/controllers/finance/reports/debtors/summaryReport.handlebars'; | ||
|
||
const DEFAULT_OPTIONS = { | ||
csvKey : 'debtors', | ||
orientation : 'landscape', | ||
footerRight : '[page] / [toPage]', | ||
footerFontSize : '7', | ||
}; | ||
|
||
/** | ||
* @method summaryReport | ||
* | ||
* @description | ||
* The HTTP interface which actually creates the report. | ||
*/ | ||
async function summaryReport(req, res, next) { | ||
try { | ||
const qs = _.extend(req.query, DEFAULT_OPTIONS); | ||
const { dateFrom, dateTo } = req.query; | ||
const groupUuid = req.query.group_uuid; | ||
const metadata = _.clone(req.session); | ||
const report = new ReportManager(TEMPLATE, metadata, qs); | ||
const serviceMap = {}; | ||
const debtorMap = {}; | ||
const emptyArray = []; | ||
const serviceIndexMap = {}; | ||
let gobalSum = 0; | ||
|
||
const serviceSql = ` SELECT id, name FROM service `; | ||
const invoiceSql = ` | ||
SELECT ent.text as ref, d.text, SUM(i.cost) as amount, BUID(i.debtor_uuid) as debtor_uuid, i.service_id | ||
FROM invoice i | ||
JOIN service s on s.id= i.service_id | ||
JOIN debtor d ON d.uuid = i.debtor_uuid | ||
JOIN debtor_group dg on dg.uuid = d.groupUuid | ||
JOIN entity_map ent ON ent.uuid = d.uuid | ||
WHERE dg.uuid = ? AND (i.date BETWEEN ? AND ?) AND i.reversed=0 | ||
GROUP BY i.debtor_uuid, i.service_id | ||
`; | ||
|
||
const debtorGroup = await db.one('SELECT name FROM debtor_group WHERE uuid=?', db.bid(groupUuid)); | ||
const services = await db.exec(serviceSql); | ||
|
||
// initilisation | ||
services.forEach((s, index) => { | ||
serviceMap[s.id] = s; | ||
serviceMap[s.id].total = 0; | ||
emptyArray.push(null); | ||
serviceIndexMap[s.id] = index; | ||
}); | ||
|
||
// let get the list of invoices for this group | ||
const invoices = await db.exec(invoiceSql, [db.bid(groupUuid), dateFrom, dateTo]); | ||
// let attribute each invoice to it service for each debtor | ||
invoices.forEach(iv => { | ||
if (!debtorMap[iv.debtor_uuid]) { | ||
debtorMap[iv.debtor_uuid] = { | ||
name : iv.text, ref : iv.ref, total : 0, services : _.clone(emptyArray), | ||
}; | ||
} | ||
debtorMap[iv.debtor_uuid].services[serviceIndexMap[iv.service_id]] += iv.amount; | ||
debtorMap[iv.debtor_uuid].total += iv.amount; | ||
serviceMap[iv.service_id].total += iv.amount; | ||
}); | ||
// rounding decimals | ||
Object.keys(debtorMap).forEach(dbt => { | ||
debtorMap[dbt].services.forEach((s, index) => { | ||
if (s) { | ||
debtorMap[dbt].services[index] = util.roundDecimal(s, 4); | ||
} | ||
}); | ||
}); | ||
|
||
// the global amount to paid for this debtor group | ||
services.forEach(s => { | ||
gobalSum += s.total; | ||
}); | ||
|
||
// then let render the report | ||
const result = await report.render({ | ||
debtorGroup, services, debtorMap, dateFrom, dateTo, gobalSum : util.roundDecimal(gobalSum, 2), | ||
}); | ||
res.set(result.headers).send(result.report); | ||
} catch (ex) { | ||
next(ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters