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

79 lines
2.4 KiB
JavaScript
Executable File

const Component = require(`${appPath}/core/component`);
const db = require(`${appPath}/core/database`);
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
module.exports = {
name: 'campaign-metrics',
async serverPrefetch() {
this.to = new Date(this.to);
this.from = new Date(this.from);
this.client = await this.fetchClient(this.clientId);
this.sales = await this.fetchSales(this.clientId, this.from, this.to);
if (!this.client)
throw new Error('Something went wrong');
},
methods: {
fetchClient(clientId) {
return db.findOne(
`SELECT
c.street,
c.socialName,
c.city,
c.postcode,
c.id,
c.name AS clientName,
p.name AS province,
co.country
FROM client c
JOIN province p ON c.provinceFk = p.id
JOIN country co ON c.countryFk = co.id
WHERE
c.id = ?`, [clientId]);
},
fetchSales(clientId, from, to) {
return db.rawSql(
`SELECT
SUM(s.quantity) AS subtotal,
s.itemFk,
s.concept,
i.subName,
i.tag5,
i.value5,
i.tag6,
i.value6,
i.tag7,
i.value7
FROM sale s
JOIN ticket t ON t.id = s.ticketFk
JOIN item i ON i.id = s.itemFk
JOIN itemType it ON it.id = i.typeFk
WHERE
t.clientFk = ? AND it.isPackaging = FALSE
AND DATE(t.shipped) BETWEEN ? AND ?
GROUP BY s.itemFk
ORDER BY i.typeFk , i.name , i.size`, [clientId, from, to]);
},
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: {
clientId: {
required: true
},
from: {
required: true,
type: Date
},
to: {
required: true,
type: Date
}
}
};