interestpayouts patch2

This commit is contained in:
Madhukar
2012-09-21 20:53:47 +05:30
parent 691e836c68
commit 4ac094fc78
3 changed files with 49 additions and 14 deletions
@@ -303,6 +303,10 @@ public class DepositAccount extends AbstractAuditableCustom<AppUser, Long> {
return date;
}
public List<DepositAccountTransaction> getDepositaccountTransactions() {
return depositaccountTransactions;
}
public BigDecimal getProjectedTotalOnMaturity() {
return projectedTotalOnMaturity;
}
@@ -547,4 +551,14 @@ public class DepositAccount extends AbstractAuditableCustom<AppUser, Long> {
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);
}
}
}
@@ -65,7 +65,7 @@ public class DepositAccountTransaction extends AbstractPersistable<Long> {
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<Long> {
}
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) {
@@ -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<LocalDate> lastTransactionDates = new ArrayList<LocalDate>();
LocalDate lastTransactionDate=null;
List<DepositAccountTransaction> 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;
}
}