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() {
|
2024-10-22 10:02:04 +00:00
|
|
|
this.date = Date.vnNew();
|
2024-10-28 10:29:15 +00:00
|
|
|
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);
|
2024-10-22 10:02:04 +00:00
|
|
|
this.date = moment(this.date).format('WW/E');
|
2024-10-21 12:57:55 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getQr(data) {
|
|
|
|
data = {
|
2024-10-22 10:02:04 +00:00
|
|
|
company: this.company,
|
2024-10-21 12:57:55 +00:00
|
|
|
user: this.userId,
|
2024-10-22 10:02:04 +00:00
|
|
|
created: this.date,
|
2024-10-21 12:57:55 +00:00
|
|
|
table: 'buy',
|
|
|
|
id: data
|
|
|
|
};
|
|
|
|
return qrcode.toDataURL(JSON.stringify(data), {
|
|
|
|
margin: 0,
|
2024-10-22 10:02:04 +00:00
|
|
|
errorCorrectionLevel: 'L'
|
2024-10-21 12:57:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
id: {
|
|
|
|
type: Number,
|
|
|
|
required: true,
|
|
|
|
description: 'The item id'
|
|
|
|
},
|
2024-10-22 10:02:04 +00:00
|
|
|
warehouseId: {
|
|
|
|
type: Number
|
|
|
|
},
|
|
|
|
packing: {
|
|
|
|
type: Number
|
|
|
|
},
|
2024-10-21 12:57:55 +00:00
|
|
|
copies: {
|
|
|
|
type: Number
|
|
|
|
},
|
|
|
|
userId: {
|
|
|
|
type: Number
|
2024-10-28 10:29:15 +00:00
|
|
|
},
|
|
|
|
typeId: {
|
|
|
|
type: String
|
2024-10-21 12:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|