refactor
gitea/salix/1466-print_refactor This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-10-24 07:41:54 +02:00
parent b099fe206b
commit 0eaba04e52
131 changed files with 179 additions and 94 deletions

View File

@ -1,10 +1,14 @@
{
"app": {
"port": 3000,
"defaultLanguage": "es",
"senderMail": "nocontestar@verdnatura.es",
"senderName": "Verdnatura"
},
"i18n": {
"locale": "es",
"fallbackLocale": "es",
"silentTranslationWarn": true
},
"pdf": {
"format": "A4",
"border": "1.5cm",

View File

@ -14,19 +14,6 @@ for (let configFile of configFiles) {
Object.assign(config, require(configFile));
}
/* let proxyConf = {};
let proxyFiles = [
'../../nginx/config.yml',
`${configPath}/config.yml`,
`${configPath}/config.${env}.yml`
];
for (let proxyFile of proxyFiles) {
if (fs.existsSync(proxyFile))
Object.assign(proxyConf, require(proxyFile));
} */
// config.proxy = proxyConf;
config.env = env;
module.exports = config;

View File

@ -6,4 +6,12 @@ module.exports = {
if (!this.pool)
this.pool = mysql.createPool(config.mysql);
},
findOne(query, params) {
return this.pool.query(query, params).then(([rows]) => {
return rows[0];
});
},
findFromDef() {
}
};

View File

@ -1,43 +1,107 @@
const Vue = require('vue');
const VueI18n = require('vue-i18n');
const renderer = require('vue-server-renderer').createRenderer();
const fs = require('fs-extra');
const fs = require('fs');
const pdf = require('html-pdf');
const juice = require('juice');
const config = require('./config');
const path = require('path');
Vue.use(VueI18n);
const config = require('./config');
const reportsPath = '../templates/reports';
if (!process.env.OPENSSL_CONF)
process.env.OPENSSL_CONF = '/etc/ssl/';
module.exports = {
Vue.use(VueI18n);
class Report {
constructor(name, args) {
this.name = name;
this.args = args;
}
get path() {
return `${reportsPath}/${this.name}`;
}
get template() {
const templatePath = `${this.path}/${this.name}.html`;
const fullPath = path.resolve(__dirname, templatePath);
return fs.readFileSync(fullPath, 'utf8');
}
get locale() {
}
get style() {
}
async render() {
const localePath = `${this.path}/locale`;
const stylePath = `${this.path}/assets/css/index`;
const stylesheet = require(stylePath);
const component = require(this.path);
component.i18n = require(localePath);
component.template = juice.inlineContent(this.template, stylesheet, {
inlinePseudoElements: true
});
const i18n = new VueI18n(config.i18n);
const app = new Vue({
i18n: i18n,
render: h => h(component, {
props: this.args
})
});
return renderer.renderToString(app);
}
async toPdfStream() {
const template = await this.render();
let options = config.pdf;
const optionsPath = `${this.path}/options.json`;
if (fs.existsSync(optionsPath))
options = Object.assign(options, require(optionsPath));
return new Promise(resolve => {
pdf.create(template, options).toStream((err, stream) => {
resolve(stream);
});
});
}
}
module.exports = Report;
/* module.exports = {
path: `${appPath}/report`,
/**
* Renders a report component
*
* @param {String} name - Report name
* @param {Object} ctx - Request context
*/
async render(name, ctx) {
const component = require(`${this.path}/${name}`);
const i18n = new VueI18n({
locale: 'es',
fallbackLocale: 'es',
silentTranslationWarn: true
const i18n = new VueI18n(config.i18n);
const app = new Vue({
i18n: i18n,
render: h => h(component, {
props: {
myProp: 'asd1'
}
})
});
const app = new Vue({i18n,
render: h => h(component)});
Vue.set(component, 'test', 'asd1');
return renderer.renderToString(app);
},
/* async preFetch(orgComponent, ctx) {
async preFetch(orgComponent, ctx) {
let component = Object.create(orgComponent);
let mergedData = {};
let asyncData = {};
@ -83,9 +147,9 @@ module.exports = {
}
return {component};
}, */
},
/* async attachAssets(component) {
async attachAssets(component) {
const localePath = `${this.path}/${component.name}/locale`;
const templatePath = `${this.path}/${component.name}/index.html`;
const stylePath = `${this.path}/${component.name}/assets/css/index`;
@ -96,7 +160,7 @@ module.exports = {
component.i18n = require(localePath);
component.template = juice.inlineContent(template, css, cssOptions);
}, */
},
async toPdf(name, ctx) {
const html = await this.render(name, ctx);
@ -113,3 +177,4 @@ module.exports = {
});
},
};
*/

View File

