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.findOne( `SELECT c.id, c.socialName, c.street, c.fi FROM ticket t JOIN client c ON c.id = t.clientFk WHERE t.id = ?`, [ticketId]); }, fetchTicket(ticketId) { return db.findOne( `SELECT t.id, t.shipped, t.companyFk FROM ticket t WHERE t.id = ?`, [ticketId]); }, fetchAddress(ticketId) { return db.findOne( `SELECT a.nickname, a.street, a.postalCode, a.city, p.name province FROM ticket t JOIN address a ON a.clientFk = t.clientFk LEFT JOIN province p ON p.id = a.provinceFk WHERE t.id = ?`, [ticketId]); }, fetchSales(ticketId) { return db.find( `SELECT s.id, s.itemFk, s.concept, s.quantity, s.price, s.price - SUM(IF(ctr.id = 6, sc.value, 0)) netPrice, s.discount, i.size, i.stems, i.category, it.id itemTypeId, o.code AS origin, i.inkFk, s.ticketFk, tcl.code vatType, ibwg.ediBotanic, ppa.denomination, pp.number passportNumber, be.isProtectedZone, c.code AS countryCode, i.tag5, i.value5, i.tag6, i.value6, i.tag7, i.value7 FROM vn.sale s LEFT JOIN saleComponent sc ON sc.saleFk = s.id LEFT JOIN component cr ON cr.id = sc.componentFk LEFT JOIN componentType ctr ON ctr.id = cr.typeFk LEFT JOIN item i ON i.id = s.itemFk LEFT JOIN ticket t ON t.id = s.ticketFk LEFT JOIN origin o ON o.id = i.originFk LEFT JOIN country c ON c.id = o.countryFk LEFT JOIN supplier sp ON sp.id = t.companyFk LEFT JOIN itemType it ON it.id = i.typeFk LEFT JOIN itemTaxCountry itc ON itc.itemFk = i.id AND itc.countryFk = sp.countryFk LEFT JOIN taxClass tcl ON tcl.id = itc.taxClassFk LEFT JOIN plantpassport pp ON pp.producerFk = i.producerFk LEFT JOIN plantpassportAuthority ppa ON ppa.id = pp.plantpassportAuthorityFk LEFT JOIN itemBotanicalWithGenus ibwg ON ibwg.itemFk = i.id LEFT JOIN botanicExport be ON be.restriction = 'pasaporte fitosanitario' LEFT JOIN ediGenus eg ON eg.id = be.ediGenusFk LEFT JOIN ediSpecie es ON es.id = be.ediSpecieFk AND ibwg.ediBotanic LIKE CONCAT( IFNULL(eg.latinGenusName, ''), IF(es.latinSpeciesName > '', CONCAT(' ', es.latinSpeciesName), ''), '%') WHERE s.ticketFk = ? GROUP BY s.id ORDER BY (it.isPackaging), s.concept, s.itemFk`, [ticketId]); }, fetchTaxes(ticketId) { return db.find(`CALL vn.ticketGetTaxAdd(?)`, [ticketId]).then(rows => { return rows[0]; }); }, fetchPackagings(ticketId) { return db.find( `SELECT tp.quantity, i.name, p.itemFk FROM ticketPackaging tp JOIN packaging p ON p.id = tp.packagingFk JOIN item i ON i.id = p.itemFk WHERE tp.ticketFk = ? ORDER BY itemFk`, [ticketId]); }, fetchServices(ticketId) { return db.find( `SELECT tc.description taxDescription, ts.description, ts.quantity, ts.price FROM ticketService ts JOIN taxClass tc ON tc.id = ts.taxClassFk WHERE ts.ticketFk = ?`, [ticketId]); }, fetchSignature(ticketId) { return db.findOne( `SELECT d.id, d.created FROM ticket t JOIN ticketDms dt ON dt.ticketFk = t.id JOIN dms d ON d.id = dt.dmsFk AND d.file LIKE '%.png' WHERE t.id = ?`, [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(); }, }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { ticketId: { type: String, required: true } } };