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

102 lines
3.0 KiB
JavaScript
Executable File

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 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');
},
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) {
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);
},
fetchEntries(travelIds) {
return this.rawSqlFromDef('entries', [travelIds]);
}
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: [
'landedFrom',
'landedTo',
'shippedFrom',
'shippedTo',
'continent'
]
};