salix/modules/ticket/back/methods/ticket/updateDiscount.js

198 lines
6.7 KiB
JavaScript
Raw Normal View History

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,
},
{
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) => {
const $t = ctx.req.__; // $translate
2019-06-05 11:39:15 +00:00
const models = Self.app.models;
const myOptions = {};
let tx;
2019-06-05 11:39:15 +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
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']
}
}
};
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;
const isLocked = await models.Ticket.isLocked(id, myOptions);
const roles = await models.Account.getRoles(userId, myOptions);
2020-11-18 07:16:37 +00:00
const hasAllowedRoles = roles.filter(role =>
role == 'salesPerson' || role == 'claimManager'
);
2020-02-27 11:31:38 +00:00
const state = await Self.app.models.TicketState.findOne({
where: {ticketFk: id}
}, myOptions);
2020-02-27 11:31:38 +00:00
const alertLevel = state ? state.alertLevel : null;
2020-11-18 07:16:37 +00:00
if (isLocked || (!hasAllowedRoles && 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';
const discountComponent = await models.Component.findOne({
where: {code: componentCode}}, myOptions);
2019-06-05 11:39:15 +00:00
const componentId = discountComponent.id;
const promises = [];
let changesMade = '';
2019-06-05 11:39:15 +00:00
for (let sale of sales) {
const oldDiscount = sale.discount;
2019-06-05 11:39:15 +00:00
const value = ((-sale.price * newDiscount) / 100);
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({
where: {
and: [
{saleFk: sale.id},
{componentFk: {inq: [manaComponent.id, manaClaimComponent.id]}}
]
}
}, myOptions);
let deletedComponent;
if (oldComponent) {
const filter = {
saleFk: sale.id,
componentFk: oldComponent.componentFk
};
deletedComponent = await models.SaleComponent.destroyAll(filter, myOptions);
2022-10-17 12:01:21 +00:00
}
const newComponent = await createSaleComponent(sale.id, value, componentId, myOptions);
2019-06-05 11:39:15 +00:00
const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions);
2019-06-05 11:39:15 +00:00
promises.push(newComponent, updatedSale, deletedComponent);
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);
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
const ticket = await models.Ticket.findById(id, {
include: {
relation: 'client',
scope: {
include: {
relation: 'salesPersonUser',
scope: {
fields: ['id', 'name']
}
}
}
}
}, myOptions);
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
});
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
}
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;
return models.SaleComponent.create({
2022-10-17 06:01:55 +00:00
saleFk: saleId,
value: value,
componentFk: componentId
}, myOptions);
}
2019-06-05 11:39:15 +00:00
};