salix/print/templates/reports/collection-label/collection-label.js

52 lines
1.6 KiB
JavaScript

const Component = require(`vn-print/core/component`);
const reportBody = new Component('report-body');
const jsBarcode = require('jsbarcode');
const {DOMImplementation, XMLSerializer} = require('xmldom');
const UserError = require('vn-loopback/util/user-error');
module.exports = {
name: 'collection-label',
props: {
id: {
type: Number,
required: true,
description: 'The ticket or collection id'
}
},
async serverPrefetch() {
let ticketIds;
const res = await this.rawSqlFromDef('tickets', [this.id]);
if (res.length) {
ticketIds = [];
for (const row of res)
ticketIds.push(row.ticketFk);
} else
ticketIds = [this.id];
this.labelsData = await this.rawSqlFromDef('labelsData', [ticketIds]);
if (!this.labelsData.length)
throw new UserError('Empty data source');
},
methods: {
getBarcode(id) {
const xmlSerializer = new XMLSerializer();
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
jsBarcode(svgNode, id, {
xmlDocument: document,
format: 'code128',
displayValue: false,
width: 3.8,
height: 110,
});
return xmlSerializer.serializeToString(svgNode);
},
},
components: {
'report-body': reportBody.build()
},
};