#164_compounding interest is greater than posting inerest

Project: http://git-wip-us.apache.org/repos/asf/incubator-fineract/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-fineract/commit/91953155
Tree: http://git-wip-us.apache.org/repos/asf/incubator-fineract/tree/91953155
Diff: http://git-wip-us.apache.org/repos/asf/incubator-fineract/diff/91953155

Branch: refs/heads/develop
Commit: 9195315512b5c654b134427d573c08a83ad19c3a
Parents: 580840c
Author: jyothsnag <jyoth...@confluxtechnologies.com>
Authored: Thu Nov 10 21:51:49 2016 +0530
Committer: jyothsnag <jyoth...@confluxtechnologies.com>
Committed: Thu Nov 10 21:51:49 2016 +0530

----------------------------------------------------------------------
 .../savings/domain/SavingsAccount.java          |  3 +-
 .../interest/AnnualCompoundingPeriod.java       |  5 +++
 .../interest/BiAnnualCompoundingPeriod.java     |  5 +++
 .../domain/interest/CompoundInterestHelper.java | 13 +++++---
 .../domain/interest/CompoundInterestValues.java | 35 ++++++++++++++++++++
 .../domain/interest/CompoundingPeriod.java      |  2 ++
 .../domain/interest/DailyCompoundingPeriod.java | 10 ++++--
 .../interest/MonthlyCompoundingPeriod.java      |  5 +++
 .../savings/domain/interest/PostingPeriod.java  | 21 +++++++++---
 .../interest/QuarterlyCompoundingPeriod.java    |  6 +++-
 10 files changed, 90 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccount.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccount.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccount.java
