use of org.mifos.customers.persistence.CustomerPersistence in project head by mifos.
the class SavingsApplyAdjustmentAction method adjustLastUserAction.
@TransactionDemarcate(validateAndResetToken = true)
public ActionForward adjustLastUserAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
request.setAttribute("method", "adjustLastUserAction");
UserContext uc = (UserContext) SessionUtils.getAttribute(Constants.USER_CONTEXT_KEY, request.getSession());
SavingsBO savings = (SavingsBO) SessionUtils.getAttribute(Constants.BUSINESS_KEY, request);
Integer accountId = savings.getAccountId();
Integer versionNum = savings.getVersionNo();
savings = savingsDao.findById(accountId);
// NOTE: initialise so when error occurs when apply adjustment, savings object is correctly initialised for
// use within Tag library in jsp.
new SavingsPersistence().initialize(savings);
checkVersionMismatch(versionNum, savings.getVersionNo());
savings.setUserContext(uc);
SessionUtils.setAttribute(Constants.BUSINESS_KEY, savings, request);
if (savings.getPersonnel() != null) {
getBizService().checkPermissionForAdjustment(AccountTypes.SAVINGS_ACCOUNT, null, uc, savings.getOffice().getOfficeId(), savings.getPersonnel().getPersonnelId());
} else {
getBizService().checkPermissionForAdjustment(AccountTypes.SAVINGS_ACCOUNT, null, uc, savings.getOffice().getOfficeId(), uc.getId());
}
SavingsApplyAdjustmentActionForm actionForm = (SavingsApplyAdjustmentActionForm) form;
if (actionForm.getLastPaymentAmount() == null) {
throw new MifosRuntimeException("Null payment amount is not allowed");
}
// date validation
Date meetingDate = new CustomerPersistence().getLastMeetingDateForCustomer(savings.getCustomer().getCustomerId());
boolean repaymentIndependentOfMeetingEnabled = new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled();
if (!savings.isTrxnDateValid(actionForm.getTrxnDateLocal().toDateMidnight().toDate(), meetingDate, repaymentIndependentOfMeetingEnabled)) {
throw new AccountException(AccountConstants.ERROR_INVALID_TRXN);
}
Long savingsId = Long.valueOf(accountId.toString());
Double adjustedAmount = Double.valueOf(actionForm.getLastPaymentAmount());
String note = actionForm.getNote();
AccountPaymentEntity adjustedPayment = savings.findPaymentById(actionForm.getPaymentId());
AccountPaymentEntity otherTransferPayment = adjustedPayment.getOtherTransferPayment();
try {
if (otherTransferPayment == null || otherTransferPayment.isSavingsDepositOrWithdrawal()) {
// regular savings payment adjustment or savings-savings transfer adjustment
SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, adjustedAmount, note, actionForm.getPaymentId(), actionForm.getTrxnDateLocal());
this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
} else {
// adjust repayment from savings account
AccountBO account = adjustedPayment.getOtherTransferPayment().getAccount();
AdjustedPaymentDto adjustedPaymentDto = new AdjustedPaymentDto(actionForm.getLastPaymentAmount(), actionForm.getTrxnDateLocal().toDateMidnight().toDate(), otherTransferPayment.getPaymentType().getId());
this.accountServiceFacade.applyHistoricalAdjustment(account.getGlobalAccountNum(), otherTransferPayment.getPaymentId(), note, uc.getId(), adjustedPaymentDto);
}
} catch (BusinessRuleException e) {
throw new AccountException(e.getMessageKey(), e);
} finally {
doCleanUp(request);
}
return mapping.findForward("account_detail_page");
}
use of org.mifos.customers.persistence.CustomerPersistence in project head by mifos.
the class PortfolioAtRiskCalculation method generatePortfolioAtRiskForTask.
// This method is used by the PortfolioAtRiskHelper to calculate the PAR for
// each group when the PAR task is running
// and not intended to be used anywhere else. This method calculated the PAR
// assuming that loans are in arrears
// are already put in the state active in bad standing (the query will
// calculated for loans in active in bad standing only
public static double generatePortfolioAtRiskForTask(Integer groupId, Short branchId, String searchId) throws Exception {
double portfolioAtRisk = -1;
CustomerPersistence customerPersistence = new CustomerPersistence();
Money atRiskLoanAmount = customerPersistence.getTotalAmountForGroup(groupId, AccountState.LOAN_ACTIVE_IN_BAD_STANDING);
Money goupLoanAmountForActiveLoans = customerPersistence.getTotalAmountForGroup(groupId, AccountState.LOAN_ACTIVE_IN_GOOD_STANDING);
Money outstandingLoanAmount = atRiskLoanAmount.add(goupLoanAmountForActiveLoans);
Money clientAtRiskLoanAmount = customerPersistence.getTotalAmountForAllClientsOfGroup(branchId, AccountState.LOAN_ACTIVE_IN_BAD_STANDING, searchId);
Money clientLoanAmountForActiveLoans = customerPersistence.getTotalAmountForAllClientsOfGroup(branchId, AccountState.LOAN_ACTIVE_IN_GOOD_STANDING, searchId);
Money clientOutstandingLoanAmount = clientAtRiskLoanAmount.add(clientLoanAmountForActiveLoans);
outstandingLoanAmount = outstandingLoanAmount.add(clientOutstandingLoanAmount);
atRiskLoanAmount = atRiskLoanAmount.add(clientAtRiskLoanAmount);
if (!outstandingLoanAmount.equals(new Money(outstandingLoanAmount.getCurrency()))) {
portfolioAtRisk = atRiskLoanAmount.divide(outstandingLoanAmount).doubleValue();
} else if (atRiskLoanAmount.equals(new Money(atRiskLoanAmount.getCurrency()))) {
portfolioAtRisk = 0.0;
}
return portfolioAtRisk;
}
use of org.mifos.customers.persistence.CustomerPersistence in project head by mifos.
the class CustomerTagGenerator method buildLink.
private void buildLink(StringBuilder strBuilder, CustomerBO customer, CustomerBO originalCustomer, boolean selfLinkRequired, Object randomNum) {
if (customer == null) {
return;
}
try {
CustomerBO customerReloaded = new CustomerPersistence().getCustomer(customer.getCustomerId());
buildLink(strBuilder, customerReloaded.getParentCustomer(), originalCustomer, selfLinkRequired, randomNum);
strBuilder.append(" / ");
createCustomerLink(strBuilder, customer, originalCustomer, selfLinkRequired, randomNum);
} catch (PersistenceException e) {
throw new RuntimeException(e);
}
}
use of org.mifos.customers.persistence.CustomerPersistence in project head by mifos.
the class SavingsBOIntegrationTest method testIsTrxnDateValid_AfterFirstMeeting.
@Test
public void testIsTrxnDateValid_AfterFirstMeeting() throws Exception {
createInitialObjects();
savingsOffering = TestObjectFactory.createSavingsProduct("dfasdasd1", "sad1", RecommendedAmountUnit.COMPLETE_GROUP);
savings = helper.createSavingsAccount(savingsOffering, group, AccountState.SAVINGS_ACTIVE, userContext);
savings = TestObjectFactory.getObject(SavingsBO.class, savings.getAccountId());
int i = -5;
for (AccountActionDateEntity actionDate : savings.getAccountActionDates()) {
((SavingsScheduleEntity) actionDate).setActionDate(offSetCurrentDate(i--));
}
savings.update();
savings = TestObjectFactory.getObject(SavingsBO.class, savings.getAccountId());
java.util.Date trxnDate = offSetCurrentDate(-5);
Date meetingDate = new CustomerPersistence().getLastMeetingDateForCustomer(savings.getCustomer().getCustomerId());
boolean repaymentIndependentOfMeetingEnabled = new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled();
if (AccountingRules.isBackDatedTxnAllowed()) {
Assert.assertTrue(savings.isTrxnDateValid(trxnDate, meetingDate, repaymentIndependentOfMeetingEnabled));
} else {
Assert.assertFalse(savings.isTrxnDateValid(trxnDate, meetingDate, repaymentIndependentOfMeetingEnabled));
}
group = TestObjectFactory.getGroup(group.getCustomerId());
}
use of org.mifos.customers.persistence.CustomerPersistence in project head by mifos.
the class SavingsServiceFacadeWebTier method closeSavingsAccount.
@Override
public void closeSavingsAccount(Long savingsId, String notes, SavingsWithdrawalDto closeAccountDto) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
SavingsBO savingsAccount = this.savingsDao.findById(savingsId);
savingsAccount.updateDetails(userContext);
PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
LocalDate closureDate = closeAccountDto.getDateOfWithdrawal();
// Assumption that all previous interest postings occurred correctly
InterestScheduledEvent postingSchedule = savingsInterestScheduledEventFactory.createScheduledEventFrom(savingsAccount.getInterestPostingMeeting());
LocalDate nextPostingDate = new LocalDate(savingsAccount.getNextIntPostDate());
LocalDate startOfPostingPeriod = postingSchedule.findFirstDateOfPeriodForMatchingDate(nextPostingDate);
CalendarPeriod postingPeriodAtClosure;
if (startOfPostingPeriod.isAfter(closureDate)) {
postingPeriodAtClosure = new CalendarPeriod(closureDate, closureDate);
} else {
postingPeriodAtClosure = new CalendarPeriod(startOfPostingPeriod, closureDate);
}
List<EndOfDayDetail> allEndOfDayDetailsForAccount = savingsDao.retrieveAllEndOfDayDetailsFor(savingsAccount.getCurrency(), Long.valueOf(savingsId));
InterestPostingPeriodResult postingPeriodAtClosureResult = determinePostingPeriodResult(postingPeriodAtClosure, savingsAccount, allEndOfDayDetailsForAccount);
savingsAccount.postInterest(postingSchedule, postingPeriodAtClosureResult, createdBy);
AccountNotesEntity notesEntity = new AccountNotesEntity(new DateTimeService().getCurrentJavaSqlDate(), notes, createdBy, savingsAccount);
try {
CustomerBO customer = savingsAccount.getCustomer();
if (closeAccountDto.getCustomerId() != null) {
List<CustomerBO> clientList = new CustomerPersistence().getActiveAndOnHoldChildren(savingsAccount.getCustomer().getSearchId(), savingsAccount.getCustomer().getOfficeId(), CustomerLevel.CLIENT);
for (CustomerBO client : clientList) {
if (closeAccountDto.getCustomerId().intValue() == client.getCustomerId().intValue()) {
customer = client;
break;
}
}
}
Money amount = new Money(savingsAccount.getCurrency(), closeAccountDto.getAmount().toString());
PaymentTypeEntity paymentType = new PaymentTypeEntity(closeAccountDto.getModeOfPayment().shortValue());
Date receiptDate = null;
if (closeAccountDto.getDateOfReceipt() != null) {
receiptDate = closeAccountDto.getDateOfReceipt().toDateMidnight().toDate();
}
AccountPaymentEntity closeAccount = new AccountPaymentEntity(savingsAccount, amount, closeAccountDto.getReceiptId(), receiptDate, paymentType, closeAccountDto.getDateOfWithdrawal().toDateMidnight().toDate());
this.transactionHelper.startTransaction();
this.transactionHelper.beginAuditLoggingFor(savingsAccount);
savingsAccount.closeAccount(closeAccount, notesEntity, customer, createdBy);
this.savingsDao.save(savingsAccount);
this.transactionHelper.commitTransaction();
} catch (BusinessRuleException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getMessageKey(), e);
} catch (PersistenceException e) {
this.transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} finally {
this.transactionHelper.closeSession();
}
}
Aggregations