-
Notifications
You must be signed in to change notification settings - Fork 98
/
adjustment_receipt.js
68 lines (56 loc) · 2.27 KB
/
adjustment_receipt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const {
_, ReportManager, Stock, NotFound, db, identifiers, barcode, STOCK_ADJUSTMENT_TEMPLATE,
getVoucherReferenceForStockMovement,
} = require('../common');
/**
* @method stockAdjustmentReceipt
*
* @description
* This method builds the stock adjustment receipt file to be sent to the client.
*/
async function stockAdjustmentReceipt(documentUuid, session, options) {
const optionReport = _.extend(options, { filename : 'STOCK.REPORTS.ADJUSTMENT' });
// set up the report with report manager
const report = new ReportManager(STOCK_ADJUSTMENT_TEMPLATE, session, optionReport);
const sql = `
SELECT i.code, i.text, BUID(m.document_uuid) AS document_uuid, m.is_exit,
m.quantity, m.unit_cost, (m.quantity * m.unit_cost) AS total , m.date, m.description,
u.display_name AS user_display_name, dm.text AS document_reference,
l.label, l.expiration_date, d.text AS depot_name
FROM stock_movement m
JOIN document_map dm ON dm.uuid = m.document_uuid
JOIN lot l ON l.uuid = m.lot_uuid
JOIN inventory i ON i.uuid = l.inventory_uuid
JOIN depot d ON d.uuid = m.depot_uuid
JOIN user u ON u.id = m.user_id
WHERE m.flux_id IN (${Stock.flux.FROM_ADJUSTMENT}, ${Stock.flux.TO_ADJUSTMENT}) AND m.document_uuid = ?
`;
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');
}
const line = rows[0];
const { key } = identifiers.STOCK_MOVEMENT;
const data = {};
data.enterprise = session.enterprise;
data.details = {
title : line.is_exit ? 'STOCK_FLUX.TO_ADJUSTMENT' : 'STOCK_FLUX.FROM_ADJUSTMENT',
is_exit : line.is_exit,
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);
}
module.exports = stockAdjustmentReceipt;