adamsaghy commented on code in PR #4837:
URL: https://github.com/apache/fineract/pull/4837#discussion_r2213107550
##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/domain/SavingsAccountDomainServiceJpa.java:
##########
@@ -326,8 +331,277 @@ public SavingsAccountTransaction
handleReversal(SavingsAccount account, List<Sav
this.savingsAccountRepository.save(account);
newTransactions.addAll(account.getSavingsAccountTransactionsWithPivotConfig());
this.savingsAccountTransactionRepository.saveAll(newTransactions);
- postJournalEntries(account, existingTransactionIds,
existingReversedTransactionIds, false, backdatedTxnsAllowedTill);
+ postJournalEntries(account, existingTransactionIds,
existingReversedTransactionIds, false, backdatedTxnsAllowedTill, false);
return reversal;
}
+
+ @Override
+ public void postInterest(SavingsAccount account, final MathContext mc,
final LocalDate interestPostingUpToDate,
+ final boolean isInterestTransfer, final boolean
isSavingsInterestPostingAtCurrentPeriodEnd,
+ final Integer financialYearBeginningMonth, final LocalDate
postInterestOnDate, final boolean backdatedTxnsAllowedTill,
+ final boolean postReversals) {
+ final List<PostingPeriod> postingPeriods =
account.calculateInterestUsing(mc, interestPostingUpToDate, isInterestTransfer,
+ isSavingsInterestPostingAtCurrentPeriodEnd,
financialYearBeginningMonth, postInterestOnDate, backdatedTxnsAllowedTill,
+ postReversals);
+ log.debug("postInterest {}", postingPeriods.size());
+
+ MonetaryCurrency currency = account.getCurrency();
+ Money interestPostedToDate = Money.zero(currency);
+
+ if (backdatedTxnsAllowedTill) {
+ interestPostedToDate = Money.of(currency,
account.getSummary().getTotalInterestPosted());
+ }
+
+ boolean recalucateDailyBalanceDetails = false;
+ boolean applyWithHoldTax =
account.isWithHoldTaxApplicableForInterestPosting();
+ final List<SavingsAccountTransaction> withholdTransactions = new
ArrayList<>();
+
+ if (backdatedTxnsAllowedTill) {
+
withholdTransactions.addAll(account.findWithHoldSavingsTransactionsWithPivotConfig());
+ } else {
+ withholdTransactions.addAll(account.findWithHoldTransactions());
+ }
+
+ for (final PostingPeriod interestPostingPeriod : postingPeriods) {
+ log.debug(" period: {}",
interestPostingPeriod.dateOfPostingTransaction());
+
+ final LocalDate interestPostingTransactionDate =
interestPostingPeriod.dateOfPostingTransaction();
+ final Money interestEarnedToBePostedForPeriod =
interestPostingPeriod.getInterestEarned();
+ log.debug(" interestEarnedToBePostedForPeriod: {}",
interestEarnedToBePostedForPeriod.toString());
+
+ if
(!interestPostingTransactionDate.isAfter(interestPostingUpToDate)) {
+ interestPostedToDate =
interestPostedToDate.plus(interestEarnedToBePostedForPeriod);
+
+ SavingsAccountTransaction postingTransaction = null;
+ if (backdatedTxnsAllowedTill) {
+ postingTransaction =
account.findInterestPostingSavingsTransactionWithPivotConfig(interestPostingTransactionDate);
+ } else {
+ postingTransaction =
account.findInterestPostingTransactionFor(interestPostingTransactionDate);
+ }
+ if (postingTransaction == null) {
+ SavingsAccountTransaction newPostingTransaction = null;
+ if
(interestEarnedToBePostedForPeriod.isGreaterThanOrEqualTo(Money.zero(currency)))
{
+ if
(interestEarnedToBePostedForPeriod.isGreaterThan(Money.zero(currency))) {
+ newPostingTransaction =
SavingsAccountTransaction.interestPosting(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod,
+ interestPostingPeriod.isUserPosting());
+ }
+ } else {
+ newPostingTransaction =
SavingsAccountTransaction.overdraftInterest(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod.negated(),
+ interestPostingPeriod.isUserPosting());
+ }
+ if (newPostingTransaction != null) {
+ if (backdatedTxnsAllowedTill) {
+
account.addTransactionToExisting(newPostingTransaction);
+ } else {
+ account.addTransaction(newPostingTransaction);
+ }
+ if
(account.savingsProduct().isAccrualBasedAccountingEnabled()) {
+ if
(MathUtil.isGreaterThanZero(interestEarnedToBePostedForPeriod)) {
+ SavingsAccountTransaction accrualTransaction =
SavingsAccountTransaction.accrual(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod,
+ interestPostingPeriod.isUserPosting(),
false);
+ if (backdatedTxnsAllowedTill) {
+
account.addTransactionToExisting(accrualTransaction);
+ } else {
+ account.addTransaction(accrualTransaction);
+ }
+ } else {
+ log.info("Accrual for Overdraft interest");
+ }
+ }
+ if (applyWithHoldTax) {
+
account.createWithHoldTransaction(interestEarnedToBePostedForPeriod.getAmount(),
interestPostingTransactionDate,
+ backdatedTxnsAllowedTill);
+ }
+ }
+ recalucateDailyBalanceDetails = true;
+ } else {
+ boolean correctionRequired = false;
+ if (postingTransaction.isInterestPostingAndNotReversed()) {
+ correctionRequired =
postingTransaction.hasNotAmount(interestEarnedToBePostedForPeriod);
+ } else {
+ correctionRequired =
postingTransaction.hasNotAmount(interestEarnedToBePostedForPeriod.negated());
+ }
+ log.debug(" correctionRequired {}", correctionRequired);
+ if (correctionRequired) {
+ boolean applyWithHoldTaxForOldTransaction = false;
+ postingTransaction.reverse();
+ SavingsAccountTransaction reversal = null;
+ if (postReversals) {
+ reversal =
SavingsAccountTransaction.reversal(postingTransaction);
+ }
+ final SavingsAccountTransaction withholdTransaction =
account.findTransactionFor(interestPostingTransactionDate,
+ withholdTransactions);
+ if (withholdTransaction != null) {
+ withholdTransaction.reverse();
+ applyWithHoldTaxForOldTransaction = true;
+ }
+ SavingsAccountTransaction newPostingTransaction;
+ if
(interestEarnedToBePostedForPeriod.isGreaterThanOrEqualTo(Money.zero(currency)))
{
+ newPostingTransaction =
SavingsAccountTransaction.interestPosting(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod,
+ interestPostingPeriod.isUserPosting());
+ } else {
+ newPostingTransaction =
SavingsAccountTransaction.overdraftInterest(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod.negated(),
+ interestPostingPeriod.isUserPosting());
+ }
+ if (backdatedTxnsAllowedTill) {
+
account.addTransactionToExisting(newPostingTransaction);
+ if (reversal != null) {
+ account.addTransactionToExisting(reversal);
+ }
+ } else {
+ account.addTransaction(newPostingTransaction);
+ if (reversal != null) {
+ account.addTransaction(reversal);
+ }
+ }
+ if
(account.savingsProduct().isAccrualBasedAccountingEnabled()
+ &&
MathUtil.isGreaterThanZero(interestEarnedToBePostedForPeriod)) {
+ log.info("TX2: {}",
interestEarnedToBePostedForPeriod.getAmount());
+ SavingsAccountTransaction accrualTransaction =
SavingsAccountTransaction.accrual(account, account.office(),
+ interestPostingTransactionDate,
interestEarnedToBePostedForPeriod,
+ interestPostingPeriod.isUserPosting(),
false);
+ if (backdatedTxnsAllowedTill) {
+
account.addTransactionToExisting(accrualTransaction);
+ } else {
+ account.addTransaction(accrualTransaction);
+ }
+ } else {
+ log.info("Accrual for Overdraft2 interest");
+ }
+ if (applyWithHoldTaxForOldTransaction) {
+
account.createWithHoldTransaction(interestEarnedToBePostedForPeriod.getAmount(),
interestPostingTransactionDate,
+ backdatedTxnsAllowedTill);
+ }
+ recalucateDailyBalanceDetails = true;
+ }
+ }
+ }
+ }
+
+ if (recalucateDailyBalanceDetails) {
+ // no openingBalance concept supported yet but probably will to
+ // allow
+ // for migrations.
+ Money openingAccountBalance = Money.zero(currency);
+
+ if (backdatedTxnsAllowedTill) {
+ if (account.getSummary().getLastInterestCalculationDate() ==
null) {
+ openingAccountBalance = Money.zero(currency);
+ } else {
+ openingAccountBalance = Money.of(currency,
account.getSummary().getRunningBalanceOnPivotDate());
+ }
+ }
+
+ // update existing transactions so derived balance fields are
+ // correct.
+ account.recalculateDailyBalances(openingAccountBalance,
interestPostingUpToDate, backdatedTxnsAllowedTill, postReversals);
+ }
+
+ if (!backdatedTxnsAllowedTill) {
+ account.getSummary().updateSummary(currency,
account.savingsAccountTransactionSummaryWrapper, account.getTransactions());
+ } else {
+ account.getSummary().updateSummaryWithPivotConfig(currency,
account.savingsAccountTransactionSummaryWrapper, null,
+ account.savingsAccountTransactions);
+ }
+ }
+
+ @Override
+ public void reverseTransfer(SavingsAccountTransaction
savingsAccountTransaction, boolean backdatedTxnsAllowedTill) {
+ final SavingsAccount account =
savingsAccountTransaction.getSavingsAccount();
+ account.setHelpers(savingsAccountTransactionSummaryWrapper,
savingsHelper);
+
+ undoTransaction(account, savingsAccountTransaction);
+ }
+
+ @Override
+ public void undoTransaction(SavingsAccount account,
SavingsAccountTransaction savingsAccountTransaction) {
+
+ final boolean isSavingsInterestPostingAtCurrentPeriodEnd =
this.configurationDomainService
+ .isSavingsInterestPostingAtCurrentPeriodEnd();
+ final Integer financialYearBeginningMonth =
this.configurationDomainService.retrieveFinancialYearBeginningMonth();
+ final Set<Long> existingTransactionIds = new HashSet<>();
+ final Set<Long> existingReversedTransactionIds = new HashSet<>();
+ updateExistingTransactionsDetails(account, existingTransactionIds,
existingReversedTransactionIds);
+
+ final Long savingsId = account.getId();
+ final Long transactionId = savingsAccountTransaction.getId();
+
+
this.savingsAccountTransactionDataValidator.validateTransactionWithPivotDate(savingsAccountTransaction.getTransactionDate(),
+ account);
+
+ if (!account.allowModify()) {
+ throw new
PlatformServiceUnavailableException("error.msg.saving.account.transaction.update.not.allowed",
+ "Savings account transaction:" + transactionId + " update
not allowed for this savings type", transactionId);
+ }
+
+ final LocalDate today = DateUtils.getBusinessLocalDate();
+ final MathContext mc = new MathContext(15,
MoneyHelper.getRoundingMode());
+
+ if (account.isNotActive()) {
+
throwValidationExceptionForActiveStatus(SavingsApiConstants.undoTransactionAction);
+ }
+ account.undoTransaction(transactionId);
+
+ // undoing transaction is withdrawal then undo withdrawal fee
transaction if any
+ if (savingsAccountTransaction.isWithdrawal()) {
+ final SavingsAccountTransaction nextSavingsAccountTransaction =
this.savingsAccountTransactionRepository
+ .findOneByIdAndSavingsAccountId(transactionId + 1,
savingsId);
+ if (nextSavingsAccountTransaction != null &&
nextSavingsAccountTransaction.isWithdrawalFeeAndNotReversed()) {
+ account.undoTransaction(transactionId + 1);
+ }
+ }
+ boolean isInterestTransfer = false;
+ LocalDate postInterestOnDate = null;
+ boolean postReversals = false;
+ checkClientOrGroupActive(account);
+ if (savingsAccountTransaction.isPostInterestCalculationRequired()
+ &&
account.isBeforeLastPostingPeriod(savingsAccountTransaction.getTransactionDate(),
false)) {
+ postInterest(account, mc, today, isInterestTransfer,
isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth,
+ postInterestOnDate, false, postReversals);
+ } else {
+ account.calculateInterestUsing(mc, today, isInterestTransfer,
isSavingsInterestPostingAtCurrentPeriodEnd,
+ financialYearBeginningMonth, postInterestOnDate, false,
postReversals);
+ }
+ List<DepositAccountOnHoldTransaction> depositAccountOnHoldTransactions
= null;
+ if (account.getOnHoldFunds().compareTo(BigDecimal.ZERO) > 0) {
+ depositAccountOnHoldTransactions =
this.depositAccountOnHoldTransactionRepository
+
.findBySavingsAccountAndReversedFalseOrderByCreatedDateAsc(account);
+ }
+
account.validateAccountBalanceDoesNotBecomeNegative(SavingsApiConstants.undoTransactionAction,
depositAccountOnHoldTransactions,
+ false);
+ account.activateAccountBasedOnBalance();
+ savingsAccountRepository.saveAndFlush(account);
+
+ postJournalEntries(account, existingTransactionIds,
existingReversedTransactionIds, false, false);
+ }
+
+ private void throwValidationExceptionForActiveStatus(final String
actionName) {
+ final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
+ final DataValidatorBuilder baseDataValidator = new
DataValidatorBuilder(dataValidationErrors)
+ .resource(SAVINGS_ACCOUNT_RESOURCE_NAME + actionName);
+
baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode("account.is.not.active");
+ throw new PlatformApiDataValidationException(dataValidationErrors);
+ }
+
+ @Override
+ public void checkClientOrGroupActive(final SavingsAccount account) {
Review Comment:
Do the validation in validation classes!
--
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]