const Component = require(`vn-print/core/component`); const reportBody = new Component('report-body'); const reportFooter = new Component('report-footer'); const db = require(`vn-print/core/database`); module.exports = { name: 'extra-community', async serverPrefetch() { this.filters = this.$options.filters; const args = { landedTo: this.landedEnd, shippedFrom: this.shippedStart, continent: this.continent, id: this.id, agencyModeFk: this.agencyModeFk, warehouseInFk: this.warehouseInFk, warehouseOutFk: this.warehouseOutFk, totalEntries: this.totalEntries, ref: this.ref, cargoSupplierFk: this.cargoSupplierFk }; const travels = await this.fetchTravels(args); const travelIds = travels.map(travel => travel.id); const entries = await this.fetchEntries(travelIds); const map = new Map(); for (let travel of travels) map.set(travel.id, travel); for (let entry of entries) { const travel = map.get(entry.travelFk); if (!travel.entries) travel.entries = []; travel.entries.push(entry); } this.travels = travels; if (!this.travels) throw new Error('Something went wrong'); }, computed: { dated: function() { return this.filters.date(new Date(), '%d-%m-%Y'); }, landedEnd: function() { if (!this.landedTo) return; return this.filters.date(this.landedTo, '%Y-%m-%d'); }, shippedStart: function() { if (!this.shippedFrom) return; return this.filters.date(this.shippedFrom, '%Y-%m-%d'); } }, methods: { fetchTravels(args) { const where = db.buildWhere(args, (key, value) => { switch (key) { case 'shippedFrom': return `t.shipped >= ${value}`; case 'landedTo': return `t.landed <= ${value}`; case 'continent': return `cnt.code = ${value}`; case 'ref': return {'t.ref': {like: `%${value}%`}}; case 'id': return `t.id = ${value}`; case 'agencyModeFk': return `am.id = ${value}`; case 'warehouseOutFk': return `wo.id = ${value}`; case 'warehouseInFk': return `w.id = ${value}`; case 'cargoSupplierFk': return `s.id = ${value}`; } }); let query = this.getSqlFromDef('travels'); query = db.merge(query, where); query = db.merge(query, 'GROUP BY t.id'); query = db.merge(query, ` ORDER BY shipped ASC, landed ASC, travelFk, loadPriority, agencyModeFk, evaNotes `); return this.rawSql(query); }, fetchEntries(travelIds) { return this.rawSqlFromDef('entries', [travelIds]); } }, components: { 'report-body': reportBody.build(), 'report-footer': reportFooter.build() }, props: [ 'landedTo', 'shippedFrom', 'continent', 'reference', 'id', 'agencyModeFk', 'warehouseOutFk', 'warehouseInFk', 'totalEntries', 'cargoSupplierFk' ] };