140 lines
4.2 KiB
JavaScript
140 lines
4.2 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`
|
|
},
|
|
{
|
|
arg: 'grouped',
|
|
type: 'boolean',
|
|
description: 'Group by item'
|
|
}
|
|
],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/consumption`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.consumption = async(ctx, filter, options) => {
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const conn = Self.dataSource.connector;
|
|
const args = ctx.args;
|
|
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});
|
|
|
|
const stmt = new ParameterizedSQL('SELECT');
|
|
if (args.grouped)
|
|
stmt.merge(`SUM(s.quantity) AS quantity,`);
|
|
else
|
|
stmt.merge(`s.quantity,`);
|
|
|
|
stmt.merge(`s.itemFk,
|
|
s.concept,
|
|
s.ticketFk,
|
|
t.shipped,
|
|
i.name AS itemName,
|
|
i.size AS itemSize,
|
|
i.typeFk AS itemTypeFk,
|
|
i.subName,
|
|
i.tag5,
|
|
i.value5,
|
|
i.tag6,
|
|
i.value6,
|
|
i.tag7,
|
|
i.value7,
|
|
i.tag8,
|
|
i.value8,
|
|
i.tag9,
|
|
i.value9,
|
|
i.tag10,
|
|
i.value10
|
|
FROM sale s
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
JOIN item i ON i.id = s.itemFk
|
|
JOIN itemType it ON it.id = i.typeFk`, [args.grouped]);
|
|
|
|
stmt.merge(conn.makeWhere(filter.where));
|
|
|
|
if (args.grouped)
|
|
stmt.merge(`GROUP BY s.itemFk`);
|
|
|
|
stmt.merge(conn.makePagination(filter));
|
|
|
|
return conn.executeStmt(stmt, myOptions);
|
|
};
|
|
};
|