salix/modules/supplier/back/methods/supplier/consumption.js

169 lines
5.5 KiB
JavaScript

const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethodCtx('consumption', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
}, {
arg: 'search',
type: 'String',
description: `If it's and integer searchs by id, otherwise it searchs by name`
}, {
arg: 'itemId',
type: 'Number',
description: 'Item id'
}, {
arg: 'categoryId',
type: 'Number',
description: 'Category id'
}, {
arg: 'typeId',
type: 'Number',
description: 'Item type id',
}, {
arg: 'buyerId',
type: 'Number',
description: 'Buyer id'
}, {
arg: 'from',
type: 'Date',
description: `The from date filter`
}, {
arg: 'to',
type: 'Date',
description: `The to date filter`
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/consumption`,
verb: 'GET'
}
});
Self.consumption = async(ctx, filter) => {
const conn = Self.dataSource.connector;
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return /^\d+$/.test(value)
? {'i.id': value}
: {'i.name': {like: `%${value}%`}};
case 'itemId':
return {'i.id': value};
case 'description':
return {'i.description': {like: `%${value}%`}};
case 'categoryId':
return {'it.categoryFk': value};
case 'typeId':
return {'it.id': value};
case 'buyerId':
return {'it.workerFk': value};
case 'from':
return {'t.shipped': {gte: value}};
case 'to':
return {'t.shipped': {lte: value}};
}
});
filter = mergeFilters(filter, {where});
let stmts = [];
let stmt;
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.entry');
stmt = new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.entry
(INDEX (id))
ENGINE = MEMORY
SELECT
e.id,
e.invoiceNumber,
e.supplierFk,
t.shipped
FROM vn.entry e
JOIN vn.travel t ON t.id = e.travelFk
JOIN buy b ON e.id = b.entryFk
JOIN item i ON i.id = b.itemFk
JOIN itemType it ON it.id = i.typeFk`);
stmt.merge(conn.makeWhere(filter.where));
stmt.merge(conn.makeGroupBy('e.id'));
stmt.merge(conn.makeLimit(filter));
stmts.push(stmt);
const entriesIndex = stmts.push('SELECT * FROM tmp.entry') - 1;
stmt = new ParameterizedSQL(
`SELECT
b.id AS buyId,
b.itemFk,
b.entryFk,
b.quantity,
CAST(b.buyingValue AS DECIMAL(10,2)) AS price,
CAST(SUM(b.buyingValue*b.quantity)AS DECIMAL(10,2)) AS total,
i.id,
i.description,
i.name AS itemName,
i.subName,
i.size AS itemSize,
i.typeFk AS itemTypeFk,
i.tag5,
i.value5,
i.tag6,
i.value6,
i.tag7,
i.value7,
i.tag8,
i.value8,
i.tag9,
i.value9,
i.tag10,
i.value10,
it.id,
it.workerFk,
it.categoryFk,
it.code AS itemTypeCode
FROM buy b
JOIN tmp.entry e ON e.id = b.entryFk
JOIN item i ON i.id = b.itemFk
JOIN itemType it ON it.id = i.typeFk`
);
stmt.merge('WHERE b.quantity > 0');
stmt.merge(conn.makeGroupBy('b.id'));
stmt.merge(conn.makeOrderBy(filter.order));
const buysIndex = stmts.push(stmt) - 1;
stmts.push(`DROP TEMPORARY TABLE tmp.entry`);
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const entries = result[entriesIndex];
const buys = result[buysIndex];
const entriesMap = new Map();
for (let entry of entries)
entriesMap.set(entry.id, entry);
for (let buy of buys) {
const entry = entriesMap.get(buy.entryFk);
if (entry) {
if (!entry.buys) entry.buys = [];
entry.buys.push(buy);
}
}
return entries;
};
};