module.exports = Self => { Self.remoteMethodCtx('regularizeClaim', { description: `Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company`, accessType: 'WRITE', accepts: [{ arg: 'id', type: 'number', description: 'The claim id', http: {source: 'path'} }], returns: { type: ['Object'], root: true }, http: { path: `/:id/regularizeClaim`, verb: 'POST' } }); Self.regularizeClaim = async(ctx, claimFk, options) => { const models = Self.app.models; const $t = ctx.req.__; // $translate const resolvedState = 3; let tx; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const claimEnds = await models.ClaimEnd.find({ include: { relation: 'claimDestination', fields: ['addressFk'] }, where: {claimFk: claimFk} }, myOptions); for (let claimEnd of claimEnds) { const destination = claimEnd.claimDestination(); const sale = await getSale(claimEnd.saleFk, myOptions); const addressId = destination && destination.addressFk; let address; if (addressId) address = await models.Address.findById(addressId, null, myOptions); const salesPerson = sale.ticket().client().salesPersonUser(); if (salesPerson) { const nickname = address && address.nickname || destination.description; const origin = ctx.req.headers.origin; const message = $t('Sent units from ticket', { quantity: sale.quantity, concept: sale.concept, itemId: sale.itemFk, ticketId: sale.ticketFk, nickname: nickname, ticketUrl: `${origin}/#!/ticket/${sale.ticketFk}/sale`, itemUrl: `${origin}/#!/item/${sale.itemFk}/summary` }); await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); } if (!address) continue; let ticketFk = await getTicketId({ addressFk: addressId, companyFk: sale.ticket().companyFk, warehouseFk: sale.ticket().warehouseFk }, myOptions); if (!ticketFk) { ctx.args = { clientId: address.clientFk, warehouseId: sale.ticket().warehouseFk, companyId: sale.ticket().companyFk, addressId: addressId }; ticketFk = await createTicket(ctx, myOptions); } await models.Sale.create({ ticketFk: ticketFk, itemFk: sale.itemFk, concept: sale.concept, quantity: -sale.quantity, price: sale.price, discount: 100 }, myOptions); } let claim = await Self.findById(claimFk, null, myOptions); claim = await claim.updateAttributes({ claimStateFk: resolvedState }, myOptions); if (tx) await tx.commit(); return claim; } catch (e) { if (tx) await tx.rollback(); throw e; } }; async function getSale(saleFk, options) { return await Self.app.models.Sale.findOne({ include: [ { relation: 'ticket', scope: { fields: ['clientFk', 'warehouseFk', 'companyFk'], include: { relation: 'client', scope: { include: { relation: 'salesPersonUser', scope: { fields: ['id', 'name'] } } } } } }], where: {id: saleFk} }, options); } async function getTicketId(params, options) { const minDate = new Date(); minDate.setHours(0, 0, 0, 0); const maxDate = new Date(); maxDate.setHours(23, 59, 59, 59); let ticket = await Self.app.models.Ticket.findOne({ where: { addressFk: params.addressFk, companyFk: params.companyFk, warehouseFk: params.warehouseFk, shipped: {between: [minDate, maxDate]}, landed: {between: [minDate, maxDate]} } }, options); return ticket && ticket.id; } async function createTicket(ctx, options) { ctx.args.shipped = new Date(); ctx.args.landed = new Date(); ctx.args.agencyModeId = null; ctx.args.routeId = null; const ticket = await Self.app.models.Ticket.new(ctx, options); return ticket.id; } };