salix/print/templates/reports/client-debt-statement/client-debt-statement.js

81 lines
2.1 KiB
JavaScript
Executable File

const Component = require(`vn-print/core/component`);
const reportBody = new Component('report-body');
const reportFooter = new Component('report-footer');
module.exports = {
name: 'client-debt-statement',
async serverPrefetch() {
this.client = await this.fetchClient(this.id);
this.sales = await this.fetchSales(this.id, this.from);
if (!this.client)
throw new Error('Something went wrong');
},
computed: {
dated: function() {
const filters = this.$options.filters;
return filters.date(new Date(), '%d-%m-%Y');
}
},
data() {
return {totalBalance: 0.00};
},
methods: {
fetchClient(id) {
return this.findOneFromDef('client', [id]);
},
fetchSales(id, from) {
return this.rawSqlFromDef('sales', [
from,
id,
from,
id,
from,
id,
from,
id,
from,
id
]);
},
getBalance(sale) {
if (sale.debtOut)
this.totalBalance += parseFloat(sale.debtOut);
if (sale.debtIn)
this.totalBalance -= parseFloat(sale.debtIn);
return parseFloat(this.totalBalance.toFixed(2));
},
getTotalDebtOut() {
let debtOut = 0.00;
for (let sale of this.sales)
debtOut += sale.debtOut ? parseFloat(sale.debtOut) : 0;
return debtOut.toFixed(2);
},
getTotalDebtIn() {
let debtIn = 0.00;
for (let sale of this.sales)
debtIn += sale.debtIn ? parseFloat(sale.debtIn) : 0;
return debtIn.toFixed(2);
},
},
components: {
'report-body': reportBody.build(),
'report-footer': reportFooter.build()
},
props: {
id: {
type: Number,
required: true,
description: 'The client id'
},
from: {
required: true
}
}
};