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

63 lines
1.9 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()) => {
let stmts = [];
stmts.push(new ParameterizedSQL(
'CALL cache.available_refresh(@availableCalc, FALSE, ?, ?)', [
warehouseFk,
dated
]
));
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
]
)) - 1;
const availableIndex = stmts.push(new ParameterizedSQL(
'SELECT available FROM cache.available WHERE calc_id = @availableCalc AND item_id = ?', [
id
]
)) - 1;
const sql = ParameterizedSQL.join(stmts, ';');
let res = await Self.rawStmt(sql);
return {
available: res[availableIndex][0] ? res[availableIndex][0].available : 0,
visible: res[visibleIndex][0] ? res[visibleIndex][0].visible : 0};
};
};