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

76 lines
2.1 KiB
JavaScript
Executable File

const Component = require(`vn-print/core/component`);
const reportBody = new Component('report-body');
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
const qrcode = require('qrcode');
module.exports = {
name: 'item-label',
async serverPrefetch() {
this.item = await this.fetchItem(this.id, this.warehouseId);
this.tags = await this.fetchItemTags(this.id);
this.barcode = await this.getBarcodeBase64(this.id);
if (!this.item)
throw new Error('Something went wrong');
},
computed: {
dated() {
const filters = this.$options.filters;
return filters.date(new Date(), '%W/%d');
},
labelPage() {
const labelNumber = this.labelNumber ? this.labelNumber : 1;
const totalLabels = this.totalLabels ? this.totalLabels : 1;
return `${labelNumber}/${totalLabels}`;
}
},
methods: {
fetchItem(id, warehouseId) {
return this.findOneFromDef('item', [id, warehouseId]);
},
fetchItemTags(id) {
return this.rawSqlFromDef('itemTags', [id]).then(rows => {
const tags = {};
rows.forEach(row => tags[row.code] = row.value);
return tags;
});
},
getBarcodeBase64(id) {
const data = String(id);
return qrcode.toDataURL(data, {margin: 0});
},
packing() {
const stems = this.item.stems ? this.item.stems : 1;
return `${this.item.packing}x${stems}`;
}
},
components: {
'report-body': reportBody.build(),
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: {
id: {
type: Number,
required: true,
description: 'The item id'
},
warehouseId: {
type: Number,
required: true
},
labelNumber: {
type: Number
},
totalLabels: {
type: Number
}
}
};