diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 2afe5bab5..5870dcb4f 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -295,5 +295,6 @@ "Ticket does not exist": "Este ticket no existe", "Ticket is already signed": "Este ticket ya ha sido firmado", "Fecha fuera de rango": "Fecha fuera de rango", - "Error while generating PDF": "Error while generating PDF" + "Error while generating PDF": "Error al generar PDF", + "Error when sending mail to client": "Error al enviar el correo al cliente" } \ No newline at end of file diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index ff814fea9..5d61bfcd0 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -131,9 +131,9 @@ module.exports = Self => { if (tx) await tx.rollback(); throw e; } - + try { - await Self.makePdf(invoiceId); + await Self.makePdf(invoiceId, myOptions); } catch (err) { console.error(err); throw new UserError('Error while generating PDF', 'pdfError'); diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js index 9a0574dba..e26a0b3f1 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js @@ -23,7 +23,7 @@ describe('InvoiceOut invoiceClient()', () => { const ctx = {req: activeCtx}; it('should make a global invoicing', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); + spyOn(models.InvoiceOut, 'makePdf').and.returnValue(new Promise(resolve => resolve(true))); spyOn(models.InvoiceOut, 'invoiceEmail'); const tx = await models.InvoiceOut.beginTransaction({}); diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index bfef57f58..027ec2fb2 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -23,9 +23,19 @@ module.exports = Self => { require('../methods/invoiceOut/negativeBasesCsv')(Self); Self.makePdf = async function(id, options) { - const fields = ['id', 'hasPdf', 'ref', 'issued']; - const invoiceOut = await Self.findById(id, {fields}, options); + let tx; + const myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + const fields = ['id', 'hasPdf', 'ref', 'issued']; + const invoiceOut = await Self.findById(id, {fields}, myOptions); const invoiceReport = new print.Report('invoice', { reference: invoiceOut.ref }); @@ -42,7 +52,7 @@ module.exports = Self => { await invoiceOut.updateAttributes({ hasPdf: true - }, options); + }, myOptions); if (process.env.NODE_ENV !== 'test') { await print.storage.write(buffer, {