Skip to content

Commit

Permalink
FINERACT-2081: Refactor GL account balance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Nov 26, 2024
1 parent ccfb8c3 commit 2e89c40
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.accounting.journalentry.data;

import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.Data;
import org.apache.fineract.accounting.glaccount.domain.GLAccount;

@Data
public class GLAccountBalanceHolder {

private final Map<Long, GLAccount> glAccountMap = new LinkedHashMap<>();
private final Map<Long, BigDecimal> debitBalances = new LinkedHashMap<>();
private final Map<Long, BigDecimal> creditBalances = new LinkedHashMap<>();

public void addToCredit(@NotNull GLAccount creditAccount, @NotNull BigDecimal amount) {
addToProperBalance(creditBalances, creditAccount, amount);
}

public void addToDebit(@NotNull GLAccount debitAccount, @NotNull BigDecimal amount) {
addToProperBalance(debitBalances, debitAccount, amount);
}

private void addToProperBalance(@NotNull Map<Long, BigDecimal> balanceMap, @NotNull @NotNull GLAccount account,
@NotNull BigDecimal amount) {
glAccountMap.putIfAbsent(account.getId(), account);
if (balanceMap.containsKey(account.getId())) {
BigDecimal totalAmount = balanceMap.get(account.getId()).add(amount);
balanceMap.put(account.getId(), totalAmount);
} else {
balanceMap.put(account.getId(), amount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,16 @@ public void createDebitJournalEntryOrReversalForLoan(final Office office, final
}
}

public void createDebitJournalEntryOrReversalForLoan(final Office office, final String currencyCode, final Long loanId,
final String transactionId, final LocalDate transactionDate, final BigDecimal amount, final Boolean isReversal,
final GLAccount account) {
if (isReversal) {
createCreditJournalEntryForLoan(office, currencyCode, account, loanId, transactionId, transactionDate, amount);
} else {
createDebitJournalEntryForLoan(office, currencyCode, account, loanId, transactionId, transactionDate, amount);
}
}

public void createDebitJournalEntryOrReversalForLoanCharges(final Office office, final String currencyCode,
final int accountMappingTypeId, final Long loanProductId, final Long chargeId, final Long loanId, final String transactionId,
final LocalDate transactionDate, final BigDecimal amount, final Boolean isReversal) {
Expand Down
Loading

0 comments on commit 2e89c40

Please sign in to comment.