GoyalRocks007 commented on code in PR #3791:
URL: https://github.com/apache/fineract/pull/3791#discussion_r1535072674


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/FixedDepositAccountInterestCalculationServiceImpl.java:
##########
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fineract.portfolio.savings.service;
+
+import static 
org.apache.fineract.portfolio.savings.DepositsApiConstants.annualInterestRateParamName;
+import static 
org.apache.fineract.portfolio.savings.DepositsApiConstants.interestCompoundingPeriodInMonthsParamName;
+import static 
org.apache.fineract.portfolio.savings.DepositsApiConstants.principalAmountParamName;
+import static 
org.apache.fineract.portfolio.savings.DepositsApiConstants.tenureInMonthsParamName;
+
+import com.google.gson.JsonElement;
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.util.HashMap;
+import lombok.RequiredArgsConstructor;
+import org.apache.fineract.infrastructure.core.api.JsonQuery;
+import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
+import org.apache.fineract.portfolio.savings.data.DepositAccountDataValidator;
+import org.springframework.stereotype.Service;
+
+@Service
+@RequiredArgsConstructor
+public class FixedDepositAccountInterestCalculationServiceImpl implements 
FixedDepositAccountInterestCalculationService {
+
+    private final DepositAccountDataValidator depositAccountDataValidator;
+    private final FromJsonHelper fromApiJsonHelper;
+
+    @Override
+    public HashMap calculateInterest(JsonQuery query) {
+        
depositAccountDataValidator.validateFixedDepositForInterestCalculation(query.json());
+        JsonElement element = query.parsedJson();
+        Long principalAmount = 
this.fromApiJsonHelper.extractLongNamed(principalAmountParamName, element);
+        BigDecimal annualInterestRate = 
this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(annualInterestRateParamName,
 element);
+        Long tenureInMonths = 
this.fromApiJsonHelper.extractLongNamed(tenureInMonthsParamName, element);
+        Long interestCompoundingPeriodInMonths = 
this.fromApiJsonHelper.extractLongNamed(interestCompoundingPeriodInMonthsParamName,
+                element);
+        BigDecimal maturityAmount = 
this.calculateInterestInternal(principalAmount, annualInterestRate, 
tenureInMonths,
+                interestCompoundingPeriodInMonths);
+        String warning = "This is an approximate calculated amount - it may 
vary slightly when the account is created";
+
+        HashMap result = new HashMap<>();
+        result.put("maturityAmount", maturityAmount);
+        result.put("warning", warning);
+
+        return result;
+    }
+
+    public BigDecimal calculateInterestInternal(Long principalAmount, 
BigDecimal annualInterestRate, Long tenureInMonths,
+            Long interestCompoundingPeriodInMonths) {
+        BigDecimal numberOfCompoundingsPerAnnum = 
BigDecimal.valueOf(12).divide(BigDecimal.valueOf(interestCompoundingPeriodInMonths));
+        Long totalNumberOfCompoundings = tenureInMonths / 
interestCompoundingPeriodInMonths;
+        MathContext mc = new MathContext(9);

Review Comment:
   However on second thought, this seems to be the best viable solution for the 
situation. Another solution would be to use MathContext directly like I used 
earlier, but it is not a good solution. I'll proceed with this only.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to