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

98 lines
3.1 KiB
JavaScript
Raw Normal View History

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('refund', {
2022-04-22 09:45:14 +00:00
description: 'Create ticket refund with lines and services changing the sign to the quantites',
accessType: 'WRITE',
accepts: [{
arg: 'sales',
description: 'The sales',
type: ['object'],
2022-04-22 09:45:14 +00:00
required: false
},
{
2022-04-22 09:45:14 +00:00
arg: 'services',
type: ['object'],
required: false,
description: 'The services'
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/refund`,
verb: 'post'
}
});
2022-04-22 09:45:14 +00:00
Self.refund = async(ctx, sales, services, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const userId = ctx.req.accessToken.userId;
const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager');
const isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant');
const hasValidRole = isClaimManager || isSalesAssistant;
if (!hasValidRole)
throw new UserError(`You don't have privileges to create refund`);
2022-04-22 09:45:14 +00:00
const salesIds = [];
if (sales) {
for (let sale of sales)
salesIds.push(sale.id);
} else
salesIds.push(null);
const serevicesIds = [];
if (services) {
for (let service of services)
serevicesIds.push(service.id);
} else
serevicesIds.push(null);
2022-04-11 08:01:00 +00:00
const query = `
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
2022-04-11 08:01:00 +00:00
DROP TEMPORARY TABLE IF EXISTS tmp.ticketService;
CREATE TEMPORARY TABLE tmp.sale
2022-04-22 09:45:14 +00:00
SELECT s.id, s.itemFk, s.quantity, s.concept, s.price, s.discount, s.ticketFk
FROM sale s
2022-03-07 09:10:45 +00:00
WHERE s.id IN (?);
2022-04-22 09:45:14 +00:00
CREATE TEMPORARY TABLE tmp.ticketService
SELECT ts.description, ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk, ts.ticketFk
FROM ticketService ts
WHERE ts.id IN (?);
CALL vn.ticket_doRefund(@newTicket);
DROP TEMPORARY TABLE tmp.sale;
DROP TEMPORARY TABLE tmp.ticketService;`;
2022-04-22 09:45:14 +00:00
await Self.rawSql(query, [salesIds, serevicesIds], myOptions);
const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions);
2022-04-22 09:45:14 +00:00
const newTicketId = newTicket.id;
2021-12-21 14:06:57 +00:00
if (tx) await tx.commit();
2022-04-22 09:45:14 +00:00
return newTicketId;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};