refs #5914 feat(transferInvoiceOut): optimization
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2023-08-04 08:22:43 +02:00
parent 725fe674c0
commit 700ba4a7b1
3 changed files with 14 additions and 13 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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);
}
};