index 46b875a..bc8a72b 100755
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccount.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccount.java
@@ -2666,8 +2666,7 @@ public class SavingsAccount extends 
AbstractPersistableCustom<Long> {
         SavingsCompoundingInterestPeriodType 
savingsCompoundingInterestPeriodType = SavingsCompoundingInterestPeriodType
                 .fromInt(interestCompoundingPeriodType);
 
-        if (postingtoCompoundMap.get(savingsPostingInterestPeriodType) == null
-                || 
!postingtoCompoundMap.get(savingsPostingInterestPeriodType).contains(savingsCompoundingInterestPeriodType))
 {
+        if (postingtoCompoundMap.get(savingsPostingInterestPeriodType) == 
null) {
             
baseDataValidator.failWithCodeNoParameterAddedToErrorCode("posting.period.type.is.less.than.compound.period.type",
                     savingsPostingInterestPeriodType.name(), 
savingsCompoundingInterestPeriodType.name());
 

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/AnnualCompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/AnnualCompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/AnnualCompoundingPeriod.java
index bc495a3..682e7d8 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/AnnualCompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/AnnualCompoundingPeriod.java
@@ -178,4 +178,9 @@ public class AnnualCompoundingPeriod implements 
CompoundingPeriod {
         this.periodInterval = periodInterval;
         this.endOfDayBalances = endOfDayBalances;
     }
+
+       @Override
+       public LocalDateInterval getPeriodInterval() {
+               return this.periodInterval;
+       }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/BiAnnualCompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/BiAnnualCompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/BiAnnualCompoundingPeriod.java
index eeaa236..f07fb0f 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/BiAnnualCompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/BiAnnualCompoundingPeriod.java
@@ -178,4 +178,9 @@ public class BiAnnualCompoundingPeriod implements 
CompoundingPeriod {
         this.periodInterval = periodInterval;
         this.endOfDayBalances = endOfDayBalances;
     }
+
+       @Override
+       public LocalDateInterval getPeriodInterval() {
+               return this.periodInterval;
+       }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java
index 40b6535..55cd5a6 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java
@@ -46,10 +46,13 @@ public class CompoundInterestHelper {
         Money interestEarned = Money.zero(currency);
 
         // total interest earned in previous periods but not yet recognised
-        BigDecimal interestEarnedButNotPosted = BigDecimal.ZERO;
+               BigDecimal compoundedInterest = BigDecimal.ZERO;
+               BigDecimal unCompoundedInterest = BigDecimal.ZERO;
+               final CompoundInterestValues compoundInterestValues = new 
CompoundInterestValues(compoundedInterest,
+                               unCompoundedInterest);
         for (final PostingPeriod postingPeriod : allPeriods) {
 
-            final BigDecimal interestEarnedThisPeriod = 
postingPeriod.calculateInterest(interestEarnedButNotPosted);
+            final BigDecimal interestEarnedThisPeriod = 
postingPeriod.calculateInterest(compoundInterestValues);
 
             final Money moneyToBePostedForPeriod = Money.of(currency, 
interestEarnedThisPeriod);
 
@@ -58,9 +61,9 @@ public class CompoundInterestHelper {
             // interest for accounts which has post interest to linked savings
             // account and if already transfered then it includes in interest
             // calculation.
-            if (postingPeriod.isInterestTransfered() || 
!interestTransferEnabled
-                    || (lockUntil != null && 
!postingPeriod.dateOfPostingTransaction().isAfter(lockUntil))) {
-                interestEarnedButNotPosted = 
interestEarnedButNotPosted.add(moneyToBePostedForPeriod.getAmount());
+            if (!(postingPeriod.isInterestTransfered() || 
!interestTransferEnabled
+                    || (lockUntil != null && 
!postingPeriod.dateOfPostingTransaction().isAfter(lockUntil)))) {
+               compoundInterestValues.setcompoundedInterest(BigDecimal.ZERO);
             }
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestValues.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestValues.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestValues.java
new file mode 100644
index 0000000..a32d5eb
--- /dev/null
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestValues.java
@@ -0,0 +1,35 @@
+package org.apache.fineract.portfolio.savings.domain.interest;
+
+import java.math.BigDecimal;
+
+public class CompoundInterestValues {
+
+       private BigDecimal compoundedInterest;
+       private BigDecimal uncompoundedInterest;
+
+       public CompoundInterestValues(final BigDecimal compoundedInterest, 
final BigDecimal uncompoundedInterest) {
+               this.compoundedInterest = compoundedInterest;
+               this.uncompoundedInterest = uncompoundedInterest;
+       }
+
+       public BigDecimal getcompoundedInterest() {
+               return this.compoundedInterest;
+       }
+
+       public BigDecimal getuncompoundedInterest() {
+               return this.uncompoundedInterest;
+       }
+
+       public void setcompoundedInterest(BigDecimal interestToBeCompounded) {
+               this.compoundedInterest = interestToBeCompounded;
+       }
+
+       public void setuncompoundedInterest(BigDecimal 
interestToBeUncompounded) {
+               this.uncompoundedInterest = interestToBeUncompounded;
+       }
+
+       public void setZeroForInterestToBeUncompounded() {
+               this.uncompoundedInterest = BigDecimal.ZERO;
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundingPeriod.java
index a236abb..7e42865 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundingPeriod.java
@@ -20,6 +20,7 @@ package org.apache.fineract.portfolio.savings.domain.interest;
 
 import java.math.BigDecimal;
 
+import org.apache.fineract.infrastructure.core.domain.LocalDateInterval;
 import 
org.apache.fineract.portfolio.savings.SavingsCompoundingInterestPeriodType;
 import org.apache.fineract.portfolio.savings.SavingsInterestCalculationType;
 
@@ -29,4 +30,5 @@ public interface CompoundingPeriod {
             SavingsInterestCalculationType interestCalculationType, BigDecimal 
interestFromPreviousPostingPeriod,
             BigDecimal interestRateAsFraction, long daysInYear, BigDecimal 
minBalanceForInterestCalculation, 
             BigDecimal overdraftInterestRateAsFraction, BigDecimal 
minOverdraftForInterestCalculation);
+    LocalDateInterval getPeriodInterval();
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/DailyCompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/DailyCompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/DailyCompoundingPeriod.java
index ade20da..7433440 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/DailyCompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/DailyCompoundingPeriod.java
@@ -30,7 +30,6 @@ import org.joda.time.LocalDate;
 
 public class DailyCompoundingPeriod implements CompoundingPeriod {
 
-    @SuppressWarnings("unused")
     private final LocalDateInterval periodInterval;
     private final List<EndOfDayBalance> endOfDayBalances;
 
@@ -77,8 +76,8 @@ public class DailyCompoundingPeriod implements 
CompoundingPeriod {
 
     @Override
     public BigDecimal calculateInterest(
-            @SuppressWarnings("unused") final 
SavingsCompoundingInterestPeriodType compoundingInterestPeriodType,
-            @SuppressWarnings("unused") final SavingsInterestCalculationType 
interestCalculationType,
+            final SavingsCompoundingInterestPeriodType 
compoundingInterestPeriodType,
+            final SavingsInterestCalculationType interestCalculationType,
             final BigDecimal interestFromPreviousPostingPeriod, final 
BigDecimal interestRateAsFraction, final long daysInYear,
             final BigDecimal minBalanceForInterestCalculation,
             final BigDecimal overdraftInterestRateAsFraction, final BigDecimal 
minOverdraftForInterestCalculation) {
@@ -97,4 +96,9 @@ public class DailyCompoundingPeriod implements 
CompoundingPeriod {
 
         return interestEarned;
     }
+
+       @Override
+       public LocalDateInterval getPeriodInterval() {
+               return this.periodInterval;
+       }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/MonthlyCompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/MonthlyCompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/MonthlyCompoundingPeriod.java
index 8ead199..ca39033 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/MonthlyCompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/MonthlyCompoundingPeriod.java
@@ -174,4 +174,9 @@ public class MonthlyCompoundingPeriod implements 
CompoundingPeriod {
         this.periodInterval = periodInterval;
         this.endOfDayBalances = endOfDayBalances;
     }
+
+       @Override
+       public LocalDateInterval getPeriodInterval() {
+               return this.periodInterval;
+       }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/PostingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/PostingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/PostingPeriod.java
index 2d9f560..2341288 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/PostingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/PostingPeriod.java
@@ -194,19 +194,32 @@ public class PostingPeriod {
         return this.openingBalance;
     }
 
-    public BigDecimal calculateInterest(final BigDecimal 
interestFromPreviousPostingPeriod) {
+    public BigDecimal calculateInterest(final CompoundInterestValues 
compoundInterestValues) {
         BigDecimal interestEarned = BigDecimal.ZERO;
 
         // for each compounding period accumulate the amount of interest
         // to be applied to the balanced for interest calculation
-        BigDecimal interestCompounded = interestFromPreviousPostingPeriod;
         for (final CompoundingPeriod compoundingPeriod : 
this.compoundingPeriods) {
 
             final BigDecimal interestUnrounded = 
compoundingPeriod.calculateInterest(this.interestCompoundingType,
-                    this.interestCalculationType, interestCompounded, 
this.interestRateAsFraction, this.daysInYear,
+                    this.interestCalculationType, 
compoundInterestValues.getcompoundedInterest(), this.interestRateAsFraction, 
this.daysInYear,
                     this.minBalanceForInterestCalculation.getAmount(),         
this.overdraftInterestRateAsFraction,
                     this.minOverdraftForInterestCalculation.getAmount());
-            interestCompounded = interestCompounded.add(interestUnrounded);
+                       BigDecimal unCompoundedInterest = 
compoundInterestValues.getuncompoundedInterest().add(interestUnrounded);
+                       
compoundInterestValues.setuncompoundedInterest(unCompoundedInterest);
+                       LocalDate compoundingPeriodEndDate = 
compoundingPeriod.getPeriodInterval().endDate();
+                       if 
(!SavingsCompoundingInterestPeriodType.DAILY.equals(this.interestCompoundingType))
 {
+                               compoundingPeriodEndDate = 
determineInterestPeriodEndDateFrom(
+                                               
compoundingPeriod.getPeriodInterval().startDate(), this.interestCompoundingType,
+                                               
compoundingPeriod.getPeriodInterval().endDate());
+                       }
+
+                       if 
(compoundingPeriodEndDate.equals(compoundingPeriod.getPeriodInterval().endDate()))
 {
+                               BigDecimal interestCompounded = 
compoundInterestValues.getcompoundedInterest()
+                                               .add(unCompoundedInterest);
+                               
compoundInterestValues.setcompoundedInterest(interestCompounded);
+                               
compoundInterestValues.setZeroForInterestToBeUncompounded();
+                       }
             interestEarned = interestEarned.add(interestUnrounded);
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-fineract/blob/91953155/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/QuarterlyCompoundingPeriod.java
----------------------------------------------------------------------
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/QuarterlyCompoundingPeriod.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/QuarterlyCompoundingPeriod.java
index ddead71..8ffdbc1 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/QuarterlyCompoundingPeriod.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/QuarterlyCompoundingPeriod.java
@@ -31,7 +31,6 @@ import org.joda.time.LocalDate;
 
 public class QuarterlyCompoundingPeriod implements CompoundingPeriod {
 
-    @SuppressWarnings("unused")
     private final LocalDateInterval periodInterval;
     private final List<EndOfDayBalance> endOfDayBalances;
 
@@ -178,4 +177,9 @@ public class QuarterlyCompoundingPeriod implements 
CompoundingPeriod {
         this.periodInterval = periodInterval;
         this.endOfDayBalances = endOfDayBalances;
     }
+
+       @Override
+       public LocalDateInterval getPeriodInterval() {
+               return this.periodInterval;
+       }
 }
\ No newline at end of file

Reply via email to