diff --git a/server/controllers/stock/reports/common.js b/server/controllers/stock/reports/common.js index 75b5da3ed1..2d3b139f22 100644 --- a/server/controllers/stock/reports/common.js +++ b/server/controllers/stock/reports/common.js @@ -32,6 +32,7 @@ const STOCK_VALUE_REPORT_TEMPLATE = `${BASE_PATH}/stock_value.report.handlebars` const _ = require('lodash'); // Application-specific imports +const q = require('q'); const db = require('../../../lib/db'); const util = require('../../../lib/util'); const Stock = require('../core'); @@ -158,6 +159,19 @@ const pdfOptions = { footerFontSize : '7', }; +async function getVoucherReferenceForStockMovement(documentUuid) { + const sql = ` + SELECT v.uuid, dm.text AS voucher_reference + FROM voucher AS v + JOIN voucher_item AS vi ON vi.voucher_uuid = v.uuid + JOIN document_map AS dm ON dm.uuid = v.uuid + WHERE vi.document_uuid = ? + LIMIT 1; + `; + + return db.exec(sql, [db.bid(documentUuid)]); +} + /** * Stock Receipt API * /receipts/stock/{{name}}/:document_uuid @@ -185,6 +199,7 @@ const stockFluxReceipt = { // Exports exports._ = _; +exports.q = q; exports.db = db; exports.util = util; exports.pdf = pdf; @@ -198,6 +213,7 @@ exports.stockFluxReceipt = stockFluxReceipt; exports.formatFilters = formatFilters; exports.identifiers = identifiers; exports.getDepotMovement = getDepotMovement; +exports.getVoucherReferenceForStockMovement = getVoucherReferenceForStockMovement; exports.pdfOptions = pdfOptions; diff --git a/server/controllers/stock/reports/stock/adjustment_receipt.js b/server/controllers/stock/reports/stock/adjustment_receipt.js index 634847b285..210ed5035e 100644 --- a/server/controllers/stock/reports/stock/adjustment_receipt.js +++ b/server/controllers/stock/reports/stock/adjustment_receipt.js @@ -1,5 +1,6 @@ const { _, ReportManager, Stock, NotFound, db, identifiers, barcode, STOCK_ADJUSTMENT_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); @@ -28,7 +29,14 @@ async function stockAdjustmentReceipt(documentUuid, session, options) { WHERE m.flux_id IN (${Stock.flux.FROM_ADJUSTMENT}, ${Stock.flux.TO_ADJUSTMENT}) AND m.document_uuid = ? `; - const rows = await db.exec(sql, [db.bid(documentUuid)]); + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); + + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; + if (!rows.length) { throw new NotFound('document not found'); } @@ -48,6 +56,7 @@ async function stockAdjustmentReceipt(documentUuid, session, options) { document_uuid : line.document_uuid, document_reference : line.document_reference, barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, }; data.rows = rows; diff --git a/server/controllers/stock/reports/stock/entry_donation_receipt.js b/server/controllers/stock/reports/stock/entry_donation_receipt.js index 43b10d6b4f..176b78663e 100644 --- a/server/controllers/stock/reports/stock/entry_donation_receipt.js +++ b/server/controllers/stock/reports/stock/entry_donation_receipt.js @@ -1,5 +1,6 @@ const { _, ReportManager, Stock, NotFound, db, barcode, identifiers, STOCK_ENTRY_DONATION_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); /** @@ -9,7 +10,7 @@ const { * This method builds the stock inventory report as either a JSON, PDF, or HTML * file to be sent to the client. */ -function stockEntryDonationReceipt(documentUuid, session, options) { +async function stockEntryDonationReceipt(documentUuid, session, options) { const data = {}; const optionReport = _.extend(options, { filename : 'STOCK.RECEIPTS.ENTRY_DONATION' }); @@ -37,35 +38,42 @@ function stockEntryDonationReceipt(documentUuid, session, options) { ORDER BY i.text, l.label `; - return db.exec(sql, [db.bid(documentUuid)]) - .then((rows) => { - if (!rows.length) { - throw new NotFound('document not found'); - } - const line = rows[0]; - const { key } = identifiers.STOCK_ENTRY; + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); - data.enterprise = session.enterprise; + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; - data.details = { - depot_name : line.depot_name, - user_display_name : line.user_display_name, - description : line.description, - date : line.date, - document_uuid : line.document_uuid, - document_reference : line.document_reference, - barcode : barcode.generate(key, line.document_uuid), - }; + if (!rows.length) { + throw new NotFound('document not found'); + } + const line = rows[0]; + const { key } = identifiers.STOCK_ENTRY; - data.rows = rows; + data.enterprise = session.enterprise; - // sum elements of rows by their `total` property - data.total = data.rows.reduce((aggregate, row) => { - return row.total + aggregate; - }, 0); + data.details = { + depot_name : line.depot_name, + user_display_name : line.user_display_name, + description : line.description, + date : line.date, + document_uuid : line.document_uuid, + document_reference : line.document_reference, + barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, + }; + + data.rows = rows; + + // sum elements of rows by their `total` property + data.total = data.rows.reduce((aggregate, row) => { + return row.total + aggregate; + }, 0); + + return report.render(data); - return report.render(data); - }); } module.exports = stockEntryDonationReceipt; diff --git a/server/controllers/stock/reports/stock/entry_integration_receipt.js b/server/controllers/stock/reports/stock/entry_integration_receipt.js index 966ee2bfc7..d50520306c 100644 --- a/server/controllers/stock/reports/stock/entry_integration_receipt.js +++ b/server/controllers/stock/reports/stock/entry_integration_receipt.js @@ -1,5 +1,6 @@ const { _, ReportManager, Stock, NotFound, db, barcode, identifiers, STOCK_ENTRY_INTEGRATION_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); /** @@ -11,7 +12,7 @@ const { * * GET /receipts/stock/entry_integration/:document_uuid */ -function stockEntryIntegrationReceipt(documentUuid, session, options) { +async function stockEntryIntegrationReceipt(documentUuid, session, options) { const data = {}; const optionReport = _.extend(options, { filename : 'STOCK.RECEIPTS.ENTRY_INTEGRATION' }); @@ -39,32 +40,39 @@ function stockEntryIntegrationReceipt(documentUuid, session, options) { ORDER BY i.text, l.label `; - return db.exec(sql, [db.bid(documentUuid)]) - .then((rows) => { - if (!rows.length) { - throw new NotFound('document not found'); - } - const line = rows[0]; - const { key } = identifiers.STOCK_ENTRY; + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); - data.enterprise = session.enterprise; + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; - data.details = { - depot_name : line.depot_name, - user_display_name : line.user_display_name, - description : line.description, - date : line.date, - document_uuid : line.document_uuid, - document_reference : line.document_reference, - integration_reference : line.integration_reference, - integration_date : line.integration_date, - project_display_name : line.project_display_name, - barcode : barcode.generate(key, line.document_uuid), - }; + if (!rows.length) { + throw new NotFound('document not found'); + } + const line = rows[0]; + const { key } = identifiers.STOCK_ENTRY; + + data.enterprise = session.enterprise; + + data.details = { + depot_name : line.depot_name, + user_display_name : line.user_display_name, + description : line.description, + date : line.date, + document_uuid : line.document_uuid, + document_reference : line.document_reference, + integration_reference : line.integration_reference, + integration_date : line.integration_date, + project_display_name : line.project_display_name, + barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, + }; + + data.rows = rows; + return report.render(data); - data.rows = rows; - return report.render(data); - }); } module.exports = stockEntryIntegrationReceipt; diff --git a/server/controllers/stock/reports/stock/entry_purchase_receipt.js b/server/controllers/stock/reports/stock/entry_purchase_receipt.js index 9561b4b5a2..6989b71282 100644 --- a/server/controllers/stock/reports/stock/entry_purchase_receipt.js +++ b/server/controllers/stock/reports/stock/entry_purchase_receipt.js @@ -1,5 +1,6 @@ const { _, ReportManager, Stock, identifiers, NotFound, db, barcode, STOCK_ENTRY_PURCHASE_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); /** @@ -9,7 +10,7 @@ const { * This method builds the stock inventory report as either a JSON, PDF, or HTML * file to be sent to the client. */ -function stockEntryPurchaseReceipt(documentUuid, session, options) { +async function stockEntryPurchaseReceipt(documentUuid, session, options) { const data = {}; const optionReport = _.extend(options, { filename : 'STOCK.RECEIPTS.ENTRY_PURCHASE' }); @@ -38,37 +39,43 @@ function stockEntryPurchaseReceipt(documentUuid, session, options) { ORDER BY i.text, l.label `; - return db.exec(sql, [db.bid(documentUuid)]) - .then((rows) => { - if (!rows.length) { - throw new NotFound('document not found'); - } + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); - const line = rows[0]; - const { key } = identifiers.STOCK_ENTRY; + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; - data.enterprise = session.enterprise; + if (!rows.length) { + throw new NotFound('document not found'); + } - data.details = { - depot_name : line.depot_name, - user_display_name : line.user_display_name, - description : line.description, - date : line.date, - document_uuid : line.document_uuid, - document_reference : line.document_reference, - purchase_reference : line.purchase_reference, - p_note : line.note, - p_cost : line.cost, - p_date : line.purchase_date, - p_method : line.payment_method, - supplier_display_name : line.supplier_display_name, - project_display_name : line.project_display_name, - barcode : barcode.generate(key, line.document_uuid), - }; + const line = rows[0]; + const { key } = identifiers.STOCK_ENTRY; - data.rows = rows; - return report.render(data); - }); + data.enterprise = session.enterprise; + + data.details = { + depot_name : line.depot_name, + user_display_name : line.user_display_name, + description : line.description, + date : line.date, + document_uuid : line.document_uuid, + document_reference : line.document_reference, + purchase_reference : line.purchase_reference, + p_note : line.note, + p_cost : line.cost, + p_date : line.purchase_date, + p_method : line.payment_method, + supplier_display_name : line.supplier_display_name, + project_display_name : line.project_display_name, + barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, + }; + + data.rows = rows; + return report.render(data); } module.exports = stockEntryPurchaseReceipt; diff --git a/server/controllers/stock/reports/stock/exit_loss_receipt.js b/server/controllers/stock/reports/stock/exit_loss_receipt.js index 09ceed91fa..9fa73e043c 100644 --- a/server/controllers/stock/reports/stock/exit_loss_receipt.js +++ b/server/controllers/stock/reports/stock/exit_loss_receipt.js @@ -1,6 +1,7 @@ const { _, ReportManager, Stock, NotFound, db, barcode, pdf, identifiers, STOCK_EXIT_LOSS_TEMPLATE, POS_STOCK_EXIT_LOSS_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); /** @@ -12,7 +13,7 @@ const { * * GET /receipts/stock/exit_loss/:document_uuid */ -function stockExitLossReceipt(documentUuid, session, options) { +async function stockExitLossReceipt(documentUuid, session, options) { const data = {}; const optionReport = _.extend(options, { filename : 'STOCK.REPORTS.EXIT_LOSS' }); @@ -41,28 +42,35 @@ function stockExitLossReceipt(documentUuid, session, options) { WHERE m.is_exit = 1 AND m.flux_id = ${Stock.flux.TO_LOSS} AND m.document_uuid = ? `; - return db.exec(sql, [db.bid(documentUuid)]) - .then((rows) => { - if (!rows.length) { - throw new NotFound('document not found'); - } - const line = rows[0]; - const { key } = identifiers.STOCK_EXIT; - data.enterprise = session.enterprise; + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); - data.details = { - depot_name : line.depot_name, - user_display_name : line.user_display_name, - description : line.description, - date : line.date, - document_uuid : line.document_uuid, - document_reference : line.document_reference, - barcode : barcode.generate(key, line.document_uuid), - }; + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; + + if (!rows.length) { + throw new NotFound('document not found'); + } + const line = rows[0]; + const { key } = identifiers.STOCK_EXIT; + data.enterprise = session.enterprise; + + data.details = { + depot_name : line.depot_name, + user_display_name : line.user_display_name, + description : line.description, + date : line.date, + document_uuid : line.document_uuid, + document_reference : line.document_reference, + barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, + }; + + data.rows = rows; + return report.render(data); - data.rows = rows; - return report.render(data); - }); } module.exports = stockExitLossReceipt; diff --git a/server/controllers/stock/reports/stock/exit_patient_receipt.js b/server/controllers/stock/reports/stock/exit_patient_receipt.js index 3adf6741ed..aeb3662985 100644 --- a/server/controllers/stock/reports/stock/exit_patient_receipt.js +++ b/server/controllers/stock/reports/stock/exit_patient_receipt.js @@ -1,6 +1,7 @@ const { _, ReportManager, NotFound, Stock, db, identifiers, pdf, barcode, STOCK_EXIT_PATIENT_TEMPLATE, POS_STOCK_EXIT_PATIENT_TEMPLATE, + getVoucherReferenceForStockMovement, } = require('../common'); /** @@ -12,7 +13,7 @@ const { * * GET /receipts/stock/exit_patient/:document_uuid */ -function stockExitPatientReceipt(documentUuid, session, options) { +async function stockExitPatientReceipt(documentUuid, session, options) { const data = {}; const optionReport = _.extend(options, { filename : 'STOCK.REPORTS.EXIT_PATIENT' }); @@ -44,32 +45,38 @@ function stockExitPatientReceipt(documentUuid, session, options) { WHERE m.is_exit = 1 AND m.flux_id = ${Stock.flux.TO_PATIENT} AND m.document_uuid = ? `; - return db.exec(sql, [db.bid(documentUuid)]) - .then((rows) => { - if (!rows.length) { - throw new NotFound('document not found'); - } - const line = rows[0]; - const { key } = identifiers.STOCK_EXIT; - data.enterprise = session.enterprise; + const results = await Promise.all([ + db.exec(sql, [db.bid(documentUuid)]), + getVoucherReferenceForStockMovement(documentUuid), + ]); - data.details = { - depot_name : line.depot_name, - patient_reference : line.patient_reference, - patient_display_name : line.patient_display_name, - full_display_name : line.patient_reference.concat(' - ', line.patient_display_name), - hospital_no : line.hospital_no, - user_display_name : line.user_display_name, - description : line.description, - date : line.date, - document_uuid : line.document_uuid, - document_reference : line.document_reference, - barcode : barcode.generate(key, line.document_uuid), - }; + const rows = results[0]; + const voucherReference = results[1][0].voucher_reference; - data.rows = rows; - return report.render(data); - }); + if (!rows.length) { + throw new NotFound('document not found'); + } + const line = rows[0]; + const { key } = identifiers.STOCK_EXIT; + data.enterprise = session.enterprise; + + data.details = { + depot_name : line.depot_name, + patient_reference : line.patient_reference, + patient_display_name : line.patient_display_name, + full_display_name : line.patient_reference.concat(' - ', line.patient_display_name), + hospital_no : line.hospital_no, + user_display_name : line.user_display_name, + description : line.description, + date : line.date, + document_uuid : line.document_uuid, + document_reference : line.document_reference, + barcode : barcode.generate(key, line.document_uuid), + voucher_reference : voucherReference, + }; + + data.rows = rows; + return report.render(data); } module.exports = stockExitPatientReceipt; diff --git a/server/controllers/stock/reports/stock_adjustment.receipt.handlebars b/server/controllers/stock/reports/stock_adjustment.receipt.handlebars index 25afb12f88..c039d4a34c 100644 --- a/server/controllers/stock/reports/stock_adjustment.receipt.handlebars +++ b/server/controllers/stock/reports/stock_adjustment.receipt.handlebars @@ -24,6 +24,9 @@
{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate "TABLE.COLUMNS.CREATED_BY"}}: {{details.user_display_name}}
diff --git a/server/controllers/stock/reports/stock_entry_donation.receipt.handlebars b/server/controllers/stock/reports/stock_entry_donation.receipt.handlebars index 36e4491cc9..3347e31895 100644 --- a/server/controllers/stock/reports/stock_entry_donation.receipt.handlebars +++ b/server/controllers/stock/reports/stock_entry_donation.receipt.handlebars @@ -22,6 +22,9 @@

