From 700ba4a7b1fce37f7575b3308ead22163687a14b Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 4 Aug 2023 08:22:43 +0200 Subject: [PATCH] refs #5914 feat(transferInvoiceOut): optimization --- .../methods/invoiceOut/transferInvoiceOut.js | 18 ++++++++---------- modules/ticket/back/methods/sale/clone.js | 2 +- .../back/methods/ticket/invoiceTickets.js | 7 +++++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js index fbb40061d..440ebe4d3 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js @@ -46,7 +46,8 @@ module.exports = Self => { Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => { const models = Self.app.models; - const myOptions = {}; + const myOptions = {userId: ctx.req.accessToken.userId}; + let tx; if (typeof options == 'object') @@ -65,20 +66,17 @@ module.exports = Self => { // Clone tickets const services = await models.TicketService.find(filter, myOptions); const servicesIds = services.map(service => service.id); - const salesFilter = {where: {ticketFk: {inq: ticketsIds}}}; - const sales = await models.Sale.find(salesFilter, myOptions); + const sales = await models.Sale.find({where: {ticketFk: {inq: ticketsIds}}}, myOptions); const clonedTickets = await models.Sale.clone(sales, servicesIds, null, false, false, myOptions); - console.log('cloned tickets', clonedTickets); + const clonedTicketIds = []; + // Update client for (const clonedTicket of clonedTickets) { - const ticket = await models.Ticket.findById(clonedTicket); - console.log(ticket); - await ticket.updateAttributes({clientFk: newClientFk}, myOptions); - // await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions); + await clonedTicket.updateAttribute('clientFk', newClientFk, myOptions); + clonedTicketIds.push(clonedTicket.id); } // Quick invoice - const clonedTicketIds = clonedTickets.map(clonedTicket => clonedTicket.id); const invoiceId = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions); // Insert InvoiceCorrection @@ -88,7 +86,7 @@ module.exports = Self => { cplusRectificationTypeFk: cplusRectificationId, cplusInvoiceType477Fk: cplusInvoiceType477Id, invoiceCorrectionType: invoiceCorrectionTypeId - }); + }, myOptions); if (tx) await tx.commit(); return invoiceId; diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index c168f4b3c..7d52fd0e6 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -92,7 +92,7 @@ module.exports = Self => { for (const updatedTicket of updatedTickets) await Self.rawSql(query, [updatedTicket.id], myOptions); if (tx) await tx.commit(); - return /* updatedTickets */updatedTickets.map(updatedTicket => updatedTicket.id); + return /* updatedTickets */updatedTickets; } } catch (e) { if (tx) await tx.rollback(); diff --git a/modules/ticket/back/methods/ticket/invoiceTickets.js b/modules/ticket/back/methods/ticket/invoiceTickets.js index ca1bf15fb..7baee133d 100644 --- a/modules/ticket/back/methods/ticket/invoiceTickets.js +++ b/modules/ticket/back/methods/ticket/invoiceTickets.js @@ -77,9 +77,11 @@ module.exports = function(Self) { if (tx) await tx.rollback(); throw e; } - - for (const invoiceId of invoicesIds) + console.log(invoicesIds, 'invoicesIds'); + for (const invoiceId of invoicesIds) { + console.log(await models.InvoiceOut.find()); await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null); + } return invoicesIds; }; @@ -98,6 +100,7 @@ module.exports = function(Self) { `, [ticketsIds], myOptions); const invoiceId = await models.Ticket.makeInvoice(ctx, 'R', companyId, Date.vnNew(), myOptions); + console.log(await models.InvoiceOut.find(null, myOptions)); invoicesIds.push(invoiceId); } };