const database = require(`${appPath}/lib/database`); const UserException = require(`${appPath}/lib/exceptions/userException`); const strftime = require('strftime'); const qrcode = require('qrcode'); module.exports = { name: 'rpt-item-label', async asyncData(ctx, params) { Object.assign(this, this.methods); if (!params.itemId) throw new UserException('No item id specified'); if (!params.warehouseId) throw new UserException('No warehouse id specified'); const data = { item: await this.fetchItem(params.itemId, params.warehouseId), tags: await this.fetchItemTags(params.itemId), barcode: await this.getBarcodeBase64(params.itemId), labelNumber: params.labelNumber, totalLabels: params.totalLabels }; return data; }, computed: { dated() { return strftime('%W/%d', new Date()); } }, methods: { fetchItem(id, warehouseId) { return database.pool.query( `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]) .then(([rows]) => { if (rows.length == 0) throw new UserException(`Item #${id} not found on warehouse #${warehouseId}`); return rows[0]; }); }, fetchItemTags(itemId) { return database.pool.query( `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}`; }, labelPage() { const labelNumber = this.labelNumber ? this.labelNumber : 1; const totalLabels = this.totalLabels ? this.totalLabels : 1; return `${labelNumber}/${totalLabels}`; } }, components: { 'report-header': require('../report-header'), 'report-footer': require('../report-footer'), }, };