From 50b184a9fc451b6a6f6f64ce86d4ce0f702667e0 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Wed, 4 Dec 2019 22:53:28 +0530 Subject: [PATCH] fix: Cashflow - Simple cashflow from Bank and Cash accounts --- reports/Cashflow/Cashflow.js | 83 +++---------------- .../FinancialStatements.js | 3 +- src/pages/Dashboard/Cashflow.vue | 4 +- 3 files changed, 14 insertions(+), 76 deletions(-) diff --git a/reports/Cashflow/Cashflow.js b/reports/Cashflow/Cashflow.js index 80fed038..1f5a5d10 100644 --- a/reports/Cashflow/Cashflow.js +++ b/reports/Cashflow/Cashflow.js @@ -1,56 +1,26 @@ -const { getData } = require('../FinancialStatements/FinancialStatements'); -const ProfitAndLoss = require('../ProfitAndLoss/ProfitAndLoss'); +const { getPeriodList } = require('../FinancialStatements/FinancialStatements'); const { DateTime } = require('luxon'); class Cashflow { async run({ fromDate, toDate, periodicity }) { - let income = await getData({ - rootType: 'Income', - balanceMustBe: 'Credit', - fromDate, - toDate, - periodicity - }); - - let expense = await getData({ - rootType: 'Expense', - balanceMustBe: 'Debit', - fromDate, - toDate, - periodicity - }); - - let cashflow = await frappe.db.sql( + let res = await frappe.db.sql( ` - select sum(credit) as inflow, sum(debit) as outflow, strftime("%m-%Y", date) as "month-year" - from AccountingLedgerEntry - where account in ( - select name from Account where accountType in ('Receivable', 'Payable', 'Stock', 'Fixed Asset', 'Equity') - ) - and date >= $fromDate and date <= $toDate + select sum(debit) as inflow, sum(credit) as outflow, strftime("%m-%Y", date) as "month-year" + from AccountingLedgerEntry + where account in ( + select name from Account where accountType in ('Cash', 'Bank') and isGroup = 0 + ) + and date >= $fromDate and date <= $toDate group by strftime("%m-%Y", date) `, { $fromDate: fromDate, $toDate: toDate } ); - let depreciation = await frappe.db.sql( - ` - select sum(credit) as credit, sum(debit) as debit, strftime("%m-%Y", date) as "month-year" - from AccountingLedgerEntry - where account in ( - select name from Account where accountType = "Depreciation" - ) - and date >= $fromDate and date <= $toDate - group by strftime("%m-%Y", date) - `, - { $fromDate: fromDate, $toDate: toDate } - ); - - let periodList = income.periodList; + let periodList = getPeriodList(fromDate, toDate, periodicity); let data = periodList.map(periodKey => { let monthYear = this.getMonthYear(periodKey, 'MMM yyyy'); - let cashflowForPeriod = cashflow.find(d => d['month-year'] === monthYear); + let cashflowForPeriod = res.find(d => d['month-year'] === monthYear); if (cashflowForPeriod) { cashflowForPeriod.periodKey = periodKey; return cashflowForPeriod; @@ -63,33 +33,6 @@ class Cashflow { }; }); - let depreciationPeriodList = periodList.map(periodKey => { - let monthYear = this.getMonthYear(periodKey, 'MMM yyyy'); - let depreciationForPeriod = depreciation.find( - d => d['month-year'] === monthYear - ); - if (depreciationForPeriod) { - depreciationForPeriod.periodKey = periodKey; - return depreciationForPeriod; - } - return { - debit: 0, - credit: 0, - periodKey, - 'month-year': monthYear - }; - }); - - data = data.map((d, i) => { - d.inflow += income.totalRow[d.periodKey]; - d.outflow -= expense.totalRow[d.periodKey]; - - let depreciation = depreciationPeriodList[i]; - d.inflow -= depreciation.credit; - d.outflow += depreciation.debit; - return d; - }); - return { data, periodList @@ -99,12 +42,6 @@ class Cashflow { getMonthYear(periodKey, format) { return DateTime.fromFormat(periodKey, format).toFormat('MM-yyyy'); } - - getPeriodKey(dateStr, format, periodicity) { - if (periodicity === 'Monthly') { - return DateTime.fromFormat(dateStr, format).toFormat('MMM yyyy'); - } - } } module.exports = Cashflow; diff --git a/reports/FinancialStatements/FinancialStatements.js b/reports/FinancialStatements/FinancialStatements.js index ded16e2f..e299435f 100644 --- a/reports/FinancialStatements/FinancialStatements.js +++ b/reports/FinancialStatements/FinancialStatements.js @@ -286,5 +286,6 @@ async function getFiscalYear() { module.exports = { getData, - getTrialBalance + getTrialBalance, + getPeriodList }; diff --git a/src/pages/Dashboard/Cashflow.vue b/src/pages/Dashboard/Cashflow.vue index bd59c59f..4dcc2046 100644 --- a/src/pages/Dashboard/Cashflow.vue +++ b/src/pages/Dashboard/Cashflow.vue @@ -124,8 +124,8 @@ export default { periodicity }); - let totalInflow = data.reduce((sum, d) => d.inflow, 0); - let totalOutflow = data.reduce((sum, d) => d.outflow, 0); + let totalInflow = data.reduce((sum, d) => d.inflow + sum, 0); + let totalOutflow = data.reduce((sum, d) => d.outflow + sum, 0); this.hasData = !(totalInflow === 0 && totalOutflow === 0); if (!this.hasData) return;