use of org.mifos.clientportfolio.newloan.domain.GroupMemberLoanDetail in project head by mifos.
the class LoanAccountServiceFacadeWebTier method createLoanAccount.
private LoanCreationResultDto createLoanAccount(CreateLoanAccount loanAccountInfo, List<LoanPaymentDto> backdatedLoanPayments, List<QuestionGroupDetail> questionGroups, LoanAccountCashFlow loanAccountCashFlow, List<DateTime> loanScheduleInstallmentDates, List<Number> totalInstallmentAmounts, List<GroupMemberAccountDto> memberDetails, boolean isBackdatedLoan) {
DateTime creationDate = new DateTime();
// 0. verify member details for GLIM group accounts
for (GroupMemberAccountDto groupMemberAccount : memberDetails) {
ClientBO member = this.customerDao.findClientBySystemId(groupMemberAccount.getGlobalId());
if (creationDate.isBefore(new DateTime(member.getCreatedDate()))) {
throw new BusinessRuleException("errors.cannotCreateLoan.because.clientsAreCreatedInFuture");
}
}
// 1. assemble loan details
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
OfficeBO userOffice = this.officeDao.findOfficeById(user.getBranchId());
PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
CustomerBO customer = this.customerDao.findCustomerById(loanAccountInfo.getCustomerId());
if (customer.isGroup()) {
customer = this.customerDao.findGroupBySystemId(customer.getGlobalCustNum());
}
// assemble
LoanAccountDetail loanAccountDetail = assembleLoanAccountDetail(loanAccountInfo);
List<AccountFeesEntity> accountFeeEntities = assembleAccountFees(loanAccountInfo.getAccountFees());
List<AccountPenaltiesEntity> accountPenaltyEntities = assembleAccountPenalties(loanAccountInfo.getAccountPenalties());
LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(loanAccountDetail.getLoanAmount(), loanAccountInfo.getDisbursementDate(), loanAccountInfo.getInterestRate(), loanAccountInfo.getNumberOfInstallments(), loanAccountInfo.getGraceDuration(), accountFeeEntities, accountPenaltyEntities);
Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
boolean loanScheduleIndependentOfCustomerMeetingEnabled = loanAccountInfo.isRepaymentScheduleIndependentOfCustomerMeeting();
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
MeetingBO repaymentDayMeeting = null;
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
repaymentDayMeeting = this.createNewMeetingForRepaymentDay(loanAccountInfo.getDisbursementDate(), loanAccountInfo, loanAccountDetail.getCustomer());
} else {
MeetingDto customerMeetingDto = customer.getCustomerMeetingValue().toDto();
repaymentDayMeeting = new MeetingFactory().create(customerMeetingDto);
Short recurAfter = loanAccountDetail.getLoanProduct().getLoanOfferingMeeting().getMeeting().getRecurAfter();
repaymentDayMeeting.getMeetingDetails().setRecurAfter(recurAfter);
}
List<DateTime> loanScheduleDates = new ArrayList<DateTime>(loanScheduleInstallmentDates);
LoanSchedule loanSchedule = assembleLoanSchedule(loanAccountDetail.getCustomer(), loanAccountDetail.getLoanProduct(), overridenDetail, configuration, repaymentDayMeeting, userOffice, loanScheduleDates, loanAccountInfo.getDisbursementDate(), totalInstallmentAmounts);
// 2. create loan
InstallmentRange installmentRange = new MaxMinNoOfInstall(loanAccountInfo.getMinAllowedNumberOfInstallments().shortValue(), loanAccountInfo.getMaxAllowedNumberOfInstallments().shortValue(), null);
AmountRange amountRange = new MaxMinLoanAmount(loanAccountInfo.getMaxAllowedLoanAmount().doubleValue(), loanAccountInfo.getMinAllowedLoanAmount().doubleValue(), null);
if (isBackdatedLoan) {
creationDate = loanAccountInfo.getDisbursementDate().toDateMidnight().toDateTime();
}
CreationDetail creationDetail = new CreationDetail(creationDate, Integer.valueOf(user.getUserId()));
LoanBO loan = LoanBO.openStandardLoanAccount(loanAccountDetail.getLoanProduct(), loanAccountDetail.getCustomer(), repaymentDayMeeting, loanSchedule, loanAccountDetail.getAccountState(), loanAccountDetail.getFund(), overridenDetail, configuration, installmentRange, amountRange, creationDetail, createdBy);
loan.setBusinessActivityId(loanAccountInfo.getLoanPurposeId());
loan.setExternalId(loanAccountInfo.getExternalId());
loan.setCollateralNote(loanAccountInfo.getCollateralNotes());
loan.setCollateralTypeId(loanAccountInfo.getCollateralTypeId());
if (isBackdatedLoan) {
loan.markAsCreatedWithBackdatedPayments();
}
//set up predefined loan account for importing loans
if (loanAccountInfo.getPredefinedAccountNumber() != null) {
loan.setGlobalAccountNum(loanAccountInfo.getPredefinedAccountNumber());
}
try {
personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
} catch (AccountException e) {
throw new MifosRuntimeException("Access denied!", e);
}
try {
transactionHelper.startTransaction();
this.loanDao.save(loan);
transactionHelper.flushSession();
//no predefined account number, generate one instead
if (loanAccountInfo.getPredefinedAccountNumber() == null) {
try {
loan.setGlobalAccountNum(loan.generateId(userOffice.getGlobalOfficeNum()));
} catch (AccountException e) {
throw new BusinessRuleException(e.getMessage());
}
this.loanDao.save(loan);
transactionHelper.flushSession();
}
//set up status flag
AccountStateFlagEntity flagEntity = null;
if (loanAccountInfo.getFlagId() != null) {
try {
flagEntity = legacyMasterDao.getPersistentObject(AccountStateFlagEntity.class, loanAccountInfo.getFlagId());
loan.setUserContext(userContext);
loan.setFlag(flagEntity);
loan.setClosedDate(new DateTimeService().getCurrentJavaDateTime());
loan.setUserContext(userContext);
} catch (PersistenceException e) {
throw new BusinessRuleException(e.getMessage());
}
this.loanDao.save(loan);
transactionHelper.flushSession();
}
// for GLIM loans only
List<GroupMemberLoanDetail> individualMembersOfGroupLoan = new ArrayList<GroupMemberLoanDetail>();
List<BigDecimal> radio = new ArrayList<BigDecimal>(loan.getNoOfInstallments());
for (GroupMemberAccountDto groupMemberAccount : memberDetails) {
ClientBO member = this.customerDao.findClientBySystemId(groupMemberAccount.getGlobalId());
Money loanAmount = new Money(loanAccountDetail.getLoanProduct().getCurrency(), groupMemberAccount.getLoanAmount());
List<CreateAccountFeeDto> defaultAccountFees = new ArrayList<CreateAccountFeeDto>();
List<CreateAccountPenaltyDto> defaultAccountPenalties = new ArrayList<CreateAccountPenaltyDto>();
radio.add(loanAmount.divide(loan.getLoanAmount()));
for (CreateAccountFeeDto createAccountFeeDto : loanAccountInfo.getAccountFees()) {
Integer feeId = createAccountFeeDto.getFeeId();
String amount = createAccountFeeDto.getAmount();
FeeBO feeBO = this.feeDao.findById(feeId.shortValue());
if (feeBO instanceof AmountFeeBO) {
amount = String.valueOf(Double.valueOf(createAccountFeeDto.getAmount()) * (loanAmount.divide(loanAccountInfo.getLoanAmount()).getAmount().doubleValue()));
}
defaultAccountFees.add(new CreateAccountFeeDto(feeId, amount));
}
int memberCount = memberDetails.size();
for (CreateAccountPenaltyDto createAccountPenaltyDto : loanAccountInfo.getAccountPenalties()) {
Integer penaltyId = createAccountPenaltyDto.getPenaltyId();
String amount = createAccountPenaltyDto.getAmount();
PenaltyBO penaltyBO = this.penaltyDao.findPenaltyById(penaltyId.shortValue());
if (penaltyBO instanceof AmountPenaltyBO) {
amount = String.valueOf(Double.valueOf(createAccountPenaltyDto.getAmount()) / memberCount);
}
defaultAccountPenalties.add(new CreateAccountPenaltyDto(penaltyId, amount));
}
List<AccountFeesEntity> feeEntities = assembleAccountFees(defaultAccountFees);
List<AccountPenaltiesEntity> penaltyEntities = assembleAccountPenalties(defaultAccountPenalties);
LoanProductOverridenDetail memberOverridenDetail = new LoanProductOverridenDetail(loanAmount, feeEntities, overridenDetail, penaltyEntities);
LoanSchedule memberSchedule = assembleLoanSchedule(member, loanAccountDetail.getLoanProduct(), memberOverridenDetail, configuration, repaymentDayMeeting, userOffice, new ArrayList<DateTime>(), loanAccountInfo.getDisbursementDate(), new ArrayList<Number>());
GroupMemberLoanDetail groupMemberLoanDetail = new GroupMemberLoanDetail(member, memberOverridenDetail, memberSchedule, groupMemberAccount.getLoanPurposeId());
individualMembersOfGroupLoan.add(groupMemberLoanDetail);
}
checkScheduleForMembers(loanSchedule, loan, individualMembersOfGroupLoan, radio);
//for original schedule persisting
List<LoanBO> memberLoans = new ArrayList<LoanBO>();
for (GroupMemberLoanDetail groupMemberAccount : individualMembersOfGroupLoan) {
LoanBO memberLoan = LoanBO.openGroupMemberLoanAccount(loan, loanAccountDetail.getLoanProduct(), groupMemberAccount.getMember(), repaymentDayMeeting, groupMemberAccount.getMemberSchedule(), groupMemberAccount.getMemberOverridenDetail(), configuration, installmentRange, amountRange, creationDetail, createdBy);
if (groupMemberAccount.getLoanPurposeId() > 0) {
memberLoan.setBusinessActivityId(groupMemberAccount.getLoanPurposeId());
}
if (!backdatedLoanPayments.isEmpty()) {
memberLoan.markAsCreatedWithBackdatedPayments();
}
this.loanDao.save(memberLoan);
transactionHelper.flushSession();
try {
memberLoan.setGlobalAccountNum(memberLoan.generateId(userOffice.getGlobalOfficeNum()));
} catch (AccountException e) {
throw new BusinessRuleException(e.getMessage());
}
this.loanDao.save(memberLoan);
transactionHelper.flushSession();
memberLoans.add(memberLoan);
}
// save question groups
if (!questionGroups.isEmpty()) {
Integer eventSourceId = questionnaireServiceFacade.getEventSourceId("Create", "Loan");
QuestionGroupDetails questionGroupDetails = new QuestionGroupDetails(Integer.valueOf(user.getUserId()).shortValue(), loan.getAccountId(), eventSourceId, questionGroups);
questionnaireServiceFacade.saveResponses(questionGroupDetails);
transactionHelper.flushSession();
}
if (loanAccountCashFlow != null && !loanAccountCashFlow.getMonthlyCashFlow().isEmpty()) {
List<MonthlyCashFlowDetail> monthlyCashFlowDetails = new ArrayList<MonthlyCashFlowDetail>();
for (MonthlyCashFlowDto monthlyCashFlow : loanAccountCashFlow.getMonthlyCashFlow()) {
MonthlyCashFlowDetail monthlyCashFlowDetail = new MonthlyCashFlowDetail(monthlyCashFlow.getMonthDate(), monthlyCashFlow.getRevenue(), monthlyCashFlow.getExpenses(), monthlyCashFlow.getNotes());
monthlyCashFlowDetails.add(monthlyCashFlowDetail);
}
org.mifos.platform.cashflow.service.CashFlowDetail cashFlowDetail = new org.mifos.platform.cashflow.service.CashFlowDetail(monthlyCashFlowDetails);
cashFlowDetail.setTotalCapital(loanAccountCashFlow.getTotalCapital());
cashFlowDetail.setTotalLiability(loanAccountCashFlow.getTotalLiability());
cashFlowService.save(cashFlowDetail);
transactionHelper.flushSession();
}
if (isBackdatedLoan) {
// 3. auto approve loan
String comment = "Automatic Status Update (Redo Loan)";
LocalDate approvalDate = loanAccountInfo.getDisbursementDate();
loan.approve(createdBy, comment, approvalDate);
// 4. disburse loan
String receiptNumber = null;
Date receiptDate = null;
PaymentTypeEntity paymentType = new PaymentTypeEntity(PaymentTypes.CASH.getValue());
if (loanAccountInfo.getDisbursalPaymentTypeId() != null) {
paymentType = new PaymentTypeEntity(loanAccountInfo.getDisbursalPaymentTypeId());
}
Date paymentDate = loanAccountInfo.getDisbursementDate().toDateMidnight().toDate();
AccountPaymentEntity disbursalPayment = new AccountPaymentEntity(loan, loan.getLoanAmount(), receiptNumber, receiptDate, paymentType, paymentDate);
disbursalPayment.setCreatedByUser(createdBy);
this.loanBusinessService.persistOriginalSchedule(loan);
for (LoanBO memberLoan : memberLoans) {
this.loanBusinessService.persistOriginalSchedule(memberLoan);
}
// refactoring of loan disbursal
if (customer.isDisbursalPreventedDueToAnyExistingActiveLoansForTheSameProduct(loan.getLoanOffering())) {
throw new BusinessRuleException("errors.cannotDisburseLoan.because.otherLoansAreActive");
}
try {
loan.updateCustomer(customer);
new ProductMixValidator().checkIfProductsOfferingCanCoexist(loan);
} catch (ServiceException e1) {
throw new AccountException(e1.getMessage());
}
loan.disburse(createdBy, disbursalPayment);
customer.updatePerformanceHistoryOnDisbursement(loan, loan.getLoanAmount());
// end of refactoring of loan disbural
this.loanDao.save(loan);
transactionHelper.flushSession();
// 5. apply each payment
for (LoanPaymentDto loanPayment : backdatedLoanPayments) {
Money amountPaidToDate = new Money(loan.getCurrency(), loanPayment.getAmount());
PaymentData paymentData = new PaymentData(amountPaidToDate, createdBy, loanPayment.getPaymentTypeId(), loanPayment.getPaymentDate().toDateMidnight().toDate());
loan.applyPayment(paymentData);
this.loanDao.save(loan);
}
}
transactionHelper.commitTransaction();
return new LoanCreationResultDto(false, loan.getAccountId(), loan.getGlobalAccountNum());
} catch (BusinessRuleException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getMessageKey(), e);
} catch (Exception e) {
this.transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} finally {
this.transactionHelper.closeSession();
}
}
use of org.mifos.clientportfolio.newloan.domain.GroupMemberLoanDetail in project head by mifos.
the class LoanAccountServiceFacadeWebTier method checkScheduleForMembers.
private void checkScheduleForMembers(LoanSchedule loanSchedule, LoanBO loan, List<GroupMemberLoanDetail> individualMembersOfGroupLoan, List<BigDecimal> radio) {
for (int i = 0; i < loan.getNoOfInstallments(); ++i) {
BigDecimal principal = loanSchedule.getRoundedLoanSchedules().get(i).getPrincipal().getAmount();
BigDecimal interest = loanSchedule.getRoundedLoanSchedules().get(i).getInterest().getAmount();
BigDecimal miscFee = loanSchedule.getRoundedLoanSchedules().get(i).getMiscFee().getAmount();
BigDecimal miscPenalty = loanSchedule.getRoundedLoanSchedules().get(i).getMiscPenalty().getAmount();
for (GroupMemberLoanDetail groupMemberLoanDetail : individualMembersOfGroupLoan) {
LoanScheduleEntity loanScheduleEntity = groupMemberLoanDetail.getMemberSchedule().getRoundedLoanSchedules().get(i);
principal = principal.subtract(loanScheduleEntity.getPrincipal().getAmount());
interest = interest.subtract(loanScheduleEntity.getInterest().getAmount());
miscFee = miscFee.subtract(loanScheduleEntity.getMiscFee().getAmount());
miscPenalty = miscPenalty.subtract(loanScheduleEntity.getMiscPenalty().getAmount());
}
if (principal.compareTo(BigDecimal.ZERO) != 0) {
for (int j = 0; j < individualMembersOfGroupLoan.size(); ++j) {
Money oldPrincipal = individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).getPrincipal();
Money newPrincipal = oldPrincipal.add(new Money(loan.getCurrency(), principal.multiply(radio.get(j))));
individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).setPrincipal(newPrincipal);
}
}
if (interest.compareTo(BigDecimal.ZERO) != 0) {
for (int j = 0; j < individualMembersOfGroupLoan.size(); ++j) {
Money oldinterest = individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).getInterest();
Money newInterest = oldinterest.add(new Money(loan.getCurrency(), interest.multiply(radio.get(j))));
individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).setInterest(newInterest);
}
}
if (miscFee.compareTo(BigDecimal.ZERO) != 0) {
for (int j = 0; j < individualMembersOfGroupLoan.size(); ++j) {
Money oldMiscFee = individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).getMiscFee();
Money newMiscFee = oldMiscFee.add(new Money(loan.getCurrency(), miscFee.multiply(radio.get(j))));
individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).setMiscFee(newMiscFee);
}
}
if (miscPenalty.compareTo(BigDecimal.ZERO) != 0) {
for (int j = 0; j < individualMembersOfGroupLoan.size(); ++j) {
Money oldMiscPenalty = individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).getMiscPenalty();
Money newMiscPenalty = oldMiscPenalty.add(new Money(loan.getCurrency(), miscPenalty.multiply(radio.get(j))));
individualMembersOfGroupLoan.get(j).getMemberSchedule().getRoundedLoanSchedules().get(i).setMiscPenalty(newMiscPenalty);
}
}
}
}
Aggregations