2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2021-11-04 10:31:26 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2022-04-01 10:20:45 +00:00
|
|
|
import { getPeriodList, getFiscalYear } from '../FinancialStatements/FinancialStatements';
|
2019-11-22 17:43:32 +00:00
|
|
|
|
|
|
|
class Cashflow {
|
|
|
|
async run({ fromDate, toDate, periodicity }) {
|
2019-12-09 20:20:32 +00:00
|
|
|
let cashAndBankAccounts = frappe.db
|
|
|
|
.knex('Account')
|
|
|
|
.select('name')
|
|
|
|
.where('accountType', 'in', ['Cash', 'Bank'])
|
|
|
|
.andWhere('isGroup', 0);
|
|
|
|
let dateAsMonthYear = frappe.db.knex.raw('strftime("%m-%Y", ??)', 'date');
|
|
|
|
let res = await frappe.db
|
|
|
|
.knex('AccountingLedgerEntry')
|
2021-11-21 13:38:04 +00:00
|
|
|
.where('reverted', 0)
|
2019-12-09 20:20:32 +00:00
|
|
|
.sum({
|
|
|
|
inflow: 'debit',
|
2022-01-20 20:57:29 +00:00
|
|
|
outflow: 'credit',
|
2019-12-09 20:20:32 +00:00
|
|
|
})
|
|
|
|
.select({
|
2022-01-20 20:57:29 +00:00
|
|
|
'month-year': dateAsMonthYear,
|
2019-12-09 20:20:32 +00:00
|
|
|
})
|
|
|
|
.where('account', 'in', cashAndBankAccounts)
|
|
|
|
.whereBetween('date', [fromDate, toDate])
|
|
|
|
.groupBy(dateAsMonthYear);
|
2019-11-22 17:43:32 +00:00
|
|
|
|
2022-04-01 10:20:45 +00:00
|
|
|
let fiscalYear = await getFiscalYear();
|
|
|
|
let periodList = getPeriodList(fromDate, toDate, periodicity, fiscalYear);
|
2019-11-22 17:43:32 +00:00
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
let data = periodList.map((periodKey) => {
|
2019-11-22 17:43:32 +00:00
|
|
|
let monthYear = this.getMonthYear(periodKey, 'MMM yyyy');
|
2022-01-20 20:57:29 +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,
|
2022-01-20 20:57:29 +00:00
|
|
|
'month-year': monthYear,
|
2019-11-22 17:43:32 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
2022-01-20 20:57:29 +00:00
|
|
|
periodList,
|
2019-11-22 17:43:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getMonthYear(periodKey, format) {
|
|
|
|
return DateTime.fromFormat(periodKey, format).toFormat('MM-yyyy');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default Cashflow;
|