const Report = require(`vn-print/core/report`); const vnReport = require('../../../core/mixins/vn-report.js'); const invoiceIncoterms = new Report('invoice-incoterms'); module.exports = { name: 'invoice', mixins: [vnReport], async serverPrefetch() { this.invoice = await this.findOneFromDef('invoice', [this.reference]); this.checkMainEntity(this.invoice); this.client = await this.findOneFromDef('client', [this.reference]); this.taxes = await this.rawSqlFromDef(`taxes`, [this.reference]); this.hasIntrastat = await this.findValueFromDef(`hasIntrastat`, [this.reference]); this.intrastat = await this.rawSqlFromDef(`intrastat`, [this.reference, this.reference, this.reference, this.reference]); this.rectified = await this.rawSqlFromDef(`rectified`, [this.reference]); this.hasIncoterms = await this.findValueFromDef(`hasIncoterms`, [this.reference]); const tickets = await this.rawSqlFromDef('tickets', [this.reference]); const sales = await this.rawSqlFromDef('sales', [this.reference, this.reference]); const map = new Map(); this.ticketObservations = ''; for (let ticket of tickets) { ticket.sales = []; map.set(ticket.id, ticket); if (ticket.description) this.ticketObservations += ticket.description + ' '; } this.ticketObservations = this.ticketObservations.trim(); for (let sale of sales) { const ticket = map.get(sale.ticketFk); if (ticket) ticket.sales.push(sale); } this.tickets = tickets; }, data() { return {totalBalance: 0.00}; }, computed: { ticketsId() { const tickets = this.tickets.map(ticket => ticket.id); return tickets.join(', '); }, botanical() { let phytosanitary = []; for (let ticket of this.tickets) { for (let sale of ticket.sales) { if (sale.botanical) phytosanitary.push(sale.botanical); } } return phytosanitary.filter((item, index) => phytosanitary.indexOf(item) == index ).join(', '); }, taxTotal() { const base = this.sumTotal(this.taxes, 'base'); const vat = this.sumTotal(this.taxes, 'vat'); return base + vat; } }, methods: { saleImport(sale) { const price = sale.quantity * sale.price; return price * (1 - sale.discount / 100); }, ticketSubtotal(ticket) { let subTotal = 0.00; for (let sale of ticket.sales) subTotal += this.saleImport(sale); return subTotal; }, sumTotal(rows, prop) { let total = 0.00; for (let row of rows) total += parseFloat(row[prop]); return total; } }, components: { 'invoice-incoterms': invoiceIncoterms.build() }, props: { reference: { type: String, description: 'The invoice ref' } } };