const strftime = require('strftime'); const database = require(`${appPath}/lib/database`); const UserException = require(`${appPath}/lib/exceptions/userException`); module.exports = { name: 'rpt-letter-debtor', async asyncData(ctx, params) { const promises = []; const data = {}; if (!params.clientFk) throw new UserException('No client id specified'); if (!params.companyFk) throw new UserException('No company id specified'); promises.push(this.methods.fetchClient(params.clientFk)); promises.push(this.methods.fetchSales(params.clientFk, params.companyFk)); return Promise.all(promises).then(result => { const [[client]] = result[0]; const [[sales]] = result[1]; if (!client) throw new UserException('No client data found'); Object.assign(data, client, {sales}); return data; }); }, created() { if (this.locale) this.$i18n.locale = this.locale; }, data() { return {totalBalance: 0.00}; }, methods: { fetchClient(clientFk) { return database.pool.query( `SELECT c.id clientId, u.lang locale, c.email AS recipient, c.socialName AS clientName, c.street, c.postcode, c.city, c.fi, p.name AS province, ct.country FROM client c JOIN account.user u ON u.id = c.id JOIN country ct ON ct.id = c.countryFk LEFT JOIN province p ON p.id = c.provinceFk WHERE c.id = ?`, [clientFk]); }, fetchSales(clientFk, companyFk) { return database.pool.query( `CALL vn.clientGetDebtDiary(?, ?)`, [clientFk, companyFk]); }, dated: () => { return strftime('%d-%m-%Y', new Date()); }, toISOString: date => { return strftime('%d-%m-%Y', date); }, 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': require('../report-header'), 'report-footer': require('../report-footer'), }, };