2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/reports/Cashflow/Cashflow.js
18alantom 1a8a23d2a2 build(electron): bump electron to 15.3.0
- refactor Common Js imports to ES6
2021-11-05 14:31:35 +05:30

55 lines
1.4 KiB
JavaScript

import frappe from 'frappejs';
import { getPeriodList } from '../FinancialStatements/FinancialStatements';
import { DateTime } from 'luxon';
class Cashflow {
async run({ fromDate, toDate, periodicity }) {
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')
.sum({
inflow: 'debit',
outflow: 'credit'
})
.select({
'month-year': dateAsMonthYear
})
.where('account', 'in', cashAndBankAccounts)
.whereBetween('date', [fromDate, toDate])
.groupBy(dateAsMonthYear);
let periodList = getPeriodList(fromDate, toDate, periodicity);
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;