in service/src/main/java/org/apache/fineract/cn/cheque/service/internal/command/handler/ChequeAggregate.java [210:259]
public String process(final CancelChequeTransactionCommand cancelChequeTransactionCommand) {
final MICR micr = MICRParser.fromIdentifier(cancelChequeTransactionCommand.identifier());
final Optional<ChequeEntity> optionalCheque = this.chequeRepository.findByChequeNumberAndBranchSortCodeAndAccountNumber(
micr.getChequeNumber(), micr.getBranchSortCode(), micr.getAccountNumber()
);
if (optionalCheque.isPresent()) {
final ChequeEntity chequeEntity = optionalCheque.get();
final JournalEntry journalEntryToReverse =
this.accountingService.findJournalEntry(chequeEntity.getJournalEntryIdentifier());
final LocalDateTime now = LocalDateTime.now(Clock.systemUTC());
final JournalEntry journalEntry = new JournalEntry();
journalEntry.setTransactionIdentifier("chq-cnl-" + UUID.randomUUID().toString());
journalEntry.setTransactionDate(DateConverter.toIsoString(now));
journalEntry.setTransactionType("CQRV");
journalEntry.setMessage("CQRV");
journalEntry.setNote("Cheque canceled, reversed transaction: " + journalEntryToReverse.getTransactionIdentifier() + ".");
journalEntry.setClerk(UserContextHolder.checkedGetUser());
journalEntry.setDebtors(
journalEntryToReverse.getCreditors().stream().map(creditor -> {
final Debtor debtor = new Debtor();
debtor.setAccountNumber(creditor.getAccountNumber());
debtor.setAmount(creditor.getAmount());
return debtor;
}).collect(Collectors.toSet())
);
journalEntry.setCreditors(
journalEntryToReverse.getDebtors().stream().map(debtor -> {
final Creditor creditor = new Creditor();
creditor.setAccountNumber(debtor.getAccountNumber());
creditor.setAmount(debtor.getAmount());
return creditor;
}).collect(Collectors.toSet())
);
this.accountingService.processJournalEntry(journalEntry);
chequeEntity.setState(State.CANCELED.name());
chequeEntity.setLastModifiedBy(UserContextHolder.checkedGetUser());
chequeEntity.setLastModifiedOn(now);
this.chequeRepository.save(chequeEntity);
return cancelChequeTransactionCommand.identifier();
} else {
throw ServiceException.notFound("Cheque {0} not found.", cancelChequeTransactionCommand.identifier());
}
}