2
0
mirror of https://github.com/frappe/books.git synced 2025-01-23 07:08:36 +00:00

fix: Cashflow

- Simple cashflow from Bank and Cash accounts
This commit is contained in:
Faris Ansari 2019-12-04 22:53:28 +05:30
parent 7a94bffcbf
commit 50b184a9fc
3 changed files with 14 additions and 76 deletions

View File

@ -1,31 +1,14 @@
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"
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 ('Receivable', 'Payable', 'Stock', 'Fixed Asset', 'Equity')
select name from Account where accountType in ('Cash', 'Bank') and isGroup = 0
)
and date >= $fromDate and date <= $toDate
group by strftime("%m-%Y", date)
@ -33,24 +16,11 @@ class Cashflow {
{ $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;

View File

@ -286,5 +286,6 @@ async function getFiscalYear() {
module.exports = {
getData,
getTrialBalance
getTrialBalance,
getPeriodList
};

View File

@ -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;