import nl.strohalm.cyclos.utils.DateHelper; //导入方法依赖的package包/类
private Loan doGrant(Loan loan, final GrantLoanDTO params) {
final LocalSettings localSettings = settingsService.getLocalSettings();
final TransferType transferType = params.getTransferType();
// Create the loan transfer
final TransferDTO transferDto = new TransferDTO();
if (params.isAutomatic()) {
transferDto.setContext(TransactionContext.AUTOMATIC_LOAN);
} else {
transferDto.setContext(TransactionContext.LOAN);
}
if (params.getDate() != null) {
transferDto.setDate(params.getDate());
}
transferDto.setToOwner(params.getMember());
transferDto.setFrom(accountService.getAccount(new AccountDTO(SystemAccountOwner.instance(), transferType.getFrom())));
transferDto.setTo(accountService.getAccount(new AccountDTO(transferDto.getToOwner(), transferType.getTo())));
transferDto.setAmount(params.getAmount());
transferDto.setDescription(params.getDescription());
transferDto.setTransferType(transferType);
transferDto.setCustomValues(params.getCustomValues());
transferDto.setRates(rateService.applyLoan(transferDto, params));
final Transfer transfer = (Transfer) paymentService.insertWithoutNotification(transferDto);
if (transfer.getProcessDate() == null && params.getDate() != null && DateHelper.daysBetween(params.getDate(), Calendar.getInstance()) != 0) {
throw new AuthorizedPaymentInPastException();
}
// Persist the loan
loan.setTransfer(transfer);
final List payments = loan.getPayments();
loan = loanDao.insert(loan);
loan.setPayments(new ArrayList());
// Insert the installments
int index = 0;
BigDecimal total = BigDecimal.ZERO;
for (final LoanPayment payment : payments) {
payment.setLoan(loan);
payment.setIndex(index++);
BigDecimal amount = localSettings.round(payment.getAmount());
if (index == payments.size()) {
// The last payment should round to total amount
amount = localSettings.round(loan.getTotalAmount().subtract(total));
} else {
total = total.add(amount);
}
payment.setAmount(amount);
loan.getPayments().add(loanPaymentDao.insert(payment));
}
// Notify
memberNotificationHandler.grantedLoanNotification(loan);
return loan;
}