salix/modules/invoiceIn/back/methods/invoice-in/getSerial.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-03-28 09:34:14 +00:00
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
2023-03-28 11:45:26 +00:00
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
2023-03-28 09:34:14 +00:00
2023-03-27 12:29:22 +00:00
module.exports = Self => {
2023-03-28 11:45:26 +00:00
Self.remoteMethodCtx('getSerial', {
2023-03-27 12:29:22 +00:00
description: 'Return invoiceIn serial',
accessType: 'READ',
2023-03-28 09:34:14 +00:00
accepts: [{
2023-03-28 11:45:26 +00:00
arg: 'filter',
type: 'object'
}, {
2023-03-28 09:34:14 +00:00
arg: 'daysAgo',
type: 'number',
2023-03-27 12:29:22 +00:00
required: true
2023-03-28 09:34:14 +00:00
}, {
arg: 'serial',
type: 'string'
}],
2023-03-27 12:29:22 +00:00
returns: {
type: 'object',
root: true
},
http: {
path: '/getSerial',
verb: 'GET'
}
});
2023-03-28 11:45:26 +00:00
Self.getSerial = async(ctx, options) => {
2023-03-28 09:34:14 +00:00
const conn = Self.dataSource.connector;
2023-03-28 11:45:26 +00:00
const args = ctx.args;
const myOptions = {};
2023-03-27 12:29:22 +00:00
2023-03-28 11:45:26 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
2023-03-27 12:29:22 +00:00
2023-03-28 11:45:26 +00:00
const where = buildFilter(args, (param, value) => {
switch (param) {
case 'serial':
return {'f.serial': {like: `%${value}%`}};
}
});
filter = mergeFilters(args.filter, {where});
const issued = Date.vnNew();
issued.setDate(issued.getDate() - args.daysAgo);
2023-03-28 09:34:14 +00:00
2023-03-28 11:45:26 +00:00
const stmts = [];
const stmt = new ParameterizedSQL(
`SELECT *
FROM (
SELECT i.serial, SUM(IF(i.isBooked, 0,1)) pending, COUNT(*) total
FROM vn.invoiceIn i
WHERE i.issued >= ?
GROUP BY i.serial) f`
, [issued]);
2023-03-28 09:34:14 +00:00
2023-03-28 11:45:26 +00:00
stmt.merge(conn.makeWhere(filter.where));
stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter));
2023-03-28 09:34:14 +00:00
2023-03-28 11:45:26 +00:00
const invoiceInIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
2023-03-27 12:29:22 +00:00
2023-03-28 11:45:26 +00:00
return invoiceInIndex === 0 ? result : result[invoiceInIndex];
2023-03-27 12:29:22 +00:00
};
};