const Component = require(`${appPath}/core/component`); const db = require(`${appPath}/core/database`); 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.itemId, this.warehouseId); this.tags = await this.fetchItemTags(this.itemId); this.barcode = await this.getBarcodeBase64(this.itemId); 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 db.findOne( `SELECT i.id, i.name, i.stems, i.size, b.packing FROM vn.item i JOIN cache.last_buy clb ON clb.item_id = i.id JOIN vn.buy b ON b.id = clb.buy_id JOIN vn.entry e ON e.id = b.entryFk WHERE i.id = ? AND clb.warehouse_id = ?`, [id, warehouseId]); }, fetchItemTags(itemId) { return db.find( `SELECT t.code, t.name, it.value FROM vn.itemTag it JOIN vn.tag t ON t.id = it.tagFk WHERE it.itemFk = ? `, [itemId]).then(rows => { const tags = {}; rows.forEach(row => tags[row.code] = row.value); return tags; }); }, getBarcodeBase64(itemId) { return qrcode.toDataURL(itemId, {margin: 0}); }, packing() { const stems = this.item.stems ? this.item.stems : 1; return `${this.item.packing}x${stems}`; } }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { itemId: { required: true }, warehouseId: { required: true }, labelNumber: { type: String }, totalLabels: { type: String } } };