diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java index d6d1badee..e192e8346 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java @@ -303,6 +303,10 @@ public class DepositAccount extends AbstractAuditableCustom { return date; } + public List getDepositaccountTransactions() { + return depositaccountTransactions; + } + public BigDecimal getProjectedTotalOnMaturity() { return projectedTotalOnMaturity; } @@ -547,4 +551,14 @@ public class DepositAccount extends AbstractAuditableCustom { this.interestAccrued = accuredtotalAmount.minus(deposit).getAmount(); } + + public void withdrawInterest(Money interest) { + + if(this.depositStatus == 300 ){ + DepositAccountTransaction depositAccountTransaction = DepositAccountTransaction.withdraw(null, new LocalDate(), interest); + depositAccountTransaction.updateAccount(this); + this.depositaccountTransactions.add(depositAccountTransaction); + } + + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountTransaction.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountTransaction.java index df67a48dc..698822fdb 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountTransaction.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccountTransaction.java @@ -65,7 +65,7 @@ public class DepositAccountTransaction extends AbstractPersistable { this.amount = amount; this.dateOf = date.toDateMidnight().toDate(); this.interest = interest; - this.total = amount.add(interest); + this.total = amount.equals(0)?new BigDecimal(0):amount.add(interest); } public Date getDateOf() { @@ -93,11 +93,11 @@ public class DepositAccountTransaction extends AbstractPersistable { } public static DepositAccountTransaction deposit(Money amount, LocalDate paymentDate, Money interest) { - return new DepositAccountTransaction(DepositAccountTransactionType.DEPOSIT, amount.getAmount(), paymentDate, interest.getAmount()); + return new DepositAccountTransaction(DepositAccountTransactionType.DEPOSIT, amount == null ? new BigDecimal(0) : amount.getAmount(), paymentDate, interest==null ? new BigDecimal(0) : interest.getAmount()); } public static DepositAccountTransaction withdraw(Money amount, LocalDate paymentDate, Money interest) { - return new DepositAccountTransaction(DepositAccountTransactionType.WITHDRAW, amount.getAmount(), paymentDate, interest.getAmount()); + return new DepositAccountTransaction(DepositAccountTransactionType.WITHDRAW, amount == null ? new BigDecimal(0) : amount.getAmount(), paymentDate, interest==null ? new BigDecimal(0) : interest.getAmount()); } public void updateAccount(DepositAccount depositAccount) { 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 3f61fd04d..2c03bc908 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 @@ -1,7 +1,9 @@ package org.mifosng.platform.saving.service; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.apache.commons.lang.StringUtils; @@ -16,6 +18,7 @@ import org.mifosng.platform.api.commands.UndoStateTransitionCommand; import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.client.domain.Note; 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.NoAuthorizationException; @@ -24,6 +27,8 @@ import org.mifosng.platform.exceptions.ProductNotFoundException; import org.mifosng.platform.saving.domain.DepositAccount; import org.mifosng.platform.saving.domain.DepositAccountRepository; import org.mifosng.platform.saving.domain.DepositAccountStatus; +import org.mifosng.platform.saving.domain.DepositAccountTransaction; +import org.mifosng.platform.saving.domain.DepositAccountTransactionType; import org.mifosng.platform.saving.domain.DepositLifecycleStateMachine; import org.mifosng.platform.saving.domain.DepositLifecycleStateMachineImpl; import org.mifosng.platform.saving.domain.FixedTermDepositInterestCalculator; @@ -332,21 +337,20 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo throw new DepositAccountNotFoundException(command.getAccountId()); } - BigDecimal actualInterestAccured = account.getAccuredInterest().getAmount(); - Integer tenure = account.getTenureInMonths(); - BigDecimal interstGettingForPeriod = actualInterestAccured.divide(new BigDecimal(tenure)); - - - Integer interestCompoundEvery = account.getInterestCompoundedEvery(); - LocalDate lastInterestTakenDate = account.getActualCommencementDate(); - LocalDate todaysDate = new LocalDate(); - Integer noOfMonthsforInterestCal = Months.monthsBetween(lastInterestTakenDate, todaysDate).getMonths(); - Integer noOfPeriods = noOfMonthsforInterestCal / interestCompoundEvery; + BigDecimal interstGettingForPeriod = BigDecimal.valueOf(account.getAccuredInterest().getAmount().doubleValue()/new Double(account.getTenureInMonths())); + LocalDate lastInterestTakenDate = getLastTxnDate(account); + Integer noOfMonthsforInterestCal = Months.monthsBetween(lastInterestTakenDate, new LocalDate()).getMonths(); + Integer noOfPeriods = noOfMonthsforInterestCal / account.getInterestCompoundedEvery(); BigDecimal availableInterestAmountForWithDrawal = interstGettingForPeriod.multiply(new BigDecimal(noOfPeriods)); + BigDecimal cmdInterest = command.getWithdrawInterest(); Integer iswithdrawable = availableInterestAmountForWithDrawal.compareTo(command.getWithdrawInterest()); + if(noOfPeriods > 0 ){ - + if(iswithdrawable >= 0){ + account.withdrawInterest(Money.of(account.getDeposit().getCurrency(), cmdInterest)); + this.depositAccountRepository.save(account); + } }else if(noOfPeriods <= 0){ if(iswithdrawable==-1){ throw new RuntimeException(); @@ -354,4 +358,21 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo } return new EntityIdentifier(account.getId()); } + + private LocalDate getLastTxnDate(DepositAccount account) { + List lastTransactionDates = new ArrayList(); + LocalDate lastTransactionDate=null; + List depositAccountTransactions=account.getDepositaccountTransactions(); + for(DepositAccountTransaction depositAccountTransaction : depositAccountTransactions){ + if(depositAccountTransaction.getTypeOf().equals(DepositAccountTransactionType.WITHDRAW)){ + lastTransactionDate = depositAccountTransaction.getTransactionDate(); + lastTransactionDates.add(lastTransactionDate); + } + } + if(lastTransactionDates.size()>0) + lastTransactionDate = Collections.max(lastTransactionDates); + else + lastTransactionDate = account.getActualCommencementDate(); + return lastTransactionDate; + } } \ No newline at end of file