{{translate 'STOCK.TO'}}

{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate 'FORM.LABELS.COST'}}: {{currency (sum rows 'total') enterprise.currency_id}}
{{translate 'STOCK.INVENTORY'}}: {{rows.length}} {{translate 'STOCK.ITEMS'}}
diff --git a/server/controllers/stock/reports/stock_entry_integration.receipt.handlebars b/server/controllers/stock/reports/stock_entry_integration.receipt.handlebars index 79e1967c9c..31d95be1fe 100644 --- a/server/controllers/stock/reports/stock_entry_integration.receipt.handlebars +++ b/server/controllers/stock/reports/stock_entry_integration.receipt.handlebars @@ -24,6 +24,9 @@

{{translate 'STOCK.TO'}}

{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate 'FORM.LABELS.COST'}}: {{currency (sum rows 'total') enterprise.currency_id}}
{{translate 'STOCK.INVENTORY'}}: {{rows.length}} {{translate 'STOCK.ITEMS'}}
diff --git a/server/controllers/stock/reports/stock_entry_purchase.receipt.handlebars b/server/controllers/stock/reports/stock_entry_purchase.receipt.handlebars index 744a926194..494ed2706c 100644 --- a/server/controllers/stock/reports/stock_entry_purchase.receipt.handlebars +++ b/server/controllers/stock/reports/stock_entry_purchase.receipt.handlebars @@ -27,6 +27,9 @@

