salix/print/templates/reports/cmr/cmr.js

53 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2023-08-02 05:35:40 +00:00
const config = require(`vn-print/core/config`);
const vnReport = require('../../../core/mixins/vn-report.js');
const md5 = require('md5');
const fs = require('fs-extra');
2024-03-14 07:36:19 +00:00
const qrcode = require('qrcode');
2023-08-02 05:35:40 +00:00
2023-08-02 11:41:44 +00:00
const prefixBase64 = 'data:image/png;base64,';
2023-08-02 05:35:40 +00:00
module.exports = {
2024-03-14 07:36:19 +00:00
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;
2023-08-02 11:41:44 +00:00
2024-03-14 07:36:19 +00:00
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;
2023-08-02 05:35:40 +00:00
2024-03-14 07:36:19 +00:00
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;
2023-08-02 05:35:40 +00:00
2024-03-14 07:36:19 +00:00
return `${prefixBase64} ${Buffer.from(fs.readFileSync(file), 'utf8').toString('base64')}`;
},
},
methods: {
getQR(id) {
return qrcode.toDataURL(String(id), {margin: 0});
},
}
};