126 lines
4.3 KiB
JavaScript
126 lines
4.3 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('updatePrice', {
|
|
description: 'Changes the price of a sale',
|
|
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) => {
|
|
const $t = ctx.req.__; // $translate
|
|
const models = Self.app.models;
|
|
const tx = await Self.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
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, options);
|
|
|
|
const isEditable = await models.Ticket.isEditable(ctx, sale.ticketFk);
|
|
if (!isEditable)
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, [id]);
|
|
|
|
if (!canEditSale)
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
const oldPrice = sale.price;
|
|
const userId = ctx.req.accessToken.userId;
|
|
const usesMana = await models.WorkerMana.findOne({where: {workerFk: userId}, fields: 'amount'}, options);
|
|
const componentCode = usesMana ? 'mana' : 'buyerDiscount';
|
|
const discount = await models.Component.findOne({where: {code: componentCode}}, options);
|
|
const componentId = discount.id;
|
|
const componentValue = newPrice - sale.price;
|
|
|
|
const where = {
|
|
componentFk: componentId,
|
|
saleFk: id
|
|
};
|
|
const saleComponent = await models.SaleComponent.findOne({where}, options);
|
|
|
|
if (saleComponent) {
|
|
await models.SaleComponent.updateAll(where, {
|
|
value: saleComponent.value + componentValue
|
|
}, options);
|
|
} else {
|
|
await models.SaleComponent.create({
|
|
saleFk: id,
|
|
componentFk: componentId,
|
|
value: componentValue
|
|
}, options);
|
|
}
|
|
await sale.updateAttributes({price: newPrice}, options);
|
|
|
|
query = `CALL vn.manaSpellersRequery(?)`;
|
|
await Self.rawSql(query, [userId], options);
|
|
|
|
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`
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
|
|
}
|
|
|
|
await tx.commit();
|
|
|
|
return sale;
|
|
} catch (error) {
|
|
await tx.rollback();
|
|
throw error;
|
|
}
|
|
};
|
|
};
|