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

66 lines
1.9 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-29 06:44:35 +00:00
const issued = Date.vnNew();
2023-03-28 11:45:26 +00:00
const where = buildFilter(args, (param, value) => {
switch (param) {
2023-03-29 06:44:35 +00:00
case 'daysAgo':
issued.setDate(issued.getDate() - value);
return {'i.issued': {gte: issued}};
2023-03-28 11:45:26 +00:00
case 'serial':
2023-03-29 06:44:35 +00:00
return {'i.serial': {like: `%${value}%`}};
2023-03-28 11:45:26 +00:00
}
});
2024-08-16 08:08:58 +00:00
const filter = mergeFilters(args.filter, {where});
2023-03-28 09:34:14 +00:00
2023-03-28 11:45:26 +00:00
const stmt = new ParameterizedSQL(
2023-03-29 06:44:35 +00:00
`SELECT i.serial, SUM(IF(i.isBooked, 0,1)) pending, COUNT(*) total
FROM vn.invoiceIn i`
);
2023-03-28 09:34:14 +00:00
2023-03-28 11:45:26 +00:00
stmt.merge(conn.makeWhere(filter.where));
2023-03-29 06:44:35 +00:00
stmt.merge(`GROUP BY i.serial`);
2023-03-28 11:45:26 +00:00
stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter));
2023-03-28 09:34:14 +00:00
2023-03-29 06:48:54 +00:00
const result = await conn.executeStmt(stmt, myOptions);
2023-03-27 12:29:22 +00:00
2023-03-29 06:48:54 +00:00
return result;
2023-03-27 12:29:22 +00:00
};
};