From 11e7f64cad38a5482ef1bff66b28843f391980e8 Mon Sep 17 00:00:00 2001 From: Madhukar Date: Tue, 25 Sep 2012 20:56:44 +0530 Subject: [PATCH] getAvailableInterest for withdrawal --- .../api/DepositAccountsApiResource.java | 6 ++--- .../platform/api/data/DepositAccountData.java | 23 ++++++++++++++++++- .../DepositAccountTransactionsException.java | 10 ++++++++ ...WritePlatformServiceJpaRepositoryImpl.java | 7 +++--- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountTransactionsException.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java index 942973320..ec49ed2c2 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java @@ -72,7 +72,7 @@ public class DepositAccountsApiResource { "interestCompoundedEvery","interestCompoundingAllowed", "interestCompoundedEveryPeriodType", "renewalAllowed", "preClosureAllowed", "preClosureInterestRate", - "withdrawnonDate","rejectedonDate","closedonDate","transactions","interestPaid","isInterestWithdrawable")); + "withdrawnonDate","rejectedonDate","closedonDate","transactions","interestPaid","isInterestWithdrawable","availableInterestForWithdrawal")); @POST @@ -112,7 +112,6 @@ public class DepositAccountsApiResource { Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); DepositPermissionData permissions = null; - BigDecimal availableInterestForWithdrawal = null; if (responseParameters.isEmpty()) { responseParameters.addAll(typicalResponseParameters); @@ -129,12 +128,11 @@ public class DepositAccountsApiResource { Set associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters()); if (!associationParameters.isEmpty()) { if (associationParameters.contains("all")) { - responseParameters.addAll(Arrays.asList("permissions","availableInterestForWithdrawal")); + responseParameters.addAll(Arrays.asList("permissions")); } else { responseParameters.addAll(associationParameters); } permissions = this.depositAccountReadPlatformService.retrieveDepositAccountsPermissions(account); - availableInterestForWithdrawal = this.depositAccountReadPlatformService.retrieveAvailableInterestForWithdrawal(account); account = new DepositAccountData(account, permissions, account.getTransactions()); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java index 8e812bc94..be7bbac26 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java @@ -6,6 +6,7 @@ import java.util.Collection; import java.util.List; import org.joda.time.LocalDate; +import org.joda.time.Months; /** * Immutable data object for deposit accounts. @@ -51,6 +52,7 @@ public class DepositAccountData { private final Collection transactions; private final DepositPermissionData permissions; + private final BigDecimal availableInterestForWithdrawal; /* * used when returning account template data but only a clientId is passed, no selected product. @@ -99,6 +101,7 @@ public class DepositAccountData { this.interestPaid=null; this.isInterestWithdrawable=false; this.interestCompoundingAllowed = true; + this.availableInterestForWithdrawal = new BigDecimal(0); } public DepositAccountData( @@ -141,6 +144,7 @@ public class DepositAccountData { this.interestPaid=account.getInterestPaid(); this.isInterestWithdrawable=account.isInterestWithdrawable(); this.interestCompoundingAllowed=account.isInterestCompoundingAllowed(); + this.availableInterestForWithdrawal=new BigDecimal(0); } public DepositAccountData(final DepositAccountData account, final DepositPermissionData permissions, final Collection transactions) { @@ -180,6 +184,7 @@ public class DepositAccountData { this.interestPaid=account.getInterestPaid(); this.isInterestWithdrawable=account.isInterestWithdrawable(); this.interestCompoundingAllowed = account.isInterestCompoundingAllowed(); + this.availableInterestForWithdrawal=getAvailableInterestForWithdrawal(account);; } public DepositAccountData( @@ -248,6 +253,7 @@ public class DepositAccountData { this.interestPaid =interestPaid; this.isInterestWithdrawable=isInterestWithdrawable; this.interestCompoundingAllowed = interestCompoundingAllowed; + this.availableInterestForWithdrawal = new BigDecimal(0); } public DepositAccountData( @@ -300,6 +306,7 @@ public class DepositAccountData { this.interestPaid=null; this.isInterestWithdrawable=false; this.interestCompoundingAllowed=interestCompoundingAllowed; + this.availableInterestForWithdrawal= new BigDecimal(0); } public Long getId() { @@ -433,5 +440,19 @@ public class DepositAccountData { public boolean isInterestCompoundingAllowed() { return interestCompoundingAllowed; } - + + public BigDecimal getAvailableInterestForWithdrawal() { + return availableInterestForWithdrawal; + } + public BigDecimal getAvailableInterestForWithdrawal(DepositAccountData account) { + if(account.getStatus().getId()==300){ + BigDecimal interstGettingForPeriod = BigDecimal.valueOf(account.getActualInterestAccrued().doubleValue()/new Double(account.getTenureInMonths())); + Integer noOfMonthsforInterestCal = Months.monthsBetween(account.getActualCommencementDate(), new LocalDate()).getMonths(); + Integer noOfPeriods = noOfMonthsforInterestCal / account.getInterestCompoundedEvery(); + return BigDecimal.valueOf(interstGettingForPeriod.multiply(new BigDecimal(noOfPeriods)).doubleValue()-account.getInterestPaid().doubleValue()); + } + else{ + return new BigDecimal(0); + } + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountTransactionsException.java b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountTransactionsException.java new file mode 100644 index 000000000..dc52c1d1a --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/DepositAccountTransactionsException.java @@ -0,0 +1,10 @@ +package org.mifosng.platform.exceptions; + + +public class DepositAccountTransactionsException extends AbstractPlatformDomainRuleException { + + public DepositAccountTransactionsException(String remainInterestForWithdrawal) { + super("deposit.transaction.interest.withdrawal.exception", "You can Withdraw "+remainInterestForWithdrawal+" only", remainInterestForWithdrawal); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java index 546f86d09..9207db5fa 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java @@ -21,6 +21,7 @@ import org.mifosng.platform.client.domain.NoteRepository; import org.mifosng.platform.currency.domain.Money; import org.mifosng.platform.exceptions.DepositAccountNotFoundException; import org.mifosng.platform.exceptions.DepositAccountReopenException; +import org.mifosng.platform.exceptions.DepositAccountTransactionsException; import org.mifosng.platform.exceptions.NoAuthorizationException; import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosng.platform.exceptions.ProductNotFoundException; @@ -349,11 +350,11 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo BigDecimal remainInterestForWithdrawal = totalAvailableInterestForWithdrawal.subtract(interestPaid); if(remainInterestForWithdrawal.doubleValue() > 0){ - if(remainInterestForWithdrawal.doubleValue() > command.getWithdrawInterest().doubleValue()){ + if(remainInterestForWithdrawal.doubleValue() >= command.getWithdrawInterest().doubleValue()&&command.getWithdrawInterest().doubleValue()>0){ account.withdrawInterest(Money.of(account.getDeposit().getCurrency(), command.getWithdrawInterest())); this.depositAccountRepository.save(account); }else { - throw new RuntimeException("You can Withdraw "+remainInterestForWithdrawal+" only, \n please enter a valid amount for withdrawal"); + throw new DepositAccountTransactionsException("You can Withdraw "+remainInterestForWithdrawal+" only"); } } @@ -369,7 +370,7 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo throw new RuntimeException(); }*/ }else{ - throw new RuntimeException("You can not withdraw interst for this account"); + throw new DepositAccountTransactionsException("You can not withdraw interst for this account"); } return new EntityIdentifier(account.getId()); }