This is an automated email from the ASF dual-hosted git repository. myrle pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/fineract-cn-accounting.git
commit c01eb0fd3e3a68f22ce3de027ba4213559551986 Author: mgeiss <[email protected]> AuthorDate: Fri Oct 13 17:20:24 2017 +0200 fixed unwanted asynchronous behavior for ledger totals --- .../domain/financial/statement/TrialBalance.java | 13 +++-- .../financial/statement/TrialBalanceEntry.java | 8 ++- .../java/io/mifos/accounting/TestTrialBalance.java | 55 +++++++++++------ .../command/AddAmountToLedgerTotalCommand.java | 38 ------------ .../command/handler/AccountCommandHandler.java | 27 ++++++--- .../command/handler/LedgerCommandHandler.java | 23 -------- .../command/handler/MigrationCommandHandler.java | 19 +++--- .../internal/service/TrialBalanceService.java | 68 ++++++++++------------ 8 files changed, 109 insertions(+), 142 deletions(-) diff --git a/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalance.java b/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalance.java index f73e9d7..3331d1e 100644 --- a/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalance.java +++ b/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalance.java @@ -15,6 +15,7 @@ */ package io.mifos.accounting.api.v1.domain.financial.statement; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -22,8 +23,8 @@ import java.util.List; public class TrialBalance { private List<TrialBalanceEntry> trialBalanceEntries; - private Double debitTotal; - private Double creditTotal; + private BigDecimal debitTotal; + private BigDecimal creditTotal; public TrialBalance() { super(); @@ -40,19 +41,19 @@ public class TrialBalance { this.trialBalanceEntries = trialBalanceEntries; } - public Double getDebitTotal() { + public BigDecimal getDebitTotal() { return this.debitTotal; } - public void setDebitTotal(final Double debitTotal) { + public void setDebitTotal(final BigDecimal debitTotal) { this.debitTotal = debitTotal; } - public Double getCreditTotal() { + public BigDecimal getCreditTotal() { return this.creditTotal; } - public void setCreditTotal(final Double creditTotal) { + public void setCreditTotal(final BigDecimal creditTotal) { this.creditTotal = creditTotal; } } diff --git a/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalanceEntry.java b/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalanceEntry.java index 589fcb3..523e0fd 100644 --- a/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalanceEntry.java +++ b/api/src/main/java/io/mifos/accounting/api/v1/domain/financial/statement/TrialBalanceEntry.java @@ -17,12 +17,14 @@ package io.mifos.accounting.api.v1.domain.financial.statement; import io.mifos.accounting.api.v1.domain.Ledger; +import java.math.BigDecimal; + @SuppressWarnings("WeakerAccess") public class TrialBalanceEntry { private Ledger ledger; private Type type; - private Double amount; + private BigDecimal amount; public TrialBalanceEntry() { super(); @@ -44,11 +46,11 @@ public class TrialBalanceEntry { this.type = Type.valueOf(type); } - public Double getAmount() { + public BigDecimal getAmount() { return this.amount; } - public void setAmount(final Double amount) { + public void setAmount(final BigDecimal amount) { this.amount = amount; } diff --git a/component-test/src/main/java/io/mifos/accounting/TestTrialBalance.java b/component-test/src/main/java/io/mifos/accounting/TestTrialBalance.java index f82d078..1fd9bd8 100644 --- a/component-test/src/main/java/io/mifos/accounting/TestTrialBalance.java +++ b/component-test/src/main/java/io/mifos/accounting/TestTrialBalance.java @@ -27,49 +27,70 @@ import io.mifos.accounting.util.LedgerGenerator; import org.junit.Assert; import org.junit.Test; +import java.math.BigDecimal; + public class TestTrialBalance extends AbstractAccountingTest { @Test public void shouldGenerateTrialBalance() throws Exception { - final Ledger ledgerOne = LedgerGenerator.createRandomLedger(); - this.testSubject.createLedger(ledgerOne); - this.eventRecorder.wait(EventConstants.POST_LEDGER, ledgerOne.getIdentifier()); + final Ledger assetLedger = LedgerGenerator.createRandomLedger(); + this.testSubject.createLedger(assetLedger); + this.eventRecorder.wait(EventConstants.POST_LEDGER, assetLedger.getIdentifier()); + + final Ledger assetSubLedgerOne = LedgerGenerator.createRandomLedger(); + this.testSubject.addSubLedger(assetLedger.getIdentifier(), assetSubLedgerOne); + this.eventRecorder.wait(EventConstants.POST_LEDGER, assetSubLedgerOne.getIdentifier()); - final Ledger subLedgerOne = LedgerGenerator.createRandomLedger(); - this.testSubject.addSubLedger(ledgerOne.getIdentifier(), subLedgerOne); - this.eventRecorder.wait(EventConstants.POST_LEDGER, subLedgerOne.getIdentifier()); + final Ledger assetSubLedgerTwo = LedgerGenerator.createRandomLedger(); + this.testSubject.addSubLedger(assetLedger.getIdentifier(), assetSubLedgerTwo); + this.eventRecorder.wait(EventConstants.POST_LEDGER, assetSubLedgerTwo.getIdentifier()); - final Ledger ledgerTwo = LedgerGenerator.createRandomLedger(); - ledgerTwo.setType(AccountType.LIABILITY.name()); - this.testSubject.createLedger(ledgerTwo); - this.eventRecorder.wait(EventConstants.POST_LEDGER, ledgerTwo.getIdentifier()); + final Ledger liabilityLedger = LedgerGenerator.createRandomLedger(); + liabilityLedger.setType(AccountType.LIABILITY.name()); + this.testSubject.createLedger(liabilityLedger); + this.eventRecorder.wait(EventConstants.POST_LEDGER, liabilityLedger.getIdentifier()); - final Account account4ledgerOne = AccountGenerator.createRandomAccount(ledgerOne.getIdentifier()); + final Ledger liabilitySubLedger = LedgerGenerator.createRandomLedger(); + liabilitySubLedger.setType(AccountType.LIABILITY.name()); + this.testSubject.addSubLedger(liabilityLedger.getIdentifier(), liabilitySubLedger); + this.eventRecorder.wait(EventConstants.POST_LEDGER, liabilitySubLedger.getIdentifier()); + + final Account account4ledgerOne = AccountGenerator.createRandomAccount(assetSubLedgerOne.getIdentifier()); this.testSubject.createAccount(account4ledgerOne); this.eventRecorder.wait(EventConstants.POST_ACCOUNT, account4ledgerOne.getIdentifier()); - final Account account4subLedgerOne = AccountGenerator.createRandomAccount(subLedgerOne.getIdentifier()); + final Account secondAccount4ledgerOne = AccountGenerator.createRandomAccount(assetSubLedgerOne.getIdentifier()); + this.testSubject.createAccount(secondAccount4ledgerOne); + this.eventRecorder.wait(EventConstants.POST_ACCOUNT, secondAccount4ledgerOne.getIdentifier()); + + final Account account4subLedgerOne = AccountGenerator.createRandomAccount(assetSubLedgerTwo.getIdentifier()); this.testSubject.createAccount(account4subLedgerOne); this.eventRecorder.wait(EventConstants.POST_ACCOUNT, account4subLedgerOne.getIdentifier()); - final Account account4ledgerTwo = AccountGenerator.createRandomAccount(ledgerTwo.getIdentifier()); + final Account account4ledgerTwo = AccountGenerator.createRandomAccount(liabilitySubLedger.getIdentifier()); account4ledgerTwo.setType(AccountType.LIABILITY.name()); this.testSubject.createAccount(account4ledgerTwo); this.eventRecorder.wait(EventConstants.POST_ACCOUNT, account4ledgerTwo.getIdentifier()); final JournalEntry firstBooking = - JournalEntryGenerator.createRandomJournalEntry(account4ledgerOne, "50.00", account4ledgerTwo, "50.00"); + JournalEntryGenerator.createRandomJournalEntry(secondAccount4ledgerOne, "50.00", account4ledgerTwo, "50.00"); this.testSubject.createJournalEntry(firstBooking); this.eventRecorder.wait(EventConstants.RELEASE_JOURNAL_ENTRY, firstBooking.getTransactionIdentifier()); final JournalEntry secondBooking = - JournalEntryGenerator.createRandomJournalEntry(account4subLedgerOne, "50.00", account4ledgerTwo, "50.00"); + JournalEntryGenerator.createRandomJournalEntry(secondAccount4ledgerOne, "50.00", account4ledgerOne, "50.00"); this.testSubject.createJournalEntry(secondBooking); this.eventRecorder.wait(EventConstants.RELEASE_JOURNAL_ENTRY, secondBooking.getTransactionIdentifier()); + final JournalEntry thirdBooking = + JournalEntryGenerator.createRandomJournalEntry(account4subLedgerOne, "50.00", account4ledgerTwo, "50.00"); + this.testSubject.createJournalEntry(thirdBooking); + this.eventRecorder.wait(EventConstants.RELEASE_JOURNAL_ENTRY, thirdBooking.getTransactionIdentifier()); + final TrialBalance trialBalance = this.testSubject.getTrialBalance(true); Assert.assertNotNull(trialBalance); Assert.assertEquals(3, trialBalance.getTrialBalanceEntries().size()); - Assert.assertEquals(100.00D, trialBalance.getDebitTotal(), 0.00D); - Assert.assertEquals(100.00D, trialBalance.getCreditTotal(), 0.00D); + final BigDecimal expectedValue = BigDecimal.valueOf(100.00D); + Assert.assertTrue(trialBalance.getDebitTotal().compareTo(expectedValue) == 0); + Assert.assertTrue(trialBalance.getCreditTotal().compareTo(expectedValue) == 0); } } diff --git a/service/src/main/java/io/mifos/accounting/service/internal/command/AddAmountToLedgerTotalCommand.java b/service/src/main/java/io/mifos/accounting/service/internal/command/AddAmountToLedgerTotalCommand.java deleted file mode 100644 index 771f384..0000000 --- a/service/src/main/java/io/mifos/accounting/service/internal/command/AddAmountToLedgerTotalCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2017 The Mifos Initiative. - * - * Licensed 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 io.mifos.accounting.service.internal.command; - -import java.math.BigDecimal; - -public class AddAmountToLedgerTotalCommand { - - private final String ledgerIdentifier; - private final BigDecimal amount; - - public AddAmountToLedgerTotalCommand(final String ledgerIdentifier, final BigDecimal amount) { - super(); - this.ledgerIdentifier = ledgerIdentifier; - this.amount = amount; - } - - public String ledgerIdentifier() { - return this.ledgerIdentifier; - } - - public BigDecimal amount() { - return this.amount; - } -} diff --git a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/AccountCommandHandler.java b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/AccountCommandHandler.java index 8c30e02..66f4f8d 100644 --- a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/AccountCommandHandler.java +++ b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/AccountCommandHandler.java @@ -21,7 +21,7 @@ import io.mifos.accounting.api.v1.domain.AccountCommand; import io.mifos.accounting.api.v1.domain.AccountEntry; import io.mifos.accounting.api.v1.domain.AccountType; import io.mifos.accounting.api.v1.domain.JournalEntry; -import io.mifos.accounting.service.internal.command.AddAmountToLedgerTotalCommand; +import io.mifos.accounting.service.ServiceConstants; import io.mifos.accounting.service.internal.command.BookJournalEntryCommand; import io.mifos.accounting.service.internal.command.CloseAccountCommand; import io.mifos.accounting.service.internal.command.CreateAccountCommand; @@ -48,7 +48,9 @@ import io.mifos.core.command.annotation.CommandLogLevel; import io.mifos.core.command.annotation.EventEmitter; import io.mifos.core.command.gateway.CommandGateway; import io.mifos.core.lang.ServiceException; +import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.transaction.annotation.Transactional; @@ -63,6 +65,7 @@ import java.util.stream.Collectors; @Aggregate public class AccountCommandHandler { + private final Logger logger; private final CommandGateway commandGateway; private final AccountRepository accountRepository; private final AccountEntryRepository accountEntryRepository; @@ -71,7 +74,7 @@ public class AccountCommandHandler { private final CommandRepository commandRepository; @Autowired - public AccountCommandHandler(//@Qualifier(ThothServiceConstants.LOGGER_NAME) final Logger logger, + public AccountCommandHandler(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger, final CommandGateway commandGateway, final AccountRepository accountRepository, final AccountEntryRepository accountEntryRepository, @@ -79,6 +82,7 @@ public class AccountCommandHandler { final JournalEntryRepository journalEntryRepository, final CommandRepository commandRepository) { super(); + this.logger = logger; this.commandGateway = commandGateway; this.accountRepository = accountRepository; this.accountEntryRepository = accountEntryRepository; @@ -353,9 +357,7 @@ public class AccountCommandHandler { accountEntryEntity.setMessage(journalEntryEntity.getMessage()); accountEntryEntity.setTransactionDate(journalEntryEntity.getTransactionDate()); this.accountEntryRepository.save(accountEntryEntity); - this.commandGateway.process( - new AddAmountToLedgerTotalCommand(savedAccountEntity.getLedger().getIdentifier(), amount) - ); + this.adjustLedgerTotals(savedAccountEntity.getLedger().getIdentifier(), amount); }); // process all creditors journalEntryEntity.getCreditors() @@ -388,9 +390,7 @@ public class AccountCommandHandler { accountEntryEntity.setMessage(journalEntryEntity.getMessage()); accountEntryEntity.setTransactionDate(journalEntryEntity.getTransactionDate()); this.accountEntryRepository.save(accountEntryEntity); - this.commandGateway.process( - new AddAmountToLedgerTotalCommand(savedAccountEntity.getLedger().getIdentifier(), amount) - ); + this.adjustLedgerTotals(savedAccountEntity.getLedger().getIdentifier(), amount); }); this.commandGateway.process(new ReleaseJournalEntryCommand(transactionIdentifier)); return transactionIdentifier; @@ -412,4 +412,15 @@ public class AccountCommandHandler { this.accountRepository.delete(accountEntity); return accountIdentifier; } + + @Transactional + public void adjustLedgerTotals(final String ledgerIdentifier, final BigDecimal amount) { + final LedgerEntity ledger = this.ledgerRepository.findByIdentifier(ledgerIdentifier); + final BigDecimal currentTotal = ledger.getTotalValue() != null ? ledger.getTotalValue() : BigDecimal.ZERO; + ledger.setTotalValue(currentTotal.add(amount)); + final LedgerEntity savedLedger = this.ledgerRepository.save(ledger); + if (savedLedger.getParentLedger() != null) { + this.adjustLedgerTotals(savedLedger.getParentLedger().getIdentifier(), amount); + } + } } diff --git a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/LedgerCommandHandler.java b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/LedgerCommandHandler.java index 4d96c8e..03258ad 100644 --- a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/LedgerCommandHandler.java +++ b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/LedgerCommandHandler.java @@ -18,7 +18,6 @@ package io.mifos.accounting.service.internal.command.handler; import io.mifos.accounting.api.v1.EventConstants; import io.mifos.accounting.api.v1.domain.Ledger; import io.mifos.accounting.service.ServiceConstants; -import io.mifos.accounting.service.internal.command.AddAmountToLedgerTotalCommand; import io.mifos.accounting.service.internal.command.AddSubLedgerCommand; import io.mifos.accounting.service.internal.command.CreateLedgerCommand; import io.mifos.accounting.service.internal.command.DeleteLedgerCommand; @@ -37,7 +36,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.transaction.annotation.Transactional; -import java.math.BigDecimal; import java.time.Clock; import java.time.LocalDateTime; import java.util.Collections; @@ -170,25 +168,4 @@ public class LedgerCommandHandler { } } } - - @Transactional - @CommandHandler - @EventEmitter(selectorName = EventConstants.SELECTOR_NAME, selectorValue = EventConstants.PUT_LEDGER) - public String process(final AddAmountToLedgerTotalCommand addAmountToLedgerTotalCommand) { - final BigDecimal amount = addAmountToLedgerTotalCommand.amount(); - if (amount.compareTo(BigDecimal.ZERO) != 0) { - final LedgerEntity ledger = this.ledgerRepository.findByIdentifier(addAmountToLedgerTotalCommand.ledgerIdentifier()); - final BigDecimal currentTotal = ledger.getTotalValue() != null ? ledger.getTotalValue() : BigDecimal.ZERO; - ledger.setTotalValue(currentTotal.add(amount)); - final LedgerEntity savedLedger = this.ledgerRepository.save(ledger); - if (savedLedger.getParentLedger() != null) { - this.commandGateway.process( - new AddAmountToLedgerTotalCommand(savedLedger.getParentLedger().getIdentifier(), amount) - ); - } - return savedLedger.getIdentifier(); - } else { - return null; - } - } } diff --git a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/MigrationCommandHandler.java b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/MigrationCommandHandler.java index 5359fcc..4864d5d 100644 --- a/service/src/main/java/io/mifos/accounting/service/internal/command/handler/MigrationCommandHandler.java +++ b/service/src/main/java/io/mifos/accounting/service/internal/command/handler/MigrationCommandHandler.java @@ -19,7 +19,6 @@ import com.datastax.driver.core.DataType; import com.datastax.driver.core.schemabuilder.SchemaBuilder; import io.mifos.accounting.api.v1.EventConstants; import io.mifos.accounting.service.ServiceConstants; -import io.mifos.accounting.service.internal.command.AddAmountToLedgerTotalCommand; import io.mifos.accounting.service.internal.command.InitializeServiceCommand; import io.mifos.accounting.service.internal.repository.AccountRepository; import io.mifos.core.cassandra.core.CassandraJourney; @@ -30,7 +29,6 @@ import io.mifos.core.command.annotation.Aggregate; import io.mifos.core.command.annotation.CommandHandler; import io.mifos.core.command.annotation.CommandLogLevel; import io.mifos.core.command.annotation.EventEmitter; -import io.mifos.core.command.gateway.CommandGateway; import io.mifos.core.mariadb.domain.FlywayFactoryBean; import org.flywaydb.core.Flyway; import org.flywaydb.core.api.MigrationInfo; @@ -38,6 +36,7 @@ import org.flywaydb.core.api.MigrationInfoService; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.transaction.annotation.Transactional; import javax.sql.DataSource; import java.math.BigDecimal; @@ -55,8 +54,8 @@ public class MigrationCommandHandler { private final FlywayFactoryBean flywayFactoryBean; private final CassandraSessionProvider cassandraSessionProvider; private final CassandraJourneyFactory cassandraJourneyFactory; - private final CommandGateway commandGateway; private final AccountRepository accountRepository; + private final AccountCommandHandler accountCommandHandler; @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired @@ -65,18 +64,19 @@ public class MigrationCommandHandler { final FlywayFactoryBean flywayFactoryBean, final CassandraSessionProvider cassandraSessionProvider, final CassandraJourneyFactory cassandraJourneyFactory, - final CommandGateway commandGateway, - final AccountRepository accountRepository) { + final AccountRepository accountRepository, + final AccountCommandHandler accountCommandHandler) { super(); this.logger = logger; this.dataSource = dataSource; this.flywayFactoryBean = flywayFactoryBean; this.cassandraSessionProvider = cassandraSessionProvider; this.cassandraJourneyFactory = cassandraJourneyFactory; - this.commandGateway = commandGateway; this.accountRepository = accountRepository; + this.accountCommandHandler = accountCommandHandler; } + @Transactional @CommandHandler(logStart = CommandLogLevel.DEBUG, logFinish = CommandLogLevel.DEBUG) @EventEmitter(selectorName = EventConstants.SELECTOR_NAME, selectorValue = EventConstants.INITIALIZE) public String initialize(final InitializeServiceCommand initializeServiceCommand) { @@ -165,13 +165,12 @@ public class MigrationCommandHandler { return versionNumber; } - private void migrateLedgerTotals() { + public void migrateLedgerTotals() { this.logger.info("Start ledger total migration ..."); this.accountRepository.findByBalanceIsNot(0.00D).forEach(accountEntity -> - this.commandGateway.process( - new AddAmountToLedgerTotalCommand(accountEntity.getLedger().getIdentifier(), BigDecimal.valueOf(accountEntity.getBalance())) - ) + this.accountCommandHandler.adjustLedgerTotals(accountEntity.getLedger().getIdentifier(), + BigDecimal.valueOf(accountEntity.getBalance())) ); } } \ No newline at end of file diff --git a/service/src/main/java/io/mifos/accounting/service/internal/service/TrialBalanceService.java b/service/src/main/java/io/mifos/accounting/service/internal/service/TrialBalanceService.java index e2fec3c..a50d000 100644 --- a/service/src/main/java/io/mifos/accounting/service/internal/service/TrialBalanceService.java +++ b/service/src/main/java/io/mifos/accounting/service/internal/service/TrialBalanceService.java @@ -19,7 +19,6 @@ import io.mifos.accounting.api.v1.domain.AccountType; import io.mifos.accounting.api.v1.domain.financial.statement.TrialBalance; import io.mifos.accounting.api.v1.domain.financial.statement.TrialBalanceEntry; import io.mifos.accounting.service.internal.mapper.LedgerMapper; -import io.mifos.accounting.service.internal.repository.AccountRepository; import io.mifos.accounting.service.internal.repository.LedgerRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,62 +30,57 @@ import java.util.Comparator; public class TrialBalanceService { private final LedgerRepository ledgerRepository; - private final AccountRepository accountRepository; @Autowired - public TrialBalanceService(final LedgerRepository ledgerRepository, - final AccountRepository accountRepository) { + public TrialBalanceService(final LedgerRepository ledgerRepository) { super(); this.ledgerRepository = ledgerRepository; - this.accountRepository = accountRepository; } public TrialBalance getTrialBalance(final boolean includeEmptyEntries) { final TrialBalance trialBalance = new TrialBalance(); - this.ledgerRepository.findAll().forEach(ledgerEntity -> { - final BigDecimal totalValue = ledgerEntity.getTotalValue() != null ? ledgerEntity.getTotalValue() : BigDecimal.ZERO; - if (!includeEmptyEntries && totalValue.compareTo(BigDecimal.ZERO) == 0) { - return; - } - final TrialBalanceEntry trialBalanceEntry = new TrialBalanceEntry(); - trialBalanceEntry.setLedger(LedgerMapper.map(ledgerEntity)); - switch (AccountType.valueOf(ledgerEntity.getType())) { - case ASSET: - case EXPENSE: - trialBalanceEntry.setType(TrialBalanceEntry.Type.DEBIT.name()); - break; - case LIABILITY: - case EQUITY: - case REVENUE: - trialBalanceEntry.setType(TrialBalanceEntry.Type.CREDIT.name()); - break; - } - trialBalanceEntry.setAmount(totalValue.doubleValue()); - trialBalance.getTrialBalanceEntries().add(trialBalanceEntry); - }); + this.ledgerRepository.findByParentLedgerIsNull().forEach(ledgerEntity -> + this.ledgerRepository.findByParentLedgerOrderByIdentifier(ledgerEntity).forEach(subLedger -> { + final BigDecimal totalValue = subLedger.getTotalValue() != null ? subLedger.getTotalValue() : BigDecimal.ZERO; + if (!includeEmptyEntries && totalValue.compareTo(BigDecimal.ZERO) == 0) { + return; + } + final TrialBalanceEntry trialBalanceEntry = new TrialBalanceEntry(); + trialBalanceEntry.setLedger(LedgerMapper.map(subLedger)); + switch (AccountType.valueOf(subLedger.getType())) { + case ASSET: + case EXPENSE: + trialBalanceEntry.setType(TrialBalanceEntry.Type.DEBIT.name()); + break; + case LIABILITY: + case EQUITY: + case REVENUE: + trialBalanceEntry.setType(TrialBalanceEntry.Type.CREDIT.name()); + break; + } + trialBalanceEntry.setAmount(totalValue); + trialBalance.getTrialBalanceEntries().add(trialBalanceEntry); + }) + ); trialBalance.setDebitTotal( trialBalance.getTrialBalanceEntries() .stream() - .filter(trialBalanceEntry -> - trialBalanceEntry.getType().equals(TrialBalanceEntry.Type.DEBIT.name()) - && trialBalanceEntry.getLedger().getParentLedgerIdentifier() == null) - .mapToDouble(TrialBalanceEntry::getAmount) - .sum() + .filter(trialBalanceEntry -> trialBalanceEntry.getType().equals(TrialBalanceEntry.Type.DEBIT.name())) + .map(TrialBalanceEntry::getAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add) ); trialBalance.setCreditTotal( trialBalance.getTrialBalanceEntries() .stream() - .filter(trialBalanceEntry -> - trialBalanceEntry.getType().equals(TrialBalanceEntry.Type.CREDIT.name()) - && trialBalanceEntry.getLedger().getParentLedgerIdentifier() == null) - .mapToDouble(TrialBalanceEntry::getAmount) - .sum() + .filter(trialBalanceEntry -> trialBalanceEntry.getType().equals(TrialBalanceEntry.Type.CREDIT.name())) + .map(TrialBalanceEntry::getAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add) ); // Sort by ledger identifier ASC - trialBalance.getTrialBalanceEntries().sort(Comparator.comparing(o -> o.getLedger().getIdentifier())); + trialBalance.getTrialBalanceEntries().sort(Comparator.comparing(trailBalanceEntry -> trailBalanceEntry.getLedger().getIdentifier())); return trialBalance; } -- To stop receiving notification emails like this one, please contact [email protected].
