use of org.mifos.dto.domain.SavingsAdjustmentDto 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.dto.domain.SavingsAdjustmentDto in project head by mifos.
the class SavingsAccountAdjustmentAndInterestCalculationServiceFacadeIntegrationTest method shouldHaveCorrectBalanceAfterDepositAndAdjustment.
@Test
public void shouldHaveCorrectBalanceAfterDepositAndAdjustment() throws Exception {
createCenterGroupClientHierarchy(aWeeklyMeeting);
SavingsOfferingBO savingsProduct = new SavingsProductBuilder().mandatory().withMandatoryAmount("33.0").appliesToClientsOnly().buildForIntegrationTests();
SavingsBO savingsAccount = new SavingsAccountBuilder().active().withActivationDate(mondayTwoWeeksAgo()).withSavingsProduct(savingsProduct).withCustomer(client).withCreatedBy(IntegrationTestObjectMother.testUser()).withBalanceOf(TestUtils.createMoney("0")).withDepositOn("20", new DateTime()).build();
IntegrationTestObjectMother.saveSavingsProductAndAssociatedSavingsAccounts(savingsProduct, savingsAccount);
Long savingsId = Long.valueOf(savingsAccount.getAccountId());
Double adjustedAmount = Double.valueOf("35");
String note = "I entered 20 but it should of being 35 which is an overpayment of the mandatory sum.";
SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, adjustedAmount, note, savingsAccount.getLastPmnt().getPaymentId(), new LocalDate(savingsAccount.getLastPmnt().getPaymentDate()));
// exercise test
this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
// verification
savingsAccount = IntegrationTestObjectMother.findSavingsAccountById(savingsId);
assertThat(savingsAccount.getSavingsBalance(), is(TestUtils.createMoney("35")));
}
use of org.mifos.dto.domain.SavingsAdjustmentDto in project head by mifos.
the class WebTierAccountServiceFacade method applyHistoricalAdjustment.
@Override
public void applyHistoricalAdjustment(String globalAccountNum, Integer paymentId, String adjustmentNote, Short personnelId, AdjustedPaymentDto adjustedPaymentDto) {
try {
AccountBO accountBO = accountBusinessService.findBySystemId(globalAccountNum);
accountBO.setUserContext(getUserContext());
checkPermissionForAdjustment(accountBO);
PersonnelBO personnelBO = personnelPersistence.findPersonnelById(personnelId);
AccountPaymentEntity accountPaymentEntity = accountBO.findPaymentById(paymentId);
if (accountPaymentEntity == null) {
throw new AccountException(AccountExceptionConstants.CANNOTADJUST);
}
monthClosingServiceFacade.validateTransactionDate(accountPaymentEntity.getPaymentDate());
PaymentDto otherTransferPayment = accountPaymentEntity.getOtherTransferPaymentDto();
//flush to avoid proxy casting problems
transactionHelper.flushAndClearSession();
transactionHelper.startTransaction();
Integer newSavingsPaymentId = null;
if (otherTransferPayment != null) {
SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(otherTransferPayment.getAccountId().longValue(), (adjustedPaymentDto == null) ? 0 : Double.valueOf(adjustedPaymentDto.getAmount()), adjustmentNote, otherTransferPayment.getPaymentId(), (adjustedPaymentDto == null) ? otherTransferPayment.getPaymentDate() : new LocalDate(adjustedPaymentDto.getPaymentDate()));
PaymentDto newSavingsPayment = this.savingsServiceFacade.adjustTransaction(savingsAdjustment, true);
newSavingsPaymentId = (newSavingsPayment == null) ? null : newSavingsPayment.getPaymentId();
}
//reload after flush & clear
accountBO = accountBusinessService.findBySystemId(globalAccountNum);
accountBO.setUserContext(getUserContext());
AccountPaymentEntity adjustedPayment = null;
Integer adjustedId;
Stack<PaymentData> paymentsToBeReapplied = new Stack<PaymentData>();
Map<Integer, Stack<PaymentData>> memberPaymentsToBeReappliedMap = new HashMap<Integer, Stack<PaymentData>>();
if (accountBO.isGroupLoanAccount()) {
for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
Stack<PaymentData> memberPaymentsToBeReapplied = new Stack<PaymentData>();
memberPaymentsToBeReappliedMap.put(memberAccount.getAccountId(), memberPaymentsToBeReapplied);
}
}
do {
adjustedPayment = accountBO.getLastPmntToBeAdjusted();
if (adjustedPayment == null) {
break;
}
adjustedId = adjustedPayment.getPaymentId();
if (!accountPaymentEntity.getPaymentId().equals(adjustedId)) {
PersonnelBO paymentCreator = (adjustedPayment.getCreatedByUser() == null) ? personnelBO : adjustedPayment.getCreatedByUser();
PaymentData paymentData = accountBO.createPaymentData(adjustedPayment.getAmount(), adjustedPayment.getPaymentDate(), adjustedPayment.getReceiptNumber(), adjustedPayment.getReceiptDate(), adjustedPayment.getPaymentType().getId(), paymentCreator);
paymentData.setOtherTransferPayment(adjustedPayment.getOtherTransferPayment());
paymentsToBeReapplied.push(paymentData);
// handling new Group Loan Members payments
for (AccountPaymentEntity memberAdjustedPayment : adjustedPayment.getMemberPayments()) {
PaymentData memberPaymentData = memberAdjustedPayment.getAccount().createPaymentData(memberAdjustedPayment.getAmount(), adjustedPayment.getPaymentDate(), adjustedPayment.getReceiptNumber(), adjustedPayment.getReceiptDate(), adjustedPayment.getPaymentType().getId(), paymentCreator);
memberPaymentsToBeReappliedMap.get(memberAdjustedPayment.getAccount().getAccountId()).push(memberPaymentData);
}
}
transactionHelper.flushAndClearSession();
//reload after flush & clear
accountBO = accountBusinessService.findBySystemId(globalAccountNum);
accountBO.setUserContext(getUserContext());
accountBO.adjustLastPayment(adjustmentNote, personnelBO);
legacyAccountDao.createOrUpdate(accountBO);
//adjust New Group Loan member payments
if (accountBO.isGroupLoanAccount()) {
for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
AccountPaymentEntity memberPayment = memberAccount.getLastPmntToBeAdjusted();
if (memberPayment.getParentPaymentId() == null) {
continue;
}
memberAccount.setUserContext(getUserContext());
memberAccount.adjustLastPayment(adjustmentNote, personnelBO);
legacyAccountDao.createOrUpdate(memberAccount);
}
}
transactionHelper.flushSession();
} while (!accountPaymentEntity.getPaymentId().equals(adjustedId));
if (adjustedPaymentDto != null) {
//reapply adjusted payment
PersonnelBO paymentCreator = (accountPaymentEntity.getCreatedByUser() == null) ? personnelBO : accountPaymentEntity.getCreatedByUser();
Money amount = new Money(accountBO.getCurrency(), adjustedPaymentDto.getAmount());
PaymentData paymentData = accountBO.createPaymentData(amount, adjustedPaymentDto.getPaymentDate(), accountPaymentEntity.getReceiptNumber(), accountPaymentEntity.getReceiptDate(), adjustedPaymentDto.getPaymentType(), paymentCreator);
paymentData.setAdjustment(true);
//new adjusted savings payment must be tied to this payment
if (newSavingsPaymentId != null) {
AccountPaymentEntity newSvngPayment = legacyAccountDao.findPaymentById(newSavingsPaymentId);
paymentData.setOtherTransferPayment(newSvngPayment);
}
accountBO.applyPayment(paymentData);
legacyAccountDao.createOrUpdate(accountBO);
transactionHelper.flushSession();
// handling new Group Loan Members payments
if (accountBO.isGroupLoanAccount()) {
for (AdjustedPaymentDto adjustedMemberPayment : adjustedPaymentDto.getMemberPayments()) {
AccountBO memberAccount = ((LoanBO) accountBO).findMemberById(adjustedMemberPayment.getAccountId());
BigDecimal adjustedMemberPaymentAmount = BigDecimal.ZERO;
if (!StringUtils.isBlank(adjustedMemberPayment.getAmount())) {
adjustedMemberPaymentAmount = new BigDecimal(adjustedMemberPayment.getAmount());
}
Money memberAmount = new Money(memberAccount.getCurrency(), adjustedMemberPaymentAmount.toString());
PaymentData memberPaymentData = memberAccount.createPaymentData(memberAmount, adjustedPaymentDto.getPaymentDate(), accountPaymentEntity.getReceiptNumber(), accountPaymentEntity.getReceiptDate(), adjustedPaymentDto.getPaymentType(), paymentCreator);
memberPaymentData.setParentPayment(accountBO.getLastPmnt());
memberAccount.applyPayment(memberPaymentData);
legacyAccountDao.createOrUpdate(memberAccount);
}
}
}
while (!paymentsToBeReapplied.isEmpty()) {
PaymentData paymentData = paymentsToBeReapplied.pop();
//avoid lazy loading exception
if (paymentData.getOtherTransferPayment() != null) {
legacyAccountDao.updatePayment(paymentData.getOtherTransferPayment());
}
accountBO.applyPayment(paymentData);
legacyAccountDao.createOrUpdate(accountBO);
transactionHelper.flushSession();
if (accountBO.isGroupLoanAccount()) {
for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
PaymentData memberPaymentData = memberPaymentsToBeReappliedMap.get(memberAccount.getAccountId()).pop();
memberPaymentData.setParentPayment(accountBO.getLastPmnt());
memberAccount.applyPayment(memberPaymentData);
legacyAccountDao.createOrUpdate(memberAccount);
}
}
}
transactionHelper.commitTransaction();
} catch (ServiceException e) {
transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} catch (AccountException e) {
transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} catch (PersistenceException e) {
transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} catch (RuntimeException e) {
transactionHelper.rollbackTransaction();
throw new MifosRuntimeException(e);
} finally {
transactionHelper.closeSession();
}
}
use of org.mifos.dto.domain.SavingsAdjustmentDto in project head by mifos.
the class SavingsAccountRESTController method applyAdjustment.
@RequestMapping(value = "account/savings/num-{globalAccountNum}/adjustment", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> applyAdjustment(@PathVariable String globalAccountNum, @RequestParam BigDecimal amount, @RequestParam String note, @RequestParam(required = false) Integer paymentId) throws Exception {
validateAmount(amount);
validateNote(note);
SavingsBO savingsBO = savingsDao.findBySystemId(globalAccountNum);
new SavingsPersistence().initialize(savingsBO);
Integer accountId = savingsBO.getAccountId();
Long savingsId = Long.valueOf(accountId.toString());
SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, amount.doubleValue(), note, (paymentId == null) ? savingsBO.getLastPmnt().getPaymentId() : paymentId, new LocalDate(savingsBO.getLastPmnt().getPaymentDate()));
Money balanceBeforePayment = savingsBO.getSavingsBalance();
this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
DateTime today = new DateTime();
CustomerBO client = savingsBO.getCustomer();
Map<String, String> map = new HashMap<String, String>();
map.put("status", "success");
map.put("clientName", client.getDisplayName());
map.put("clientNumber", client.getGlobalCustNum());
map.put("savingsDisplayName", savingsBO.getSavingsOffering().getPrdOfferingName());
map.put("adjustmentDate", today.toLocalDate().toString());
map.put("adjustmentTime", today.toLocalTime().toString());
map.put("adjustmentAmount", savingsBO.getLastPmnt().getAmount().toString());
map.put("adjustmentMadeBy", personnelDao.findPersonnelById((short) user.getUserId()).getDisplayName());
map.put("balanceBeforeAdjustment", balanceBeforePayment.toString());
map.put("balanceAfterAdjustment", savingsBO.getSavingsBalance().toString());
map.put("note", note);
return map;
}
Aggregations