private BigDecimal calculateRunningBalance(
     JournalEntryData entry, Map<Long, BigDecimal> runningBalanceMap) {
   BigDecimal runningBalance = BigDecimal.ZERO;
   if (runningBalanceMap.containsKey(entry.getGlAccountId())) {
     runningBalance = runningBalanceMap.get(entry.getGlAccountId());
   }
   GLAccountType accounttype = GLAccountType.fromInt(entry.getGlAccountType().getId().intValue());
   JournalEntryType entryType = JournalEntryType.fromInt(entry.getEntryType().getId().intValue());
   boolean isIncrease = false;
   switch (accounttype) {
     case ASSET:
       if (entryType.isDebitType()) {
         isIncrease = true;
       }
       break;
     case EQUITY:
       if (entryType.isCreditType()) {
         isIncrease = true;
       }
       break;
     case EXPENSE:
       if (entryType.isDebitType()) {
         isIncrease = true;
       }
       break;
     case INCOME:
       if (entryType.isCreditType()) {
         isIncrease = true;
       }
       break;
     case LIABILITY:
       if (entryType.isCreditType()) {
         isIncrease = true;
       }
       break;
   }
   if (isIncrease) {
     runningBalance = runningBalance.add(entry.getAmount());
   } else {
     runningBalance = runningBalance.subtract(entry.getAmount());
   }
   runningBalanceMap.put(entry.getGlAccountId(), runningBalance);
   return runningBalance;
 }
  @Override
  public OfficeOpeningBalancesData retrieveOfficeOpeningBalances(
      final Long officeId, String currencyCode) {

    final FinancialActivityAccount financialActivityAccountId =
        this.financialActivityAccountRepositoryWrapper
            .findByFinancialActivityTypeWithNotFoundDetection(300);
    final Long contraId = financialActivityAccountId.getGlAccount().getId();
    if (contraId == null) {
      throw new GeneralPlatformDomainRuleException(
          "error.msg.financial.activity.mapping.opening.balance.contra.account.cannot.be.null",
          "office-opening-balances-contra-account value can not be null",
          "office-opening-balances-contra-account");
    }

    final JournalEntryAssociationParametersData associationParametersData =
        new JournalEntryAssociationParametersData();
    final GLAccountData contraAccount =
        this.glAccountReadPlatformService.retrieveGLAccountById(
            contraId, associationParametersData);
    if (!GLAccountType.fromInt(contraAccount.getTypeId()).isEquityType()) {
      throw new GeneralPlatformDomainRuleException(
          "error.msg.configuration.opening.balance.contra.account.value.is.invalid.account.type",
          "Global configuration 'office-opening-balances-contra-account' value is not an equity type account",
          contraId);
    }

    final OfficeData officeData = this.officeReadPlatformService.retrieveOffice(officeId);
    final List<JournalEntryData> allOpeningTransactions =
        populateAllTransactionsFromGLAccounts(contraId);
    final String contraTransactionId =
        retrieveContraAccountTransactionId(officeId, contraId, currencyCode);

    List<JournalEntryData> existingOpeningBalanceTransactions = new ArrayList<>();
    if (StringUtils.isNotBlank(contraTransactionId)) {
      existingOpeningBalanceTransactions =
          retrieveOfficeBalanceTransactions(officeId, contraTransactionId, currencyCode);
    }
    final List<JournalEntryData> transactions =
        populateOpeningBalances(existingOpeningBalanceTransactions, allOpeningTransactions);
    final List<JournalEntryData> assetAccountOpeningBalances = new ArrayList<>();
    final List<JournalEntryData> liabityAccountOpeningBalances = new ArrayList<>();
    final List<JournalEntryData> incomeAccountOpeningBalances = new ArrayList<>();
    final List<JournalEntryData> equityAccountOpeningBalances = new ArrayList<>();
    final List<JournalEntryData> expenseAccountOpeningBalances = new ArrayList<>();

    for (final JournalEntryData journalEntryData : transactions) {
      final GLAccountType type =
          GLAccountType.fromInt(journalEntryData.getGlAccountType().getId().intValue());
      if (type.isAssetType()) {
        assetAccountOpeningBalances.add(journalEntryData);
      } else if (type.isLiabilityType()) {
        liabityAccountOpeningBalances.add(journalEntryData);
      } else if (type.isEquityType()) {
        equityAccountOpeningBalances.add(journalEntryData);
      } else if (type.isIncomeType()) {
        incomeAccountOpeningBalances.add(journalEntryData);
      } else if (type.isExpenseType()) {
        expenseAccountOpeningBalances.add(journalEntryData);
      }
    }

    final LocalDate transactionDate = DateUtils.getLocalDateOfTenant();

    final OfficeOpeningBalancesData officeOpeningBalancesData =
        OfficeOpeningBalancesData.createNew(
            officeId,
            officeData.name(),
            transactionDate,
            contraAccount,
            assetAccountOpeningBalances,
            liabityAccountOpeningBalances,
            incomeAccountOpeningBalances,
            equityAccountOpeningBalances,
            expenseAccountOpeningBalances);
    return officeOpeningBalancesData;
  }