in service/src/main/java/org/apache/fineract/cn/accounting/service/rest/JournalRestController.java [73:120]
ResponseEntity<Void> createJournalEntry(@RequestBody @Valid final JournalEntry journalEntry) {
if (this.journalEntryService.findJournalEntry(journalEntry.getTransactionIdentifier()).isPresent()) {
throw ServiceException.conflict("Journal entry {0} already exists.", journalEntry.getTransactionIdentifier());
}
if (journalEntry.getDebtors().size() == 0) {
throw ServiceException.badRequest("Debtors must be given.");
}
if (journalEntry.getCreditors().size() == 0) {
throw ServiceException.badRequest("Creditors must be given.");
}
final Double debtorAmountSum = journalEntry.getDebtors()
.stream()
.peek(debtor -> {
final Optional<Account> accountOptional = this.accountService.findAccount(debtor.getAccountNumber());
if (!accountOptional.isPresent()) {
throw ServiceException.badRequest("Unknown debtor account{0}.", debtor.getAccountNumber());
}
if (!accountOptional.get().getState().equals(Account.State.OPEN.name())) {
throw ServiceException.conflict("Debtor account{0} must be in state open.", debtor.getAccountNumber());
}
})
.map(debtor -> Double.valueOf(debtor.getAmount()))
.reduce(0.0D, (x, y) -> x + y);
final Double creditorAmountSum = journalEntry.getCreditors()
.stream()
.peek(creditor -> {
final Optional<Account> accountOptional = this.accountService.findAccount(creditor.getAccountNumber());
if (!accountOptional.isPresent()) {
throw ServiceException.badRequest("Unknown creditor account{0}.", creditor.getAccountNumber());
}
if (!accountOptional.get().getState().equals(Account.State.OPEN.name())) {
throw ServiceException.conflict("Creditor account{0} must be in state open.", creditor.getAccountNumber());
}
})
.map(creditor -> Double.valueOf(creditor.getAmount()))
.reduce(0.0D, (x, y) -> x + y);
if (!debtorAmountSum.equals(creditorAmountSum)) {
throw ServiceException.conflict(
"Sum of debtor and sum of creditor amounts must be equals.");
}
this.commandGateway.process(new CreateJournalEntryCommand(journalEntry));
return ResponseEntity.accepted().build();
}