/fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/DepositAccountPreMatureCalculationPlatformServiceImpl.java

https://gitlab.com/skylabase/incubator-fineract · Java · 113 lines · 80 code · 14 blank · 19 comment · 3 complexity · 3bf7728004bac86c3f37c3b3fc02b733 MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.fineract.portfolio.savings.service;
  20. import static org.apache.fineract.portfolio.savings.DepositsApiConstants.closedOnDateParamName;
  21. import java.util.Collection;
  22. import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
  23. import org.apache.fineract.infrastructure.core.api.JsonQuery;
  24. import org.apache.fineract.infrastructure.core.data.EnumOptionData;
  25. import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
  26. import org.apache.fineract.portfolio.paymenttype.data.PaymentTypeData;
  27. import org.apache.fineract.portfolio.paymenttype.service.PaymentTypeReadPlatformService;
  28. import org.apache.fineract.portfolio.savings.DepositAccountOnClosureType;
  29. import org.apache.fineract.portfolio.savings.DepositAccountType;
  30. import org.apache.fineract.portfolio.savings.data.DepositAccountData;
  31. import org.apache.fineract.portfolio.savings.data.DepositAccountTransactionDataValidator;
  32. import org.apache.fineract.portfolio.savings.data.FixedDepositAccountData;
  33. import org.apache.fineract.portfolio.savings.data.RecurringDepositAccountData;
  34. import org.apache.fineract.portfolio.savings.data.SavingsAccountData;
  35. import org.apache.fineract.portfolio.savings.domain.DepositAccountAssembler;
  36. import org.apache.fineract.portfolio.savings.domain.FixedDepositAccount;
  37. import org.apache.fineract.portfolio.savings.domain.RecurringDepositAccount;
  38. import org.apache.fineract.portfolio.savings.domain.SavingsAccount;
  39. import org.joda.time.LocalDate;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import com.google.gson.JsonElement;
  44. @Service
  45. public class DepositAccountPreMatureCalculationPlatformServiceImpl implements DepositAccountPreMatureCalculationPlatformService {
  46. private final FromJsonHelper fromJsonHelper;
  47. private final DepositAccountTransactionDataValidator depositAccountTransactionDataValidator;
  48. private final DepositAccountAssembler depositAccountAssembler;
  49. private final SavingsAccountReadPlatformService savingsAccountReadPlatformService;
  50. private final ConfigurationDomainService configurationDomainService;
  51. private final PaymentTypeReadPlatformService paymentTypeReadPlatformService;
  52. @Autowired
  53. public DepositAccountPreMatureCalculationPlatformServiceImpl(final FromJsonHelper fromJsonHelper,
  54. final DepositAccountTransactionDataValidator depositAccountTransactionDataValidator,
  55. final DepositAccountAssembler depositAccountAssembler,
  56. final SavingsAccountReadPlatformService savingsAccountReadPlatformService,
  57. final ConfigurationDomainService configurationDomainService, PaymentTypeReadPlatformService paymentTypeReadPlatformService) {
  58. this.fromJsonHelper = fromJsonHelper;
  59. this.depositAccountTransactionDataValidator = depositAccountTransactionDataValidator;
  60. this.depositAccountAssembler = depositAccountAssembler;
  61. this.savingsAccountReadPlatformService = savingsAccountReadPlatformService;
  62. this.configurationDomainService = configurationDomainService;
  63. this.paymentTypeReadPlatformService = paymentTypeReadPlatformService;
  64. }
  65. @Transactional
  66. @Override
  67. public DepositAccountData calculatePreMatureAmount(final Long accountId, final JsonQuery query,
  68. final DepositAccountType depositAccountType) {
  69. final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
  70. .isSavingsInterestPostingAtCurrentPeriodEnd();
  71. final Integer financialYearBeginningMonth = this.configurationDomainService.retrieveFinancialYearBeginningMonth();
  72. this.depositAccountTransactionDataValidator.validatePreMatureAmountCalculation(query.json(), depositAccountType);
  73. final SavingsAccount account = this.depositAccountAssembler.assembleFrom(accountId, depositAccountType);
  74. DepositAccountData accountData = null;
  75. Collection<EnumOptionData> onAccountClosureOptions = SavingsEnumerations
  76. .depositAccountOnClosureType(new DepositAccountOnClosureType[] { DepositAccountOnClosureType.WITHDRAW_DEPOSIT,
  77. DepositAccountOnClosureType.TRANSFER_TO_SAVINGS });
  78. final Collection<PaymentTypeData> paymentTypeOptions = this.paymentTypeReadPlatformService.retrieveAllPaymentTypes();
  79. final Collection<SavingsAccountData> savingsAccountDatas = this.savingsAccountReadPlatformService.retrieveActiveForLookup(
  80. account.clientId(), DepositAccountType.SAVINGS_DEPOSIT);
  81. final JsonElement element = this.fromJsonHelper.parse(query.json());
  82. final LocalDate preMaturityDate = this.fromJsonHelper.extractLocalDateNamed(closedOnDateParamName, element);
  83. // calculate interest before one day of closure date
  84. final LocalDate interestCalculatedToDate = preMaturityDate.minusDays(1);
  85. final boolean isPreMatureClosure = true;
  86. if (depositAccountType.isFixedDeposit()) {
  87. final FixedDepositAccount fd = (FixedDepositAccount) account;
  88. accountData = FixedDepositAccountData.preClosureDetails(account.getId(), fd.calculatePreMatureAmount(interestCalculatedToDate,
  89. isPreMatureClosure, isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth), onAccountClosureOptions,
  90. paymentTypeOptions, savingsAccountDatas);
  91. } else if (depositAccountType.isRecurringDeposit()) {
  92. final RecurringDepositAccount rd = (RecurringDepositAccount) account;
  93. accountData = RecurringDepositAccountData.preClosureDetails(account.getId(), rd.calculatePreMatureAmount(
  94. interestCalculatedToDate, isPreMatureClosure, isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth),
  95. onAccountClosureOptions, paymentTypeOptions, savingsAccountDatas);
  96. }
  97. return accountData;
  98. }
  99. }