2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-28 13:21:04 +00:00
|
|
|
module.exports = Self => {
|
2019-03-06 07:54:56 +00:00
|
|
|
Self.remoteMethodCtx('updateDiscount', {
|
2018-06-28 13:21:04 +00:00
|
|
|
description: 'Changes the discount of a sale',
|
|
|
|
accessType: '',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'params',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: 'sale ID, newDiscount, price',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/updateDiscount`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-06 07:54:56 +00:00
|
|
|
Self.updateDiscount = async(ctx, params) => {
|
2018-07-16 07:19:38 +00:00
|
|
|
if (isNaN(params.editLines[0].discount))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The value should be a number`);
|
2018-07-13 13:21:37 +00:00
|
|
|
|
2018-06-28 13:21:04 +00:00
|
|
|
let model = Self.app.models;
|
|
|
|
let thisTicketIsEditable = await model.Ticket.isEditable(params.editLines[0].ticketFk);
|
|
|
|
let ticket = await model.Ticket.find({
|
|
|
|
where: {
|
|
|
|
id: params.editLines[0].ticketFk
|
|
|
|
},
|
|
|
|
include: [{
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['salesPersonFk']
|
|
|
|
}
|
|
|
|
}],
|
2019-03-06 07:54:56 +00:00
|
|
|
fields: ['id', 'clientFk', 'refFk']
|
2018-06-28 13:21:04 +00:00
|
|
|
});
|
|
|
|
|
2019-03-06 07:54:56 +00:00
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
let isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant');
|
|
|
|
|
|
|
|
if ((!thisTicketIsEditable && !isSalesAssistant) || (ticket.refFk && isSalesAssistant))
|
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
|
|
|
|
|
2018-06-28 13:21:04 +00:00
|
|
|
let componentToUse;
|
2018-07-13 13:21:37 +00:00
|
|
|
let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'});
|
2018-06-28 13:21:04 +00:00
|
|
|
|
|
|
|
if (usesMana)
|
|
|
|
componentToUse = 37;
|
|
|
|
else
|
|
|
|
componentToUse = 34;
|
|
|
|
|
|
|
|
for (let i = 0; i < params.editLines.length; i++) {
|
2018-07-13 13:21:37 +00:00
|
|
|
let currentLine = await model.Sale.findOne({where: {id: params.editLines[i].id}, fields: 'price'});
|
2018-07-16 07:19:38 +00:00
|
|
|
let value = ((-currentLine.price * params.editLines[i].discount) / 100);
|
2018-07-13 13:21:37 +00:00
|
|
|
await model.SaleComponent.upsert({saleFk: params.editLines[i].id, value: value, componentFk: componentToUse});
|
2018-06-28 13:21:04 +00:00
|
|
|
|
2018-07-13 13:21:37 +00:00
|
|
|
await model.Sale.update({id: params.editLines[i].id}, {discount: params.editLines[i].discount});
|
2018-06-28 13:21:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 11:06:14 +00:00
|
|
|
if (usesMana) {
|
|
|
|
query = `
|
2018-06-28 13:21:04 +00:00
|
|
|
call vn.manaSpellersRequery(?)`;
|
2019-02-14 11:06:14 +00:00
|
|
|
await Self.rawSql(query, [ticket[0].client().salesPersonFk]);
|
|
|
|
}
|
2018-06-28 13:21:04 +00:00
|
|
|
};
|
|
|
|
};
|