2023-03-28 09:34:14 +00:00
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
|
2023-03-27 12:29:22 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('getSerial', {
|
|
|
|
description: 'Return invoiceIn serial',
|
|
|
|
accessType: 'READ',
|
2023-03-28 09:34:14 +00:00
|
|
|
accepts: [{
|
|
|
|
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 09:34:14 +00:00
|
|
|
Self.getSerial = async(daysAgo, serial) => {
|
|
|
|
const conn = Self.dataSource.connector;
|
|
|
|
const stmt = [];
|
2023-03-27 12:29:22 +00:00
|
|
|
|
2023-03-28 09:34:14 +00:00
|
|
|
const issued = Date.vnNew();
|
|
|
|
issued.setDate(issued.getDate() - daysAgo);
|
2023-03-27 12:29:22 +00:00
|
|
|
|
2023-03-28 09:34:14 +00:00
|
|
|
stmt.push(new ParameterizedSQL(`
|
2023-03-27 12:29:22 +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
|
|
|
WHERE i.issued >= ? `, [issued]));
|
|
|
|
|
|
|
|
if (serial)
|
|
|
|
stmt.push(new ParameterizedSQL(`AND i.serial LIKE ? `, [serial]));
|
|
|
|
|
|
|
|
stmt.push(`GROUP BY i.serial`);
|
|
|
|
|
|
|
|
const sql = ParameterizedSQL.join(stmt);
|
|
|
|
const result = await conn.executeStmt(sql);
|
2023-03-27 12:29:22 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
};
|