From 7a7ad6ac5f50869af57e9db0871160e6f1a16726 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 29 Sep 2022 13:09:44 +0200 Subject: [PATCH] InvoiceOut_sendQueued --- db/changes/10490-august/00-ACL.sql | 1 + .../back/methods/invoiceOut/sendQueued.js | 133 ++++++++++++++++++ modules/invoiceOut/back/models/invoice-out.js | 1 + 3 files changed, 135 insertions(+) create mode 100644 modules/invoiceOut/back/methods/invoiceOut/sendQueued.js diff --git a/db/changes/10490-august/00-ACL.sql b/db/changes/10490-august/00-ACL.sql index 1c773da89..0a3556a1a 100644 --- a/db/changes/10490-august/00-ACL.sql +++ b/db/changes/10490-august/00-ACL.sql @@ -19,6 +19,7 @@ INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalTyp ('Client', 'incotermsAuthorizationEmail', 'WRITE', 'ALLOW', 'ROLE', 'employee'), ('InvoiceOut', 'invoiceEmail', 'WRITE', 'ALLOW', 'ROLE', 'employee'), ('InvoiceOut', 'exportationPdf', 'READ', 'ALLOW', 'ROLE', 'employee'), + ('InvoiceOut', 'sendQueued', 'WRITE', 'ALLOW', 'ROLE', 'system'), ('Supplier', 'campaignMetricsPdf', 'READ', 'ALLOW', 'ROLE', 'employee'), ('Supplier', 'campaignMetricsEmail', 'WRITE', 'ALLOW', 'ROLE', 'employee'), ('Travel', 'extraCommunityPdf', 'READ', 'ALLOW', 'ROLE', 'employee'), diff --git a/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js b/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js new file mode 100644 index 000000000..846e48c14 --- /dev/null +++ b/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js @@ -0,0 +1,133 @@ +const {Email, Report, storage} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethod('sendQueued', { + description: 'Send all queued invoices', + accessType: 'WRITE', + accepts: [], + returns: { + type: 'object', + root: true + }, + http: { + path: '/send-queued', + verb: 'POST' + } + }); + + Self.sendQueued = async() => { + const invoices = await Self.rawSql(` + SELECT + io.id, + io.clientFk, + io.issued, + io.ref, + c.email recipient, + c.salesPersonFk, + c.isToBeMailed, + c.hasToInvoice, + co.hasDailyInvoice, + eu.email salesPersonEmail + FROM invoiceOut_queue ioq + JOIN invoiceOut io ON io.id = ioq.invoiceFk + JOIN client c ON c.id = io.clientFk + JOIN province p ON p.id = c.provinceFk + JOIN country co ON co.id = p.countryFk + LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + WHERE status = ''`); + + let invoiceId; + for (const invoiceOut of invoices) { + try { + const tx = await Self.beginTransaction({}); + const myOptions = {transaction: tx}; + + invoiceId = invoiceOut.id; + + const args = { + reference: invoiceOut.ref, + recipientId: invoiceOut.clientFk, + recipient: invoiceOut.recipient, + replyTo: invoiceOut.salesPersonEmail + }; + + const invoiceReport = new Report('invoice', args); + const stream = await invoiceReport.toPdfStream(); + + const issued = invoiceOut.issued; + const year = issued.getFullYear().toString(); + const month = (issued.getMonth() + 1).toString(); + const day = issued.getDate().toString(); + + const fileName = `${year}${invoiceOut.ref}.pdf`; + + // Store invoice + storage.write(stream, { + type: 'invoice', + path: `${year}/${month}/${day}`, + fileName: fileName + }); + + await Self.rawSql(` + UPDATE invoiceOut + SET hasPdf = true + WHERE id = ?`, + [invoiceOut.id], myOptions); + + const isToBeMailed = invoiceOut.recipient && invoiceOut.salesPersonFk && invoiceOut.isToBeMailed; + + if (isToBeMailed) { + const mailOptions = { + overrideAttachments: true, + attachments: [] + }; + + const invoiceAttachment = { + filename: fileName, + content: stream + }; + + if (invoiceOut.serial == 'E' && invoiceOut.companyCode == 'VNL') { + const exportation = new Report('exportation', args); + const stream = await exportation.toPdfStream(); + const fileName = `CITES-${invoiceOut.ref}.pdf`; + + mailOptions.attachments.push({ + filename: fileName, + content: stream + }); + } + + mailOptions.attachments.push(invoiceAttachment); + + const email = new Email('invoice', args); + await email.send(mailOptions); + } + // Update queue status + const date = new Date(); + await Self.rawSql(` + UPDATE invoiceOut_queue + SET status = "printed", + printed = ? + WHERE invoiceFk = ?`, + [date, invoiceOut.id], myOptions); + + await tx.commit(); + } catch (error) { + await tx.rollback(); + + await Self.rawSql(` + UPDATE invoiceOut_queue + SET status = ? + WHERE invoiceFk = ?`, + [error.message, invoiceId]); + + throw e; + } + } + + return { + message: 'Success' + }; + }; +}; diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 8e9e5bc7a..f58fe3978 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -11,4 +11,5 @@ module.exports = Self => { require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); require('../methods/invoiceOut/exportationPdf')(Self); + require('../methods/invoiceOut/sendQueued')(Self); };