Skip to content

Commit

Permalink
perf(stock): fix the exit helper
Browse files Browse the repository at this point in the history
Improves performance during stock movement creation and lookup in the
helpers.
  • Loading branch information
jniles committed Mar 4, 2022
1 parent 22a3cf1 commit 910d718
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/src/modules/stock/StockExitForm.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ function StockExitFormService(
*/
StockExitForm.prototype.setPatientDistribution = function setPatientDistribution(patient) {
this.details.entity_uuid = patient.uuid;
this.details.invoice_uuid = patient.invoice.uuid;
this.details.invoice_uuid = patient.invoice.details.uuid;
this.details.flux_id = TO_PATIENT;
this.store.clear();

Expand Down
10 changes: 4 additions & 6 deletions client/src/modules/stock/StockExitFormHelper.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ function StockExitFormHelperService($q, $translate, Patients, Invoices, Services

// load the required information for the patient description
const queries = $q.all([
Patients.read(null, { uuid : details.entity_uuid }),
Invoices.read(null, { uuid : details.invoice_uuid }),
Patients.read(details.entity_uuid),
Invoices.read(details.invoice_uuid),
]);

return queries
.then(([patients, invoices]) => {
const [patient] = patients;
const [invoice] = invoices;
.then(([patient, invoice]) => {

Object.assign(i18nKeys, {
patient : patient.display_name.concat(` (${patient.reference})`),
Expand All @@ -56,7 +54,7 @@ function StockExitFormHelperService($q, $translate, Patients, Invoices, Services
*/
function getDescriptionForService(details, i18nKeys) {
// load the required information for the service description
return Services.read(null, { uuid : details.entity_uuid })
return Services.read(details.entity_uuid)
.then(([{ name }]) => {

Object.assign(i18nKeys, { service : name });
Expand Down
12 changes: 12 additions & 0 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ async function createMovement(req, res, next) {
}
}

// get unit costs from stock_value
const inventoryUuids = params.lots.map(l => db.bid(l.inventory_uuid));
const unitCosts = await db.exec(
'SELECT BUID(inventory_uuid) as inventory_uuid, wac FROM stock_value WHERE inventory_uuid in (?);',
[inventoryUuids]);

const unitCostMap = new Map(unitCosts.map(cost => [cost.inventory_uuid, cost.wac]));

params.lots.forEach(lot => {
lot.unit_cost = unitCostMap.get(lot.inventory_uuid);
});

// NOTE(@jniles) - the id here is the period id, not the fiscal year id.
const periodId = (await Fiscal.lookupFiscalYearByDate(params.date)).id;
params.period_id = periodId;
Expand Down

0 comments on commit 910d718

Please sign in to comment.