const config = require(`${appPath}/core/config`); const db = require(`${appPath}/core/database`); const Component = require(`${appPath}/core/component`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); const md5 = require('md5'); 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() { const pathHash = md5(this.signature.id.toString()).substring(0, 3); const hostPath = `file:///${config.storage.root}/${pathHash}`; if (this.signature && this.signature.id) return `${hostPath}/${this.signature.id}.png`; }, serviceTotal() { let total = 0.00; this.services.forEach(service => { total += parseFloat(service.price) * service.quantity; }); return total; } }, methods: { fetchClient(ticketId) { return db.findOneFromDef('getClient', [ticketId], __dirname); }, fetchTicket(ticketId) { return db.findOneFromDef('getTicket', [ticketId], __dirname); }, fetchAddress(ticketId) { return db.findOneFromDef(`getAddress`, [ticketId], __dirname); }, fetchSignature(ticketId) { return db.findOneFromDef('getSignature', [ticketId], __dirname); }, fetchTaxes(ticketId) { return db.findOneFromDef(`getTaxes`, [ticketId], __dirname); }, fetchSales(ticketId) { return db.rawSqlFromDef('getSales', [ticketId], __dirname); }, fetchPackagings(ticketId) { return db.rawSqlFromDef('getPackagings', [ticketId], __dirname); }, fetchServices(ticketId) { return db.rawSqlFromDef('getServices', [ticketId], __dirname); }, 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(); } }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { ticketId: { type: String, required: true } } };