const config = require(`${appPath}/core/config`); const Component = require(`${appPath}/core/component`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); const md5 = require('md5'); const fs = require('fs-extra'); module.exports = { name: 'delivery-note', async serverPrefetch() { this.client = await this.fetchClient(this.ticketId); this.ticket = await this.fetchTicket(this.ticketId); this.sales = await this.fetchSales(this.ticketId); this.address = await this.fetchAddress(this.ticketId); this.services = await this.fetchServices(this.ticketId); this.taxes = await this.fetchTaxes(this.ticketId); this.packagings = await this.fetchPackagings(this.ticketId); this.signature = await this.fetchSignature(this.ticketId); if (!this.ticket) throw new Error('Something went wrong'); }, data() { return {totalBalance: 0.00}; }, computed: { dmsPath() { if (!this.signature) return; const hash = md5(this.signature.id.toString()).substring(0, 3); const file = `${config.storage.root}/${hash}/${this.signature.id}.png`; if (!fs.existsSync(file)) return null; const src = fs.readFileSync(file); const base64 = Buffer.from(src, 'utf8').toString('base64'); return `data:image/png;base64, ${base64}`; }, serviceTotal() { let total = 0.00; this.services.forEach(service => { total += parseFloat(service.price) * service.quantity; }); return total; } }, methods: { fetchClient(ticketId) { return this.findOneFromDef('client', [ticketId]); }, fetchTicket(ticketId) { return this.findOneFromDef('ticket', [ticketId]); }, fetchAddress(ticketId) { return this.findOneFromDef(`address`, [ticketId]); }, fetchSignature(ticketId) { return this.findOneFromDef('signature', [ticketId]); }, fetchTaxes(ticketId) { return this.findOneFromDef(`taxes`, [ticketId]); }, fetchSales(ticketId) { return this.rawSqlFromDef('sales', [ticketId]); }, fetchPackagings(ticketId) { return this.rawSqlFromDef('packagings', [ticketId]); }, fetchServices(ticketId) { return this.rawSqlFromDef('services', [ticketId]); }, getSubTotal() { let subTotal = 0.00; this.sales.forEach(sale => { subTotal += sale.quantity * sale.price * (1 - sale.discount / 100); }); return subTotal; }, getTotalBase() { let totalBase = 0.00; this.taxes.forEach(tax => { totalBase += parseFloat(tax.Base); }); return totalBase; }, getTotalTax() { let totalTax = 0.00; this.taxes.forEach(tax => { totalTax += parseFloat(tax.tax); }); return totalTax; }, getTotal() { return this.getTotalBase() + this.getTotalTax(); }, getBotanical() { let phytosanitary = []; this.sales.forEach(sale => { if (sale.botanical) phytosanitary.push(sale.botanical); }); return phytosanitary.filter((item, index) => phytosanitary.indexOf(item) == index ).join(', '); }, footerType() { const translatedType = this.$t(this.type); return `${translatedType} ${this.ticketId}`; } }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { ticketId: { type: [Number, String], required: true }, type: { type: String, required: true } } };