2019-06-05 11:39:15 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2019-07-22 11:14:00 +00:00
|
|
|
Self.remoteMethodCtx('updateDiscount', {
|
2019-06-05 11:39:15 +00:00
|
|
|
description: 'Changes the discount of a sale',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
description: 'The ticket id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'salesIds',
|
|
|
|
description: 'The sales id',
|
|
|
|
type: ['number'],
|
|
|
|
required: true,
|
2021-07-29 15:20:57 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 11:39:15 +00:00
|
|
|
arg: 'newDiscount',
|
|
|
|
description: 'The new discount',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
2022-03-29 11:42:37 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-11 07:04:05 +00:00
|
|
|
arg: 'manaCode',
|
2022-03-29 11:42:37 +00:00
|
|
|
description: 'The type of mana',
|
|
|
|
type: 'string',
|
2022-03-29 12:00:53 +00:00
|
|
|
required: false
|
2019-06-05 11:39:15 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateDiscount`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-11 07:04:05 +00:00
|
|
|
Self.updateDiscount = async(ctx, id, salesIds, newDiscount, manaCode, options) => {
|
2021-07-29 15:20:57 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2019-06-05 11:39:15 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-21 16:48:59 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2019-06-05 11:39:15 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2019-06-13 07:21:36 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
try {
|
2019-06-05 11:39:15 +00:00
|
|
|
const filter = {
|
|
|
|
where: {
|
|
|
|
id: {inq: salesIds}
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['salesPersonFk']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fields: ['id', 'clientFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const sales = await models.Sale.find(filter, myOptions);
|
2019-06-05 11:39:15 +00:00
|
|
|
|
|
|
|
if (sales.length === 0)
|
|
|
|
throw new UserError('Please select at least one sale');
|
|
|
|
|
|
|
|
const allFromSameTicket = sales.every(sale => sale.ticketFk === id);
|
|
|
|
if (!allFromSameTicket)
|
|
|
|
throw new UserError('All sales must belong to the same ticket');
|
|
|
|
|
2020-06-25 15:12:53 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2021-09-21 16:48:59 +00:00
|
|
|
const isLocked = await models.Ticket.isLocked(id, myOptions);
|
2023-04-18 13:03:23 +00:00
|
|
|
const canEditDiscount = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'editDiscount');
|
2020-11-18 07:16:37 +00:00
|
|
|
|
2020-02-27 11:31:38 +00:00
|
|
|
const state = await Self.app.models.TicketState.findOne({
|
|
|
|
where: {ticketFk: id}
|
2021-09-21 16:48:59 +00:00
|
|
|
}, myOptions);
|
2020-02-27 11:31:38 +00:00
|
|
|
const alertLevel = state ? state.alertLevel : null;
|
|
|
|
|
2023-04-18 13:03:23 +00:00
|
|
|
if (isLocked || (!canEditDiscount && alertLevel > 0))
|
2019-06-05 11:39:15 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
|
2022-10-21 07:01:08 +00:00
|
|
|
const usesMana = await models.Sale.usesMana(ctx, myOptions);
|
2022-04-11 07:04:05 +00:00
|
|
|
const componentCode = usesMana ? manaCode : 'buyerDiscount';
|
2019-12-27 07:01:53 +00:00
|
|
|
const discountComponent = await models.Component.findOne({
|
2021-09-21 16:48:59 +00:00
|
|
|
where: {code: componentCode}}, myOptions);
|
2019-06-05 11:39:15 +00:00
|
|
|
|
|
|
|
const componentId = discountComponent.id;
|
2021-07-29 15:20:57 +00:00
|
|
|
const promises = [];
|
|
|
|
let changesMade = '';
|
|
|
|
|
2019-06-05 11:39:15 +00:00
|
|
|
for (let sale of sales) {
|
2021-07-29 15:20:57 +00:00
|
|
|
const oldDiscount = sale.discount;
|
2019-06-05 11:39:15 +00:00
|
|
|
const value = ((-sale.price * newDiscount) / 100);
|
2022-10-14 12:30:37 +00:00
|
|
|
|
|
|
|
const manaComponent = await models.Component.findOne({
|
|
|
|
where: {code: 'mana'}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const manaClaimComponent = await models.Component.findOne({
|
|
|
|
where: {code: 'manaClaim'}
|
|
|
|
}, myOptions);
|
|
|
|
|
2022-10-17 06:29:09 +00:00
|
|
|
const [oldComponent] = await models.SaleComponent.find({
|
2022-10-14 12:30:37 +00:00
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{saleFk: sale.id},
|
|
|
|
{componentFk: {inq: [manaComponent.id, manaClaimComponent.id]}}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2022-10-26 06:23:36 +00:00
|
|
|
let deletedComponent;
|
2022-10-14 12:30:37 +00:00
|
|
|
if (oldComponent) {
|
|
|
|
const filter = {
|
|
|
|
saleFk: sale.id,
|
|
|
|
componentFk: oldComponent.componentFk
|
|
|
|
};
|
2022-10-26 06:23:36 +00:00
|
|
|
deletedComponent = await models.SaleComponent.destroyAll(filter, myOptions);
|
2022-10-17 12:01:21 +00:00
|
|
|
}
|
2022-10-14 12:30:37 +00:00
|
|
|
|
2022-10-26 06:23:36 +00:00
|
|
|
const newComponent = await createSaleComponent(sale.id, value, componentId, myOptions);
|
2019-06-05 11:39:15 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions);
|
2019-06-05 11:39:15 +00:00
|
|
|
|
2022-10-26 06:23:36 +00:00
|
|
|
promises.push(newComponent, updatedSale, deletedComponent);
|
2021-11-24 08:25:24 +00:00
|
|
|
|
|
|
|
const change = `${oldDiscount}% ➔ *${newDiscount}%*`;
|
|
|
|
changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${change}`;
|
2019-06-05 11:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2021-11-24 08:25:24 +00:00
|
|
|
await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions);
|
|
|
|
await Self.rawSql('CALL vn.ticket_recalc(?)', [id], myOptions);
|
2019-06-05 11:39:15 +00:00
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
const ticket = await models.Ticket.findById(id, {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 16:48:59 +00:00
|
|
|
}, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
|
|
|
const salesPerson = ticket.client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
|
|
|
|
const message = $t('Changed sale discount', {
|
|
|
|
ticketId: id,
|
|
|
|
ticketUrl: `${origin}/#!/ticket/${id}/sale`,
|
|
|
|
changes: changesMade
|
|
|
|
});
|
2021-10-15 13:31:13 +00:00
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
2019-06-05 11:39:15 +00:00
|
|
|
}
|
|
|
|
};
|
2022-10-17 06:01:55 +00:00
|
|
|
|
|
|
|
async function createSaleComponent(saleId, value, componentId, myOptions) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
|
2022-11-07 08:51:33 +00:00
|
|
|
return models.SaleComponent.upsert({
|
2022-10-17 06:01:55 +00:00
|
|
|
saleFk: saleId,
|
|
|
|
value: value,
|
|
|
|
componentFk: componentId
|
|
|
|
}, myOptions);
|
|
|
|
}
|
2019-06-05 11:39:15 +00:00
|
|
|
};
|