diff --git a/modules/invoiceOut/back/methods/invoiceOut/createManualInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/createManualInvoice.js index 561f559fd..297afa8e8 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/createManualInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/createManualInvoice.js @@ -65,6 +65,7 @@ module.exports = Self => { let clientId = args.clientFk; let maxShipped = args.maxShipped; let companyId; + let newInvoice; let query; try { if (ticketId) { @@ -137,18 +138,18 @@ module.exports = Self => { ], myOptions); } - const [newInvoice] = await Self.rawSql(`SELECT @newInvoiceId id`, null, myOptions); + [newInvoice] = await Self.rawSql(`SELECT @newInvoiceId id`, null, myOptions); if (tx) await tx.commit(); - - if (newInvoice.id) - await Self.createPdf(ctx, newInvoice.id); - - return newInvoice; } catch (e) { if (tx) await tx.rollback(); throw e; } + + if (newInvoice.id) + await Self.createPdf(ctx, newInvoice.id); + + return newInvoice; }; async function isInvoiceable(clientId, options) { diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 5989e941b..6b901872e 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -43,8 +43,6 @@ module.exports = Self => { Self.globalInvoicing = async(ctx, options) => { const args = ctx.args; - const invoicesIds = []; - const failedClients = []; let tx; const myOptions = {}; @@ -57,6 +55,8 @@ module.exports = Self => { myOptions.transaction = tx; } + const invoicesIds = []; + const failedClients = []; let query; try { query = ` @@ -155,16 +155,16 @@ module.exports = Self => { await notifyFailures(ctx, failedClients, myOptions); if (tx) await tx.commit(); - - // Print invoices PDF - for (let invoiceId of invoicesIds) - await Self.createPdf(ctx, invoiceId); - - return invoicesIds; } catch (e) { if (tx) await tx.rollback(); throw e; } + + // Print invoices PDF + for (let invoiceId of invoicesIds) + await Self.createPdf(ctx, invoiceId); + + return invoicesIds; }; async function getNegativeBase(options) { diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index 0f0e5b62a..befc75ffc 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -38,6 +38,8 @@ module.exports = function(Self) { myOptions.transaction = tx; } + let serial; + let invoiceId; try { const tickets = await models.Ticket.find({ where: { @@ -68,7 +70,7 @@ module.exports = function(Self) { companyId, 'R' ], myOptions); - const serial = result.serial; + serial = result.serial; await Self.rawSql(` DROP TEMPORARY TABLE IF EXISTS ticketToInvoice; @@ -83,7 +85,7 @@ module.exports = function(Self) { const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], myOptions); - const invoiceId = resultInvoice.id; + invoiceId = resultInvoice.id; for (let ticket of tickets) { const ticketInvoice = await models.Ticket.findById(ticket.id, { @@ -104,14 +106,14 @@ module.exports = function(Self) { await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], myOptions); if (tx) await tx.commit(); - - if (serial != 'R' && invoiceId) - await models.InvoiceOut.createPdf(ctx, invoiceId); - - return {invoiceFk: invoiceId, serial: serial}; } catch (e) { if (tx) await tx.rollback(); throw e; } + + if (serial != 'R' && invoiceId) + await models.InvoiceOut.createPdf(ctx, invoiceId); + + return {invoiceFk: invoiceId, serial: serial}; }; };