64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
const Report = require('vn-print/core/report'); // Put inside block to avoid circular dependency
|
|
|
|
module.exports = {
|
|
async start(tickets) {
|
|
if (tickets.length == 0) return;
|
|
|
|
const failedtickets = [];
|
|
for (const ticket of tickets) {
|
|
try {
|
|
await db.rawSql(`CALL vn.ticket_closeByTicket(?)`, [ticket.id]);
|
|
|
|
const invoiceOut = await db.findOne(`
|
|
SELECT io.id, io.ref, io.serial, cny.code companyCode, io.issued
|
|
FROM ticket t
|
|
JOIN invoiceOut io ON io.ref = t.refFk
|
|
JOIN company cny ON cny.id = io.companyFk
|
|
WHERE t.id = ?
|
|
`, [ticket.id]);
|
|
|
|
|
|
const mailOptions = {
|
|
overrideAttachments: true,
|
|
attachments: []
|
|
};
|
|
|
|
// Store invoice
|
|
if (invoiceOut) {
|
|
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`;
|
|
|
|
storage.write(stream, {
|
|
type: 'invoice',
|
|
path: `${year}/${month}/${day}`,
|
|
fileName: fileName
|
|
});
|
|
|
|
mailOptions.attachments.push({
|
|
filename: fileName,
|
|
content: stream
|
|
});
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
// Domain not found
|
|
if (error.responseCode == 450)
|
|
return invalidEmail(ticket);
|
|
|
|
// Save tickets on a list of failed ids
|
|
failedtickets.push({
|
|
id: ticket.id,
|
|
stacktrace: error
|
|
});
|
|
}
|
|
}
|
|
};
|