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:27:29 +00:00
|
|
|
module.exports = Self => {
|
2019-07-22 11:14:00 +00:00
|
|
|
Self.remoteMethodCtx('updatePrice', {
|
2019-06-03 06:01:24 +00:00
|
|
|
description: 'Changes the price of a sale',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2019-06-03 06:01:24 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
description: 'The sale id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
http: {source: 'path'}
|
|
|
|
}, {
|
|
|
|
arg: 'newPrice',
|
|
|
|
description: 'The new price',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
2018-06-28 13:27:29 +00:00
|
|
|
returns: {
|
2021-07-29 15:20:57 +00:00
|
|
|
type: 'number',
|
2018-06-28 13:27:29 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2019-06-03 06:01:24 +00:00
|
|
|
path: `/:id/updatePrice`,
|
2018-06-28 13:27:29 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
Self.updatePrice = async(ctx, id, newPrice, options) => {
|
2021-07-29 15:20:57 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2018-06-28 13:27:29 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
2021-07-29 15:20:57 +00:00
|
|
|
const filter = {
|
2019-06-03 06:01:24 +00:00
|
|
|
include: {
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
2021-07-29 15:20:57 +00:00
|
|
|
fields: ['salesPersonFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 06:01:24 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
fields: ['id', 'clientFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-28 13:27:29 +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 isEditable = await models.Ticket.isEditable(ctx, sale.ticketFk, myOptions);
|
2019-06-03 06:01:24 +00:00
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-06-28 13:27:29 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, [id], myOptions);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
const oldPrice = sale.price;
|
2020-05-15 10:17:34 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2022-10-20 12:25:55 +00:00
|
|
|
|
2022-10-20 13:05:46 +00:00
|
|
|
// const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions);
|
|
|
|
// const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions);
|
|
|
|
// const workerDepartment = await models.WorkerDepartment.findById(userId);
|
|
|
|
// const usesMana = departments.find(department => department.id == workerDepartment.departmentFk);
|
2022-10-20 12:25:55 +00:00
|
|
|
|
2022-10-20 13:05:46 +00:00
|
|
|
const usesMana = await models.Sale.usesMana();
|
|
|
|
console.log(usesMana ? 'mana' : 'buyerDiscount');
|
2021-07-29 15:20:57 +00:00
|
|
|
const componentCode = usesMana ? 'mana' : 'buyerDiscount';
|
2021-09-29 06:27:18 +00:00
|
|
|
const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
const componentId = discount.id;
|
|
|
|
const componentValue = newPrice - sale.price;
|
2020-06-30 08:16:22 +00:00
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
const where = {
|
2019-05-29 10:41:26 +00:00
|
|
|
componentFk: componentId,
|
2019-06-03 06:01:24 +00:00
|
|
|
saleFk: id
|
|
|
|
};
|
2021-09-29 06:27:18 +00:00
|
|
|
const saleComponent = await models.SaleComponent.findOne({where}, myOptions);
|
2019-06-03 06:01:24 +00:00
|
|
|
if (saleComponent) {
|
2019-06-05 05:41:20 +00:00
|
|
|
await models.SaleComponent.updateAll(where, {
|
2019-06-03 06:01:24 +00:00
|
|
|
value: saleComponent.value + componentValue
|
2021-09-29 06:27:18 +00:00
|
|
|
}, myOptions);
|
2019-06-03 06:01:24 +00:00
|
|
|
} else {
|
2019-06-05 05:41:20 +00:00
|
|
|
await models.SaleComponent.create({
|
2019-06-03 06:01:24 +00:00
|
|
|
saleFk: id,
|
|
|
|
componentFk: componentId,
|
|
|
|
value: componentValue
|
2021-09-29 06:27:18 +00:00
|
|
|
}, myOptions);
|
2019-06-03 06:01:24 +00:00
|
|
|
}
|
2021-09-29 06:27:18 +00:00
|
|
|
await sale.updateAttributes({price: newPrice}, myOptions);
|
2019-05-29 10:41:26 +00:00
|
|
|
|
2021-11-24 08:25:24 +00:00
|
|
|
await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions);
|
|
|
|
await Self.rawSql('CALL vn.ticket_recalc(?)', [sale.ticketFk], myOptions);
|
2019-05-29 10:41:26 +00:00
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
const salesPerson = sale.ticket().client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
const message = $t('Changed sale price', {
|
|
|
|
ticketId: sale.ticket().id,
|
|
|
|
itemId: sale.itemFk,
|
|
|
|
concept: sale.concept,
|
|
|
|
quantity: sale.quantity,
|
|
|
|
oldPrice: oldPrice,
|
|
|
|
newPrice: newPrice,
|
|
|
|
ticketUrl: `${origin}/#!/ticket/${sale.ticket().id}/sale`,
|
|
|
|
itemUrl: `${origin}/#!/item/${sale.itemFk}/summary`
|
|
|
|
});
|
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();
|
2019-07-02 10:12:15 +00:00
|
|
|
|
|
|
|
return sale;
|
2019-06-03 06:01:24 +00:00
|
|
|
} catch (error) {
|
2021-09-29 06:27:18 +00:00
|
|
|
if (tx) await tx.rollback();
|
2019-06-03 06:01:24 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2018-06-28 13:27:29 +00:00
|
|
|
};
|