salix/services/loopback/common/methods/item/getSummary.js

108 lines
3.1 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('getSummary', {
description: 'Updates the item taxes',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The item id',
http: {source: 'path'}
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/getSummary`,
verb: 'GET'
}
});
Self.getSummary = async id => {
let summary = {};
// Item basic data and taxes
let filter = {
where: {id: id},
include: [
{relation: 'itemType',
scope: {
fields: ['id', 'name', 'workerFk', 'warehouseFk'],
include: [{
relation: 'worker',
scope: {
fields: ['id', 'name', 'firstName']
}
}]
}
},
{relation: 'intrastat'},
{relation: 'expence'},
{relation: 'origin'},
{relation: 'taxes',
scope: {
fields: ['id', 'countryFk', 'taxClassFk'],
include: [{
relation: 'country',
scope: {
fields: ['id', 'country']
}
}, {
relation: 'taxClass',
scope: {
fields: ['id', 'description']
}
}]
}
}
]
};
[summary.item] = await Self.app.models.Item.find(filter);
// Tags
filter = {
where: {
itemFk: id
},
include: {
relation: 'tag'
}
};
summary.tags = await Self.app.models.ItemTag.find(filter);
// Botanical
filter = {
where: {itemFk: id},
include: [{relation: 'genus'}, {relation: 'specie'}]
};
[summary.botanical] = await Self.app.models.ItemBotanical.find(filter);
// Niches
filter = {
where: {itemFk: id},
include: {relation: 'warehouse'}
};
summary.niches = await Self.app.models.ItemNiche.find(filter);
// Barcodes
filter = {
where: {
itemFk: id
}
};
summary.barcodes = await Self.app.models.ItemNiche.find(filter);
// Visible Avaible
let query = `
CALL vn.getItemVisibleAvailable(?,curdate(),?,?)`;
let options = [summary.item.id, summary.item.itemType().warehouseFk, false];
let [res] = await Self.rawSql(query, options);
summary.available = res[0].available ? res[0].available : '-';
summary.visible = res[0].visible ? res[0].visible : '-';
return summary;
};
};