diff --git a/print/config/print.json b/print/config/print.json index 7ad2e1316..6a8d8b112 100755 --- a/print/config/print.json +++ b/print/config/print.json @@ -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", diff --git a/print/lib/config.js b/print/lib/config.js index 47e58ec77..d07d4f3fc 100644 --- a/print/lib/config.js +++ b/print/lib/config.js @@ -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; diff --git a/print/lib/database.js b/print/lib/database.js index bd0e18c54..22d2f8c84 100644 --- a/print/lib/database.js +++ b/print/lib/database.js @@ -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() { + + } }; diff --git a/print/lib/reportEngine.js b/print/lib/report.js similarity index 58% rename from print/lib/reportEngine.js rename to print/lib/report.js index 3e634ae54..6cfaee4b4 100644 --- a/print/lib/reportEngine.js +++ b/print/lib/report.js @@ -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 = { }); }, }; + */ diff --git a/print/lib/router.js b/print/lib/router.js index 59e8a5b74..eff4b5c9e 100644 --- a/print/lib/router.js +++ b/print/lib/router.js @@ -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)); - }); }; diff --git a/print/report/claim-pickup-order/assets/css/index.js b/print/templates/email/claim-pickup-order/assets/css/index.js similarity index 100% rename from print/report/claim-pickup-order/assets/css/index.js rename to print/templates/email/claim-pickup-order/assets/css/index.js diff --git a/print/report/claim-pickup-order/index.html b/print/templates/email/claim-pickup-order/index.html similarity index 100% rename from print/report/claim-pickup-order/index.html rename to print/templates/email/claim-pickup-order/index.html diff --git a/print/report/claim-pickup-order/index.js b/print/templates/email/claim-pickup-order/index.js similarity index 100% rename from print/report/claim-pickup-order/index.js rename to print/templates/email/claim-pickup-order/index.js diff --git a/print/report/claim-pickup-order/locale.js b/print/templates/email/claim-pickup-order/locale.js similarity index 100% rename from print/report/claim-pickup-order/locale.js rename to print/templates/email/claim-pickup-order/locale.js diff --git a/print/report/client-lcr/assets/css/index.js b/print/templates/email/client-lcr/assets/css/index.js similarity index 100% rename from print/report/client-lcr/assets/css/index.js rename to print/templates/email/client-lcr/assets/css/index.js diff --git a/print/report/client-lcr/index.html b/print/templates/email/client-lcr/index.html similarity index 100% rename from print/report/client-lcr/index.html rename to print/templates/email/client-lcr/index.html diff --git a/print/report/client-lcr/index.js b/print/templates/email/client-lcr/index.js similarity index 100% rename from print/report/client-lcr/index.js rename to print/templates/email/client-lcr/index.js diff --git a/print/report/client-lcr/locale.js b/print/templates/email/client-lcr/locale.js similarity index 100% rename from print/report/client-lcr/locale.js rename to print/templates/email/client-lcr/locale.js diff --git a/print/report/client-welcome/assets/css/index.js b/print/templates/email/client-welcome/assets/css/index.js similarity index 100% rename from print/report/client-welcome/assets/css/index.js rename to print/templates/email/client-welcome/assets/css/index.js diff --git a/print/report/client-welcome/index.html b/print/templates/email/client-welcome/index.html similarity index 100% rename from print/report/client-welcome/index.html rename to print/templates/email/client-welcome/index.html diff --git a/print/report/client-welcome/index.js b/print/templates/email/client-welcome/index.js similarity index 100% rename from print/report/client-welcome/index.js rename to print/templates/email/client-welcome/index.js diff --git a/print/report/client-welcome/locale.js b/print/templates/email/client-welcome/locale.js similarity index 100% rename from print/report/client-welcome/locale.js rename to print/templates/email/client-welcome/locale.js diff --git a/print/report/delivery-note/assets/css/index.js b/print/templates/email/delivery-note/assets/css/index.js similarity index 100% rename from print/report/delivery-note/assets/css/index.js rename to print/templates/email/delivery-note/assets/css/index.js diff --git a/print/report/delivery-note/index.html b/print/templates/email/delivery-note/index.html similarity index 100% rename from print/report/delivery-note/index.html rename to print/templates/email/delivery-note/index.html diff --git a/print/report/delivery-note/index.js b/print/templates/email/delivery-note/index.js similarity index 100% rename from print/report/delivery-note/index.js rename to print/templates/email/delivery-note/index.js diff --git a/print/report/delivery-note/locale.js b/print/templates/email/delivery-note/locale.js similarity index 100% rename from print/report/delivery-note/locale.js rename to print/templates/email/delivery-note/locale.js diff --git a/print/report/driver-route/assets/css/index.js b/print/templates/email/driver-route/assets/css/index.js similarity index 100% rename from print/report/driver-route/assets/css/index.js rename to print/templates/email/driver-route/assets/css/index.js diff --git a/print/report/driver-route/index.html b/print/templates/email/driver-route/index.html similarity index 100% rename from print/report/driver-route/index.html rename to print/templates/email/driver-route/index.html diff --git a/print/report/driver-route/index.js b/print/templates/email/driver-route/index.js similarity index 100% rename from print/report/driver-route/index.js rename to print/templates/email/driver-route/index.js diff --git a/print/report/driver-route/locale.js b/print/templates/email/driver-route/locale.js similarity index 100% rename from print/report/driver-route/locale.js rename to print/templates/email/driver-route/locale.js diff --git a/print/report/email-footer/assets/css/index.js b/print/templates/email/email-footer/assets/css/index.js similarity index 100% rename from print/report/email-footer/assets/css/index.js rename to print/templates/email/email-footer/assets/css/index.js diff --git a/print/report/email-footer/assets/css/style.css b/print/templates/email/email-footer/assets/css/style.css similarity index 100% rename from print/report/email-footer/assets/css/style.css rename to print/templates/email/email-footer/assets/css/style.css diff --git a/print/report/email-footer/assets/images/action.png b/print/templates/email/email-footer/assets/images/action.png similarity index 100% rename from print/report/email-footer/assets/images/action.png rename to print/templates/email/email-footer/assets/images/action.png diff --git a/print/report/email-footer/assets/images/facebook.png b/print/templates/email/email-footer/assets/images/facebook.png similarity index 100% rename from print/report/email-footer/assets/images/facebook.png rename to print/templates/email/email-footer/assets/images/facebook.png diff --git a/print/report/email-footer/assets/images/info.png b/print/templates/email/email-footer/assets/images/info.png similarity index 100% rename from print/report/email-footer/assets/images/info.png rename to print/templates/email/email-footer/assets/images/info.png diff --git a/print/report/email-footer/assets/images/instagram.png b/print/templates/email/email-footer/assets/images/instagram.png similarity index 100% rename from print/report/email-footer/assets/images/instagram.png rename to print/templates/email/email-footer/assets/images/instagram.png diff --git a/print/report/email-footer/assets/images/linkedin.png b/print/templates/email/email-footer/assets/images/linkedin.png similarity index 100% rename from print/report/email-footer/assets/images/linkedin.png rename to print/templates/email/email-footer/assets/images/linkedin.png diff --git a/print/report/email-footer/assets/images/pinterest.png b/print/templates/email/email-footer/assets/images/pinterest.png similarity index 100% rename from print/report/email-footer/assets/images/pinterest.png rename to print/templates/email/email-footer/assets/images/pinterest.png diff --git a/print/report/email-footer/assets/images/twitter.png b/print/templates/email/email-footer/assets/images/twitter.png similarity index 100% rename from print/report/email-footer/assets/images/twitter.png rename to print/templates/email/email-footer/assets/images/twitter.png diff --git a/print/report/email-footer/assets/images/youtube.png b/print/templates/email/email-footer/assets/images/youtube.png similarity index 100% rename from print/report/email-footer/assets/images/youtube.png rename to print/templates/email/email-footer/assets/images/youtube.png diff --git a/print/report/email-footer/index.html b/print/templates/email/email-footer/index.html similarity index 100% rename from print/report/email-footer/index.html rename to print/templates/email/email-footer/index.html diff --git a/print/report/email-footer/index.js b/print/templates/email/email-footer/index.js similarity index 100% rename from print/report/email-footer/index.js rename to print/templates/email/email-footer/index.js diff --git a/print/report/email-footer/locale.js b/print/templates/email/email-footer/locale.js similarity index 100% rename from print/report/email-footer/locale.js rename to print/templates/email/email-footer/locale.js diff --git a/print/report/email-header/assets/css/index.js b/print/templates/email/email-header/assets/css/index.js similarity index 100% rename from print/report/email-header/assets/css/index.js rename to print/templates/email/email-header/assets/css/index.js diff --git a/print/report/email-header/assets/css/style.css b/print/templates/email/email-header/assets/css/style.css similarity index 100% rename from print/report/email-header/assets/css/style.css rename to print/templates/email/email-header/assets/css/style.css diff --git a/print/report/email-header/assets/images/email-logo.png b/print/templates/email/email-header/assets/images/email-logo.png similarity index 100% rename from print/report/email-header/assets/images/email-logo.png rename to print/templates/email/email-header/assets/images/email-logo.png diff --git a/print/report/email-header/index.html b/print/templates/email/email-header/index.html similarity index 100% rename from print/report/email-header/index.html rename to print/templates/email/email-header/index.html diff --git a/print/report/email-header/index.js b/print/templates/email/email-header/index.js similarity index 100% rename from print/report/email-header/index.js rename to print/templates/email/email-header/index.js diff --git a/print/report/email-header/locale.js b/print/templates/email/email-header/locale.js similarity index 100% rename from print/report/email-header/locale.js rename to print/templates/email/email-header/locale.js diff --git a/print/report/letter-debtor-nd/assets/css/index.js b/print/templates/email/letter-debtor-nd/assets/css/index.js similarity index 100% rename from print/report/letter-debtor-nd/assets/css/index.js rename to print/templates/email/letter-debtor-nd/assets/css/index.js diff --git a/print/report/letter-debtor-nd/index.html b/print/templates/email/letter-debtor-nd/index.html similarity index 100% rename from print/report/letter-debtor-nd/index.html rename to print/templates/email/letter-debtor-nd/index.html diff --git a/print/report/letter-debtor-nd/index.js b/print/templates/email/letter-debtor-nd/index.js similarity index 100% rename from print/report/letter-debtor-nd/index.js rename to print/templates/email/letter-debtor-nd/index.js diff --git a/print/report/letter-debtor-nd/locale.js b/print/templates/email/letter-debtor-nd/locale.js similarity index 100% rename from print/report/letter-debtor-nd/locale.js rename to print/templates/email/letter-debtor-nd/locale.js diff --git a/print/report/letter-debtor-st/assets/css/index.js b/print/templates/email/letter-debtor-st/assets/css/index.js similarity index 100% rename from print/report/letter-debtor-st/assets/css/index.js rename to print/templates/email/letter-debtor-st/assets/css/index.js diff --git a/print/report/letter-debtor-st/index.html b/print/templates/email/letter-debtor-st/index.html similarity index 100% rename from print/report/letter-debtor-st/index.html rename to print/templates/email/letter-debtor-st/index.html diff --git a/print/report/letter-debtor-st/index.js b/print/templates/email/letter-debtor-st/index.js similarity index 100% rename from print/report/letter-debtor-st/index.js rename to print/templates/email/letter-debtor-st/index.js diff --git a/print/report/letter-debtor-st/locale.js b/print/templates/email/letter-debtor-st/locale.js similarity index 100% rename from print/report/letter-debtor-st/locale.js rename to print/templates/email/letter-debtor-st/locale.js diff --git a/print/report/payment-update/assets/css/index.js b/print/templates/email/payment-update/assets/css/index.js similarity index 100% rename from print/report/payment-update/assets/css/index.js rename to print/templates/email/payment-update/assets/css/index.js diff --git a/print/report/payment-update/index.html b/print/templates/email/payment-update/index.html similarity index 100% rename from print/report/payment-update/index.html rename to print/templates/email/payment-update/index.html diff --git a/print/report/payment-update/index.js b/print/templates/email/payment-update/index.js similarity index 100% rename from print/report/payment-update/index.js rename to print/templates/email/payment-update/index.js diff --git a/print/report/payment-update/locale.js b/print/templates/email/payment-update/locale.js similarity index 100% rename from print/report/payment-update/locale.js rename to print/templates/email/payment-update/locale.js diff --git a/print/report/printer-setup/assets/css/index.js b/print/templates/email/printer-setup/assets/css/index.js similarity index 100% rename from print/report/printer-setup/assets/css/index.js rename to print/templates/email/printer-setup/assets/css/index.js diff --git a/print/report/printer-setup/assets/files/model.ezp b/print/templates/email/printer-setup/assets/files/model.ezp similarity index 100% rename from print/report/printer-setup/assets/files/model.ezp rename to print/templates/email/printer-setup/assets/files/model.ezp diff --git a/print/report/printer-setup/assets/files/port.png b/print/templates/email/printer-setup/assets/files/port.png similarity index 100% rename from print/report/printer-setup/assets/files/port.png rename to print/templates/email/printer-setup/assets/files/port.png diff --git a/print/report/printer-setup/index.html b/print/templates/email/printer-setup/index.html similarity index 100% rename from print/report/printer-setup/index.html rename to print/templates/email/printer-setup/index.html diff --git a/print/report/printer-setup/index.js b/print/templates/email/printer-setup/index.js similarity index 100% rename from print/report/printer-setup/index.js rename to print/templates/email/printer-setup/index.js diff --git a/print/report/printer-setup/locale.js b/print/templates/email/printer-setup/locale.js similarity index 100% rename from print/report/printer-setup/locale.js rename to print/templates/email/printer-setup/locale.js diff --git a/print/report/sepa-core/assets/css/index.js b/print/templates/email/sepa-core/assets/css/index.js similarity index 100% rename from print/report/sepa-core/assets/css/index.js rename to print/templates/email/sepa-core/assets/css/index.js diff --git a/print/report/sepa-core/index.html b/print/templates/email/sepa-core/index.html similarity index 100% rename from print/report/sepa-core/index.html rename to print/templates/email/sepa-core/index.html diff --git a/print/report/sepa-core/index.js b/print/templates/email/sepa-core/index.js similarity index 100% rename from print/report/sepa-core/index.js rename to print/templates/email/sepa-core/index.js diff --git a/print/report/sepa-core/locale.js b/print/templates/email/sepa-core/locale.js similarity index 100% rename from print/report/sepa-core/locale.js rename to print/templates/email/sepa-core/locale.js diff --git a/print/report/rpt-claim-pickup-order/assets/css/index.js b/print/templates/reports/receipt/assets/css/index.js similarity index 100% rename from print/report/rpt-claim-pickup-order/assets/css/index.js rename to print/templates/reports/receipt/assets/css/index.js diff --git a/print/report/rpt-receipt/assets/css/style.css b/print/templates/reports/receipt/assets/css/style.css similarity index 100% rename from print/report/rpt-receipt/assets/css/style.css rename to print/templates/reports/receipt/assets/css/style.css diff --git a/print/report/rpt-lcr/assets/images/signature.png b/print/templates/reports/receipt/assets/images/signature.png similarity index 100% rename from print/report/rpt-lcr/assets/images/signature.png rename to print/templates/reports/receipt/assets/images/signature.png diff --git a/print/report/rpt-receipt/index.js b/print/templates/reports/receipt/index.js similarity index 54% rename from print/report/rpt-receipt/index.js rename to print/templates/reports/receipt/index.js index a4f04ad34..93fdad6d5 100755 --- a/print/report/rpt-receipt/index.js +++ b/print/templates/reports/receipt/index.js @@ -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: '
{{$t('payed', [ - 'Silla', - receipt.payed.getDate(), - $t('months')[receipt.payed.getMonth()], - receipt.payed.getFullYear()]) - }} +