const config = require(`vn-print/core/config`); const Component = require(`vn-print/core/component`); const reportBody = new Component('report-body'); 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.id); this.ticket = await this.fetchTicket(this.id); this.sales = await this.fetchSales(this.id); this.address = await this.fetchAddress(this.id); this.services = await this.fetchServices(this.id); this.taxes = await this.fetchTaxes(this.id); this.packagings = await this.fetchPackagings(this.id); this.signature = await this.fetchSignature(this.id); 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}`; }, deliverNoteType() { return this.type ? this.type : 'deliveryNote'; }, serviceTotal() { let total = 0.00; this.services.forEach(service => { total += parseFloat(service.price) * service.quantity; }); return total; }, showPrices() { return this.deliverNoteType != 'withoutPrices'; }, footerType() { const translatedType = this.$t(this.deliverNoteType); return `${translatedType} ${this.id}`; }, hasObservations() { return this.ticket.description !== null; } }, methods: { fetchClient(id) { return this.findOneFromDef('client', [id]); }, fetchTicket(id) { return this.findOneFromDef('ticket', [id]); }, fetchAddress(id) { return this.findOneFromDef(`address`, [id]); }, fetchSignature(id) { return this.findOneFromDef('signature', [id]); }, fetchTaxes(id) { return this.findOneFromDef(`taxes`, [id]); }, fetchSales(id) { return this.rawSqlFromDef('sales', [id]); }, fetchPackagings(id) { return this.rawSqlFromDef('packagings', [id]); }, fetchServices(id) { return this.rawSqlFromDef('services', [id]); }, 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(', '); } }, components: { 'report-body': reportBody.build(), 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { id: { type: Number, required: true, description: 'The ticket id' }, type: { type: String, required: false } } };