const {DOMImplementation, XMLSerializer} = require('xmldom'); const vnReport = require('../../../core/mixins/vn-report.js'); const {toDataURL} = require('qrcode'); const jsBarcode = require('jsbarcode'); module.exports = { name: 'collection-label', mixins: [vnReport], 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]; const labels = await this.rawSqlFromDef('labelsData', [ticketIds]); const [{scannableCodeType}] = await this.rawSqlFromDef('barcodeType'); if (scannableCodeType === 'qr') { for (const labelData of labels) labelData.qrData = await this.getQr(labelData?.ticketFk, labelData?.workerFk); } this.labelsData = labels; this.checkMainEntity(this.labelsData); }, methods: { async getQr(ticketFk, workerFk = null) { const QRdata = JSON.stringify({ company: 'vnl', user: workerFk, created: Date.vnNew(), table: 'ticket', id: ticketFk }); return toDataURL(QRdata, {margin: 0}); }, 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; }, getAgencyDescription(labelData) { let value; if (labelData.agencyDescription) value = labelData.agencyDescription.toUpperCase().substring(0, 11); if (labelData.routeFk) { let routeFk = labelData.routeFk.toString(); value = `${value} [${routeFk.substring(routeFk.length - 3)}]`; } return value; }, dashIfEmpty(value) { return value || '---'; } }, };