2023-12-12 12:24:18 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('mark', {
|
|
|
|
description: 'Insert an itemShelvingSale and Modify a saleTracking record',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'saleFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'originalQuantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'code',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isChecked',
|
2023-12-12 15:00:43 +00:00
|
|
|
type: 'boolean',
|
2023-12-12 12:24:18 +00:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'buyFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isScanned',
|
2024-01-04 12:11:39 +00:00
|
|
|
type: 'boolean',
|
2023-12-12 12:24:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'itemShelvingFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: `/mark`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.mark = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned, quantity, itemShelvingFk, options) => {
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
const models = Self.app.models;
|
2024-01-22 11:36:52 +00:00
|
|
|
const $t = ctx.req.__;
|
2023-12-12 12:24:18 +00:00
|
|
|
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);
|
|
|
|
|
2024-01-22 11:36:52 +00:00
|
|
|
const itemShelving = await models.ItemShelving.findById(itemShelvingFk, myOptions);
|
2023-12-12 12:24:18 +00:00
|
|
|
|
2024-01-22 11:36:52 +00:00
|
|
|
await itemShelving.updateAttributes({visible: itemShelving.visible - quantity}, myOptions);
|
2023-12-12 12:24:18 +00:00
|
|
|
|
|
|
|
await Self.updateAll(
|
|
|
|
{saleFk},
|
2023-12-12 15:00:43 +00:00
|
|
|
{isChecked: true},
|
2023-12-12 12:24:18 +00:00
|
|
|
myOptions
|
|
|
|
);
|
|
|
|
|
2024-01-22 11:36:52 +00:00
|
|
|
await Self.updateTracking(ctx, saleFk, originalQuantity, code, isChecked, null, isScanned, myOptions);
|
2023-12-12 12:24:18 +00:00
|
|
|
|
2024-01-22 12:32:50 +00:00
|
|
|
try {
|
|
|
|
const buy = await models.Buy.findById(buyFk, myOptions);
|
|
|
|
if (buy.itemOriginalFk) await models.SaleBuy.create({saleFk, buyFk}, myOptions);
|
2024-01-22 14:13:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new UserError($t('The sale can not be tracked'));
|
|
|
|
}
|
2024-01-22 12:32:50 +00:00
|
|
|
|
2023-12-12 12:24:18 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
2024-01-22 14:13:49 +00:00
|
|
|
if (e.message == $t('The sale can not be tracked')) {
|
|
|
|
if (tx) tx.commit();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2023-12-12 12:24:18 +00:00
|
|
|
if (tx) await tx.rollback();
|
2024-01-22 11:36:52 +00:00
|
|
|
throw new UserError($t('The line could not be marked'));
|
2023-12-12 12:24:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|