2023-12-01 10:43:07 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('updateFromSale', {
|
2023-12-01 12:54:09 +00:00
|
|
|
description: 'Update the visible items',
|
2023-12-01 10:43:07 +00:00
|
|
|
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({
|
2023-12-01 11:48:21 +00:00
|
|
|
where: {saleFk},
|
|
|
|
include: {relation: 'itemShelving'}
|
2023-12-01 10:43:07 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
2023-12-01 11:48:21 +00:00
|
|
|
const itemShelving = itemShelvingSale.itemShelving();
|
2023-12-01 10:43:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|