use of org.mifos.security.util.UserContext in project head by mifos.
the class LoanAccountServiceFacadeWebTier method applyLoanRepayment.
@Override
public void applyLoanRepayment(String globalAccountNumber, LocalDate paymentDate, BigDecimal repaymentAmount, String receiptId, LocalDate receiptDate, Short modeOfPayment) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
try {
this.transactionHelper.startTransaction();
LoanBO loan = loanDao.findByGlobalAccountNum(globalAccountNumber);
PersonnelBO personnel = personnelDao.findPersonnelById((short) user.getUserId());
Money outstandingOverpayment = loan.applyNewPaymentMechanism(paymentDate, repaymentAmount, personnel, receiptId, receiptDate, modeOfPayment);
// 3. pay off principal of next installment and recalculate interest if 'over paid'
if (outstandingOverpayment.isGreaterThanZero()) {
Money totalPrincipalDueNow = loan.getTotalPrincipalDue().subtract(outstandingOverpayment);
// assemble into domain entities
LoanOfferingBO loanProduct = this.loanProductDao.findById(loan.getLoanOffering().getPrdOfferingId().intValue());
CustomerBO customer = this.customerDao.findCustomerById(loan.getCustomer().getCustomerId());
List<AccountFeesEntity> accountFeeEntities = new ArrayList<AccountFeesEntity>();
Integer unpaidInstallments = loan.getDetailsOfUnpaidInstallmentsOn(paymentDate).size();
Integer gracePeriodDiff = loan.getNoOfInstallments().intValue() - loan.getGracePeriodDuration().intValue();
Integer gracePeriodsRemaining = Math.max(0, unpaidInstallments - gracePeriodDiff);
LocalDate disbursementDate = new LocalDate(loan.getDetailsOfUpcomigInstallment().getActionDate());
LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(totalPrincipalDueNow, disbursementDate, loan.getInterestRate(), unpaidInstallments, gracePeriodsRemaining, accountFeeEntities, new ArrayList<AccountPenaltiesEntity>());
Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
boolean loanScheduleIndependentOfCustomerMeetingEnabled = false;
MeetingBO loanMeeting = customer.getCustomerMeetingValue();
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
RecurringSchedule createLoanSchedule = new MonthlyOnDayOfMonthSchedule(Integer.valueOf(1), Integer.valueOf(5));
loanMeeting = this.createNewMeetingForRepaymentDay(disbursementDate, createLoanSchedule, customer);
if (loanProduct.isVariableInstallmentsAllowed()) {
loanMeeting.setMeetingStartDate(disbursementDate.toDateMidnight().toDate());
}
}
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
Short userBranchOfficeId = userContext.getBranchId();
LoanSchedule loanSchedule = this.loanScheduleService.generate(loanProduct, customer, loanMeeting, overridenDetail, configuration, userBranchOfficeId, accountFeeEntities, disbursementDate);
loan.rescheduleRemainingUnpaidInstallments(loanSchedule, paymentDate);
loan.recordOverpayment(outstandingOverpayment, paymentDate, personnel, receiptId, receiptDate, modeOfPayment);
}
this.loanDao.save(loan);
this.transactionHelper.commitTransaction();
} catch (BusinessRuleException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getMessageKey(), e);
} catch (AccountException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getKey(), e);
} finally {
this.transactionHelper.closeSession();
}
}
use of org.mifos.security.util.UserContext in project head by mifos.
the class LoanAccountServiceFacadeWebTier method retrieveAllActiveBranchesAndLoanOfficerDetails.
@Override
public ChangeAccountStatusDto retrieveAllActiveBranchesAndLoanOfficerDetails() {
MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(mifosUser);
List<PersonnelDto> loanOfficers = new ArrayList<PersonnelDto>();
List<OfficeDetailsDto> activeBranches = this.officeDao.findActiveBranches(userContext.getBranchId());
if (onlyOneActiveBranchExists(activeBranches)) {
OfficeDetailsDto singleOffice = activeBranches.get(0);
CenterCreation officeDetails = new CenterCreation(singleOffice.getOfficeId(), userContext.getId(), userContext.getLevelId(), userContext.getPreferredLocale());
loanOfficers = this.personnelDao.findActiveLoanOfficersForOffice(officeDetails);
}
boolean loanPendingApprovalStateEnabled = ProcessFlowRules.isLoanPendingApprovalStateEnabled();
Short accountState = AccountState.LOAN_PARTIAL_APPLICATION.getValue();
if (loanPendingApprovalStateEnabled) {
accountState = AccountState.LOAN_PENDING_APPROVAL.getValue();
}
boolean centerHierarchyExists = ClientRules.getCenterHierarchyExists();
return new ChangeAccountStatusDto(activeBranches, loanOfficers, loanPendingApprovalStateEnabled, accountState, centerHierarchyExists);
}
use of org.mifos.security.util.UserContext in project head by mifos.
the class LoanAccountServiceFacadeWebTier method retrieveLoanInformation.
@Override
public LoanInformationDto retrieveLoanInformation(String globalAccountNum) {
MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(mifosUser);
LoanBO loan = this.loanDao.findByGlobalAccountNum(globalAccountNum);
if (loan.isDecliningBalanceInterestRecalculation()) {
loanBusinessService.computeExtraInterest(loan, DateUtils.getCurrentDateWithoutTimeStamp());
}
try {
personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
} catch (AccountException e) {
throw new MifosRuntimeException("Access denied!", e);
}
String fundName = null;
if (loan.getFund() != null) {
fundName = loan.getFund().getFundName();
}
// boolean activeSurveys = surveysPersistence.isActiveSurveysForSurveyType(SurveyType.LOAN);
boolean activeSurveys = false;
List<SurveyDto> accountSurveys = loanDao.getAccountSurveyDto(loan.getAccountId());
LoanSummaryDto loanSummary = new LoanSummaryDto(loan.getLoanSummary().getOriginalPrincipal().toString(), loan.getLoanSummary().getPrincipalPaid().toString(), loan.getLoanSummary().getPrincipalDue().toString(), loan.getLoanSummary().getOriginalInterest().toString(), loan.getLoanSummary().getInterestPaid().toString(), loan.getLoanSummary().getInterestDue().toString(), loan.getLoanSummary().getOriginalFees().toString(), loan.getLoanSummary().getFeesPaid().toString(), loan.getLoanSummary().getFeesDue().toString(), loan.getLoanSummary().getOriginalPenalty().toString(), loan.getLoanSummary().getPenaltyPaid().toString(), loan.getLoanSummary().getPenaltyDue().toString(), loan.getLoanSummary().getTotalLoanAmnt().toString(), loan.getLoanSummary().getTotalAmntPaid().toString(), loan.getLoanSummary().getTotalAmntDue().toString());
LoanPerformanceHistoryEntity performanceHistory = loan.getPerformanceHistory();
LoanPerformanceHistoryDto loanPerformanceHistory = new LoanPerformanceHistoryDto(performanceHistory.getNoOfPayments(), performanceHistory.getTotalNoOfMissedPayments(), performanceHistory.getDaysInArrears(), performanceHistory.getLoanMaturityDate());
Set<AccountFeesDto> accountFeesDtos = new HashSet<AccountFeesDto>();
if (!loan.getAccountFees().isEmpty()) {
for (AccountFeesEntity accountFeesEntity : loan.getAccountFees()) {
AccountFeesDto accountFeesDto = new AccountFeesDto(accountFeesEntity.getFees().getFeeFrequency().getFeeFrequencyType().getId(), (accountFeesEntity.getFees().getFeeFrequency().getFeePayment() != null ? accountFeesEntity.getFees().getFeeFrequency().getFeePayment().getId() : null), accountFeesEntity.getFeeStatus(), accountFeesEntity.getFees().getFeeName(), accountFeesEntity.getAccountFeeAmount().toString(), getMeetingRecurrence(accountFeesEntity.getFees().getFeeFrequency().getFeeMeetingFrequency(), userContext), accountFeesEntity.getFees().getFeeId());
accountFeesDtos.add(accountFeesDto);
}
}
Set<AccountPenaltiesDto> accountPenaltiesDtos = new HashSet<AccountPenaltiesDto>();
if (!loan.getAccountPenalties().isEmpty()) {
for (AccountPenaltiesEntity accountPenaltiesEntity : loan.getAccountPenalties()) {
accountPenaltiesDtos.add(new AccountPenaltiesDto(accountPenaltiesEntity.getPenalty().getPenaltyFrequency().getId(), accountPenaltiesEntity.getPenaltyStatus(), accountPenaltiesEntity.getPenalty().getPenaltyName(), accountPenaltiesEntity.getAccountPenaltyAmount().toString(), accountPenaltiesEntity.getPenalty().getPenaltyFrequency().getName(), accountPenaltiesEntity.getPenalty().getPenaltyId()));
}
}
Set<String> accountFlagNames = getAccountStateFlagEntityNames(loan.getAccountFlags());
Short accountStateId = loan.getAccountState().getId();
String accountStateName = getAccountStateName(accountStateId);
boolean disbursed = AccountState.isDisbursed(accountStateId);
String gracePeriodTypeName = getGracePeriodTypeName(loan.getGracePeriodType().getId());
Short interestType = loan.getInterestType().getId();
String interestTypeName = getInterestTypeName(interestType);
List<CustomerNoteDto> recentNoteDtos = new ArrayList<CustomerNoteDto>();
List<AccountNotesEntity> recentNotes = loan.getRecentAccountNotes();
for (AccountNotesEntity accountNotesEntity : recentNotes) {
recentNoteDtos.add(new CustomerNoteDto(accountNotesEntity.getCommentDate(), accountNotesEntity.getComment(), accountNotesEntity.getPersonnelName()));
}
CustomValueDto customValueDto = legacyMasterDao.getLookUpEntity(MasterConstants.COLLATERAL_TYPES);
List<CustomValueListElementDto> collateralTypes = customValueDto.getCustomValueListElements();
String collateralTypeName = null;
for (CustomValueListElementDto collateralType : collateralTypes) {
if (collateralType.getId() == loan.getCollateralTypeId()) {
collateralTypeName = collateralType.getName();
break;
}
}
return new LoanInformationDto(loan.getLoanOffering().getPrdOfferingName(), globalAccountNum, accountStateId, accountStateName, disbursed, accountFlagNames, loan.getDisbursementDate(), loan.isRedone(), loan.getBusinessActivityId(), loan.getAccountId(), gracePeriodTypeName, interestType, interestTypeName, loan.getCustomer().getCustomerId(), loan.getAccountType().getAccountTypeId(), loan.getOffice().getOfficeId(), loan.getPersonnel().getPersonnelId(), loan.getNextMeetingDate(), loan.getTotalAmountDue().toString(), loan.getTotalAmountInArrears().toString(), loanSummary, loan.getLoanActivityDetails().isEmpty() ? false : true, loan.getInterestRate(), loan.isInterestDeductedAtDisbursement(), loan.getLoanMeeting().getMeetingDetails().getRecurAfter(), loan.getLoanMeeting().getMeetingDetails().getRecurrenceType().getRecurrenceId(), loan.getLoanOffering().isPrinDueLastInst(), loan.getNoOfInstallments(), loan.getMaxMinNoOfInstall().getMinNoOfInstall(), loan.getMaxMinNoOfInstall().getMaxNoOfInstall(), loan.getGracePeriodDuration(), fundName, loan.getCollateralTypeId(), collateralTypeName, loan.getCollateralNote(), loan.getExternalId(), accountFeesDtos, loan.getCreatedDate(), loanPerformanceHistory, loan.getCustomer().isGroup(), getRecentActivityView(globalAccountNum), activeSurveys, accountSurveys, loan.getCustomer().getDisplayName(), loan.getCustomer().getGlobalCustNum(), loan.getOffice().getOfficeName(), recentNoteDtos, accountPenaltiesDtos, AccountingRules.isGroupLoanWithMembers());
}
use of org.mifos.security.util.UserContext in project head by mifos.
the class LoanAccountServiceFacadeWebTier method retrieveLoanRepaymentDetails.
@Override
public RepayLoanDto retrieveLoanRepaymentDetails(String globalAccountNumber) {
MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(mifosUser);
LoanBO loan = this.loanDao.findByGlobalAccountNum(globalAccountNumber);
try {
personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
} catch (AccountException e) {
throw new MifosRuntimeException(e.getMessage(), e);
}
Money repaymentAmount;
Money waiverAmount;
if (loan.isDecliningBalanceInterestRecalculation()) {
RepaymentResultsHolder repaymentResultsHolder = scheduleCalculatorAdaptor.computeRepaymentAmount(loan, DateUtils.getCurrentDateWithoutTimeStamp());
repaymentAmount = new Money(loan.getCurrency(), repaymentResultsHolder.getTotalRepaymentAmount());
waiverAmount = new Money(loan.getCurrency(), repaymentResultsHolder.getWaiverAmount());
} else {
repaymentAmount = loan.getEarlyRepayAmount();
waiverAmount = loan.waiverAmount();
}
Money waivedRepaymentAmount = repaymentAmount.subtract(waiverAmount);
List<SavingsDetailDto> savingsInUse = clientServiceFacade.retrieveSavingsInUseForClient(loan.getCustomer().getCustomerId());
List<SavingsDetailDto> accountsForTransfer = new ArrayList<SavingsDetailDto>();
if (savingsInUse != null) {
for (SavingsDetailDto savingsAccount : savingsInUse) {
if (savingsAccount.getAccountStateId().equals(AccountState.SAVINGS_ACTIVE.getValue())) {
accountsForTransfer.add(savingsAccount);
}
}
}
return new RepayLoanDto(repaymentAmount.toString(), waivedRepaymentAmount.toString(), loan.isInterestWaived(), accountsForTransfer);
}
use of org.mifos.security.util.UserContext in project head by mifos.
the class LoanAccountServiceFacadeWebTier method retrieveInstallmentDetails.
@Override
public LoanInstallmentDetailsDto retrieveInstallmentDetails(Integer accountId) {
MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(mifosUser);
LoanBO loanBO = this.loanDao.findById(accountId);
try {
personnelDao.checkAccessPermission(userContext, loanBO.getOfficeId(), loanBO.getCustomer().getLoanOfficerId());
} catch (AccountException e) {
throw new MifosRuntimeException(e.getMessage(), e);
}
InstallmentDetailsDto viewUpcomingInstallmentDetails;
InstallmentDetailsDto viewOverDueInstallmentDetails;
if (loanBO.isGroupLoanAccount() && null == loanBO.getParentAccount()) {
List<AccountActionDateEntity> memberDetailsOfNextInstallment = new ArrayList<AccountActionDateEntity>();
List<List<AccountActionDateEntity>> memberDetailsOfInstallmentsInArrears = new ArrayList<List<AccountActionDateEntity>>();
for (LoanBO member : loanBO.getMemberAccounts()) {
memberDetailsOfNextInstallment.add(member.getDetailsOfNextInstallment());
memberDetailsOfInstallmentsInArrears.add(member.getDetailsOfInstallmentsInArrears());
}
viewUpcomingInstallmentDetails = getUpcomingInstallmentDetailsForGroupLoan(memberDetailsOfNextInstallment, loanBO.getCurrency());
viewOverDueInstallmentDetails = getOverDueInstallmentDetailsForGroupLoan(memberDetailsOfInstallmentsInArrears, loanBO.getCurrency());
} else {
viewUpcomingInstallmentDetails = getUpcomingInstallmentDetails(loanBO.getDetailsOfNextInstallment(), loanBO.getCurrency());
viewOverDueInstallmentDetails = getOverDueInstallmentDetails(loanBO.getDetailsOfInstallmentsInArrears(), loanBO.getCurrency());
}
Money upcomingInstallmentSubTotal = new Money(loanBO.getCurrency(), viewUpcomingInstallmentDetails.getSubTotal());
Money overdueInstallmentSubTotal = new Money(loanBO.getCurrency(), viewOverDueInstallmentDetails.getSubTotal());
Money totalAmountDue = upcomingInstallmentSubTotal.add(overdueInstallmentSubTotal);
return new LoanInstallmentDetailsDto(viewUpcomingInstallmentDetails, viewOverDueInstallmentDetails, totalAmountDue.toString(), loanBO.getNextMeetingDate());
}
Aggregations