Skip to content

Commit

Permalink
feat(patients): add cumsum to patient activity
Browse files Browse the repository at this point in the history
This commit adds a cumulative summation to the Patient Activity page. It
should help the auditors track what the patient's balance is a lot
easier and at what point they had paid their bill in full.

Closes #1248.
  • Loading branch information
Jonathan Niles authored and sfount committed Mar 3, 2017
1 parent e1be96e commit 332b99f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ exports.configure = function configure(app) {
app.get('/tree', tree.generate);

// snis controller
app.get('/snis/healthZones',snis.healthZones);
app.get('/snis/healthZones', snis.healthZones);

// Employee management
app.get('/employee_list/', employees.list);
Expand Down
7 changes: 2 additions & 5 deletions server/controllers/finance/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* @requires Patient
*/

const _ = require('lodash');

const Patient = require('./reports/financial.patient');

/**
* @method patientBalance
* Return the patient balance status
*/
function patientBalance(req, res, next) {

Patient.financialActivities(req.params.uuid)
.then(result => {
let balance = result.sum && result.sum.balance ? result.sum.balance : null;
.then((result) => {
const balance = result.aggregates && result.aggregates.balance ? result.aggregates.balance : null;
res.status(200).json(balance);
})
.catch(next)
Expand Down
28 changes: 15 additions & 13 deletions server/controllers/finance/reports/financial.patient.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

{{> header }}

<h3 class="text-center">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.REPORT_TITLE"}}</h3>
<h4 class="text-center"><strong>{{ debtor.reference }}</strong> | {{ debtor.display_name }} </h4>
<h4 class="text-center">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.DEBTOR_GROUP"}} : {{ debtor.debtor_group_name }} </h4>
<h4 class="text-center">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.ACCOUNT"}} : {{ debtor.number }} </h4>
<h3 class="text-center text-capitalize">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.REPORT_TITLE"}}</h3>
<h4 class="text-center"><strong>{{ patient.reference }}</strong> | {{ patient.display_name }} </h4>
<h4 class="text-center">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.DEBTOR_GROUP"}}: {{ patient.debtor_group_name }} </h4>
<h4 class="text-center">{{translate "PATIENT_RECORDS.FINANCIAL_ACTIVITY.ACCOUNT"}}: {{ patient.number }} </h4>

<section>
<table class="table table-condensed table-bordered table-report">
Expand All @@ -18,10 +18,11 @@
<th style="width: 5%;">{{translate "TABLE.COLUMNS.TRANSACTION_ID" }}</th>
<th>{{translate "TABLE.COLUMNS.DESCRIPTION" }}</th>
<th style="width: 15%;" class="text-center">{{translate "TABLE.COLUMNS.AMOUNT" }}</th>
<th style="width: 15%;" class="text-center">{{translate "TABLE.COLUMNS.BALANCE" }}</th>
</tr>
</thead>
<tbody>
{{#each patients}}
{{#each transactions}}
<tr>
<td>{{date this.trans_date}}</td>
<td class="text-right">{{this.document}}</td>
Expand All @@ -34,35 +35,36 @@
{{currency this.debit ../metadata.enterprise.currency_id}}
{{/if}}
</td>
<td class="text-right">{{currency this.cumsum ../metadata.enterprise.currency_id}} </td>
</tr>
{{else}}
{{>emptyTable columns=5}}
{{/each}}
</tbody>
<tfoot style="background-color: #ededed;">
<tr>
<th colspan="4" class="text-right">{{translate "FORM.LABELS.TOTAL_BILLED" }}</th>
<th colspan="5" class="text-right">{{translate "FORM.LABELS.TOTAL_BILLED" }}</th>
<th class="text-right">
<span>{{currency sum.debit metadata.enterprise.currency_id}}</span>
<span>{{currency aggregates.debit metadata.enterprise.currency_id}}</span>
</th>
</tr>
<tr>
<th colspan="4" class="text-right">{{translate "FORM.LABELS.TOTAL_PAID" }}</th>
<th colspan="5" class="text-right">{{translate "FORM.LABELS.TOTAL_PAID" }}</th>
<th class="text-right">
<span class="text-danger">({{currency sum.credit metadata.enterprise.currency_id}})</span>
<span class="text-danger">({{currency aggregates.credit metadata.enterprise.currency_id}})</span>
</th>
</tr>
<tr>
<th colspan="4" class="text-right">
<th colspan="5" class="text-right">
{{translate "FORM.LABELS.TOTAL_BALANCE_REMAINING" }}
</th>
<th class="text-right">
{{#unless sum.hasDebitBalance }}
{{#unless aggregates.hasDebitBalance }}
<span class="text-danger">
({{currency sum.balance metadata.enterprise.currency_id}})
({{currency aggregates.balance metadata.enterprise.currency_id}})
</span>
{{else}}
{{currency sum.balance metadata.enterprise.currency_id}}
{{currency aggregates.balance metadata.enterprise.currency_id}}
{{/unless}}
</th>
</tr>
Expand Down
78 changes: 42 additions & 36 deletions server/controllers/finance/reports/financial.patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
* @overview server/controllers/finance/reports/financial.patient.js
*
* @description
* This file contains code to create a PDF report for financial activites of a patient
* This file contains code to create a PDF report for financial activities of a patient
*
* @requires lodash
* @requires Patients
* @requires ReportManager
* @requires Debtors
*/

const _ = require('lodash');

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

const Patients = require('../../medical/patients');

const Debtors = require('../debtors');

const TEMPLATE = './server/controllers/finance/reports/financial.patient.handlebars';

/**
Expand All @@ -30,7 +26,6 @@ const TEMPLATE = './server/controllers/finance/reports/financial.patient.handleb
*/
function build(req, res, next) {
const options = req.query;
let debtorData = {};
let report;

// set up the report with report manager
Expand All @@ -40,12 +35,9 @@ function build(req, res, next) {
return next(e);
}

// enforce detailed columns
options.detailed = 1;

financialActivities(req.params.uuid)
return financialActivities(req.params.uuid)
.then(result => report.render(result))
.then(result => {
.then((result) => {
res.set(result.headers).send(result.report);
})
.catch(next)
Expand All @@ -54,33 +46,47 @@ function build(req, res, next) {

/**
* @method financialActivities
* Return details of financial activites of a given patient
* Return details of financial activities of a given patient
*/
function financialActivities(patientUuid) {
let glb = {};
function financialActivities(debtorUuid) {
const data = {};

return Patients.lookupByDebtorUuid(patientUuid)
.then(debtor => {
glb.debtor = debtor;
return Debtors.financialPatient(patientUuid);
})
.then(patients => {
let sum = {
credit : 0,
debit : 0,
balance: 0
};
const sql = `
SELECT trans_id, entity_uuid, description, record_uuid, trans_date, debit, credit, document,
(@cumsum := balance + @cumsum) AS cumsum
FROM (
SELECT combined_ledger.trans_id, combined_ledger.entity_uuid, combined_ledger.description,
combined_ledger.record_uuid, combined_ledger.trans_date, SUM(combined_ledger.debit_equiv) AS debit,
SUM(combined_ledger.credit_equiv) AS credit, document_map.text AS document,
(SUM(combined_ledger.debit_equiv) - SUM(combined_ledger.credit_equiv)) AS balance
FROM combined_ledger
LEFT JOIN document_map ON document_map.uuid = combined_ledger.record_uuid
WHERE combined_ledger.entity_uuid = ?
GROUP BY combined_ledger.record_uuid
ORDER BY combined_ledger.trans_date ASC, combined_ledger.trans_id
)c, (SELECT @cumsum := 0)z
ORDER BY trans_date ASC, trans_id;
`;

// why does this loop through patients?
patients.forEach(function (patient) {
sum.debit += patient.debit;
sum.credit += patient.credit;
sum.balance = sum.debit - sum.credit;
sum.hasDebitBalance = sum.balance > 0;
});
const aggregateQuery = `
SELECT SUM(combined_ledger.debit_equiv) AS debit, SUM(combined_ledger.credit_equiv) AS credit,
SUM(combined_ledger.debit_equiv - combined_ledger.credit_equiv) AS balance
FROM combined_ledger
WHERE combined_ledger.entity_uuid = ?
GROUP BY entity_uuid;
`;

const debtor = glb.debtor;
return { patients, debtor, sum };
return Patients.lookupByDebtorUuid(debtorUuid)
.then((patient) => {
data.patient = patient;
const buid = db.bid(debtorUuid);
return q.all([db.exec(sql, buid), db.one(aggregateQuery, buid)]);
})
.spread((transactions, aggregates) => {
const patient = data.patient;
aggregates.hasDebitBalance = aggregates.balance > 0;
return { transactions, patient, aggregates };
});
}

Expand Down

0 comments on commit 332b99f

Please sign in to comment.