2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/reports/Cashflow/Cashflow.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

import frappe from 'frappe';
import { DateTime } from 'luxon';
import { getPeriodList, getFiscalYear } from '../FinancialStatements/FinancialStatements';
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',
outflow: 'credit',
2019-12-09 20:20:32 +00:00
})
.select({
'month-year': dateAsMonthYear,
2019-12-09 20:20:32 +00:00
})
.where('account', 'in', cashAndBankAccounts)
.whereBetween('date', [fromDate, toDate])
.groupBy(dateAsMonthYear);
let fiscalYear = await getFiscalYear();
let periodList = getPeriodList(fromDate, toDate, periodicity, fiscalYear);
let data = periodList.map((periodKey) => {
let monthYear = this.getMonthYear(periodKey, 'MMM yyyy');
let cashflowForPeriod = res.find((d) => d['month-year'] === monthYear);
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');
}
}
export default Cashflow;