const config = require(`vn-print/core/config`); const vnReport = require('../../../core/mixins/vn-report.js'); const md5 = require('md5'); const fs = require('fs-extra'); const qrcode = require('qrcode'); const prefixBase64 = 'data:image/png;base64,'; module.exports = { name: 'cmr', mixins: [vnReport], async serverPrefetch() { this.data = await this.findOneFromDef('data', [this.id]); if (this.data.ticketFk) { this.merchandises = await this.rawSqlFromDef('merchandise', [this.data.ticketFk]); this.signature = await this.findOneFromDef('signature', [this.data.ticketFk]); } else this.merchandises = null; this.senderStamp = (this.data.senderStamp) ? `${prefixBase64} ${this.data.senderStamp.toString('base64')}` : null; this.deliveryStamp = (this.data.deliveryStamp) ? `${prefixBase64} ${this.data.deliveryStamp.toString('base64')}` : null; this.truckPlateQr = await this.getQR(this.data.truckPlate); }, props: { id: { type: Number, required: true, description: 'The cmr id' }, }, computed: { signPath() { if (!this.signature) return; const signatureName = this.signature.signature; const hash = md5(signatureName.toString()).substring(0, 3); const file = `${config.storage.root}/${hash}/${signatureName}.png`; if (!fs.existsSync(file)) return null; return `${prefixBase64} ${Buffer.from(fs.readFileSync(file), 'utf8').toString('base64')}`; }, }, methods: { getQR(id) { const data = String(id); return qrcode.toDataURL(data, {margin: 0}); }, } };