mirror of
https://github.com/frappe/books.git
synced 2025-02-08 23:18:31 +00:00
fix: Cashflow
- Simple cashflow from Bank and Cash accounts
This commit is contained in:
parent
7a94bffcbf
commit
50b184a9fc
@ -1,31 +1,14 @@
|
|||||||
const { getData } = require('../FinancialStatements/FinancialStatements');
|
const { getPeriodList } = require('../FinancialStatements/FinancialStatements');
|
||||||
const ProfitAndLoss = require('../ProfitAndLoss/ProfitAndLoss');
|
|
||||||
const { DateTime } = require('luxon');
|
const { DateTime } = require('luxon');
|
||||||
|
|
||||||
class Cashflow {
|
class Cashflow {
|
||||||
async run({ fromDate, toDate, periodicity }) {
|
async run({ fromDate, toDate, periodicity }) {
|
||||||
let income = await getData({
|
let res = await frappe.db.sql(
|
||||||
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"
|
select sum(debit) as inflow, sum(credit) as outflow, strftime("%m-%Y", date) as "month-year"
|
||||||
from AccountingLedgerEntry
|
from AccountingLedgerEntry
|
||||||
where account in (
|
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
|
and date >= $fromDate and date <= $toDate
|
||||||
group by strftime("%m-%Y", date)
|
group by strftime("%m-%Y", date)
|
||||||
@ -33,24 +16,11 @@ class Cashflow {
|
|||||||
{ $fromDate: fromDate, $toDate: toDate }
|
{ $fromDate: fromDate, $toDate: toDate }
|
||||||
);
|
);
|
||||||
|
|
||||||
let depreciation = await frappe.db.sql(
|
let periodList = getPeriodList(fromDate, toDate, periodicity);
|
||||||
`
|
|
||||||
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 data = periodList.map(periodKey => {
|
||||||
let monthYear = this.getMonthYear(periodKey, 'MMM yyyy');
|
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) {
|
if (cashflowForPeriod) {
|
||||||
cashflowForPeriod.periodKey = periodKey;
|
cashflowForPeriod.periodKey = periodKey;
|
||||||
return cashflowForPeriod;
|
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 {
|
return {
|
||||||
data,
|
data,
|
||||||
periodList
|
periodList
|
||||||
@ -99,12 +42,6 @@ class Cashflow {
|
|||||||
getMonthYear(periodKey, format) {
|
getMonthYear(periodKey, format) {
|
||||||
return DateTime.fromFormat(periodKey, format).toFormat('MM-yyyy');
|
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;
|
module.exports = Cashflow;
|
||||||
|
@ -286,5 +286,6 @@ async function getFiscalYear() {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getData,
|
getData,
|
||||||
getTrialBalance
|
getTrialBalance,
|
||||||
|
getPeriodList
|
||||||
};
|
};
|
||||||
|
@ -124,8 +124,8 @@ export default {
|
|||||||
periodicity
|
periodicity
|
||||||
});
|
});
|
||||||
|
|
||||||
let totalInflow = data.reduce((sum, d) => d.inflow, 0);
|
let totalInflow = data.reduce((sum, d) => d.inflow + sum, 0);
|
||||||
let totalOutflow = data.reduce((sum, d) => d.outflow, 0);
|
let totalOutflow = data.reduce((sum, d) => d.outflow + sum, 0);
|
||||||
this.hasData = !(totalInflow === 0 && totalOutflow === 0);
|
this.hasData = !(totalInflow === 0 && totalOutflow === 0);
|
||||||
if (!this.hasData) return;
|
if (!this.hasData) return;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user