salix/print/templates/reports/supplier-campaign-metrics/supplier-campaign-metrics.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-12-16 07:05:41 +00:00
const Component = require(`${appPath}/core/component`);
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
module.exports = {
name: 'supplier-campaign-metrics',
async serverPrefetch() {
this.supplier = await this.fetchSupplier(this.recipientId);
let entries = await this.fetchEntries(this.recipientId, this.from, this.to);
const entriesId = [];
for (let entry of entries)
entriesId.push(entry.id);
const buys = await this.fetchBuys(entriesId);
const entriesMap = new Map();
for (let entry of entries)
entriesMap.set(entry.id, entry);
for (let buy of buys) {
const entry = entriesMap.get(buy.entryFk);
if (entry) {
if (!entry.buys) entry.buys = [];
entry.buys.push(buy);
}
}
this.entries = entries;
if (!this.supplier)
throw new Error('Something went wrong');
},
methods: {
fetchSupplier(supplierId) {
return this.findOneFromDef('supplier', [supplierId]);
},
fetchEntries(supplierId, from, to) {
return this.rawSqlFromDef('entries', [supplierId, from, to]);
},
fetchBuys(entriesId) {
return this.rawSqlFromDef('buys', {entriesId});
}
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: {
recipientId: {
required: true
},
from: {
required: true
},
to: {
required: true
}
}
};