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

111 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 = {
landedTo: this.landedEnd,
shippedFrom: this.shippedStart,
2022-03-28 09:57:19 +00:00
continent: this.continent,
2022-03-28 11:24:26 +00:00
id: this.id,
agencyFk: this.agencyFk,
warehouseInFk: this.warehouseInFk,
warehouseOutFk: this.warehouseOutFk,
totalEntries: this.totalEntries,
ref: this.ref,
cargoSupplierFk: this.cargoSupplierFk
2021-01-18 07:33:20 +00:00
};
2021-06-16 11:04:54 +00:00
const travels = await this.fetchTravels(args);
2021-06-16 11:14:13 +00:00
const travelIds = travels.map(travel => travel.id);
const entries = await this.fetchEntries(travelIds);
2021-01-18 07:33:20 +00:00
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: {
2021-06-16 11:04:54 +00:00
fetchTravels(args) {
2021-01-18 07:33:20 +00:00
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}`;
2022-03-28 11:24:26 +00:00
case 'ref':
return {'t.ref': {like: `%${value}%`}};
case 'id':
return `t.id = ${value}`;
case 'agencyFk':
return `am.id = ${value}`;
2022-03-28 09:57:19 +00:00
case 'warehouseOutFk':
return `wo.id = ${value}`;
2022-03-28 11:24:26 +00:00
case 'warehouseInFk':
return `w.id = ${value}`;
case 'cargoSupplierFk':
return `s.id = ${value}`;
2021-01-18 07:33:20 +00:00
}
});
let query = this.getSqlFromDef('travels');
query = db.merge(query, where);
query = db.merge(query, 'GROUP BY t.id');
2022-03-28 11:24:26 +00:00
query = db.merge(query, 'ORDER BY `shipped` ASC, `landed` ASC, `travelFk`, `loadPriority`, `agencyModeFk`, `evaNotes`');
2021-01-18 07:33:20 +00:00
2021-06-16 11:04:54 +00:00
return this.rawSql(query);
2021-01-18 07:33:20 +00:00
},
2021-06-16 11:14:13 +00:00
fetchEntries(travelIds) {
return this.rawSqlFromDef('entries', [travelIds]);
2021-01-18 07:33:20 +00:00
}
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: [
'landedTo',
'shippedFrom',
2022-03-28 09:57:19 +00:00
'continent',
2022-03-28 11:24:26 +00:00
'ref',
'id',
'agencyFk',
'warehouseOutFk',
'warehouseInFk',
'totalEntries',
'cargoSupplierFk'
2021-01-18 07:33:20 +00:00
]
};