Skip to content

Commit

Permalink
fix(invoices): registry filters and sorting
Browse files Browse the repository at this point in the history
This commit ensures that the invoice registry loads the most recent data
first, and shows the reference column when searching.

This is a bug fix.
  • Loading branch information
Jonathan Niles authored and sfount committed Dec 28, 2016
1 parent 3c484a8 commit 7644dc6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion client/src/partials/patients/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function PatientRegistryController($state, Patients, Notify, AppCache, util, Rec
{ field : 'display_name', displayName : 'TABLE.COLUMNS.NAME', headerCellFilter: 'translate' },
{ field : 'patientAge', displayName : 'TABLE.COLUMNS.AGE', headerCellFilter: 'translate' },
{ field : 'sex', displayName : 'TABLE.COLUMNS.GENDER', headerCellFilter: 'translate' },
{ field : 'hospital_no', displayName : 'TABLE.COLUMNS.HOSPITAL_FILE_NR', headerCellFilter: 'translate' },
{ field : 'hospital_no', displayName : 'TABLE.COLUMNS.HOSPITAL_FILE_NR', headerCellFilter: 'translate' },
{ field : 'registration_date', cellFilter:'date', displayName : 'TABLE.COLUMNS.DATE_REGISTERED', headerCellFilter: 'translate' },
{ field : 'last_visit', cellFilter:'date', displayName : 'TABLE.COLUMNS.LAST_VISIT', headerCellFilter: 'translate' },
{ field : 'dob', cellFilter:'date', displayName : 'TABLE.COLUMNS.DOB', headerCellFilter: 'translate' },
Expand Down
2 changes: 1 addition & 1 deletion client/src/partials/vouchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function VoucherController(Vouchers, $translate, Notify, Filtering, uiGridGroupi
action: search, color: 'btn-default'
},
{ icon: 'fa fa-filter', color: 'btn-default',
action: toggleFilter,
action: toggleFilter,
}
];

Expand Down
35 changes: 23 additions & 12 deletions server/controllers/finance/patientInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function listInvoices() {
JOIN user ON user.id = invoice.user_id
JOIN project ON invoice.project_id = project.id
JOIN enterprise ON enterprise.id = project.enterprise_id
ORDER BY invoice.date ASC, invoice.reference ASC;
ORDER BY invoice.date DESC, invoice.reference DESC;
`;

// TODO - this shouldn't throw an error...
Expand Down Expand Up @@ -110,7 +110,7 @@ function lookupInvoice(invoiceUuid) {
JOIN project ON project.id = invoice.project_id
JOIN enterprise ON enterprise.id = project.enterprise_id
JOIN user ON user.id = invoice.user_id
WHERE invoice.uuid = ?`;
WHERE invoice.uuid = ?;`;

let invoiceItemsQuery =
`SELECT BUID(invoice_item.uuid) as uuid, invoice_item.quantity, invoice_item.inventory_price,
Expand Down Expand Up @@ -204,10 +204,11 @@ function find(options) {
};

let sql =`
SELECT BUID(invoice.uuid) as uuid, invoice.project_id, CONCAT_WS('.', '${identifiers.INVOICE}', project.abbr, invoice.reference) AS ref,
invoice.date, patient.display_name as patientName, invoice.cost,
BUID(invoice.debtor_uuid) as debtor_uuid, invoice.user_id, invoice.is_distributable,
service.name as serviceName, user.display_name, enterprise.currency_id, voucher.type_id
SELECT BUID(invoice.uuid) as uuid, invoice.project_id, invoice.date,
patient.display_name as patientName, invoice.cost, BUID(invoice.debtor_uuid) as debtor_uuid,
CONCAT_WS('.', '${identifiers.INVOICE}', project.abbr, invoice.reference) AS reference,
service.name as serviceName, user.display_name, enterprise.currency_id, voucher.type_id,
invoice.user_id, invoice.is_distributable
FROM invoice
LEFT JOIN patient ON invoice.debtor_uuid = patient.debtor_uuid
LEFT JOIN voucher ON voucher.reference_uuid = invoice.uuid
Expand All @@ -232,6 +233,13 @@ function find(options) {
delete options.reference;
}

if (options.debtor_uuid) {
options.debtor_uuid = db.bid(options.debtor_uuid);
conditions.statements.push('invoice.debtor_uuid = ?');
conditions.parameters.push(options.debtor_uuid);
delete options.debtor_uuid;
}

if (options.billingDateFrom) {
conditions.statements.push('DATE(invoice.date) >= DATE(?)');
conditions.parameters.push(options.billingDateFrom);
Expand Down Expand Up @@ -265,18 +273,21 @@ function find(options) {
sql = query.query;
let parameters = conditions.parameters.concat(query.conditions);

// if nothing was submitted to the search, get all records
// this writes in WHERE 1; to the SQL query
if (!parameters.length) {
sql += ' 1';
}

// add in the ORDER BY date DESC
sql += ' ORDER BY invoice.date DESC, invoice.reference DESC ';

// finally, apply the LIMIT query
if (!isNaN(limit)) {
sql += 'LIMIT ?;';
parameters.push(limit);
}

// if nothing was submitted to the search, get all records
// this writes in WHERE 1; to the SQL query
if (!parameters.length) {
sql += ' 1;';
}

parameters = parameters.concat(conditions.parameters);

return db.exec(sql, parameters);
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/finance/reports/invoices/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
* @overview
* Invoice Reports
Expand All @@ -9,7 +11,6 @@
* @todo - implement the filtering portion of this. See patient registrations
* for inspiration.
*/
'use strict';

const q = require('q');
const _ = require('lodash');
Expand Down Expand Up @@ -89,7 +90,6 @@ function receipt(req, res, next) {
})
.then(headerResult => {
_.extend(invoiceResponse, headerResult, metadata);

return Exchange.getExchangeRate(enterpriseId, currencyId, new Date());
})
.then(exchangeResult => {
Expand Down
12 changes: 2 additions & 10 deletions test/integration/patientInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ describe('(/invoices) Patient Invoices', function () {
const fetchableInvoiceUuid = '957e4e79-a6bb-4b4d-a8f7-c42152b2c2f6';
const debtorUuid = '3be232f9-a4b9-4af6-984c-5d3f87d5c107';

/* a reference for one of the invoices in the database */
const reference = 'TPA1';

// run the 'BillingScenarios' test suite
describe('(POST /invoices)', BillingScenarios);

Expand Down Expand Up @@ -79,15 +76,10 @@ describe('(/invoices) Patient Invoices', function () {
.catch(helpers.handler);
});

/**
* valid filter, all results
* @fixme: there is more than one `debtor_uuid` which are returned,
* the reason is the join between invoice and patient tables
*/
it.skip('GET /invoices/search?debtor_uuid=3be232f9-a4b9-4af6-984c-5d3f87d5c107 should return two invoices', function () {
it('GET /invoices/search?debtor_uuid=3be232f9-a4b9-4af6-984c-5d3f87d5c107 should return two invoices', function () {
return agent.get('/invoices/search?debtor_uuid=3be232f9-a4b9-4af6-984c-5d3f87d5c107')
.then(function (res) {
helpers.api.listed(res, numCreatedInvoices);
helpers.api.listed(res, 5);
})
.catch(helpers.handler);
});
Expand Down

0 comments on commit 7644dc6

Please sign in to comment.