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