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

102 lines
3.1 KiB
JavaScript
Executable File

const vnReport = require('../../../core/mixins/vn-report.js');
const db = require(`vn-print/core/database`);
module.exports = {
name: 'extra-community',
mixins: [vnReport],
async serverPrefetch() {
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);
this.checkMainEntity(travels);
const travelIds = travels.map(travel => travel.id);
const entries = await this.rawSqlFromDef('entries', [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;
},
computed: {
landedEnd: function() {
if (!this.landedTo) return;
return formatDate(this.landedTo, '%Y-%m-%d');
},
shippedStart: function() {
if (!this.shippedFrom) return;
return formatDate(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);
},
},
props: [
'landedTo',
'shippedFrom',
'continent',
'reference',
'id',
'agencyModeFk',
'warehouseOutFk',
'warehouseInFk',
'totalEntries',
'cargoSupplierFk'
]
};