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-05-29 10:41:26 +00:00
|
|
|
Self.remoteMethod('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: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2019-06-03 06:01:24 +00:00
|
|
|
path: `/:id/updatePrice`,
|
2018-06-28 13:27:29 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
Self.updatePrice = async(id, newPrice) => {
|
2019-06-05 05:41:20 +00:00
|
|
|
let models = Self.app.models;
|
2019-06-13 07:21:36 +00:00
|
|
|
let tx = await Self.beginTransaction({});
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
try {
|
2019-06-13 07:21:36 +00:00
|
|
|
let options = {transaction: tx};
|
2018-06-28 13:27:29 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
let filter = {
|
|
|
|
fields: ['id', 'ticketFk', 'price'],
|
|
|
|
include: {
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['salesPersonFk']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fields: ['id', 'clientFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-06-05 05:41:20 +00:00
|
|
|
let sale = await models.Sale.findById(id, filter, options);
|
2018-06-28 13:27:29 +00:00
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
let isEditable = await models.Ticket.isEditable(sale.ticketFk);
|
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
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
let salesPerson = sale.ticket().client().salesPersonFk;
|
2019-06-05 05:41:20 +00:00
|
|
|
let usesMana = await models.WorkerMana.findOne({where: {workerFk: salesPerson}, fields: 'amount'}, options);
|
2019-06-03 06:01:24 +00:00
|
|
|
let componentCode = usesMana ? 'mana' : 'buyerDiscount';
|
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
let discount = await models.ComponentRate.findOne({where: {code: componentCode}}, options);
|
2019-06-03 06:01:24 +00:00
|
|
|
let componentId = discount.id;
|
|
|
|
let componentValue = newPrice - sale.price;
|
2018-06-28 13:27:29 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
let where = {
|
2019-05-29 10:41:26 +00:00
|
|
|
componentFk: componentId,
|
2019-06-03 06:01:24 +00:00
|
|
|
saleFk: id
|
|
|
|
};
|
2019-06-05 05:41:20 +00:00
|
|
|
let saleComponent = await models.SaleComponent.findOne({where}, options);
|
2018-06-28 13:27:29 +00:00
|
|
|
|
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
|
|
|
|
}, options);
|
|
|
|
} 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
|
|
|
|
}, options);
|
|
|
|
}
|
2018-08-14 10:48:12 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
await sale.updateAttributes({price: newPrice}, options);
|
2019-05-29 10:41:26 +00:00
|
|
|
|
2019-06-03 06:01:24 +00:00
|
|
|
query = `call vn.manaSpellersRequery(?)`;
|
|
|
|
await Self.rawSql(query, [salesPerson], options);
|
2019-05-29 10:41:26 +00:00
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
await tx.commit();
|
2019-06-03 06:01:24 +00:00
|
|
|
} catch (error) {
|
2019-06-13 07:21:36 +00:00
|
|
|
await tx.rollback();
|
2019-06-03 06:01:24 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2018-06-28 13:27:29 +00:00
|
|
|
};
|