salix/print/templates/reports/item-label-qr/item-label-qr.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-10-21 12:57:55 +00:00
const UserError = require('vn-loopback/util/user-error');
const moment = require('moment');
const qrcode = require('qrcode');
module.exports = {
name: 'item-label-qr',
async serverPrefetch() {
this.date = Date.vnNew();
if (this.typeId === 'item') {
this.company = await this.findOneFromDef('company', [this.warehouseId]);
if (!this.company)
throw new UserError(`There is no company associated with that warehouse`);
this.lastBuy = await this.findOneFromDef('lastBuy', [
this.id,
this.warehouseId,
this.date
]);
}
this.items = await this.rawSqlFromDef('item', [this.copies || 1, this.lastBuy?.id || this.id]);
2024-10-21 12:57:55 +00:00
if (!this.items.length) throw new UserError(`Empty data source`);
this.qr = await this.getQr(this.items[0].buyFk);
this.date = moment(this.date).format('WW/E');
2024-10-21 12:57:55 +00:00
},
methods: {
getQr(data) {
data = {
company: this.company,
2024-10-21 12:57:55 +00:00
user: this.userId,
created: this.date,
2024-10-21 12:57:55 +00:00
table: 'buy',
id: data
};
return qrcode.toDataURL(JSON.stringify(data), {
margin: 0,
errorCorrectionLevel: 'L'
2024-10-21 12:57:55 +00:00
});
}
},
props: {
id: {
type: Number,
required: true,
description: 'The item id'
},
warehouseId: {
type: Number
},
packing: {
type: Number
},
2024-10-21 12:57:55 +00:00
copies: {
type: Number
},
userId: {
type: Number
},
typeId: {
type: String
2024-10-21 12:57:55 +00:00
}
}
};