salix/print/templates/reports/extra-community/extra-community.js

116 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-01-18 07:33:20 +00:00
const Component = require(`${appPath}/core/component`);
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
const db = require(`${appPath}/core/database`);
module.exports = {
name: 'extra-community',
async serverPrefetch() {
this.filters = this.$options.filters;
const args = {
landedFrom: this.landedStart,
landedTo: this.landedEnd,
shippedFrom: this.shippedStart,
shippedTo: this.shippedEnd,
continent: this.continent
};
const connection = await db.pool.getConnection();
await this.fetchTravels(args, connection);
const travels = await this.rawSql(`
SELECT * FROM tmp.travel`, connection);
const entries = await this.fetchEntries(connection);
await this.rawSql(`
DROP TEMPORARY TABLE
tmp.travel`, connection);
await connection.release();
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');
},
landedStart: function() {
if (!this.landedFrom) return;
return this.filters.date(this.landedFrom, '%Y-%m-%d');
},
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');
},
shippedEnd: function() {
if (!this.shippedTo) return;
return this.filters.date(this.shippedTo, '%Y-%m-%d');
}
},
methods: {
fetchTravels(args, connection) {
const where = db.buildWhere(args, (key, value) => {
switch (key) {
case 'shippedFrom':
return `t.shipped >= ${value}`;
case 'shippedTo':
return `t.shipped <= ${value}`;
case 'landedFrom':
return `t.landed >= ${value}`;
case 'landedTo':
return `t.landed <= ${value}`;
case 'continent':
return `cnt.code = ${value}`;
}
});
let query = this.getSqlFromDef('travels');
query = db.merge(query, where);
query = db.merge(query, 'GROUP BY t.id');
return this.rawSql(query, connection);
},
fetchEntries(connection) {
let query = this.getSqlFromDef('entries');
query = db.merge(query, 'GROUP BY e.id');
return this.rawSql(query, connection);
}
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: [
'landedFrom',
'landedTo',
'shippedFrom',
'shippedTo',
'continent'
]
};