diff --git a/services/loopback/common/methods/item/getCard.js b/services/loopback/common/methods/item/getCard.js new file mode 100644 index 0000000000..b076d46c81 --- /dev/null +++ b/services/loopback/common/methods/item/getCard.js @@ -0,0 +1,67 @@ +module.exports = Self => { + Self.remoteMethod('getCard', { + description: 'Return the card for a given item id', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'The item id', + http: {source: 'path'} + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/getCard`, + verb: 'GET' + } + }); + + Self.getCard = async id => { + let item = {}; + + // Item basic data + let filter = { + where: {id: id}, + include: [ + {relation: 'itemType', + scope: { + fields: ['id', 'name', 'workerFk', 'warehouseFk'], + include: [{ + relation: 'worker', + scope: { + fields: ['id', 'name', 'firstName'] + } + }] + } + }, + {relation: 'tags', + scope: { + fields: ['id', 'value', 'tagFk'], + include: [{ + relation: 'tag', + scope: { + fields: ['id', 'name'] + } + }] + } + } + ] + }; + [item] = await Self.app.models.Item.find(filter); + + // Visible Avaible + let query = ` + CALL vn.getItemVisibleAvailable(?,curdate(),?,?)`; + + let options = [item.id, item.itemType().warehouseFk, false]; + let [res] = await Self.rawSql(query, options); + + item.available = res[0].available ? res[0].available : '-'; + item.visible = res[0].visible ? res[0].visible : '-'; + + return item; + }; +}; diff --git a/services/loopback/common/models/item.js b/services/loopback/common/models/item.js index 4f4ac1dd39..a242e79878 100644 --- a/services/loopback/common/models/item.js +++ b/services/loopback/common/models/item.js @@ -7,6 +7,7 @@ module.exports = Self => { require('../methods/item/getDiary')(Self); require('../methods/item/getLastEntries')(Self); require('../methods/item/getSummary')(Self); + require('../methods/item/getCard')(Self); Self.validatesPresenceOf('name', {message: 'Cannot be blank'}); Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});