2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/reports/Cashflow/Cashflow.ts
18alantom 27aed52044 incr: type some files, frappe -> fyo
- get books to serve albiet broken
- one step at a time
2022-05-23 16:18:22 +05:30

53 lines
1.2 KiB
TypeScript

import { Fyo } from 'fyo';
import { DateTime } from 'luxon';
import {
getFiscalYear,
getPeriodList,
} from 'reports/FinancialStatements/financialStatements';
import { FinancialStatementOptions } from 'reports/types';
class Cashflow {
fyo: Fyo;
constructor(fyo: Fyo) {
this.fyo = fyo;
}
async run(options: FinancialStatementOptions) {
const { fromDate, toDate, periodicity } = options;
const res = await this.fyo.db.getCashflow(fromDate, toDate);
const fiscalYear = await getFiscalYear(this.fyo);
const periodList = getPeriodList(
fromDate,
toDate,
periodicity!,
fiscalYear
);
const data = periodList.map((periodKey) => {
const monthYear = this.getMonthYear(periodKey, 'MMM yyyy');
const cashflowForPeriod = res.find((d) => d['month-year'] === monthYear);
if (cashflowForPeriod) {
return { ...cashflowForPeriod, periodKey };
}
return {
inflow: 0,
outflow: 0,
periodKey,
'month-year': monthYear,
};
});
return {
data,
periodList,
};
}
getMonthYear(periodKey: string, format: string) {
return DateTime.fromFormat(periodKey, format).toFormat('MM-yyyy');
}
}
export default Cashflow;