134 lines
4.5 KiB
JavaScript
134 lines
4.5 KiB
JavaScript
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 invoiceOutQueue 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 invoiceOutQueue
|
|
SET status = "printed",
|
|
printed = ?
|
|
WHERE invoiceFk = ?`,
|
|
[date, invoiceOut.id], myOptions);
|
|
|
|
await tx.commit();
|
|
} catch (error) {
|
|
await tx.rollback();
|
|
|
|
await Self.rawSql(`
|
|
UPDATE invoiceOutQueue
|
|
SET status = ?
|
|
WHERE invoiceFk = ?`,
|
|
[error.message, invoiceId]);
|
|
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
return {
|
|
message: 'Success'
|
|
};
|
|
};
|
|
};
|