Skip to content

Commit

Permalink
Merge #4114
Browse files Browse the repository at this point in the history
4114: improvement(Stock Mouvement Document) r=jniles a=lomamech

Set Voucher reference to
- Adjustment Receipt
- Donation Receipt
- Integration Receipt
- Purchase Receipt
- Loss Receipt
- Patient Receipt

closes #2694

Co-authored-by: Chris Lomame <lomamech@gmail.com>
  • Loading branch information
bors[bot] and lomamech committed Jan 24, 2020
2 parents 6cdecbe + fcdf98c commit ef04780
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 124 deletions.
16 changes: 16 additions & 0 deletions server/controllers/stock/reports/common.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -185,6 +199,7 @@ const stockFluxReceipt = {
// Exports
exports._ = _;

exports.q = q;
exports.db = db;
exports.util = util;
exports.pdf = pdf;
Expand All @@ -198,6 +213,7 @@ exports.stockFluxReceipt = stockFluxReceipt;
exports.formatFilters = formatFilters;
exports.identifiers = identifiers;
exports.getDepotMovement = getDepotMovement;
exports.getVoucherReferenceForStockMovement = getVoucherReferenceForStockMovement;

exports.pdfOptions = pdfOptions;

Expand Down
11 changes: 10 additions & 1 deletion 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');


Expand Down Expand Up @@ -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');
}
Expand All @@ -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;
Expand Down
58 changes: 33 additions & 25 deletions 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');

/**
Expand All @@ -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' });

Expand Down Expand Up @@ -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;
56 changes: 32 additions & 24 deletions 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');

/**
Expand All @@ -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' });

Expand Down Expand Up @@ -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;
63 changes: 35 additions & 28 deletions 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');

/**
Expand All @@ -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' });

Expand Down Expand Up @@ -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;
50 changes: 29 additions & 21 deletions 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');

/**
Expand All @@ -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' });

Expand Down Expand Up @@ -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;

0 comments on commit ef04780

Please sign in to comment.