2023-03-13 14:05:19 +00:00
|
|
|
const {Report, Email} = require('vn-print');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-03-14 07:17:46 +00:00
|
|
|
Self.printReport = async function(ctx, id, reportName) {
|
2023-03-13 14:05:19 +00:00
|
|
|
const args = Object.assign({}, ctx.args);
|
|
|
|
const params = {lang: ctx.req.getLocale()};
|
|
|
|
|
|
|
|
delete args.ctx;
|
|
|
|
for (const param in args)
|
|
|
|
params[param] = args[param];
|
|
|
|
|
|
|
|
const report = new Report(reportName, params);
|
|
|
|
const stream = await report.toPdfStream();
|
|
|
|
|
2023-03-14 07:17:46 +00:00
|
|
|
let fileName = `${reportName}`;
|
|
|
|
if (id) fileName += `-${id}`;
|
|
|
|
|
|
|
|
return [stream, 'application/pdf', `filename="${fileName}.pdf"`];
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.printEmail = async function(ctx, id, templateName) {
|
|
|
|
const {accessToken} = ctx.req;
|
|
|
|
const args = Object.assign({}, ctx.args);
|
|
|
|
const params = {lang: ctx.req.getLocale()};
|
|
|
|
|
|
|
|
delete args.ctx;
|
|
|
|
for (const param in args)
|
|
|
|
params[param] = args[param];
|
|
|
|
|
|
|
|
params.isPreview = true;
|
|
|
|
params.access_token = accessToken.id;
|
|
|
|
|
|
|
|
const report = new Email(templateName, params);
|
|
|
|
const html = await report.render();
|
|
|
|
|
|
|
|
let fileName = `${templateName}`;
|
|
|
|
if (id) fileName += `-${id}`;
|
|
|
|
|
|
|
|
return [html, 'text/html', `filename=${fileName}.pdf"`];
|
2023-03-13 14:05:19 +00:00
|
|
|
};
|
|
|
|
|
2023-03-14 07:17:46 +00:00
|
|
|
Self.sendTemplate = async function(ctx, templateName) {
|
2023-03-13 14:05:19 +00:00
|
|
|
const args = Object.assign({}, ctx.args);
|
|
|
|
const params = {
|
|
|
|
recipient: args.recipient,
|
|
|
|
lang: ctx.req.getLocale()
|
|
|
|
};
|
|
|
|
|
|
|
|
delete args.ctx;
|
|
|
|
for (const param in args)
|
|
|
|
params[param] = args[param];
|
|
|
|
|
2023-03-14 07:17:46 +00:00
|
|
|
const email = new Email(templateName, params);
|
2023-03-13 14:05:19 +00:00
|
|
|
|
|
|
|
return email.send();
|
|
|
|
};
|
|
|
|
};
|