2024-03-14 07:36:19 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('setPicked', {
|
|
|
|
description: 'Add the sales line of the item and set the tracking.',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'saleFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'originalQuantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'code',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isChecked',
|
|
|
|
type: 'boolean',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'buyFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isScanned',
|
|
|
|
type: 'boolean',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'itemShelvingFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: `/setPicked`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.setPicked = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned, quantity, itemShelvingFk, options) => {
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
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 {
|
|
|
|
await models.ItemShelvingSale.create({
|
|
|
|
itemShelvingFk,
|
|
|
|
saleFk,
|
|
|
|
quantity,
|
|
|
|
userFk: userId
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const itemShelving = await models.ItemShelving.findById(itemShelvingFk, null, myOptions);
|
|
|
|
|
|
|
|
await itemShelving.updateAttributes({visible: itemShelving.visible - quantity}, myOptions);
|
|
|
|
|
|
|
|
await Self.updateAll(
|
|
|
|
{saleFk},
|
|
|
|
{isChecked: true},
|
|
|
|
myOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
await Self.updateTracking(ctx, saleFk, originalQuantity, code, isChecked, null, isScanned, myOptions);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const {itemOriginalFk} = await models.Buy.findById(buyFk, {fields: ['itemOriginalFk']}, myOptions);
|
|
|
|
if (itemOriginalFk) await models.SaleBuy.create({saleFk, buyFk}, myOptions);
|
|
|
|
} catch (e) {
|
2024-03-25 12:00:55 +00:00
|
|
|
if (tx) return tx.commit();
|
2024-03-14 07:36:19 +00:00
|
|
|
}
|
2024-03-22 05:56:27 +00:00
|
|
|
if (tx) await tx.commit();
|
2024-03-14 07:36:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw new UserError('The line could not be marked');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|