adamsaghy commented on code in PR #3973:
URL: https://github.com/apache/fineract/pull/3973#discussion_r1680890688


##########
fineract-savings/src/main/java/org/apache/fineract/portfolio/savings/service/accrual/SavingsAccrualDomainServiceImpl.java:
##########
@@ -0,0 +1,196 @@
+/**
+ * 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.accrual;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import 
org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
+import org.apache.fineract.infrastructure.core.domain.LocalDateInterval;
+import org.apache.fineract.infrastructure.core.service.MathUtil;
+import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
+import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
+import org.apache.fineract.organisation.monetary.domain.Money;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import 
org.apache.fineract.portfolio.savings.SavingsCompoundingInterestPeriodType;
+import 
org.apache.fineract.portfolio.savings.SavingsInterestCalculationDaysInYearType;
+import org.apache.fineract.portfolio.savings.SavingsInterestCalculationType;
+import org.apache.fineract.portfolio.savings.SavingsPostingInterestPeriodType;
+import org.apache.fineract.portfolio.savings.data.SavingsAccrualData;
+import org.apache.fineract.portfolio.savings.domain.SavingsAccount;
+import org.apache.fineract.portfolio.savings.domain.SavingsAccountAssembler;
+import org.apache.fineract.portfolio.savings.domain.SavingsAccountCharge;
+import org.apache.fineract.portfolio.savings.domain.SavingsAccountChargePaidBy;
+import 
org.apache.fineract.portfolio.savings.domain.SavingsAccountRepositoryWrapper;
+import org.apache.fineract.portfolio.savings.domain.SavingsAccountTransaction;
+import org.apache.fineract.portfolio.savings.domain.SavingsHelper;
+import 
org.apache.fineract.portfolio.savings.domain.interest.CompoundInterestValues;
+import org.apache.fineract.portfolio.savings.domain.interest.PostingPeriod;
+import 
org.apache.fineract.portfolio.savings.domain.interest.SavingsAccountTransactionDetailsForPostingPeriod;
+import 
org.apache.fineract.portfolio.savings.service.SavingsAccountDomainService;
+import 
org.apache.fineract.portfolio.savings.service.SavingsAccountReadPlatformService;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Slf4j
+@Service
+@Transactional
+@RequiredArgsConstructor
+public class SavingsAccrualDomainServiceImpl {
+
+    private final SavingsAccountReadPlatformService 
savingsAccountReadPlatformService;
+    private final SavingsAccountAssembler savingsAccountAssembler;
+    private final SavingsAccountRepositoryWrapper savingsAccountRepository;
+    private final SavingsHelper savingsHelper;
+    private final ConfigurationDomainService configurationDomainService;
+    private final SavingsAccountDomainService savingsAccountDomainService;
+
+    public void addAccrualEntries(LocalDate tillDate) throws 
JobExecutionException {
+        final Collection<SavingsAccrualData> savingsAccrualData = 
savingsAccountReadPlatformService.retrievePeriodicAccrualData(tillDate,
+                null);
+        log.debug("Savings Accrual for date {} : {}", tillDate, 
savingsAccrualData.size());
+        final Integer financialYearBeginningMonth = 
configurationDomainService.retrieveFinancialYearBeginningMonth();
+        final boolean isSavingsInterestPostingAtCurrentPeriodEnd = 
this.configurationDomainService
+                .isSavingsInterestPostingAtCurrentPeriodEnd();
+        final MathContext mc = MoneyHelper.getMathContext();
+
+        List<Throwable> errors = new ArrayList<>();
+        for (SavingsAccrualData savingsAccrual : savingsAccrualData) {
+            try {
+                SavingsAccount savingsAccount = 
savingsAccountAssembler.assembleFrom(savingsAccrual.getId(), false);
+                LocalDate fromDate = savingsAccrual.getAccruedTill();
+                if (fromDate == null) {
+                    fromDate = savingsAccount.getActivationDate();
+                }
+                log.debug("Processing savings account {} from date {} till 
date {}", savingsAccrual.getAccountNo(), fromDate, tillDate);
+                addAccrualTransactions(savingsAccount, fromDate, tillDate, 
financialYearBeginningMonth,
+                        isSavingsInterestPostingAtCurrentPeriodEnd, mc);
+            } catch (Exception e) {
+                log.error("Failed to add accrual transaction for savings {} : 
{}", savingsAccrual.getAccountNo(), e.getMessage());
+                errors.add(e.getCause());
+            }
+        }
+        if (!errors.isEmpty()) {
+            throw new JobExecutionException(errors);
+        }
+    }
+
+    public boolean isChargeToBeRecognizedAsAccrual(final Collection<Long> 
chargeIds, final SavingsAccountCharge savingsAccountCharge) {
+        if (chargeIds.isEmpty()) {
+            return false;
+        }
+        return chargeIds.contains(savingsAccountCharge.getCharge().getId());
+    }
+
+    public SavingsAccountTransaction 
addSavingsChargeAccrualTransaction(SavingsAccount savingsAccount,
+            SavingsAccountCharge savingsAccountCharge, LocalDate 
transactionDate) {
+        final MonetaryCurrency currency = savingsAccount.getCurrency();
+        final Money chargeAmount = savingsAccountCharge.getAmount(currency);
+        SavingsAccountTransaction savingsAccountTransaction = 
SavingsAccountTransaction.accrual(savingsAccount, savingsAccount.office(),

Review Comment:
   You are creating accrual for a charge... so charges should be supported 
along with interest, no?



-- 
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: commits-unsubscr...@fineract.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to