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

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-01-23 12:15:30 +00:00
const vnReport = require('../../../core/mixins/vn-report.js');
2020-12-16 07:05:41 +00:00
module.exports = {
name: 'supplier-campaign-metrics',
2023-01-23 12:15:30 +00:00
mixins: [vnReport],
2020-12-16 07:05:41 +00:00
async serverPrefetch() {
2023-01-23 12:15:30 +00:00
this.supplier = await this.findOneFromDef('supplier', [this.id]);
this.checkMainEntity(this.supplier);
let entries = await this.rawSqlFromDef('entries', [this.id, this.from, this.to]);
2024-04-22 10:54:48 +00:00
this.total = {quantity: 0, price: 0};
2020-12-16 07:05:41 +00:00
const entriesId = [];
for (let entry of entries)
entriesId.push(entry.id);
2023-01-23 12:15:30 +00:00
const buys = await this.rawSqlFromDef('buys', [entriesId]);
2020-12-16 07:05:41 +00:00
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 = [];
2024-04-22 10:54:48 +00:00
this.total.quantity = this.total.quantity + buy.quantity;
this.total.price = this.total.price + (buy.price * buy.quantity);
2020-12-16 07:05:41 +00:00
entry.buys.push(buy);
}
}
this.entries = entries;
},
2020-12-16 07:05:41 +00:00
props: {
2022-09-26 11:33:27 +00:00
id: {
2022-10-28 13:53:57 +00:00
type: Number,
2022-09-26 11:33:27 +00:00
required: true,
description: 'The supplier id'
2020-12-16 07:05:41 +00:00
},
from: {
required: true
},
to: {
required: true
}
}
};