From 70b8e818c090e971300db27674040b7edc600029 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 22 Nov 2019 23:13:32 +0530 Subject: [PATCH] fix: Dashboard - Remove hardcoded values - Calculate cashflow - PeriodSelector --- reports/Cashflow/Cashflow.js | 110 +++++++ src/pages/Dashboard.vue | 286 ------------------ src/pages/Dashboard/Dashboard.vue | 388 +++++++++++++++++++++++++ src/pages/Dashboard/PeriodSelector.vue | 52 ++++ src/pages/Dashboard/SectionHeader.vue | 6 + src/router.js | 2 +- 6 files changed, 557 insertions(+), 287 deletions(-) create mode 100644 reports/Cashflow/Cashflow.js delete mode 100644 src/pages/Dashboard.vue create mode 100644 src/pages/Dashboard/Dashboard.vue create mode 100644 src/pages/Dashboard/PeriodSelector.vue create mode 100644 src/pages/Dashboard/SectionHeader.vue diff --git a/reports/Cashflow/Cashflow.js b/reports/Cashflow/Cashflow.js new file mode 100644 index 00000000..80fed038 --- /dev/null +++ b/reports/Cashflow/Cashflow.js @@ -0,0 +1,110 @@ +const { getData } = require('../FinancialStatements/FinancialStatements'); +const ProfitAndLoss = require('../ProfitAndLoss/ProfitAndLoss'); +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( + ` + 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 + 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 data = periodList.map(periodKey => { + let monthYear = this.getMonthYear(periodKey, 'MMM yyyy'); + let cashflowForPeriod = cashflow.find(d => d['month-year'] === monthYear); + if (cashflowForPeriod) { + cashflowForPeriod.periodKey = periodKey; + return cashflowForPeriod; + } + return { + inflow: 0, + outflow: 0, + periodKey, + 'month-year': monthYear + }; + }); + + 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 + }; + } + + 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/src/pages/Dashboard.vue b/src/pages/Dashboard.vue deleted file mode 100644 index b02debc4..00000000 --- a/src/pages/Dashboard.vue +++ /dev/null @@ -1,286 +0,0 @@ - - - - diff --git a/src/pages/Dashboard/Dashboard.vue b/src/pages/Dashboard/Dashboard.vue new file mode 100644 index 00000000..3bc587d0 --- /dev/null +++ b/src/pages/Dashboard/Dashboard.vue @@ -0,0 +1,388 @@ + + + + diff --git a/src/pages/Dashboard/PeriodSelector.vue b/src/pages/Dashboard/PeriodSelector.vue new file mode 100644 index 00000000..d05cc610 --- /dev/null +++ b/src/pages/Dashboard/PeriodSelector.vue @@ -0,0 +1,52 @@ + + + diff --git a/src/pages/Dashboard/SectionHeader.vue b/src/pages/Dashboard/SectionHeader.vue new file mode 100644 index 00000000..f3d711a4 --- /dev/null +++ b/src/pages/Dashboard/SectionHeader.vue @@ -0,0 +1,6 @@ + diff --git a/src/router.js b/src/router.js index 2083af6c..2eeef68a 100644 --- a/src/router.js +++ b/src/router.js @@ -2,7 +2,7 @@ import Vue from 'vue'; import Router from 'vue-router'; import ListView from '@/pages/ListView/ListView'; -import Dashboard from '@/pages/Dashboard'; +import Dashboard from '@/pages/Dashboard/Dashboard'; import FormView from '@/pages/FormView/FormView'; import PrintView from '@/pages/PrintView'; import QuickEditForm from '@/pages/QuickEditForm';