Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opening balances to general ledger report #1029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 92 additions & 17 deletions reports/GeneralLedger/GeneralLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class GeneralLedger extends LedgerReport {
usePagination = true;
loading = false;

includeOpenings = true;
ascending = false;
reverted = false;
referenceType: ReferenceType = 'All';
Expand All @@ -52,7 +53,54 @@ export class GeneralLedger extends LedgerReport {
sort = false;
}

const map = this._getGroupedMap(sort);
let reportData = this._rawData;
let mapOpenings: GroupedMap;
let openingCredit = 0;
let openingDebit = 0;
if (this.fromDate && this.includeOpenings) {
const fromDate = new Date(this.fromDate);
reportData = [];
const openingData = [];
for (const entry of this._rawData) {
if (entry.date < fromDate) {
openingData.push(entry);
} else {
reportData.push(entry);
}
}

mapOpenings = this._getGroupedMap(sort, null, openingData);
this._setIndexOnEntries(mapOpenings);
this._getTotalsAndSetBalance(mapOpenings);
}

const map = this._getGroupedMap(sort, null, reportData);

if (mapOpenings) {
for (const key of mapOpenings.keys()) {
const { debit, credit } = this._getBalance(mapOpenings.get(key));

openingDebit += debit;
openingCredit += credit;

if (this.groupBy !== 'none') {
map.get(key)?.unshift({
name: -1, // Italics
account: t`Opening`,
date: null,
debit,
credit,
balance: debit - credit,
referenceType: '',
referenceName: '',
party: '',
reverted: false,
reverts: '',
});
}
}
}

this._setIndexOnEntries(map);
const { totalDebit, totalCredit } = this._getTotalsAndSetBalance(map);
const consolidated = this._consolidateEntries(map);
Expand All @@ -64,16 +112,32 @@ export class GeneralLedger extends LedgerReport {
this._pushBlankEntry(consolidated);
}

if (this.groupBy === 'none') {
consolidated.unshift({
name: -2, // Bold
account: t`Opening`,
date: null,
debit: openingDebit,
credit: openingCredit,
balance: openingDebit - openingCredit,
referenceType: '',
referenceName: '',
party: '',
reverted: false,
reverts: '',
});
}

/**
* Set the closing row
*/
consolidated.push({
name: -2, // Bold
account: t`Closing`,
date: null,
debit: totalDebit,
credit: totalCredit,
balance: totalDebit - totalCredit,
debit: openingDebit + totalDebit,
credit: openingCredit + totalCredit,
balance: openingDebit + totalDebit - openingCredit - totalCredit,
referenceType: '',
referenceName: '',
party: '',
Expand Down Expand Up @@ -192,23 +256,29 @@ export class GeneralLedger extends LedgerReport {
});
}

_getBalance(entries: LedgerEntry[]) {
let balance = 0;
let debit = 0;
let credit = 0;

for (const entry of entries) {
debit += entry.debit!;
credit += entry.credit!;

const diff = entry.debit! - entry.credit!;
balance += diff;
entry.balance = balance;
}

return { balance, debit, credit };
}

_getTotalsAndSetBalance(map: GroupedMap) {
let totalDebit = 0;
let totalCredit = 0;

for (const key of map.keys()) {
let balance = 0;
let debit = 0;
let credit = 0;

for (const entry of map.get(key)!) {
debit += entry.debit!;
credit += entry.credit!;

const diff = entry.debit! - entry.credit!;
balance += diff;
entry.balance = balance;
}
const { debit, credit } = this._getBalance(map.get(key)!);

/**
* Total row incase groupBy is used
Expand Down Expand Up @@ -261,7 +331,7 @@ export class GeneralLedger extends LedgerReport {
(filters.date as string[]).push('<=', this.toDate as string);
}

if (this.fromDate) {
if (!this.includeOpenings && this.fromDate) {
filters.date ??= [];
(filters.date as string[]).push('>=', this.fromDate as string);
}
Expand Down Expand Up @@ -352,6 +422,11 @@ export class GeneralLedger extends LedgerReport {
label: t`Ascending Order`,
fieldname: 'ascending',
},
{
fieldtype: 'Check',
label: t`Include Openings`,
fieldname: 'includeOpenings',
},
] as Field[];
}

Expand Down
11 changes: 8 additions & 3 deletions reports/LedgerReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ export abstract class LedgerReport extends Report {
return groupBy;
}

_getGroupedMap(sort: boolean, groupBy?: GroupByKey): GroupedMap {
_getGroupedMap(
sort: boolean,
groupBy?: GroupByKey,
data?: LedgerEntry[]
): GroupedMap {
groupBy ??= this._getGroupByKey();
data ??= this._rawData;
/**
* Sort rows by ascending or descending
*/
if (sort) {
this._rawData.sort((a, b) => {
data.sort((a, b) => {
if (this.ascending) {
return +a.date! - +b.date!;
}
Expand All @@ -63,7 +68,7 @@ export abstract class LedgerReport extends Report {
* ∴ presorting maintains grouping order
*/
const map: GroupedMap = new Map();
for (const entry of this._rawData) {
for (const entry of data) {
const groupingKey = entry[groupBy];
if (!map.has(groupingKey)) {
map.set(groupingKey, []);
Expand Down
Loading