114 lines
3.7 KiB
JavaScript
114 lines
3.7 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('refund', {
|
|
description: 'Create ticket with the selected lines changing the sign to the quantites',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'sales',
|
|
description: 'The sales',
|
|
type: ['object'],
|
|
required: false
|
|
},
|
|
{
|
|
arg: 'ticketId',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id'
|
|
}],
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/refund`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.refund = async(ctx, sales, ticketId, 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 salesIds = [];
|
|
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`);
|
|
|
|
if (sales) {
|
|
for (let sale of sales)
|
|
salesIds.push(sale.id);
|
|
|
|
const query = `
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
|
|
FROM sale s
|
|
WHERE s.id IN (?);
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticketService(
|
|
description VARCHAR(50),
|
|
quantity DECIMAL (10,2),
|
|
price DECIMAL (10,2),
|
|
taxClassFk INT,
|
|
ticketServiceTypeFk INT
|
|
);
|
|
|
|
CALL vn.ticket_doRefund(?, @newTicket);
|
|
|
|
DROP TEMPORARY TABLE tmp.sale;
|
|
DROP TEMPORARY TABLE tmp.ticketService;`;
|
|
|
|
await Self.rawSql(query, [salesIds, ticketId], myOptions);
|
|
} else {
|
|
const query = `
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticketService;
|
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
|
|
FROM sale s
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
WHERE t.id IN (?);
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticketService
|
|
SELECT ts.description, - ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk
|
|
FROM ticketService ts
|
|
WHERE ts.ticketFk IN (?);
|
|
|
|
CALL vn.ticket_doRefund(?, @newTicket);
|
|
|
|
DROP TEMPORARY TABLE tmp.sale;
|
|
DROP TEMPORARY TABLE tmp.ticketService;`;
|
|
|
|
await Self.rawSql(query, [ticketId, ticketId, ticketId], myOptions);
|
|
}
|
|
|
|
const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions);
|
|
ticketId = newTicket.id;
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return ticketId;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|