refs #6174 feat first approach

This commit is contained in:
Javier Segarra 2024-01-15 10:52:02 +01:00
parent 199656afb8
commit 831d358e41
2 changed files with 7 additions and 5 deletions

View File

@ -50,6 +50,7 @@ module.exports = Self => {
Self.transferInvoice = async(ctx, options) => { Self.transferInvoice = async(ctx, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let makePdf = false;
const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args; const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args;
let tx; let tx;
if (typeof options == 'object') if (typeof options == 'object')
@ -63,6 +64,7 @@ module.exports = Self => {
if (!myOptions.transaction) { if (!myOptions.transaction) {
tx = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
myOptions.transaction = tx; myOptions.transaction = tx;
makePdf = true;
} }
try { try {
const filterRef = {where: {refFk: refFk}}; const filterRef = {where: {refFk: refFk}};
@ -93,11 +95,9 @@ module.exports = Self => {
await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions); await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions);
const [invoiceId] = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, null, myOptions); const [invoiceId] = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, null, myOptions);
if (tx) { tx && await tx.commit();
await tx.commit();
await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
}
makePdf && await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
return invoiceId; return invoiceId;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -32,6 +32,7 @@ module.exports = function(Self) {
const models = Self.app.models; const models = Self.app.models;
const date = Date.vnNew(); const date = Date.vnNew();
date.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0);
let makePdf = false;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
@ -42,6 +43,7 @@ module.exports = function(Self) {
if (!myOptions.transaction) { if (!myOptions.transaction) {
tx = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
myOptions.transaction = tx; myOptions.transaction = tx;
makePdf = true;
} }
let invoicesIds = []; let invoicesIds = [];
@ -81,7 +83,7 @@ module.exports = function(Self) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;
} }
if (tx) { if (makePdf) {
for (const invoiceId of invoicesIds) for (const invoiceId of invoicesIds)
await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null); await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
} }