diff --git a/db/changes/10451-april/00-invoiceOut_queue.sql b/db/changes/10451-april/00-invoiceOut_queue.sql index 80f6aed40..0f91b809e 100644 --- a/db/changes/10451-april/00-invoiceOut_queue.sql +++ b/db/changes/10451-april/00-invoiceOut_queue.sql @@ -2,7 +2,7 @@ create table `vn`.`invoiceOut_queue` ( invoiceFk int(10) unsigned not null, dated datetime default now() null, - printed tinyint(1) default 0 null, + `status` VARCHAR(50) default '' null, constraint invoiceOut_queue_pk primary key (invoiceFk), constraint invoiceOut_queue_invoiceOut_id_fk diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index f2c988909..c77021ff2 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('globalInvoicing', { description: 'Make a global invoice', diff --git a/print/methods/notify/invocing.js b/print/methods/notify/invocing.js deleted file mode 100644 index 1172ef0ea..000000000 --- a/print/methods/notify/invocing.js +++ /dev/null @@ -1,45 +0,0 @@ -const db = require('vn-print/core/database'); -const Email = require('vn-print/core/email'); - -module.exports = async function(request, response, next) { - try { - const reqArgs = request.body; - - if (!reqArgs.clientIds) - throw new Error('The argument clientIds is required'); - if (!reqArgs.from) - throw new Error('The argument from is required'); - if (!reqArgs.to) - throw new Error('The argument to is required'); - - response.status(200).json({ - message: 'Success' - }); - - const invoices = await db.rawSql(` - SELECT - * FROM invoiceOut_queue - WHERE status = 'PENDING'`); - - // const clientData = new Map(); - // for (const client of clients) - // clientData.set(client.id, client); - - // for (const clientId of reqArgs.clientIds) { - // const client = clientData.get(clientId); - - // if (client) { - // const args = Object.assign({ - // recipientId: clientId, - // recipient: client.email, - // replyTo: client.salesPersonEmail - // }, response.locals); - - // const email = new Email('campaign-metrics', args); - // await email.send(); - // } - // } - } catch (error) { - next(error); - } -}; diff --git a/print/methods/routes.js b/print/methods/routes.js index ea40e0743..28671b3da 100644 --- a/print/methods/routes.js +++ b/print/methods/routes.js @@ -16,7 +16,7 @@ module.exports = [ cb: require('./closure') }, { - url: '/api/notify', - cb: require('./notify') + url: '/api/schedule', + cb: require('./schedule') } ]; diff --git a/print/methods/notify/consumption.js b/print/methods/schedule/consumption.js similarity index 100% rename from print/methods/notify/consumption.js rename to print/methods/schedule/consumption.js diff --git a/print/methods/notify/index.js b/print/methods/schedule/index.js similarity index 76% rename from print/methods/notify/index.js rename to print/methods/schedule/index.js index df4705d1e..05d54b2ed 100644 --- a/print/methods/notify/index.js +++ b/print/methods/schedule/index.js @@ -2,5 +2,6 @@ const express = require('express'); const router = new express.Router(); router.post('/consumption', require('./consumption')); +router.post('/invoice', require('./invoice')); module.exports = router; diff --git a/print/methods/schedule/invoice.js b/print/methods/schedule/invoice.js new file mode 100644 index 000000000..d27557d34 --- /dev/null +++ b/print/methods/schedule/invoice.js @@ -0,0 +1,118 @@ +const db = require('vn-print/core/database'); +const Email = require('vn-print/core/email'); +const Report = require('vn-print/core/report'); +const storage = require('vn-print/core/storage'); + +module.exports = async function(request, response, next) { + try { + const reqArgs = request.body; + + response.status(200).json({ + message: 'Success' + }); + + const invoices = await db.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 = ''`); + + for (const invoiceOut of invoices) { + try { + const args = Object.assign({ + invoiceId: invoiceOut.id, + recipientId: invoiceOut.clientFk, + recipient: invoiceOut.recipient, + replyTo: invoiceOut.salesPersonEmail + }, reqArgs); + + 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 db.rawSql('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id]); + + 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); + } + } catch (error) { + console.log(error); + } + } + + // const clientData = new Map(); + // for (const client of clients) + // clientData.set(client.id, client); + + // for (const clientId of reqArgs.clientIds) { + // const client = clientData.get(clientId); + + // if (client) { + // const args = Object.assign({ + // recipientId: clientId, + // recipient: client.email, + // replyTo: client.salesPersonEmail + // }, response.locals); + + // const email = new Email('campaign-metrics', args); + // await email.send(); + // } + // } + } catch (error) { + next(error); + } +};