const UserError = require('vn-loopback/util/user-error'); const moment = require('moment'); const qrcode = require('qrcode'); module.exports = { name: 'buy-label-qr', async serverPrefetch() { this.date = Date.vnNew(); this.buys = await this.rawSqlFromDef('buy', [this.copies || 1, this.id]); if (!this.buys.length) throw new UserError(`Empty data source`); this.qr = await this.getQr(this.buys[0].buyFk); this.date = moment(this.date).format('WW/DD'); }, methods: { getQr(data) { data = { company: this.buys.company, user: this.userId, created: this.date, table: 'buy', id: data }; return qrcode.toDataURL(JSON.stringify(data), { margin: 0, errorCorrectionLevel: 'L' }); }, formatNumber(number) { return new Intl.NumberFormat('es-ES', {maximumFractionDigits: 0}).format(number); } }, props: { id: { type: Number, required: true, description: 'The item id' }, warehouseId: { type: Number }, packing: { type: Number }, copies: { type: Number }, userId: { type: Number }, typeId: { type: String } } };