salix/modules/item/back/methods/item/getVisibleAvailable.js

75 lines
2.1 KiB
JavaScript

const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethod('getVisibleAvailable', {
description: 'Returns visible and available for params',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
},
{
arg: 'warehouseFk',
type: 'number',
required: true,
},
{
arg: 'dated',
type: 'date',
required: false,
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/getVisibleAvailable`,
verb: 'GET'
}
});
Self.getVisibleAvailable = async(id, warehouseFk, dated = new Date(), options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const stmts = [];
stmts.push(new ParameterizedSQL(
'CALL cache.available_refresh(@availableCalc, FALSE, ?, ?)',
[
warehouseFk,
dated
],
myOptions
));
stmts.push(new ParameterizedSQL(
'CALL cache.visible_refresh(@visibleCalc, FALSE,?)', [
warehouseFk
]
));
const visibleIndex = stmts.push(new ParameterizedSQL(
'SELECT visible FROM cache.visible WHERE calc_id = @visibleCalc AND item_id = ?',
[id],
myOptions
)) - 1;
const availableIndex = stmts.push(new ParameterizedSQL(
'SELECT available FROM cache.available WHERE calc_id = @availableCalc AND item_id = ?',
[id],
myOptions
)) - 1;
const sql = ParameterizedSQL.join(stmts, ';');
const res = await Self.rawStmt(sql, myOptions);
return {
available: res[availableIndex][0] ? res[availableIndex][0].available : 0,
visible: res[visibleIndex][0] ? res[visibleIndex][0].visible : 0};
};
};