2022-04-28 12:59:29 +00:00
|
|
|
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 {
|
|
|
|
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 = ''`);
|
|
|
|
|
2022-05-02 11:52:57 +00:00
|
|
|
let connection;
|
2022-05-02 07:51:49 +00:00
|
|
|
let invoiceId;
|
2022-04-28 12:59:29 +00:00
|
|
|
for (const invoiceOut of invoices) {
|
|
|
|
try {
|
2022-05-02 07:51:49 +00:00
|
|
|
invoiceId = invoiceOut.id;
|
2022-05-02 11:52:57 +00:00
|
|
|
connection = await db.getConnection();
|
|
|
|
connection.query('START TRANSACTION');
|
|
|
|
|
2022-04-28 12:59:29 +00:00
|
|
|
const args = Object.assign({
|
|
|
|
invoiceId: invoiceOut.id,
|
|
|
|
recipientId: invoiceOut.clientFk,
|
|
|
|
recipient: invoiceOut.recipient,
|
|
|
|
replyTo: invoiceOut.salesPersonEmail
|
2022-05-02 07:51:49 +00:00
|
|
|
}, response.locals);
|
2022-04-28 12:59:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2022-05-02 11:52:57 +00:00
|
|
|
connection.query('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id]);
|
2022-04-28 12:59:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-05-02 07:51:49 +00:00
|
|
|
// Update queue status
|
2022-06-09 11:33:01 +00:00
|
|
|
const date = new Date();
|
2022-05-02 07:51:49 +00:00
|
|
|
sql = `UPDATE invoiceOut_queue
|
|
|
|
SET status = "printed",
|
2022-06-09 11:33:01 +00:00
|
|
|
printed = ?
|
2022-05-02 07:51:49 +00:00
|
|
|
WHERE invoiceFk = ?`;
|
2022-06-09 11:33:01 +00:00
|
|
|
connection.query(sql, [date, invoiceOut.id]);
|
2022-05-02 11:52:57 +00:00
|
|
|
connection.query('COMMIT');
|
2022-04-28 12:59:29 +00:00
|
|
|
} catch (error) {
|
2022-05-02 11:52:57 +00:00
|
|
|
connection.query('ROLLBACK');
|
|
|
|
connection.release();
|
2022-05-02 07:51:49 +00:00
|
|
|
sql = `UPDATE invoiceOut_queue
|
|
|
|
SET status = ?
|
|
|
|
WHERE invoiceFk = ?`;
|
|
|
|
await db.rawSql(sql, [error.message, invoiceId]);
|
2022-04-28 12:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|