salix/print/methods/csv/invoice/send.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-01-18 12:15:42 +00:00
const path = require('path');
const db = require('vn-print/core/database');
const Email = require('vn-print/core/email');
const {toCSV} = require('../csv');
const sqlPath = path.join(__dirname, 'sql');
module.exports = async function(request, response, next) {
try {
const reqArgs = request.query;
2022-08-10 10:13:31 +00:00
if (!reqArgs.refFk)
throw new Error('The argument refFk is required');
2022-01-18 12:15:42 +00:00
2022-08-10 10:13:31 +00:00
const refFk = reqArgs.refFk;
const invoice = await db.findOneFromDef(`${sqlPath}/invoice`, [refFk]);
const sales = await db.rawSqlFromDef(`${sqlPath}/sales`, [refFk]);
2022-01-18 12:15:42 +00:00
const args = Object.assign({
2022-08-10 10:13:31 +00:00
refFk: invoice.refFk,
2022-01-18 12:15:42 +00:00
recipientId: invoice.clientFk,
recipient: invoice.recipient,
replyTo: invoice.salesPersonEmail
}, response.locals);
const content = toCSV(sales);
2022-08-10 10:13:31 +00:00
const fileName = `invoice_${refFk}.csv`;
2022-01-18 12:15:42 +00:00
const email = new Email('invoice', args);
await email.send({
overrideAttachments: true,
attachments: [{
filename: fileName,
content: content
}]
});
response.status(200).json({message: 'Success'});
} catch (error) {
next(error);
}
};