const UserError = require('vn-loopback/util/user-error'); const {DOMImplementation, XMLSerializer} = require('xmldom'); const moment = require('moment'); const jsbarcode = require('jsbarcode'); module.exports = { name: 'item-label-barcode', async serverPrefetch() { this.company = await this.findOneFromDef('company', [this.warehouseId]); if (!this.company) throw new UserError(`There is no company associated with that warehouse`); this.date = Date.vnNew(); this.lastBuy = await this.findOneFromDef('lastBuy', [ this.id, this.warehouseId, this.date ]); this.items = await this.rawSqlFromDef('item', [this.copies || 1, this.lastBuy.id]); if (!this.items.length) throw new UserError(`Empty data source`); this.date = moment(this.date).format('WW/E'); }, methods: { getBarcode(data) { 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, data, { xmlDocument: document, format: 'code128', displayValue: false, width: 3.8, height: 85, margin: 0 }); return new XMLSerializer().serializeToString(svgNode); } }, props: { id: { type: Number, required: true, description: 'The item id' }, warehouseId: { type: Number }, packing: { type: Number }, copies: { type: Number }, userId: { type: Number } } };