2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-28 13:25:09 +00:00
|
|
|
module.exports = Self => {
|
2021-05-26 07:14:18 +00:00
|
|
|
Self.remoteMethodCtx('updateQuantity', {
|
2018-06-28 13:25:09 +00:00
|
|
|
description: 'Changes the quantity of a sale',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-06-28 13:25:09 +00:00
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'sale ID',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}, {
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'newQuantity'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateQuantity`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
Self.updateQuantity = async(ctx, id, newQuantity, options) => {
|
2021-05-26 07:14:18 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
2023-01-31 08:04:45 +00:00
|
|
|
await models.Sale.canEdit(ctx, [id], myOptions);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const filter = {
|
|
|
|
include: {
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
2021-07-29 15:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 06:27:18 +00:00
|
|
|
};
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const sale = await models.Sale.findById(id, filter, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const oldQuantity = sale.quantity;
|
|
|
|
const result = await sale.updateAttributes({quantity: newQuantity}, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const salesPerson = sale.ticket().client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
2023-10-19 06:37:28 +00:00
|
|
|
const url = await Self.app.models.Url.getUrl();
|
2021-09-29 06:27:18 +00:00
|
|
|
const message = $t('Changed sale quantity', {
|
|
|
|
ticketId: sale.ticket().id,
|
|
|
|
itemId: sale.itemFk,
|
|
|
|
concept: sale.concept,
|
|
|
|
oldQuantity: oldQuantity,
|
|
|
|
newQuantity: newQuantity,
|
2023-10-16 16:24:58 +00:00
|
|
|
ticketUrl: `${url}ticket/${sale.ticket().id}/sale`,
|
|
|
|
itemUrl: `${url}item/${sale.itemFk}/summary`
|
2021-09-29 06:27:18 +00:00
|
|
|
});
|
2023-10-15 09:08:33 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
|
|
|
|
}
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
2018-06-28 13:25:09 +00:00
|
|
|
};
|
|
|
|
};
|