2023-12-11 12:01:34 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateTracking', {
|
|
|
|
description: 'Modify a saleTracking record and, if applicable, add a corresponding record in saleBuy.',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'saleFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'originalQuantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'code',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isChecked',
|
2024-01-04 11:27:02 +00:00
|
|
|
type: 'boolean',
|
2023-12-11 12:01:34 +00:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'buyFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isScanned',
|
2024-01-04 11:35:00 +00:00
|
|
|
type: 'boolean',
|
2023-12-11 12:01:34 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: `/updateTracking`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.updateTracking = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned, 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 {
|
|
|
|
const state = await models.State.findOne({
|
|
|
|
where: {code},
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const attributes = {
|
|
|
|
saleFk,
|
|
|
|
isChecked,
|
|
|
|
originalQuantity,
|
|
|
|
workerFk: userId,
|
|
|
|
stateFk: state.id,
|
2024-01-04 11:35:00 +00:00
|
|
|
isScanned: isScanned === undefined ? null : isScanned,
|
2023-12-11 12:01:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const saleTracking = await models.SaleTracking.findOne({
|
|
|
|
where: attributes,
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
if (!saleTracking)
|
|
|
|
await models.SaleTracking.create(attributes, myOptions);
|
|
|
|
|
|
|
|
else
|
|
|
|
await saleTracking.updateAttributes(attributes, myOptions);
|
|
|
|
|
|
|
|
let isBuy;
|
|
|
|
if (buyFk) {
|
|
|
|
isBuy = await models.Buy.findOne({
|
|
|
|
where: {
|
|
|
|
id: buyFk,
|
|
|
|
itemOriginalFk: {
|
|
|
|
neq: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (isBuy)
|
|
|
|
await models.SaleBuy.create({saleFk, buyFk}, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|