Skip to content

Commit

Permalink
Merge pull request #638 from jniles/refactor-patient-invoice-api
Browse files Browse the repository at this point in the history
(refactor) patient invoice api
  • Loading branch information
jniles committed Aug 15, 2016
2 parents ba35606 + 0333c1b commit 8c1a5de
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 74 deletions.
68 changes: 16 additions & 52 deletions client/src/js/services/PatientInvoiceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('bhima.services')
.service('PatientInvoiceService', PatientInvoiceService);

PatientInvoiceService.$inject = [
'$http', '$uibModal', 'util', 'SessionService'
'$uibModal', 'util', 'SessionService', 'PrototypeApiService'
];

/**
Expand All @@ -12,35 +12,13 @@ PatientInvoiceService.$inject = [
* through the PatientService, but for queries not tied to particular patients,
* this service is particularly useful.
*/
function PatientInvoiceService($http, Modal, util, Session) {
var service = this;
var baseUrl = '/invoices/';
function PatientInvoiceService(Modal, util, Session, Api) {
var service = new Api('/invoices/');

service.read = read;
service.create = create;
service.search = search;
service.openSearchModal = openSearchModal;
service.formatFilterParameters = formatFilterParameters;
service.openCreditNoteModal = openCreditNoteModal;

/**
* @method read
*
* @description
* Retrieves a particular invoice by UUID or a list of all invoices if no UUID is
* specified.
*
* @param {String} uuid (optional) - the uuid of the patient invoice to look
* up in the database.
* @param {Object} options (optional) - options to be passed as query string
* parameters to the http request
* @returns {Promise} promise - the result of the HTTP request
*/
function read(uuid, options) {
var target = baseUrl.concat(uuid || '');
return $http.get(target)
.then(util.unwrapHttpResponse);
}
service.openCreditNoteModal = openCreditNoteModal;

/**
* @method create
Expand All @@ -60,30 +38,18 @@ function PatientInvoiceService($http, Modal, util, Session) {
billingServices = billingServices || [];
subsidies = subsidies || [];

// concat into a single object to send back to the client
// concatenate into a single object to send back to the client
invoice.items = invoiceItems.map(filterInventorySource);
invoice.billingServices = billingServices;
invoice.subsidies = subsidies;

return $http.post(baseUrl, { invoice : invoice })
.then(util.unwrapHttpResponse);
}
invoice.billingServices = billingServices.map(function (billingService) {
return billingService.billing_service_id;
});

/**
* @method search
*
* @description
* This method is responsible for searching for invoice(s) based on passed parameters
*
* @param {Object} options - query string parameters to be passed to $http for
* serialization.
*
* @returns {Promise} - a promise resolving to the HTTP result.
*/
function search (options) {
var target = baseUrl.concat('search');
return $http.get(target, { params : options })
.then(util.unwrapHttpResponse);
invoice.subsidies = subsidies.map(function (subsidy) {
return subsidy.subsidy_id;
});

return Api.create.call(this, { invoice: invoice });
}

// utility methods
Expand Down Expand Up @@ -119,10 +85,8 @@ function PatientInvoiceService($http, Modal, util, Session) {
return Modal.open({
templateUrl : 'partials/patient_invoice/registry/modalCreditNote.html',
resolve : {
data : {
invoice : invoice
}
},
data : { invoice : invoice }
},
size : 'md',
animation : true,
keyboard : false,
Expand Down Expand Up @@ -160,4 +124,4 @@ function PatientInvoiceService($http, Modal, util, Session) {
}

return service;
}
}
35 changes: 31 additions & 4 deletions server/controllers/finance/invoice/patientInvoice.create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

/**
* Patient Invoice - Create State
* @module controllers/finance/patientInvoice
Expand Down Expand Up @@ -90,15 +91,41 @@ function processInvoice(invoiceUuid, invoice) {
return _.values(invoice);
}

/** @todo there is a proposal to transition this API to accept an array of billing service IDs, this method will have to updated in this re-factor */
/**
* @method processBillingServices
*
* @description
* Maps an array of billing service ids into billing service ids and invoice
* UUID tuples.
*
* @param {Buffer} invoiceUuid - the binary invoice UUID
* @param {Array|Undefined} subsidiesDetails - an array of billing service ids
* if they exist.
* @returns {Array} - a possibly empty array billing service ids and invoice UUID pairs.
*
* @private
*/
function processBillingServices(invoiceUuid, billingServiceDetails) {
let billingServices = billingServiceDetails || [];
return billingServices.map(billingService => [billingService.billing_service_id, invoiceUuid]);
return billingServices.map(billingServiceId => [billingServiceId, invoiceUuid]);
}

/**
* @method processSubsidies
*
* @description
* Maps an array of subsidy ids into subsidy id and invoice uuid tuples
*
* @param {Buffer} invoiceUuid - the binary invoice uuid
* @param {Array|Undefined} subsidiesDetails - an array of subsidy ids if they
* exist.
* @returns {Array} - a possibly empty array subsidy ids and invoice UUID pairs.
*
* @private
*/
function processSubsidies(invoiceUuid, subsidiesDetails) {
let subsidies = subsidiesDetails || [];
return subsidies.map(subsidy => [subsidy.subsidy_id, invoiceUuid]);
return subsidies.map(subsidyId => [subsidyId, invoiceUuid]);
}

// process invoice items, transforming UUIDs into binary.
Expand All @@ -117,7 +144,7 @@ function processInvoiceItems(invoiceUuid, invoiceItems) {
item.debit = 0;
});

// create a filter to align invoice item columns to the sql columns
// create a filter to align invoice item columns to the SQL columns
let filter =
util.take('uuid', 'inventory_uuid', 'quantity', 'transaction_price', 'inventory_price', 'debit', 'credit', 'invoice_uuid');

Expand Down
23 changes: 13 additions & 10 deletions server/controllers/finance/patientInvoice.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

/**
* Patient Invoice API Controller
*.@module controllers/finance/patientInvoice
*
*@module controllers/finance/patientInvoice
*
* @todo (required) major bug - Invoice items are entered based on order or attributes sent from client - this doesn't seem to be consistent as of 2.X
* @todo GET /invoices/patient/:uuid - retrieve all patient invoices for a specific patient
* - should this be /patients/:uuid/invoices?
* @todo Factor in subsidies, this depends on price lists and billing services infrastructure
* @todo Credit note logic pending on clear design
*/

const q = require('q');
const db = require('../../lib/db');
const uuid = require('node-uuid');
Expand Down Expand Up @@ -74,8 +76,9 @@ function list(req, res, next) {


/**
* lookupInvoice
* @method lookupInvoice
*
* @description
* Find an invoice by id in the database.
*
* @param {string} invoiceUuid - the uuid of the invoice in question
Expand Down Expand Up @@ -110,11 +113,12 @@ function lookupInvoice(invoiceUuid) {
JOIN billing_service ON billing_service.id = invoice_billing_service.billing_service_id
WHERE invoice_billing_service.invoice_uuid = ?`;

let invoiceSubsidyQuery =
`SELECT invoice_subsidy.value, subsidy.label, subsidy.value AS subsidy_value
let invoiceSubsidyQuery = `
SELECT invoice_subsidy.value, subsidy.label, subsidy.value AS subsidy_value
FROM invoice_subsidy
JOIN subsidy ON subsidy.id = invoice_subsidy.subsidy_id
WHERE invoice_subsidy.invoice_uuid = ?`;
WHERE invoice_subsidy.invoice_uuid = ?;
`;

return db.exec(invoiceDetailQuery, [buid])
.then(function (rows) {
Expand All @@ -128,7 +132,6 @@ function lookupInvoice(invoiceUuid) {
})
.then(function (rows) {
record.items = rows;

return db.exec(invoiceBillingQuery, [buid]);
})
.then(function (rows) {
Expand All @@ -138,7 +141,7 @@ function lookupInvoice(invoiceUuid) {
})
.then(function (rows) {
record.subsidy = rows;

return record;
});
}
Expand Down Expand Up @@ -187,8 +190,8 @@ function find(options) {
let sql =`
SELECT BUID(invoice.uuid) as uuid, invoice.project_id, CONCAT(project.abbr, invoice.reference) AS reference,
invoice.date, CONCAT(patient.first_name, ' - ', patient.last_name) as patientNames, invoice.cost,
BUID(invoice.debtor_uuid) as debtor_uuid, invoice.user_id, invoice.is_distributable,
service.name as serviceName, CONCAT(user.first, ' - ', user.last) as createdBy, voucher.type_id
BUID(invoice.debtor_uuid) as debtor_uuid, invoice.user_id, invoice.is_distributable,
service.name as serviceName, CONCAT(user.first, ' - ', user.last) as createdBy, voucher.type_id
FROM invoice
LEFT JOIN patient ON invoice.debtor_uuid = patient.debtor_uuid
LEFT JOIN voucher ON voucher.reference_uuid = invoice.uuid
Expand Down
10 changes: 2 additions & 8 deletions server/test/api/patientInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ function BillingScenarios() {
credit: 25
}],

/* @todo - change this API to take in an array of billing service ids */
billingServices : [{
billing_service_id : 1
}]
billingServices : [ 1 ]
};

it('creates and posts a patient invoice (simple + 1 billing service)', () => {
Expand Down Expand Up @@ -379,10 +376,7 @@ function BillingScenarios() {
credit: 40.95
}],

/* @todo - change this API to take in an array of subsidy ids */
subsidies : [{
subsidy_id : 1
}]
subsidies : [ 1 ]
};

it('creates and posts a patient invoice (simple + 1 subsidy)', () => {
Expand Down

0 comments on commit 8c1a5de

Please sign in to comment.