salix/modules/ticket/back/methods/sale/updatePrice.js

138 lines
4.7 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2019-07-22 11:14:00 +00:00
Self.remoteMethodCtx('updatePrice', {
description: 'Changes the price of a sale',
2019-05-22 10:35:24 +00:00
accessType: 'WRITE',
accepts: [
{
arg: 'id',
description: 'The sale id',
type: 'number',
required: true,
http: {source: 'path'}
}, {
arg: 'newPrice',
description: 'The new price',
type: 'number',
required: true
}
],
returns: {
type: 'number',
root: true
},
http: {
path: `/:id/updatePrice`,
verb: 'post'
}
});
Self.updatePrice = async(ctx, id, newPrice, options) => {
const $t = ctx.req.__; // $translate
const models = Self.app.models;
2023-06-01 06:32:06 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
2018-08-14 10:48:12 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const filter = {
include: {
relation: 'ticket',
scope: {
include: {
relation: 'client',
scope: {
fields: ['salesPersonFk'],
include: {
relation: 'salesPersonUser',
scope: {
fields: ['id', 'name']
}
}
}
},
fields: ['id', 'clientFk']
}
}
};
const sale = await models.Sale.findById(id, filter, myOptions);
await models.Sale.canEdit(ctx, [id], myOptions);
const oldPrice = sale.price;
2020-05-15 10:17:34 +00:00
const userId = ctx.req.accessToken.userId;
2022-10-21 06:00:24 +00:00
const usesMana = await models.Sale.usesMana(ctx, myOptions);
const componentCode = usesMana ? 'mana' : 'buyerDiscount';
const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions);
const componentId = discount.id;
const componentValue = newPrice - sale.price;
2020-06-30 08:16:22 +00:00
const where = {
2019-05-29 10:41:26 +00:00
componentFk: componentId,
saleFk: id
};
const saleComponent = await models.SaleComponent.findOne({where}, myOptions);
if (saleComponent) {
2019-06-05 05:41:20 +00:00
await models.SaleComponent.updateAll(where, {
value: saleComponent.value + componentValue
}, myOptions);
} else {
2019-06-05 05:41:20 +00:00
await models.SaleComponent.create({
saleFk: id,
componentFk: componentId,
value: componentValue
}, myOptions);
}
2024-10-25 05:22:36 +00:00
const [priceFixed] = await Self.rawSql(`
SELECT SUM(value) value
FROM sale s
JOIN saleComponent sc ON sc.saleFk = s.id
JOIN component c ON c.id = sc.componentFk
JOIN componentType ct ON ct.id = c.typeFk
WHERE ct.isBase
AND s.id = ?
`, [id], myOptions);
await sale.updateAttributes({
price: newPrice,
priceFixed: priceFixed.value
}, myOptions);
2019-05-29 10:41:26 +00:00
await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions);
2023-06-29 08:39:07 +00:00
await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [sale.ticketFk], myOptions);
2019-05-29 10:41:26 +00:00
const salesPerson = sale.ticket().client().salesPersonUser();
if (salesPerson) {
const url = await Self.app.models.Url.getUrl();
const message = $t('Changed sale price', {
ticketId: sale.ticket().id,
itemId: sale.itemFk,
concept: sale.concept,
quantity: sale.quantity,
oldPrice: oldPrice,
newPrice: newPrice,
2023-10-16 16:24:58 +00:00
ticketUrl: `${url}ticket/${sale.ticket().id}/sale`,
itemUrl: `${url}item/${sale.itemFk}/summary`
});
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
}
if (tx) await tx.commit();
return sale;
} catch (error) {
if (tx) await tx.rollback();
throw error;
}
};
};