public String bookJournalEntry()

in service/src/main/java/org/apache/fineract/cn/accounting/service/internal/command/handler/AccountCommandHandler.java [327:407]


  public String bookJournalEntry(final BookJournalEntryCommand bookJournalEntryCommand) {
    final String transactionIdentifier = bookJournalEntryCommand.transactionIdentifier();

    final Optional<JournalEntryEntity> optionalJournalEntry = this.journalEntryRepository.findJournalEntry(transactionIdentifier);
    if (optionalJournalEntry.isPresent()) {
      final JournalEntryEntity journalEntryEntity = optionalJournalEntry.get();
      if (!journalEntryEntity.getState().equals(JournalEntry.State.PENDING.name())) {
        return null;
      }
      // process all debtors
      journalEntryEntity.getDebtors()
          .forEach(debtor -> {
            final String accountNumber = debtor.getAccountNumber();
            final AccountEntity accountEntity = this.accountRepository.findByIdentifier(accountNumber);
            final AccountType accountType = AccountType.valueOf(accountEntity.getType());
            final BigDecimal amount;
            switch (accountType) {
              case ASSET:
              case EXPENSE:
                accountEntity.setBalance(accountEntity.getBalance() + debtor.getAmount());
                amount = BigDecimal.valueOf(debtor.getAmount());
                break;
              case LIABILITY:
              case EQUITY:
              case REVENUE:
                accountEntity.setBalance(accountEntity.getBalance() - debtor.getAmount());
                amount = BigDecimal.valueOf(debtor.getAmount()).negate();
                break;
              default:
                amount = BigDecimal.ZERO;
            }
            final AccountEntity savedAccountEntity = this.accountRepository.save(accountEntity);
            final AccountEntryEntity accountEntryEntity = new AccountEntryEntity();
            accountEntryEntity.setType(AccountEntry.Type.DEBIT.name());
            accountEntryEntity.setAccount(savedAccountEntity);
            accountEntryEntity.setBalance(savedAccountEntity.getBalance());
            accountEntryEntity.setAmount(debtor.getAmount());
            accountEntryEntity.setMessage(journalEntryEntity.getMessage());
            accountEntryEntity.setTransactionDate(journalEntryEntity.getTransactionDate());
            this.accountEntryRepository.save(accountEntryEntity);
            this.adjustLedgerTotals(savedAccountEntity.getLedger().getIdentifier(), amount);
          });
      // process all creditors
      journalEntryEntity.getCreditors()
          .forEach(creditor -> {
            final String accountNumber = creditor.getAccountNumber();
            final AccountEntity accountEntity = this.accountRepository.findByIdentifier(accountNumber);
            final AccountType accountType = AccountType.valueOf(accountEntity.getType());
            final BigDecimal amount;
            switch (accountType) {
              case ASSET:
              case EXPENSE:
                accountEntity.setBalance(accountEntity.getBalance() - creditor.getAmount());
                amount = BigDecimal.valueOf(creditor.getAmount()).negate();
                break;
              case LIABILITY:
              case EQUITY:
              case REVENUE:
                accountEntity.setBalance(accountEntity.getBalance() + creditor.getAmount());
                amount = BigDecimal.valueOf(creditor.getAmount());
                break;
              default:
                amount = BigDecimal.ZERO;
            }
            final AccountEntity savedAccountEntity = this.accountRepository.save(accountEntity);
            final AccountEntryEntity accountEntryEntity = new AccountEntryEntity();
            accountEntryEntity.setType(AccountEntry.Type.CREDIT.name());
            accountEntryEntity.setAccount(savedAccountEntity);
            accountEntryEntity.setBalance(savedAccountEntity.getBalance());
            accountEntryEntity.setAmount(creditor.getAmount());
            accountEntryEntity.setMessage(journalEntryEntity.getMessage());
            accountEntryEntity.setTransactionDate(journalEntryEntity.getTransactionDate());
            this.accountEntryRepository.save(accountEntryEntity);
            this.adjustLedgerTotals(savedAccountEntity.getLedger().getIdentifier(), amount);
          });
      this.commandGateway.process(new ReleaseJournalEntryCommand(transactionIdentifier));
      return transactionIdentifier;
    } else {
      return null;
    }
  }