From 21ce57d21701d99a2446be7de0c1d791f9e061ea Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 17 Jan 2024 10:31:07 +0100 Subject: [PATCH] refs #6174 feat: update back to separate invoice and pdf --- .../back/methods/invoiceOut/makePdfList.js | 30 +++++++++++++ .../methods/invoiceOut/transferInvoice.js | 14 +++--- modules/invoiceOut/back/models/invoice-out.js | 1 + .../back/methods/ticket/invoiceTickets.js | 8 +--- .../methods/ticket/invoiceTicketsAndPdf.js | 44 +++++++++++++++++++ .../ticket/specs/invoiceTickets.spec.js | 2 +- modules/ticket/back/models/ticket-methods.js | 1 + 7 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 modules/invoiceOut/back/methods/invoiceOut/makePdfList.js create mode 100644 modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js diff --git a/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js b/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js new file mode 100644 index 0000000000..cf041ee170 --- /dev/null +++ b/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js @@ -0,0 +1,30 @@ + +module.exports = Self => { + Self.remoteMethodCtx('makePdfList', { + description: 'Handle a list of invoices to generate PDF and send it to client', + accessType: 'WRITE', + accepts: [ + { + arg: 'ids', + type: ['number'], + description: 'The invoice id', + required: true, + http: {source: 'path'} + }, { + arg: 'printerFk', + type: 'number', + description: 'The printer to print' + } + ], + http: { + path: '/:id/makePdfList', + verb: 'POST' + } + }); + + Self.makePdfList = async function(ctx, ids, printerFk, options) { + ids.forEach(async id => { + await Self.makePdfAndNotify(ctx, id, printerFk, options); + }); + }; +}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js index 3372c84ddf..8e234d7ccc 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js @@ -50,7 +50,6 @@ module.exports = Self => { Self.transferInvoice = async(ctx, options) => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; - let makePdf = false; const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args; let tx; if (typeof options == 'object') @@ -64,7 +63,6 @@ module.exports = Self => { if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; - makePdf = true; } try { const filterRef = {where: {refFk: refFk}}; @@ -88,16 +86,18 @@ module.exports = Self => { clonedTicketIds.push(clonedTicket.id); } - const invoiceCorrection = - {correctedFk: id, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk}; + const invoiceCorrection = { + correctedFk: id, + cplusRectificationTypeFk, + siiTypeInvoiceOutFk, + invoiceCorrectionTypeFk + }; const refundTicketIds = refundTickets.map(ticket => ticket.id); await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions); - const [invoiceId] = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, null, myOptions); - tx && await tx.commit(); + const [invoiceId] = await models.Ticket.invoiceTicketsAndPdf(ctx, clonedTicketIds, null, myOptions); - makePdf && await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null); return invoiceId; } catch (e) { if (tx) await tx.rollback(); diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index ca77c856f6..91f4883ade 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -13,6 +13,7 @@ module.exports = Self => { require('../methods/invoiceOut/createManualInvoice')(Self); require('../methods/invoiceOut/clientsToInvoice')(Self); require('../methods/invoiceOut/invoiceClient')(Self); + require('../methods/invoiceOut/makePdfList')(Self); require('../methods/invoiceOut/makePdfAndNotify')(Self); require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); diff --git a/modules/ticket/back/methods/ticket/invoiceTickets.js b/modules/ticket/back/methods/ticket/invoiceTickets.js index 162ddda036..53400e724f 100644 --- a/modules/ticket/back/methods/ticket/invoiceTickets.js +++ b/modules/ticket/back/methods/ticket/invoiceTickets.js @@ -32,7 +32,6 @@ module.exports = function(Self) { const models = Self.app.models; const date = Date.vnNew(); date.setHours(0, 0, 0, 0); - let makePdf = false; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; @@ -43,7 +42,6 @@ module.exports = function(Self) { if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; - makePdf = true; } let invoicesIds = []; @@ -78,15 +76,11 @@ module.exports = function(Self) { for (const ticketIds of ticketsByAddress) invoicesIds.push(await createInvoice(ctx, companyId, ticketIds, invoiceCorrection, myOptions)); - if (tx) await tx.commit(); + tx && await tx.commit(); } catch (e) { if (tx) await tx.rollback(); throw e; } - if (makePdf) { - for (const invoiceId of invoicesIds) - await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null); - } return invoicesIds; }; diff --git a/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js b/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js new file mode 100644 index 0000000000..3831854a72 --- /dev/null +++ b/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js @@ -0,0 +1,44 @@ + +module.exports = function(Self) { + Self.remoteMethodCtx('invoiceTicketsAndPdf', { + description: 'Make out an invoice from one or more tickets', + accessType: 'WRITE', + accepts: [ + { + arg: 'ticketsIds', + description: 'The tickets id', + type: ['number'], + required: true + }, + { + arg: 'invoiceCorrection', + description: 'The invoice correction', + type: 'object', + } + + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/invoiceTicketsAndPdf`, + verb: 'POST' + } + }); + + Self.invoiceTicketsAndPdf = async(ctx, ticketsIds, invoiceCorrection, options) => { + let invoiceIds = null; + + try { + invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options); + if (!invoiceIds) return []; + if (!options.transaction) + await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options); + return invoiceIds; + } catch (error) { + return []; + } + }; +}; + diff --git a/modules/ticket/back/methods/ticket/specs/invoiceTickets.spec.js b/modules/ticket/back/methods/ticket/specs/invoiceTickets.spec.js index 162dc066a6..7d511cfd25 100644 --- a/modules/ticket/back/methods/ticket/specs/invoiceTickets.spec.js +++ b/modules/ticket/back/methods/ticket/specs/invoiceTickets.spec.js @@ -102,7 +102,7 @@ describe('ticket invoiceTickets()', () => { const options = {transaction: tx}; const ticketsIds = [11]; - const invoicesIds = await models.Ticket.invoiceTickets(ctx, ticketsIds, null, options); + const invoicesIds = await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); expect(invoicesIds.length).toBeGreaterThan(0); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 14cb104be5..ce54959e72 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -42,5 +42,6 @@ module.exports = function(Self) { require('../methods/ticket/expeditionPalletLabel')(Self); require('../methods/ticket/saveSign')(Self); require('../methods/ticket/invoiceTickets')(Self); + require('../methods/ticket/invoiceTicketsAndPdf')(Self); require('../methods/ticket/docuwareDownload')(Self); };