module.exports = Self => { Self.remoteMethodCtx('clone', { description: 'Clone sales, services, and ticket packaging provided', accessType: 'WRITE', accepts: [ {arg: 'salesIds', type: ['number']}, {arg: 'servicesIds', type: ['number']}, {arg: 'ticketPackagingIds', type: ['number']}, {arg: 'withWarehouse', type: 'boolean', required: true}, {arg: 'negative', type: 'boolean'} ], returns: {type: ['object'], root: true}, http: {path: `/clone`, verb: 'POST'} }); Self.clone = async(ctx, salesIds, servicesIds, ticketPackagingIds, withWarehouse, negative, options) => { const models = Self.app.models; const myOptions = {}; let tx; const newTickets = []; if (typeof options === 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { let sales; let services; let ticketPackaging; if (salesIds?.length) { sales = await models.Sale.find({ where: {id: {inq: salesIds}}, include: { relation: 'components', scope: { fields: ['saleFk', 'componentFk', 'value'] } } }, myOptions); } if (servicesIds?.length) { services = await models.TicketService.find({ where: {id: {inq: servicesIds}} }, myOptions); } if (ticketPackagingIds?.length) { ticketPackaging = await models.TicketPackaging.find({ where: {id: {inq: ticketPackagingIds}} }, myOptions); } let ticketsIds = sales ? [...new Set(sales.map(sale => sale.ticketFk))] : [...new Set(services.map(service => service.ticketFk))]; const mappedTickets = new Map(); for (let ticketId of ticketsIds) { const newTicket = await createTicket( ctx, ticketId, withWarehouse, myOptions ); newTickets.push(newTicket); mappedTickets.set(ticketId, newTicket.id); } if (sales) { for (const sale of sales) { const newTicketId = mappedTickets.get(sale.ticketFk); const createdSale = await models.Sale.create({ ticketFk: newTicketId, 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 (services) { for (const service of services) { 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: newTicketId, ticketServiceTypeFk: service.ticketServiceTypeFk, }, myOptions); } } if (ticketPackaging) { for (const packaging of ticketPackaging) { const newTicketId = mappedTickets.get(packaging.ticketFk); await models.TicketPackaging.create({ ticketFk: newTicketId, packagingFk: packaging.packagingFk, quantity: negative ? -packaging.quantity : packaging.quantity }, myOptions); } } if (tx) await tx.commit(); return newTickets; } catch (e) { if (tx) await tx.rollback(); throw e; } async function createTicket(ctx, ticketId, withWarehouse, myOptions) { const models = Self.app.models; const now = Date.vnNew(); const ticket = await models.Ticket.findById(ticketId, null, myOptions); if (!ctx.args) ctx.args = {}; ctx.args.clientId = ticket.clientFk; ctx.args.shipped = now; ctx.args.landed = now; ctx.args.warehouseId = withWarehouse ? ticket.warehouseFk : null; ctx.args.companyId = ticket.companyFk; ctx.args.addressId = ticket.addressFk; const newTicket = await models.Ticket.new(ctx, myOptions); const existingRefund = await models.TicketRefund.findOne({ where: { originalTicketFk: ticketId, refundTicketFk: newTicket.id }, myOptions }); if (!existingRefund) { await models.TicketRefund.create({ originalTicketFk: ticketId, refundTicketFk: newTicket.id }, myOptions); } return newTicket; } }; };