in service/src/main/java/org/apache/fineract/cn/deposit/service/internal/service/TransactionService.java [188:227]
private TransactionEntity doWithdraw(@NotNull TransactionRequestData request, @NotNull AccountWrapper accountWrapper,
List<Charge> charges, LocalDateTime transactionDate, String accountId) {
BigDecimal amount = request.getAmount().getAmount();
TransactionEntity txn = createTransaction(request, TransactionTypeEnum.WITHDRAWAL, transactionDate, DEBIT,
null, accountId, accountWrapper.instance);
String creditAccountIdentifier = accountWrapper.productDefinition.getCashAccountIdentifier();
/* if subtxn is provided and it has an account configured the do credit that account*/
if(StringUtils.isNotBlank(request.getSubTxnId())){
Optional<SubTransactionType> subTxnTypeOpt = this.subTxnTypesService.findByIdentifier(request.getSubTxnId());
if(subTxnTypeOpt.isPresent()) {
txn.setSubTxnType(subTxnTypeOpt.get().getIdentifier());
if (subTxnTypeOpt.get().getLedgerAccount() != null) {
creditAccountIdentifier = subTxnTypeOpt.get().getLedgerAccount();
}
}else{
throw ServiceException.notFound("Sub Txn Type {0} not found.", request.getSubTxnId());
}
}
final JournalEntry journalEntry = createJournalEntry(txn.getIdentifier(), TransactionTypeEnum.WITHDRAWAL.getCode(),
DateConverter.toIsoString(transactionDate), request.getNote(), getLoginUser());
HashSet<Debtor> debtors = new HashSet<>(1);
HashSet<Creditor> creditors = new HashSet<>(1);
addCreditor(creditAccountIdentifier, amount.doubleValue(), creditors);
addDebtor(accountWrapper.account.getIdentifier(), amount.doubleValue(), debtors);
prepareCharges(request, accountWrapper, charges, debtors, creditors, txn, accountId);
if (debtors.isEmpty()) // must be same size as creditors
throw ServiceException.badRequest("Debit and Credit doesn't match");
journalEntry.setDebtors(debtors);
journalEntry.setCreditors(creditors);
transactionRepository.save(txn);
ledgerManager.createJournalEntry(journalEntry);
return txn;
}