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' }, labelCount: { type: Number, required: false, description: 'The number of labels' } }, 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: 115, }); return xmlSerializer.serializeToString(svgNode); }, getVertical(labelData) { let value; if (labelData.collectionFk) { value = `${labelData.collectionFk} ~ `; if (labelData.code == 'V') value = value + `${labelData.wagon}-${labelData.level}`; else value = value + `${labelData.color.substring(0, 4)}`; } else value = '-'.repeat(19); return value; }, }, components: { 'report-body': reportBody.build() }, };