diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 7a973460d..40df9a712 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -175,5 +175,6 @@ "Pass expired": "The password has expired, change it from Salix", "Can't transfer claimed sales": "Can't transfer claimed sales", "Invalid quantity": "Invalid quantity", - "Failed to upload delivery note": "Error to upload delivery note {{id}}" + "Failed to upload delivery note": "Error to upload delivery note {{id}}", + "Mail not sent": "There has been an error sending the invoice to the client [{{clientId}}]({{{clientUrl}}}), please check the email address" } diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 5870dcb4f..ce93b27ce 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -296,5 +296,7 @@ "Ticket is already signed": "Este ticket ya ha sido firmado", "Fecha fuera de rango": "Fecha fuera de rango", "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 + "Error when sending mail to client": "Error al enviar el correo al cliente", + "Mail not sent": "Se ha producido un fallo al enviar la factura al cliente [{{clientId}}]({{{clientUrl}}}), por favor revisa la dirección de correo electrónico" + +} diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index 5d61bfcd0..d010ef631 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -51,6 +51,9 @@ module.exports = Self => { const args = ctx.args; const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; + const $t = ctx.req.__; // $translate + const origin = ctx.req.headers.origin; + let tx; if (typeof options == 'object') @@ -119,7 +122,7 @@ module.exports = Self => { include: { relation: 'client', scope: { - fields: ['email', 'isToBeMailed'] + fields: ['email', 'isToBeMailed', 'salesPersonFk'] } } }, myOptions); @@ -131,21 +134,34 @@ module.exports = Self => { if (tx) await tx.rollback(); throw e; } - + try { - await Self.makePdf(invoiceId, myOptions); + await Self.makePdf(invoiceId); } catch (err) { console.error(err); throw new UserError('Error while generating PDF', 'pdfError'); } if (invoiceOut.client().isToBeMailed) { - ctx.args = { - reference: invoiceOut.ref, - recipientId: args.clientId, - recipient: invoiceOut.client().email - }; - await models.InvoiceOut.invoiceEmail(ctx, invoiceOut.ref); + try { + ctx.args = { + reference: invoiceOut.ref, + recipientId: args.clientId, + recipient: invoiceOut.client().email + }; + await models.InvoiceOut.invoiceEmail(ctx, invoiceOut.ref); + } catch (err) { + const message = $t('Mail not sent', { + clientId: args.clientId, + clientUrl: `${origin}/#!/claim/${args.id}/summary` + }); + const salesPersonId = invoiceOut.client().salesPersonFk; + + if (salesPersonId) + await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + + throw new UserError('Error when sending mail to client', 'mailNotSent'); + } } else { const query = ` CALL vn.report_print( diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js index 45894e9c5..113526484 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js @@ -77,7 +77,7 @@ module.exports = Self => { try { return email.send(mailOptions); - } catch(err) { + } catch (err) { throw new UserError('Error when sending mail to client', 'mailNotSent'); } }; diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 027ec2fb2..0d5ed021c 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -23,19 +23,8 @@ module.exports = Self => { require('../methods/invoiceOut/negativeBasesCsv')(Self); Self.makePdf = async function(id, 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 invoiceOut = await Self.findById(id, {fields}, options); const invoiceReport = new print.Report('invoice', { reference: invoiceOut.ref }); @@ -52,7 +41,7 @@ module.exports = Self => { await invoiceOut.updateAttributes({ hasPdf: true - }, myOptions); + }, options); if (process.env.NODE_ENV !== 'test') { await print.storage.write(buffer, { @@ -61,5 +50,5 @@ module.exports = Self => { fileName: fileName }); } - } + }; }; diff --git a/modules/invoiceOut/front/global-invoicing/index.js b/modules/invoiceOut/front/global-invoicing/index.js index 36552d25b..930dfd459 100644 --- a/modules/invoiceOut/front/global-invoicing/index.js +++ b/modules/invoiceOut/front/global-invoicing/index.js @@ -118,25 +118,7 @@ class Controller extends Section { }; switch (error?.code) { case 'pdfError': - isWarning = true; - break; case 'mailNotSent': - this.$http.get(`Clients/findOne`, {filter}) - .then(res => { - const salesPersonFk = res.data.salesPersonFk; - if (!salesPersonFk) return; - - const message = this.$t('Mail not sent', { - clientId: address.clientId, - clientUrl: `${window.location.origin}/#!/client/${this.clientId}/summary` - }); - - const params = { - workerId: salesPersonFk, - message: message, - }; - this.$http.post('Chats/sendCheckingPresence', params); - }); isWarning = true; break; } diff --git a/modules/invoiceOut/front/global-invoicing/locale/en.yml b/modules/invoiceOut/front/global-invoicing/locale/en.yml deleted file mode 100644 index 171822ef7..000000000 --- a/modules/invoiceOut/front/global-invoicing/locale/en.yml +++ /dev/null @@ -1 +0,0 @@ -Mail not sent: There has been an error sending the invoice to the client {{clientId}} {{clientUrl}}, please check the email address diff --git a/modules/invoiceOut/front/global-invoicing/locale/es.yml b/modules/invoiceOut/front/global-invoicing/locale/es.yml index 4c31df729..ed61e832a 100644 --- a/modules/invoiceOut/front/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/global-invoicing/locale/es.yml @@ -19,4 +19,3 @@ One client: Un solo cliente Choose a valid client: Selecciona un cliente válido Stop: Parar Critical invoicing error, proccess stopped: Error crítico al facturar, proceso detenido -Mail not sent: Se ha producido un fallo al enviar la factura al cliente {{clientId}} {{clientUrl}}, por favor revisa la dirección de correo electrónico