From fbdc715fa59af3beb06419aaf082b4f7415597b7 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 25 Feb 2025 14:59:49 +0100 Subject: [PATCH 1/2] feat: enhance ticket closing process with email notifications and error handling --- .../ticket/back/methods/ticket/closeAll.js | 83 ++++++++++++------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/modules/ticket/back/methods/ticket/closeAll.js b/modules/ticket/back/methods/ticket/closeAll.js index b645c714c9..9559333c87 100644 --- a/modules/ticket/back/methods/ticket/closeAll.js +++ b/modules/ticket/back/methods/ticket/closeAll.js @@ -1,5 +1,6 @@ const smtp = require('vn-print/core/smtp'); const config = require('vn-print/core/config'); +const Email = require('vn-print/core/email'); module.exports = Self => { Self.remoteMethodCtx('closeAll', { @@ -44,8 +45,7 @@ module.exports = Self => { LIMIT 1`, [toDate, toDate], myOptions); await Self.rawSql(` - DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close; - CREATE TEMPORARY TABLE tmp.ticket_close + CREATE OR REPLACE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY WITH wTickets AS( SELECT t.id ticketFk @@ -63,11 +63,12 @@ module.exports = Self => { FROM wTicketsTracking wt JOIN ticketTracking tt ON tt.id = wt.maxTracking ) SELECT tls.ticketFk, - t.clientFk, + t.clientFk clientId, c.name clientName, c.email recipient, + c.isToBeMailed, eu.email salesPersonEmail, - t.addressFk, + t.addressFk addressId, c.hasDailyInvoice, c.hasToInvoiceByAddress, t.totalWithVat, @@ -79,7 +80,7 @@ module.exports = Self => { JOIN agencyMode am ON am.id = t.agencyModeFk JOIN client c ON c.id = t.clientFk LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk - WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered')); + WHERE al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'); CALL ticket_close(); `, [dateFrom, dateTo], myOptions); @@ -98,21 +99,27 @@ module.exports = Self => { AND tob.id IS NULL AND t.routeFk`, [dateFrom, dateTo], myOptions); - const [clients] = await Self.rawSql(` - SELECT clientFk clientId, - clientName, - recipient, - salesPersonEmail, - addressFk addressId, - companyFk, + const clients = await Self.rawSql(` + SELECT *, SUM(totalWithVat) total, 'quick' serialType FROM tmp.ticket_close WHERE hasDailyInvoice - GROUP BY IF (hasToInvoiceByAddress, addressFk, clientFk), companyFk - HAVING total > 0; + GROUP BY IF (hasToInvoiceByAddress, addressId, clientId), companyFk + HAVING total > 0 + `, [], myOptions); + + const [ticketsToMail] = await Self.rawSql(` + SELECT * + FROM tmp.ticket_close + WHERE NOT hasDailyInvoice + AND isToBeMailed + AND salesPersonEmail IS NOT NULL + AND salesPersonEmail <> '' + AND recipient IS NOT NULL + AND recipient <> ''; DROP TEMPORARY TABLE tmp.ticket_close; - `, [], myOptions); + `, [], myOptions); if (tx) await tx.commit(); @@ -130,21 +137,23 @@ module.exports = Self => { if (id) await Self.app.models.InvoiceOut.makePdfAndNotify(ctx, id, null, nestedTransaction); } catch (error) { - await Self.rawSql(` - INSERT INTO util.debug (variable, value) - VALUES ('invoicingTicketError', ?) - `, [client.clientId + ' - ' + error]); + await handleInvoicingError(error, client, failedClients); + } + } - if (error.responseCode == 450) { - await invalidEmail(client); - continue; - } + for (const ticket of ticketsToMail) { + const args = { + id: ticket.ticketFk, + recipientId: ticket.clientId, + recipient: ticket.recipient, + replyTo: ticket.salesPersonEmail, + }; - failedClients.push({ - id: client.clientId, - address: client.addressId, - error - }); + try { + const email = new Email('delivery-note-link', args); + await email.send(); + } catch (error) { + await handleInvoicingError(error, ticket, failedClients); } } @@ -188,4 +197,22 @@ module.exports = Self => { html: body, }).catch(err => console.error(err)); } + + async function handleInvoicingError(error, entity, failedClients) { + await Self.rawSql(` + INSERT INTO util.debug (variable, value) + VALUES ('invoicingTicketError', ?) + `, [entity.clientId + ' - ' + error]); + + if (error.responseCode == 450) { + await invalidEmail(entity); + return; + } + + failedClients.push({ + id: entity.clientId, + address: entity.addressId, + error + }); + } }; From 2914dfff2e456f052e8753afd55c20dbaa81caad Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 25 Feb 2025 14:59:54 +0100 Subject: [PATCH 2/2] feat: add Catalan localization for email templates and footer --- .../core/components/email-footer/locale/ca.yml | 18 ++++++++++++++++++ .../email/delivery-note-link/locale/ca.yml | 11 +++++++++++ 2 files changed, 29 insertions(+) create mode 100644 print/core/components/email-footer/locale/ca.yml create mode 100644 print/templates/email/delivery-note-link/locale/ca.yml diff --git a/print/core/components/email-footer/locale/ca.yml b/print/core/components/email-footer/locale/ca.yml new file mode 100644 index 0000000000..b0e3fee865 --- /dev/null +++ b/print/core/components/email-footer/locale/ca.yml @@ -0,0 +1,18 @@ +buttons: + webAcccess: Visita la nostra Web + info: Ajuda'ns a millorar +fiscalAddress: VERDNATURA LEVANTE SL, B97367486 C/ Fenollar, 2. 46680 ALGEMESÍ + · verdnatura.es · clientes@verdnatura.es +disclaimer: '- AVÍS - Aquest missatge és privat i confidencial, i ha de ser utilitzat + exclusivament per la persona destinatària del mateix. Si has rebut aquest missatge + per error, et preguem que ho comuniquis al remitent i esborres aquest missatge + i qualsevol document adjunt que pugui contenir. Verdnatura Levante SL no renuncia + a la confidencialitat ni a cap privilegi per causa de transmissió errònia o mal + funcionament. Igualment, no es fa responsable dels canvis, alteracions, errors + o omissions que es puguin fer al missatge un cop enviat.' +privacy: En compliment del que disposa la Llei Orgànica 15/1999, de Protecció de + Dades de Caràcter Personal, et comuniquem que les dades personals que facilitis + s'inclouran en fitxers automatitzats de VERDNATURA LEVANTE S.L., podent en tot + moment exercir els drets d'accés, rectificació, cancel·lació i oposició, + comunicant-ho per escrit al domicili social de l'entitat. La finalitat del + fitxer és la gestió administrativa, comptabilitat i facturació. diff --git a/print/templates/email/delivery-note-link/locale/ca.yml b/print/templates/email/delivery-note-link/locale/ca.yml new file mode 100644 index 0000000000..41f0c128c7 --- /dev/null +++ b/print/templates/email/delivery-note-link/locale/ca.yml @@ -0,0 +1,11 @@ +subject: El teu albarà +title: El teu albarà +dear: Estimat client +description: Ja està disponible l'albarà corresponent a la comanda {0}.
+ Pots veure'l fent clic en aquest enllaç. +copyLink: 'Com a alternativa, pots copiar el següent enllaç al teu navegador:' +poll: Si ho desitges, pots respondre a la nostra enquesta de satisfacció per + ajudar-nos a oferir un millor servei. La teva opinió és molt important per a nosaltres! +help: Qualsevol dubte que tinguis, no dubtis a consultar-nos, estem aquí + per atendre't! +conclusion: Gràcies per la teva atenció!