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

140 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-06-12 07:18:19 +00:00
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',
2021-07-08 13:53:30 +00:00
type: 'object',
2020-06-12 07:18:19 +00:00
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
2021-07-08 13:53:30 +00:00
},
{
2020-06-12 07:18:19 +00:00
arg: 'search',
2021-07-08 13:53:30 +00:00
type: 'string',
2020-06-12 07:18:19 +00:00
description: `If it's and integer searchs by id, otherwise it searchs by name`
2021-07-08 13:53:30 +00:00
},
{
2020-08-12 06:26:00 +00:00
arg: 'itemId',
2021-07-08 13:53:30 +00:00
type: 'number',
2020-06-12 07:18:19 +00:00
description: 'Item id'
2021-07-08 13:53:30 +00:00
},
{
2020-08-12 06:26:00 +00:00
arg: 'categoryId',
2021-07-08 13:53:30 +00:00
type: 'number',
2020-06-12 07:18:19 +00:00
description: 'Category id'
2021-07-08 13:53:30 +00:00
},
{
2020-08-12 06:26:00 +00:00
arg: 'typeId',
2021-07-08 13:53:30 +00:00
type: 'number',
2020-06-12 07:18:19 +00:00
description: 'Item type id',
2021-07-08 13:53:30 +00:00
},
{
2020-08-12 06:26:00 +00:00
arg: 'buyerId',
2021-07-08 13:53:30 +00:00
type: 'number',
2020-06-12 07:18:19 +00:00
description: 'Buyer id'
2021-07-08 13:53:30 +00:00
},
{
2020-06-12 07:18:19 +00:00
arg: 'from',
2021-07-08 13:53:30 +00:00
type: 'date',
2020-06-12 07:18:19 +00:00
description: `The from date filter`
2021-07-08 13:53:30 +00:00
},
{
2020-06-12 07:18:19 +00:00
arg: 'to',
2021-07-08 13:53:30 +00:00
type: 'date',
2020-06-12 07:18:19 +00:00
description: `The to date filter`
2021-07-08 13:53:30 +00:00
},
{
2020-06-12 07:18:19 +00:00
arg: 'grouped',
2021-07-08 13:53:30 +00:00
type: 'boolean',
2020-06-12 07:18:19 +00:00
description: 'Group by item'
}
],
returns: {
2021-07-08 13:53:30 +00:00
type: ['object'],
2020-06-12 07:18:19 +00:00
root: true
},
http: {
path: `/consumption`,
verb: 'GET'
}
});
2021-07-08 13:53:30 +00:00
Self.consumption = async(ctx, filter, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2020-06-12 07:18:19 +00:00
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};
2020-08-12 06:26:00 +00:00
case 'from':
return {'t.shipped': {gte: value}};
case 'to':
return {'t.shipped': {lte: value}};
2020-06-12 07:18:19 +00:00
}
});
filter = mergeFilters(filter, {where});
2021-07-08 13:53:30 +00:00
const stmt = new ParameterizedSQL('SELECT');
2020-06-12 07:18:19 +00:00
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));
2021-07-08 13:53:30 +00:00
return conn.executeStmt(stmt, myOptions);
2020-06-12 07:18:19 +00:00
};
};