2024-07-29 07:21:35 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-03-14 07:36:19 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('updateFromSale', {
|
|
|
|
description: 'Update the visible items',
|
|
|
|
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},
|
|
|
|
include: {relation: 'itemShelving'}
|
|
|
|
}, myOptions);
|
|
|
|
|
2024-07-29 07:21:35 +00:00
|
|
|
if (!itemShelvingSale?.itemShelving())
|
|
|
|
throw new UserError('The sale not exists in the item shelving');
|
|
|
|
|
2024-03-14 07:36:19 +00:00
|
|
|
const itemShelving = itemShelvingSale.itemShelving();
|
|
|
|
const quantity = itemShelving.visible + itemShelvingSale.quantity;
|
2024-09-25 10:24:00 +00:00
|
|
|
const available = itemShelving.available + itemShelvingSale.quantity;
|
2024-03-14 07:36:19 +00:00
|
|
|
|
|
|
|
await itemShelving.updateAttributes(
|
2024-09-25 10:24:00 +00:00
|
|
|
{
|
|
|
|
visible: quantity,
|
|
|
|
available: available
|
|
|
|
},
|
2024-03-14 07:36:19 +00:00
|
|
|
myOptions
|
|
|
|
);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|