2021-12-15 14:24:45 +00:00
|
|
|
module.exports = Self => {
|
2021-12-21 13:43:53 +00:00
|
|
|
Self.remoteMethodCtx('payBack', {
|
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'],
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'ticketId',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The ticket id'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'number',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2021-12-21 13:43:53 +00:00
|
|
|
path: `/payBack`,
|
2021-12-15 14:24:45 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-21 13:43:53 +00:00
|
|
|
Self.payBack = 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 = [];
|
|
|
|
const params = [];
|
|
|
|
sales.forEach(sale => {
|
|
|
|
salesIds.push(sale.id);
|
|
|
|
params.push('?');
|
|
|
|
});
|
|
|
|
|
|
|
|
const paramsString = params.join();
|
|
|
|
|
|
|
|
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 (${paramsString});
|
|
|
|
CALL vn.ticket_doRefund(${ticketId}, @newTicket);
|
|
|
|
DROP TEMPORARY TABLE tmp.sale;`;
|
|
|
|
|
|
|
|
await Self.rawSql(query, salesIds, myOptions);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|