2019-12-04 17:23:28 +00:00
|
|
|
const { getPeriodList } = require('../FinancialStatements/FinancialStatements');
|
2019-11-22 17:43:32 +00:00
|
|
|
const { DateTime } = require('luxon');
|
|
|
|
|
|
|
|
class Cashflow {
|
|
|
|
async run({ fromDate, toDate, periodicity }) {
|
2019-12-04 17:23:28 +00:00
|
|
|
let res = await frappe.db.sql(
|
2019-11-22 17:43:32 +00:00
|
|
|
`
|
2019-12-04 17:23:28 +00:00
|
|
|
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
|
2019-11-22 17:43:32 +00:00
|
|
|
group by strftime("%m-%Y", date)
|
|
|
|
`,
|
|
|
|
{ $fromDate: fromDate, $toDate: toDate }
|
|
|
|
);
|
|
|
|
|
2019-12-04 17:23:28 +00:00
|
|
|
let periodList = getPeriodList(fromDate, toDate, periodicity);
|
2019-11-22 17:43:32 +00:00
|
|
|
|
|
|
|
let data = periodList.map(periodKey => {
|
|
|
|
let monthYear = this.getMonthYear(periodKey, 'MMM yyyy');
|
2019-12-04 17:23:28 +00:00
|
|
|
let cashflowForPeriod = res.find(d => d['month-year'] === monthYear);
|
2019-11-22 17:43:32 +00:00
|
|
|
if (cashflowForPeriod) {
|
|
|
|
cashflowForPeriod.periodKey = periodKey;
|
|
|
|
return cashflowForPeriod;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
inflow: 0,
|
|
|
|
outflow: 0,
|
|
|
|
periodKey,
|
|
|
|
'month-year': monthYear
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
periodList
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getMonthYear(periodKey, format) {
|
|
|
|
return DateTime.fromFormat(periodKey, format).toFormat('MM-yyyy');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Cashflow;
|