@ -1,10 +1,65 @@
const reportEngine = require('./reportEngine.js');
const Report = require('./report');
const emailEngine = require('./emailEngine');
const express = require('express');
const routes = require(`../config/routes.json`);
const path = require('path');
const vue = require('vue');
const fs = require('fs');
const templatesPath = path.resolve(__dirname, '../templates');
module.exports = app => {
this.path = `${appPath}/report`;
/**
* Serve static files
*/
const templatesDir = fs.readdirSync(templatesPath);
templatesDir.forEach(directory => {
const templateTypeDir = path.join(templatesPath, '/', directory);
const templates = fs.readdirSync(templateTypeDir);
templates.forEach(templateName => {
const templateDir = path.join(templatesPath, '/', directory, '/', templateName);
const assetsDir = `${templateDir}/assets`;
app.use(`/api/assets`, express.static(assetsDir));
});
});
app.get(`/api/report/:name`, async(req, res) => {
const args = Object.assign({}, req.body, req.query);
const report = new Report(req.params.name, args);
const stream = await report.toPdfStream();
res.setHeader('Content-type', 'application/pdf');
stream.pipe(res);
});
/* app.get(`/api/email/${name}`, (request, response) => {
emailEngine.render(name, request).then(rendered => {
response.send(rendered.html);
}).catch(e => {
next(e);
});
});
app.post(`/api/email/${name}`, (request, response, next) => {
emailEngine.toEmail(name, request).then(() => {
response.status(200).json({status: 200});
}).catch(e => {
next(e);
});
}); */
/* routes.forEach(route => {
if (route.type === 'email')
registerEmail(route.name);
else if (route.type === 'report')
registerReport(route.name);
const staticPath = this.path + `/${route.name}/assets`;
}); */
/**
* Enables a report
@ -14,13 +69,13 @@ module.exports = app => {
function registerReport(name) {
if (!name) throw new Error('Report name required');
app.get(`/api/report/${name}`, (request, response, next) => {
reportEngine.toPdf(name, request).then(stream => {
response.setHeader('Content-type', 'application/pdf');
stream.pipe(response);
}).catch(e => {
next(e);
});
app.get(`/api/report/${name}`, async(request, response) => {
const args = Object.assign({}, request.body, request.query);
const report = new Report(name, args);
const stream = await report.toPdf();
response.setHeader('Content-type', 'application/pdf');
stream.pipe(response);
});
}
@ -48,17 +103,4 @@ module.exports = app => {
});
});
}
/**
* Register routes
*/
routes.forEach(route => {
if (route.type === 'email')
registerEmail(route.name);
else if (route.type === 'report')
registerReport(route.name);
const staticPath = this.path + `/${route.name}/assets`;
app.use(`/api/assets`, express.static(staticPath));
});
};

View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 101 KiB

View File

@ -1,25 +1,12 @@
const strftime = require('strftime');
const database = require(`${appPath}/lib/database`);
const db = require(`${appPath}/lib/database`);
const UserException = require(`${appPath}/lib/exceptions/userException`);
module.exports = {
name: 'rpt-receipt',
serverPrefetch() {
console.log(this.test);
/* return new Promise(accept => {
this.client = this.fetchClient();
}); */
},
async asyncData(ctx, params) {
Object.assign(this, this.methods);
const [[client]] = await this.fetchClient(params.receiptFk);
const [[receipt]] = await this.fetchReceipt(params.receiptFk);
if (!receipt)
throw new UserException('No receipt data found');
return {client, receipt};
name: 'receipt',
async serverPrefetch() {
this.client = await this.fetchClient(this.receiptId);
this.receipt = await this.fetchReceipt(this.receiptId);
},
created() {
/* if (this.client.locale)
@ -37,8 +24,8 @@ module.exports = {
};
},
methods: {
fetchClient(receiptFk) {
return database.pool.query(
fetchClient(receiptId) {
return db.findOne(
`SELECT
c.id,
c.socialName,
@ -46,10 +33,10 @@ module.exports = {
FROM receipt r
JOIN client c ON c.id = r.clientFk
JOIN account.user u ON u.id = c.id
WHERE r.id = ?`, [receiptFk]);
WHERE r.id = ?`, [receiptId]);
},
fetchReceipt(receiptFk) {
return database.pool.query(
fetchReceipt(receiptId) {
return db.findOne(
`SELECT
r.id,
r.amountPaid,
@ -58,15 +45,12 @@ module.exports = {
r.companyFk
FROM receipt r
JOIN client c ON c.id = r.clientFk
WHERE r.id = ?`, [receiptFk]);
WHERE r.id = ?`, [receiptId]);
}
/* dated: () => {
return strftime('%d-%m-%Y', new Date());
}, */
},
components: {
'report-header': require('../report-header'),
'report-footer': require('../report-footer'),
},
template: '<div>asd</div>'
template: '<div>{{client}}</div>',
props: ['userId', 'receiptId']
};

View File

@ -17,12 +17,7 @@
</p>
<section class="signature">
<img :src="embeded['/assets/images/signature.png']">
<p class="centered">{{$t('payed', [
'Silla',
receipt.payed.getDate(),
$t('months')[receipt.payed.getMonth()],
receipt.payed.getFullYear()])
}}
<p class="centered">
</p>
</section>
</section>

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Some files were not shown because too many files have changed in this diff Show More