const Component = require(`vn-print/core/component`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); module.exports = { name: 'invoiceIn', async serverPrefetch() { this.invoice = await this.fetchInvoice(this.id); this.taxes = await this.fetchTaxes(this.id); if (!this.invoice) throw new Error('Something went wrong'); const entries = await this.fetchEntry(this.id); const buys = await this.fetchBuy(this.id); const map = new Map(); for (let entry of entries) { entry.buys = []; map.set(entry.id, entry); } for (let buy of buys) { const entry = map.get(buy.entryFk); if (entry) entry.buys.push(buy); } this.entries = entries; }, computed: { }, methods: { fetchInvoice(id) { return this.findOneFromDef('invoice', [id]); }, fetchEntry(id) { return this.rawSqlFromDef('entry', [id]); }, fetchBuy(id) { return this.rawSqlFromDef('buy', [id]); }, fetchTaxes(id) { return this.rawSqlFromDef(`taxes`, [id]); }, buyImport(buy) { return buy.quantity * buy.buyingValue; }, entrySubtotal(entry) { let subTotal = 0.00; for (let buy of entry.buys) subTotal += this.buyImport(buy); return subTotal; }, sumTotal(rows, prop) { let total = 0.00; for (let row of rows) total += parseFloat(row[prop]); return total; } }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build(), }, props: { id: { type: [Number, String], description: 'The invoice id' } } };