{{translate 'STOCK.TO'}}

{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate 'FORM.LABELS.COST'}}: {{currency (sum rows 'total') enterprise.currency_id}}
{{translate 'STOCK.INVENTORY'}}: {{rows.length}} {{translate 'STOCK.ITEMS'}}
diff --git a/server/controllers/stock/reports/stock_exit_loss.receipt.handlebars b/server/controllers/stock/reports/stock_exit_loss.receipt.handlebars index 3b98819e39..7659439c65 100644 --- a/server/controllers/stock/reports/stock_exit_loss.receipt.handlebars +++ b/server/controllers/stock/reports/stock_exit_loss.receipt.handlebars @@ -21,6 +21,9 @@
{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate "TABLE.COLUMNS.CREATED_BY"}}: {{details.user_display_name}}
diff --git a/server/controllers/stock/reports/stock_exit_patient.receipt.handlebars b/server/controllers/stock/reports/stock_exit_patient.receipt.handlebars index 5c4e8e7018..29723f5394 100644 --- a/server/controllers/stock/reports/stock_exit_patient.receipt.handlebars +++ b/server/controllers/stock/reports/stock_exit_patient.receipt.handlebars @@ -22,6 +22,9 @@
{{translate 'STOCK.DEPOT'}}: {{details.depot_name}}
{{translate 'FORM.LABELS.DOCUMENT'}}: {{details.document_reference}}
+ {{#if details.voucher_reference}} + {{translate 'FORM.LABELS.VOUCHER'}}: {{details.voucher_reference}}
+ {{/if}} {{translate 'FORM.LABELS.DATE'}}: {{date details.date}}
{{translate "TABLE.COLUMNS.CREATED_BY"}}: {{details.user_display_name}}