2022-01-21 10:40:48 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2021-12-15 14:24:45 +00:00
|
|
|
module.exports = Self => {
|
2022-04-04 11:26:53 +00:00
|
|
|
Self.remoteMethodCtx('refund', {
|
2021-12-15 14:24:45 +00:00
|
|
|
description: 'Create ticket with the selected lines changing the sign to the quantites',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'sales',
|
|
|
|
description: 'The sales',
|
|
|
|
type: ['object'],
|
2022-04-11 08:01:00 +00:00
|
|
|
required: true
|
2021-12-15 14:24:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'ticketId',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The ticket id'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'number',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2022-04-04 11:26:53 +00:00
|
|
|
path: `/refund`,
|
2021-12-15 14:24:45 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
Self.refund = async(ctx, sales, ticketId, options) => {
|
2021-12-15 14:24:45 +00:00
|
|
|
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 = [];
|
2022-01-21 10:40:48 +00:00
|
|
|
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');
|
2022-01-25 14:09:29 +00:00
|
|
|
const hasValidRole = isClaimManager || isSalesAssistant;
|
2022-01-21 10:40:48 +00:00
|
|
|
|
2022-01-25 14:09:29 +00:00
|
|
|
if (!hasValidRole)
|
2022-04-04 11:26:53 +00:00
|
|
|
throw new UserError(`You don't have privileges to create refund`);
|
2022-01-21 10:40:48 +00:00
|
|
|
|
2022-04-11 08:01:00 +00:00
|
|
|
for (let sale of sales)
|
|
|
|
salesIds.push(sale.id);
|
2021-12-15 14:24:45 +00:00
|
|
|
|
2022-04-11 08:01:00 +00:00
|
|
|
const query = `
|
2021-12-15 14:24:45 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
2022-04-11 08:01:00 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticketService;
|
2022-04-04 11:26:53 +00:00
|
|
|
|
2021-12-15 14:24:45 +00:00
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
|
|
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
|
|
|
|
FROM sale s
|
2022-03-07 09:10:45 +00:00
|
|
|
WHERE s.id IN (?);
|
2022-04-04 11:26:53 +00:00
|
|
|
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticketService(
|
|
|
|
description VARCHAR(50),
|
|
|
|
quantity DECIMAL (10,2),
|
|
|
|
price DECIMAL (10,2),
|
|
|
|
taxClassFk INT,
|
|
|
|
ticketServiceTypeFk INT
|
|
|
|
);
|
2022-04-11 08:01:00 +00:00
|
|
|
|
2022-03-07 09:10:45 +00:00
|
|
|
CALL vn.ticket_doRefund(?, @newTicket);
|
2021-12-15 14:24:45 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.sale;
|
|
|
|
DROP TEMPORARY TABLE tmp.ticketService;`;
|
|
|
|
|
2022-04-11 08:01:00 +00:00
|
|
|
await Self.rawSql(query, [salesIds, ticketId], myOptions);
|
2022-04-04 11:26:53 +00:00
|
|
|
|
2021-12-15 14:24:45 +00:00
|
|
|
const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions);
|
|
|
|
ticketId = newTicket.id;
|
2021-12-21 14:06:57 +00:00
|
|
|
|
2021-12-15 14:24:45 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return ticketId;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|