2019-11-04 07:54:21 +00:00
|
|
|
const Component = require(`${appPath}/core/component`);
|
2019-11-07 10:19:05 +00:00
|
|
|
const db = require(`${appPath}/core/database`);
|
2019-11-04 07:54:21 +00:00
|
|
|
const reportHeader = new Component('report-header');
|
|
|
|
const reportFooter = new Component('report-footer');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
name: 'letter-debtor',
|
|
|
|
async serverPrefetch() {
|
|
|
|
this.client = await this.fetchClient(this.clientId);
|
|
|
|
this.sales = await this.fetchSales(this.clientId, this.companyId);
|
|
|
|
|
|
|
|
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(clientId) {
|
|
|
|
return db.findOne(
|
|
|
|
`SELECT
|
|
|
|
c.id,
|
|
|
|
c.socialName,
|
|
|
|
c.street,
|
|
|
|
c.postcode,
|
|
|
|
c.city,
|
|
|
|
c.fi,
|
|
|
|
p.name AS province,
|
|
|
|
ct.country
|
|
|
|
FROM client c
|
|
|
|
JOIN country ct ON ct.id = c.countryFk
|
|
|
|
LEFT JOIN province p ON p.id = c.provinceFk
|
|
|
|
WHERE c.id = ?`, [clientId]);
|
|
|
|
},
|
|
|
|
fetchSales(clientId, companyId) {
|
|
|
|
return db.find(
|
2019-11-07 10:19:05 +00:00
|
|
|
`CALL vn.clientGetDebtDiary(:clientId, :companyId)`, {
|
|
|
|
clientId: clientId,
|
|
|
|
companyId: companyId,
|
|
|
|
}).then(rows => {
|
2019-11-04 07:54:21 +00:00
|
|
|
return rows[0];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
this.sales.forEach(sale => {
|
|
|
|
debtOut += sale.debtOut ? parseFloat(sale.debtOut) : 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return debtOut.toFixed(2);
|
|
|
|
},
|
|
|
|
getTotalDebtIn() {
|
|
|
|
let debtIn = 0.00;
|
|
|
|
this.sales.forEach(sale => {
|
|
|
|
debtIn += sale.debtIn ? parseFloat(sale.debtIn) : 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return debtIn.toFixed(2);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
'report-header': reportHeader.build(),
|
|
|
|
'report-footer': reportFooter.build()
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
clientId: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
companyId: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|