module.exports = Self => { Self.remoteMethodCtx('refund', { description: 'Create refund tickets with sales and services if provided', accessType: 'WRITE', accepts: [ { arg: 'salesIds', type: ['number'], required: true }, { arg: 'servicesIds', type: ['number'] }, { arg: 'withWarehouse', type: 'boolean', required: true } ], returns: { type: ['number'], root: true }, http: { path: `/refund`, verb: 'post' } }); /* Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const refundAgencyMode = await models.AgencyMode.findOne({ include: { relation: 'zones', scope: { limit: 1, field: ['id', 'name'] } }, where: {code: 'refund'} }, myOptions); const refoundZoneId = refundAgencyMode.zones()[0].id; 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 now = Date.vnNew(); const [firstTicketId] = ticketsIds; const refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions); for (const sale of sales) { const createdSale = await models.Sale.create({ ticketFk: refundTicket.id, itemFk: sale.itemFk, 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) { await models.TicketService.create({ description: service.description, quantity: - service.quantity, price: service.price, taxClassFk: service.taxClassFk, ticketFk: refundTicket.id, ticketServiceTypeFk: service.ticketServiceTypeFk, }, myOptions); } } const query = `CALL vn.ticket_recalc(?, NULL)`; await Self.rawSql(query, [refundTicket.id], myOptions); if (tx) await tx.commit(); return refundTicket; } catch (e) { if (tx) await tx.rollback(); throw e; } }; */ Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const refundAgencyMode = await models.AgencyMode.findOne({ include: { relation: 'zones', scope: { limit: 1, field: ['id', 'name'] } }, where: {code: 'refund'} }, myOptions); const refoundZoneId = refundAgencyMode.zones()[0].id; const salesFilter = { where: {id: {inq: salesIds}}, include: { relation: 'components', scope: { fields: ['saleFk', 'componentFk', 'value'] } } }; const sales = await models.Sale.find(salesFilter, myOptions); const group = true; const isRefund = true; const refundTicket = await clone( sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions ); if (tx) await tx.commit(); return refundTicket; } catch (e) { if (tx) await tx.rollback(); throw e; } }; async function clone(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) { const models = Self.app.models; const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; const [firstTicketId] = ticketsIds; const now = Date.vnNew(); let refundTickets = []; let refundTicket; if (!group) { for (const ticketId of ticketsIds) { refundTicket = await createTicketRefund( ticketId, now, refundAgencyMode, refoundZoneId, null, myOptions ); refundTickets.push(refundTicket); } } else { refundTicket = await createTicketRefund( firstTicketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions ); } for (const sale of sales) { const createdSale = await models.Sale.create({ ticketFk: (group) ? refundTicket.id : sale.ticketFk, itemFk: sale.itemFk, quantity: (isRefund) ? - 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) { await models.TicketService.create({ description: service.description, quantity: (isRefund) ? - service.quantity : service.quantity, price: service.price, taxClassFk: service.taxClassFk, ticketFk: (group) ? refundTicket.id : service.ticketFk, ticketServiceTypeFk: service.ticketServiceTypeFk, }, myOptions); } } const query = `CALL vn.ticket_recalc(?, NULL)`; if (refundTickets.length > 0) { for (const refundTicket of refundTickets) await Self.rawSql(query, [refundTicket.id], myOptions); return refundTickets.map(refundTicket => refundTicket.id); } else { await Self.rawSql(query, [refundTicket.id], myOptions); return refundTicket; } } async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) { const models = Self.app.models; const filter = {include: {relation: 'address'}}; 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: refundAgencyMode.id, nickname: ticket.address().nickname, warehouseFk: withWarehouse ? ticket.warehouseFk : null, companyFk: ticket.companyFk, landed: now, zoneFk: refoundZoneId }, myOptions); await models.TicketRefund.create({ refundTicketFk: refundTicket.id, originalTicketFk: ticket.id, }, myOptions); return refundTicket; } };