use of org.mifos.accounts.business.AccountFeesEntity in project head by mifos.
the class GroupLoanAccountServiceFacadeWebTier method getGroupLoanScheduleDto.
@Override
public GroupLoanScheduleDto getGroupLoanScheduleDto(CreateGroupLoanAccount loanAccountInfo, List<CreateLoanAccount> memberDetails) {
DateTime creationDate = new DateTime();
// 0. verify member details for GLIM group accounts
for (CreateLoanAccount groupMemberAccount : memberDetails) {
ClientBO member = this.customerDao.findClientById(groupMemberAccount.getCustomerId());
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.getGroupLoanAccountDetails().getCustomerId());
if (customer.isGroup()) {
customer = this.customerDao.findGroupBySystemId(customer.getGlobalCustNum());
}
// assemble
LoanAccountDetail loanAccountDetail = assembleLoanAccountDetail(loanAccountInfo);
List<AccountFeesEntity> accountFeeEntities = assembleAccountFees(loanAccountInfo.getGroupLoanAccountDetails().getAccountFees());
List<AccountPenaltiesEntity> accountPenaltyEntities = assembleAccountPenalties(loanAccountInfo.getGroupLoanAccountDetails().getAccountPenalties());
LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(loanAccountDetail.getLoanAmount(), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loanAccountInfo.getGroupLoanAccountDetails().getInterestRate(), loanAccountInfo.getGroupLoanAccountDetails().getNumberOfInstallments(), loanAccountInfo.getGroupLoanAccountDetails().getGraceDuration(), accountFeeEntities, accountPenaltyEntities);
Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
boolean loanScheduleIndependentOfCustomerMeetingEnabled = loanAccountInfo.getGroupLoanAccountDetails().isRepaymentScheduleIndependentOfCustomerMeeting();
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
MeetingBO repaymentDayMeeting = null;
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
repaymentDayMeeting = this.createNewMeetingForRepaymentDay(loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loanAccountInfo.getGroupLoanAccountDetails(), 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>();
LoanSchedule loanSchedule = assembleLoanSchedule(loanAccountDetail.getCustomer(), loanAccountDetail.getLoanProduct(), overridenDetail, configuration, repaymentDayMeeting, userOffice, loanScheduleDates, loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), new ArrayList<Number>());
// 2. create loan
InstallmentRange installmentRange = new MaxMinNoOfInstall(loanAccountInfo.getGroupLoanAccountDetails().getMinAllowedNumberOfInstallments().shortValue(), loanAccountInfo.getGroupLoanAccountDetails().getMaxAllowedNumberOfInstallments().shortValue(), null);
AmountRange amountRange = new MaxMinLoanAmount(loanAccountInfo.getGroupLoanAccountDetails().getMaxAllowedLoanAmount().doubleValue(), loanAccountInfo.getGroupLoanAccountDetails().getMinAllowedLoanAmount().doubleValue(), null);
creationDate = loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate().toDateMidnight().toDateTime();
CreationDetail creationDetail = new CreationDetail(creationDate, Integer.valueOf(user.getUserId()));
LoanBO loan = LoanBO.openGroupLoanAccount(loanAccountDetail.getLoanProduct(), loanAccountDetail.getCustomer(), repaymentDayMeeting, loanSchedule, loanAccountDetail.getAccountState(), loanAccountDetail.getFund(), overridenDetail, configuration, installmentRange, amountRange, creationDetail, createdBy);
loan.setBusinessActivityId(loanAccountInfo.getGroupLoanAccountDetails().getLoanPurposeId());
loan.setExternalId(loanAccountInfo.getGroupLoanAccountDetails().getExternalId());
loan.setCollateralNote(loanAccountInfo.getGroupLoanAccountDetails().getCollateralNotes());
loan.setCollateralTypeId(loanAccountInfo.getGroupLoanAccountDetails().getCollateralTypeId());
loan.markAsCreatedWithBackdatedPayments();
//set up predefined loan account for importing loans
if (loanAccountInfo.getGroupLoanAccountDetails().getPredefinedAccountNumber() != null) {
loan.setGlobalAccountNum(loanAccountInfo.getGroupLoanAccountDetails().getPredefinedAccountNumber());
}
// for GLIM loans only
List<CreateLoanAccount> individualMembersOfGroupLoan = new ArrayList<CreateLoanAccount>();
List<BigDecimal> radio = new ArrayList<BigDecimal>(loan.getNoOfInstallments());
List<LoanProductOverridenDetail> memberOverridenDetails = new ArrayList<LoanProductOverridenDetail>();
List<LoanSchedule> memberLoanSchedules = new ArrayList<LoanSchedule>();
List<ClientBO> clients = new ArrayList<ClientBO>();
for (CreateLoanAccount groupMemberAccount : memberDetails) {
ClientBO member = this.customerDao.findClientById(groupMemberAccount.getCustomerId());
Money loanAmount = new Money(loanAccountDetail.getLoanProduct().getCurrency(), groupMemberAccount.getLoanAmount());
List<CreateAccountPenaltyDto> defaultAccountPenalties = new ArrayList<CreateAccountPenaltyDto>();
radio.add(loanAmount.divide(loan.getLoanAmount()));
int memberCount = memberDetails.size();
for (CreateAccountPenaltyDto createAccountPenaltyDto : loanAccountInfo.getGroupLoanAccountDetails().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(groupMemberAccount.getAccountFees());
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.getGroupLoanAccountDetails().getDisbursementDate(), new ArrayList<Number>());
memberOverridenDetails.add(memberOverridenDetail);
memberLoanSchedules.add(memberSchedule);
clients.add(member);
individualMembersOfGroupLoan.add(groupMemberAccount);
}
//for original schedule persisting
List<LoanBO> memberLoans = new ArrayList<LoanBO>();
int index = 0;
for (CreateLoanAccount groupMemberAccount : individualMembersOfGroupLoan) {
LoanBO memberLoan = LoanBO.openGroupLoanForAccount(loan, loanAccountDetail.getLoanProduct(), clients.get(index), repaymentDayMeeting, memberLoanSchedules.get(index), memberOverridenDetails.get(index), configuration, installmentRange, amountRange, creationDetail, createdBy, Boolean.TRUE);
if (groupMemberAccount.getLoanPurposeId() > 0) {
memberLoan.setBusinessActivityId(groupMemberAccount.getLoanPurposeId());
}
memberLoan.markAsCreatedWithBackdatedPayments();
memberLoans.add(memberLoan);
index++;
}
// update loan schedule for Group Loan Account
loanSchedule = this.loanScheduleService.generateGroupLoanSchedule(loanAccountDetail.getLoanProduct(), repaymentDayMeeting, loanSchedule, memberLoanSchedules, loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), overridenDetail, configuration, userOffice.getOfficeId(), loanAccountDetail.getCustomer(), accountFeeEntities);
fixMemberAndParentInstallmentDetails(loan, memberLoans);
// translate schedules to dto form
Map<Integer, LoanScheduleEntity> parentScheduleEntities = loan.getLoanScheduleEntityMap();
List<LoanCreationInstallmentDto> parentInstallments = new ArrayList<LoanCreationInstallmentDto>();
Map<Integer, List<LoanCreationInstallmentDto>> memberInstallments = new HashMap<Integer, List<LoanCreationInstallmentDto>>();
for (CreateLoanAccount member : memberDetails) {
memberInstallments.put(member.getCustomerId(), new ArrayList<LoanCreationInstallmentDto>());
}
Short digitsAfterDecimal = AccountingRules.getDigitsAfterDecimal();
for (Integer installmentId : parentScheduleEntities.keySet()) {
LoanScheduleEntity loanScheduleEntity = parentScheduleEntities.get(installmentId);
parentInstallments.add(loanScheduleEntity.toLoanCreationInstallmentDto(digitsAfterDecimal));
for (LoanBO memberLoan : memberLoans) {
LoanScheduleEntity memberLoanScheduleEntity = memberLoan.getLoanScheduleEntityMap().get(installmentId);
memberInstallments.get(memberLoan.getCustomer().getCustomerId()).add(memberLoanScheduleEntity.toLoanCreationInstallmentDto(digitsAfterDecimal));
}
}
GroupLoanScheduleDto groupScheduleDto = new GroupLoanScheduleDto(customer.getDisplayName(), Double.valueOf(loan.getLoanAmount().getAmountDoubleValue()), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loan.getGraceType().getValue().intValue(), parentInstallments);
groupScheduleDto.setMemberSchedules(new HashMap<Integer, LoanScheduleDto>());
for (LoanBO memberLoan : memberLoans) {
Integer id = memberLoan.getCustomer().getCustomerId();
LoanScheduleDto memberScheduleDto = new LoanScheduleDto(memberLoan.getCustomer().getDisplayName(), Double.valueOf(memberLoan.getLoanAmount().getAmountDoubleValue()), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loan.getGraceType().getValue().intValue(), memberInstallments.get(id));
groupScheduleDto.getMemberSchedules().put(id, memberScheduleDto);
}
return groupScheduleDto;
}
use of org.mifos.accounts.business.AccountFeesEntity in project head by mifos.
the class GroupLoanAccountServiceFacadeWebTier method assembleAccountFees.
private List<AccountFeesEntity> assembleAccountFees(List<CreateAccountFeeDto> defaultAccountFees) {
List<AccountFeesEntity> accountFeeEntities = new ArrayList<AccountFeesEntity>();
for (CreateAccountFeeDto defaultFee : defaultAccountFees) {
FeeBO fee = this.feeDao.findById(defaultFee.getFeeId().shortValue());
AccountFeesEntity deafultAccountFeeEntity = new AccountFeesEntity(null, fee, Double.valueOf(defaultFee.getAmount()));
accountFeeEntities.add(deafultAccountFeeEntity);
}
return accountFeeEntities;
}
use of org.mifos.accounts.business.AccountFeesEntity in project head by mifos.
the class GroupServiceFacadeWebTier method convertFeeViewsToAccountFeeEntities.
private List<AccountFeesEntity> convertFeeViewsToAccountFeeEntities(List<ApplicableAccountFeeDto> feesToApply) {
List<AccountFeesEntity> feesForCustomerAccount = new ArrayList<AccountFeesEntity>();
for (ApplicableAccountFeeDto feeDto : feesToApply) {
FeeBO fee = feeDao.findById(feeDto.getFeeId().shortValue());
Double feeAmount = new LocalizationConverter().getDoubleValueForCurrentLocale(feeDto.getAmount());
AccountBO nullReferenceForNow = null;
AccountFeesEntity accountFee = new AccountFeesEntity(nullReferenceForNow, fee, feeAmount);
feesForCustomerAccount.add(accountFee);
}
return feesForCustomerAccount;
}
use of org.mifos.accounts.business.AccountFeesEntity in project head by mifos.
the class ClientServiceFacadeWebTier method convertFeeViewsToAccountFeeEntities.
private List<AccountFeesEntity> convertFeeViewsToAccountFeeEntities(List<ApplicableAccountFeeDto> feesToApply) {
List<AccountFeesEntity> feesForCustomerAccount = new ArrayList<AccountFeesEntity>();
for (ApplicableAccountFeeDto feeDto : feesToApply) {
FeeBO fee = this.feeDao.findById(feeDto.getFeeId().shortValue());
Double feeAmount = new LocalizationConverter().getDoubleValueForCurrentLocale(feeDto.getAmount());
AccountBO nullReferenceForNow = null;
AccountFeesEntity accountFee = new AccountFeesEntity(nullReferenceForNow, fee, feeAmount);
feesForCustomerAccount.add(accountFee);
}
return feesForCustomerAccount;
}
use of org.mifos.accounts.business.AccountFeesEntity in project head by mifos.
the class ClientServiceFacadeWebTier method createNewClient.
@Override
public CustomerDetailsDto createNewClient(ClientCreationDetail clientCreationDetail, MeetingDto meetingDto, List<SavingsDetailDto> allowedSavingProducts) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
OfficeBO userOffice = this.officeDao.findOfficeById(userContext.getBranchId());
userContext.setBranchGlobalNum(userOffice.getGlobalOfficeNum());
try {
ClientBO client = null;
List<AccountFeesEntity> feesForCustomerAccount = convertFeeViewsToAccountFeeEntities(clientCreationDetail.getFeesToApply());
List<SavingsOfferingBO> selectedOfferings = new ArrayList<SavingsOfferingBO>();
for (Short productId : clientCreationDetail.getSelectedSavingProducts()) {
if (productId != null) {
for (SavingsDetailDto savingsOffering : allowedSavingProducts) {
if (productId.equals(savingsOffering.getPrdOfferingId())) {
SavingsOfferingBO savingsProduct = savingsProductDao.findById(productId.intValue());
selectedOfferings.add(savingsProduct);
}
}
}
}
List<ClientInitialSavingsOfferingEntity> offeringsAssociatedInCreate = new ArrayList<ClientInitialSavingsOfferingEntity>();
for (SavingsOfferingBO offering : selectedOfferings) {
offeringsAssociatedInCreate.add(new ClientInitialSavingsOfferingEntity(null, offering));
}
Short personnelId = null;
Short officeId = null;
ClientNameDetailDto spouseNameDetailView = null;
if (ClientRules.isFamilyDetailsRequired()) {
// actionForm.setFamilyDateOfBirth();
// actionForm.constructFamilyDetails();
} else {
spouseNameDetailView = clientCreationDetail.getSpouseFatherName();
}
String secondMiddleName = null;
ClientNameDetailEntity clientNameDetailEntity = new ClientNameDetailEntity(null, secondMiddleName, clientCreationDetail.getClientNameDetailDto());
ClientNameDetailEntity spouseFatherNameDetailEntity = null;
if (spouseNameDetailView != null) {
spouseFatherNameDetailEntity = new ClientNameDetailEntity(null, secondMiddleName, spouseNameDetailView);
}
ClientDetailEntity clientDetailEntity = new ClientDetailEntity();
clientDetailEntity.updateClientDetails(clientCreationDetail.getClientPersonalDetailDto());
DateTime dob = new DateTime(clientCreationDetail.getDateOfBirth());
boolean trainedBool = clientCreationDetail.isTrained();
DateTime trainedDateTime = null;
if (clientCreationDetail.getTrainedDate() != null) {
trainedDateTime = new DateTime(clientCreationDetail.getTrainedDate());
}
String clientFirstName = clientCreationDetail.getClientNameDetailDto().getFirstName();
String clientLastName = clientCreationDetail.getClientNameDetailDto().getLastName();
String secondLastName = clientCreationDetail.getClientNameDetailDto().getSecondLastName();
CustomerStatus clientStatus = CustomerStatus.fromInt(clientCreationDetail.getClientStatus());
PersonnelBO formedBy = this.personnelDao.findPersonnelById(clientCreationDetail.getFormedBy());
Address address = null;
if (clientCreationDetail.getAddress() != null) {
AddressDto dto = clientCreationDetail.getAddress();
address = new Address(dto.getLine1(), dto.getLine2(), dto.getLine3(), dto.getCity(), dto.getState(), dto.getCountry(), dto.getZip(), dto.getPhoneNumber());
}
if (YesNoFlag.YES.getValue().equals(clientCreationDetail.getGroupFlag())) {
Integer parentGroupId = Integer.parseInt(clientCreationDetail.getParentGroupId());
CustomerBO group = this.customerDao.findCustomerById(parentGroupId);
if (group.getPersonnel() != null) {
personnelId = group.getPersonnel().getPersonnelId();
}
officeId = group.getOffice().getOfficeId();
client = ClientBO.createNewInGroupHierarchy(userContext, clientCreationDetail.getClientName(), clientStatus, new DateTime(clientCreationDetail.getMfiJoiningDate()), group, formedBy, clientNameDetailEntity, dob, clientCreationDetail.getGovernmentId(), trainedBool, trainedDateTime, clientCreationDetail.getGroupFlag(), clientFirstName, clientLastName, secondLastName, spouseFatherNameDetailEntity, clientDetailEntity, offeringsAssociatedInCreate, clientCreationDetail.getExternalId(), address, clientCreationDetail.getActivationDate());
if (ClientRules.isFamilyDetailsRequired()) {
client.setFamilyAndNameDetailSets(clientCreationDetail.getFamilyNames(), clientCreationDetail.getFamilyDetails());
}
this.customerService.createClient(client, client.getCustomerMeetingValue(), feesForCustomerAccount, selectedOfferings);
} else {
personnelId = clientCreationDetail.getLoanOfficerId();
officeId = clientCreationDetail.getOfficeId();
PersonnelBO loanOfficer = this.personnelDao.findPersonnelById(personnelId);
OfficeBO office = this.officeDao.findOfficeById(officeId);
int lastSearchIdCustomerValue = customerDao.retrieveLastSearchIdValueForNonParentCustomersInOffice(officeId);
MeetingBO clientMeeting = null;
if (meetingDto != null) {
clientMeeting = new MeetingFactory().create(meetingDto);
clientMeeting.setUserContext(userContext);
}
client = ClientBO.createNewOutOfGroupHierarchy(userContext, clientCreationDetail.getClientName(), clientStatus, new DateTime(clientCreationDetail.getMfiJoiningDate()), office, loanOfficer, clientMeeting, formedBy, clientNameDetailEntity, dob, clientCreationDetail.getGovernmentId(), trainedBool, trainedDateTime, clientCreationDetail.getGroupFlag(), clientFirstName, clientLastName, secondLastName, spouseFatherNameDetailEntity, clientDetailEntity, offeringsAssociatedInCreate, clientCreationDetail.getExternalId(), address, lastSearchIdCustomerValue);
if (ClientRules.isFamilyDetailsRequired()) {
client.setFamilyAndNameDetailSets(clientCreationDetail.getFamilyNames(), clientCreationDetail.getFamilyDetails());
}
try {
personnelDao.checkAccessPermission(userContext, client.getOfficeId(), client.getLoanOfficerId());
} catch (AccountException e) {
throw new MifosRuntimeException("Access denied!", e);
}
this.customerService.createClient(client, clientMeeting, feesForCustomerAccount, selectedOfferings);
}
clientPhotoService.create(client.getCustomerId().longValue(), clientCreationDetail.getPicture());
return new CustomerDetailsDto(client.getCustomerId(), client.getGlobalCustNum());
} catch (CustomerException e) {
throw new BusinessRuleException(e.getKey(), e.getValues(), e);
}
}
Aggregations