diff --git a/modules/item/back/methods/item-shelving/updateFromSale.js b/modules/item/back/methods/item-shelving/updateFromSale.js new file mode 100644 index 000000000..cd2012edb --- /dev/null +++ b/modules/item/back/methods/item-shelving/updateFromSale.js @@ -0,0 +1,51 @@ +module.exports = Self => { + Self.remoteMethod('updateFromSale', { + description: 'Returns a list of items and possible alternative locations', + accessType: 'WRITE', + accepts: [{ + arg: 'saleFk', + type: 'number', + required: true, + }], + http: { + path: `/updateFromSale`, + verb: 'POST' + } + }); + + Self.updateFromSale = async(saleFk, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const itemShelvingSale = await models.ItemShelvingSale.findOne({ + where: {saleFk} + }, myOptions); + + const itemShelving = await models.ItemShelving.findOne({ + where: { + id: itemShelvingSale.ItemShelvingFk + } + }); + const quantity = itemShelving.visible + itemShelvingSale.quantity; + + await itemShelving.updateAttributes( + {visible: quantity}, + myOptions + ); + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/item/back/models/item-shelving.js b/modules/item/back/models/item-shelving.js index e610aabec..4eb5374e8 100644 --- a/modules/item/back/models/item-shelving.js +++ b/modules/item/back/models/item-shelving.js @@ -3,4 +3,5 @@ module.exports = Self => { require('../methods/item-shelving/getInventory')(Self); require('../methods/item-shelving/makeMulti')(Self); require('../methods/item-shelving/return')(Self); + require('../methods/item-shelving/updateFromSale')(Self); };