ref #5914 fix ticketrefund
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-10-26 09:20:25 +02:00
parent da2bc416f5
commit 3a1227bdc2
1 changed files with 19 additions and 14 deletions

View File

@ -3,7 +3,7 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx; let tx;
const refundTickets = []; const newTickets = [];
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -31,21 +31,22 @@ module.exports = Self => {
if (group) ticketsIds = [ticketsIds[0]]; if (group) ticketsIds = [ticketsIds[0]];
for (let ticketId of ticketsIds) { for (let ticketId of ticketsIds) {
const ticketRefund = await createTicketRefund( const newTicket = await createTicket(
ctx, ctx,
ticketId, ticketId,
withWarehouse, withWarehouse,
negative,
myOptions myOptions
); );
refundTickets.push(ticketRefund); newTickets.push(newTicket);
mappedTickets.set(ticketId, ticketRefund.id); mappedTickets.set(ticketId, newTicket.id);
} }
for (const sale of sales) { for (const sale of sales) {
const refundTicketId = mappedTickets.get(sale.ticketFk); const newTicketId = mappedTickets.get(sale.ticketFk);
const createdSale = await models.Sale.create({ const createdSale = await models.Sale.create({
ticketFk: refundTicketId, ticketFk: newTicketId,
itemFk: sale.itemFk, itemFk: sale.itemFk,
quantity: negative ? - sale.quantity : sale.quantity, quantity: negative ? - sale.quantity : sale.quantity,
concept: sale.concept, concept: sale.concept,
@ -67,14 +68,14 @@ module.exports = Self => {
const services = await models.TicketService.find(servicesFilter, myOptions); const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) { for (const service of services) {
const refundTicketId = mappedTickets.get(service.ticketFk); const newTicketId = mappedTickets.get(service.ticketFk);
await models.TicketService.create({ await models.TicketService.create({
description: service.description, description: service.description,
quantity: negative ? - service.quantity : service.quantity, quantity: negative ? - service.quantity : service.quantity,
price: service.price, price: service.price,
taxClassFk: service.taxClassFk, taxClassFk: service.taxClassFk,
ticketFk: refundTicketId, ticketFk: newTicketId,
ticketServiceTypeFk: service.ticketServiceTypeFk, ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions); }, myOptions);
} }
@ -82,16 +83,17 @@ module.exports = Self => {
if (tx) await tx.commit(); if (tx) await tx.commit();
return refundTickets; return newTickets;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;
} }
async function createTicketRefund( async function createTicket(
ctx, ctx,
ticketId, ticketId,
withWarehouse, withWarehouse,
negative,
myOptions myOptions
) { ) {
const models = Self.app.models; const models = Self.app.models;
@ -106,10 +108,13 @@ module.exports = Self => {
ctx.args.addressId = ticket.addressFk; ctx.args.addressId = ticket.addressFk;
const newTicket = await models.Ticket.new(ctx, myOptions); const newTicket = await models.Ticket.new(ctx, myOptions);
if (negative) {
await models.TicketRefund.create({ await models.TicketRefund.create({
originalTicketFk: ticketId, originalTicketFk: ticketId,
refundTicketFk: newTicket.id refundTicketFk: newTicket.id
}, myOptions); }, myOptions);
}
return newTicket; return newTicket;
} }