2019-11-14 13:19:39 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
2019-11-21 11:00:56 +00:00
|
|
|
Self.remoteMethodCtx('recalculatePrice', {
|
2021-11-30 12:47:16 +00:00
|
|
|
description: 'Calculates the price of sales and its components',
|
2019-11-14 13:19:39 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2021-11-30 12:47:16 +00:00
|
|
|
arg: 'sales',
|
|
|
|
description: 'The sales',
|
|
|
|
type: ['object'],
|
2019-11-14 13:19:39 +00:00
|
|
|
required: true,
|
2021-11-30 12:47:16 +00:00
|
|
|
http: {source: 'body'}
|
2019-11-14 13:19:39 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2021-12-01 10:39:48 +00:00
|
|
|
type: 'number',
|
2019-11-14 13:19:39 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2021-11-30 12:47:16 +00:00
|
|
|
path: `/recalculatePrice`,
|
2019-11-14 13:19:39 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-30 12:47:16 +00:00
|
|
|
Self.recalculatePrice = async(ctx, sales, options) => {
|
2019-11-14 13:19:39 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2019-11-14 13:19:39 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2019-11-14 13:19:39 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-11-30 12:47:16 +00:00
|
|
|
const salesIds = [];
|
|
|
|
const params = [];
|
|
|
|
sales.forEach(sale => {
|
|
|
|
salesIds.push(sale.id);
|
|
|
|
params.push('?');
|
|
|
|
});
|
2021-09-29 06:27:18 +00:00
|
|
|
|
2021-11-30 12:47:16 +00:00
|
|
|
const isEditable = await models.Ticket.isEditable(ctx, sales[0].ticketFk, myOptions);
|
2021-09-29 06:27:18 +00:00
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2019-11-14 13:19:39 +00:00
|
|
|
|
2021-11-30 12:47:16 +00:00
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, sales, myOptions);
|
2021-09-29 06:27:18 +00:00
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-11-30 12:47:16 +00:00
|
|
|
const paramsString = params.join();
|
|
|
|
|
|
|
|
const query = `
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.recalculateSales;
|
|
|
|
CREATE TEMPORARY TABLE tmp.recalculateSales
|
|
|
|
SELECT s.id
|
|
|
|
FROM sale s
|
|
|
|
WHERE s.id IN (${paramsString});
|
2021-11-30 14:13:20 +00:00
|
|
|
CALL vn.sale_recalcComponent(null);
|
2021-11-30 12:47:16 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.recalculateSales;`;
|
|
|
|
|
|
|
|
const recalculation = await Self.rawSql(query, salesIds, myOptions);
|
2021-09-29 06:27:18 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return recalculation;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-11-14 13:19:39 +00:00
|
|
|
};
|
|
|
|
};
|