module.exports = Self => { Self.clone = async(salesIds, servicesIds, withWarehouse, group, negative, options) => { const models = Self.app.models; const myOptions = {}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const salesFilter = { where: {id: {inq: salesIds}}, include: { relation: 'components', scope: { fields: ['saleFk', 'componentFk', 'value'] } } }; const sales = await models.Sale.find(salesFilter, myOptions); const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; const refundTickets = []; const mappedTickets = new Map(); const now = Date.vnNew(); const [firstTicketId] = ticketsIds; if (group) { await createTicketRefund( firstTicketId, withWarehouse, refundTickets, mappedTickets, now, myOptions ); } else { for (let ticketId of ticketsIds) { await createTicketRefund( ticketId, withWarehouse, refundTickets, mappedTickets, now, myOptions ); } } for (const sale of sales) { const refundTicketId = await getTicketRefundId(group, sale.ticketFk, refundTickets, mappedTickets); const createdSale = await models.Sale.create({ ticketFk: refundTicketId, itemFk: sale.itemFk, quantity: negative ? - sale.quantity : sale.quantity, concept: sale.concept, price: sale.price, discount: sale.discount, }, myOptions); const components = sale.components(); for (const component of components) component.saleFk = createdSale.id; await models.SaleComponent.create(components, myOptions); } if (servicesIds && servicesIds.length > 0) { const servicesFilter = { where: {id: {inq: servicesIds}} }; const services = await models.TicketService.find(servicesFilter, myOptions); for (const service of services) { const refundTicketId = await getTicketRefundId(group, service.ticketFk, refundTickets, mappedTickets); await models.TicketService.create({ description: service.description, quantity: negative ? - service.quantity : service.quantity, price: service.price, taxClassFk: service.taxClassFk, ticketFk: refundTicketId, ticketServiceTypeFk: service.ticketServiceTypeFk, }, myOptions); } } if (tx) await tx.commit(); return refundTickets; } catch (e) { if (tx) await tx.rollback(); throw e; } }; async function createTicketRefund( ticketId, withWarehouse, refundTickets, mappedTickets, now, myOptions ) { const models = Self.app.models; const filter = { include: [ { relation: 'address' }, { relation: 'agencyMode' }, { relation: 'zone' } ] }; const ticket = await models.Ticket.findById(ticketId, filter, myOptions); const refundTicket = await models.Ticket.create({ clientFk: ticket.clientFk, shipped: now, addressFk: ticket.address().id, agencyModeFk: ticket.agencyMode().id, nickname: ticket.address().nickname, warehouseFk: withWarehouse ? ticket.warehouseFk : null, companyFk: ticket.companyFk, zonePrice: ticket.zonePrice, zoneBonus: ticket.zoneBonus, weight: ticket.weight, landed: now, zoneFk: ticket.zone().id, }, myOptions); refundTickets.push(refundTicket); mappedTickets.set(ticketId, refundTicket.id); } async function getTicketRefundId(group, ticketId, refundTickets, mappedTickets) { if (group) { const [firstRefundTicket] = refundTickets; return firstRefundTicket.id; } else return mappedTickets.get(ticketId); } };