Repository navigation
FINERACT-2284: Enforce Charge Rounding using Money rounding rules - #5940
Conversation
38bd72e to
8009f8c
Compare
There was a problem hiding this comment.
I stopped review at half time.. I dont like this approach, neither i think it is correct.
The introduced 0 amount handling is incorrect. 0 amount is incorrect outcome and charge cannot be created with this value, but nevertheless, we cannot accept the charge creation action and after not create the charge. This is incorrect and unwanted behaviour:
- Add proper validation (first thing to do)
- Calculate the amount, if it is incorrect (like 0 amount) -> Raise error and explain the issue!
- Remove the unnecessary flags and checks... it makes the code hard to read and maintained...
- Remove the unrelated changes, the PR should focus on 1 thing only!
|
@adamsaghy Thanks for your suggestions! I am working on the fix. |
|
Hi @adamsaghy, I have a question regarding future charges such as Savings Account Percentage of Withdrawal Charges and Share Account Redeem Charges. In these cases, the charge is attached successfully first and the amount is calculated only when the business transaction occurs. If the calculated charge amount becomes 0 after rounding, what is the expected behavior:
Could you please clarify the expected behavior for these future-charge scenarios so I can align the implementation consistently across Savings and Shares? Thank you! |
@bharathc27 fyi @Dhanno98 let me review |
I see no changes, no reply on my questions and concerns.. i have stopped reviewing the PR at half time... |
Hi @adamsaghy, Sorry for the confusion. When I wrote that I had updated the charge attachment flows locally, I meant that the changes were only completed in my local branch and had not yet been pushed or amended into the PR. My intention was to first finish addressing all of your review comments, validate everything thoroughly, and then push a single updated commit together with replies to each review comment. Because of that, I had not yet responded individually to the review feedback. I realize now that my comment made it sound like the PR had already been updated, which was not the case. Sorry for causing you to spend time reopening the review. I am currently working through all of the review comments and will push the updated changes together with responses shortly. Regarding the future-charge scenarios, that question was meant as a design clarification while implementing the remaining changes so I can keep the behavior consistent across Savings and Shares. This is the only thing that I need to understand and resolve and then I will ammend the commit. Thank you for your patience and for the detailed review feedback. |
Thank you for the clarification ;) Regarding zero amount: Lets use option 1 Reject the business transaction (withdrawal/redeem) with a validation error. |
Thank you for the clarification. Just to confirm the expected behavior for percentage-based future charges: Suppose a Savings Account has a "Percentage of Withdrawal" charge configured as 0.5%. When the charge is attached to the account, the percentage value (0.5%) is stored successfully because the actual charge amount is not yet known. Later, if a withdrawal of 100 is performed and the account currency uses 0 decimal places (for example JPY), the calculated charge amount would be: 0.5% × 100 = 0.5 which would then round to 0 according to the currency rounding rules. With option 1, my understanding is that the withdrawal transaction itself should be rejected with a validation error because the calculated charge amount becomes zero after rounding. Is that the intended behavior, even though the withdrawal amount itself is valid and only the associated charge rounds to zero? I just want to confirm this edge case before implementing the validation in the future-charge flows. |
With this use case, it’s not as straightforward as it seems. I might have been overly strict here. Just because the charge is too small, we still want to allow the withdrawal. So, if the calculated charge is zero, we simply ignore it and don’t create it. @bharathc27, what are your thoughts on this? |
|
@adamsaghy @Dhanno98 In any entity and scenario if the calculated charge amount is zero (after rounding) that charge should not be created. |
Hi @bharathcgowda @adamsaghy For percentage-based future charges (withdrawal/redeem), I understand that the charge definition should be stored and, when the calculated amount rounds to zero, the transaction should continue and the charge should simply not be applied. For flat future charges (for example a Shares Flat Redeem Charge of 0.5 in a currency with 0 decimal places), the rounded charge amount is known to be zero already at attachment time and can never become non-zero later. Currently, the existing behavior appears to allow such a Flat Redeem Charge to be attached to the share account without any validation. During the attachment flow, the configured charge amount (0.5 in this example) is stored as-is and no currency rounding is performed. The amount is only passed through For this scenario, what should be the expected behavior:
|
I dont think we should reject and throw error if charge became 0... @bharathcgowda thoughts? |
8009f8c to
84f7719
Compare
84f7719 to
8a82b79
Compare
Thanks @adamsaghy. The current implementation in this PR already follows that behavior for Share Redeem Charges and Savings Percentage Withdrawal Charges. These charges continue to be attached and stored as configured. When they are later applied, the calculated charge amount goes through the existing So Share Flat Redeem Charges that eventually round to 0 during the Redeem Shares flow are currently not rejected during attachment. I've also updated the PR with the latest changes and fixes. |
8a82b79 to
1bcae1a
Compare
|
@Dhanno98 Please review the failing checks |
1bcae1a to
e87adeb
Compare
Hi @adamsaghy ! I investigated the CI failures. One of the failures ( After tracing the failure, I found that I've updated the
I've amended the PR with this fix. The other CI failure ( |
|
c4ef473 to
a20e3a5
Compare
I investigated the CI failure further. Initially, I fixed the issue in However, on the next CI run, After reproducing this locally by running:
I found that all three reproduced the same problem. The root cause is that these tests create charge names using I updated all of the newly added charge rounding tests to generate UUIDs without hyphens ( Since |
adamsaghy
left a comment
There was a problem hiding this comment.
Technically, I see one real blocker: the PR fixes ShareAccountTransaction.createChargeTransaction() to use the rounded/applied amount, but activation charges still create ShareAccountChargePaidBy with charge.percentageOrAmount() in ShareAccountDataSerializer.
That means for a flat activation charge like 19.8 rounded to 20:
share transaction amount = 20
chargesPaid.amount = 19.8
That is dangerous because share accounting consumes chargesPaid.amount and checks that the charge-payment breakdown totals match the transaction amount. With accounting enabled, this can trip the “charges for shares have broken accounting code” integrity exception.
To be fixed: in the activation charge path, pass the derived amount into ShareAccountChargePaidBy, not charge.percentageOrAmount().
Please also add a test covering activation share charges with accounting enabled, because the current rounding tests appear to use non-accounting share products and won’t catch this.
Thanks for catching that! I'll update the activation charge path to use the derived amount and add an accounting-enabled test. |
|
Hi @adamsaghy While implementing the integration test for the accounting-rule-2 share product flow, I noticed that the Because of that, the generated My understanding is that the Swagger request schema is incomplete rather than the backend implementation. My plan is to add these optional request fields to the OpenAPI schema and regenerate the client. Before doing that, I wanted to check whether this is the preferred approach, since it may affect the OpenAPI backward compatibility checks. |
Yes, please. Your understanding is correct. Swagger might be incomplete or incorrect in certain cases. However, the relevant information is always the incoming requests (fields), DTOs, and response DTOs. |
50ef7d5 to
0e46f0c
Compare
|
@Dhanno98 Please rebase this PR |
6737367 to
bbc2558
Compare
|
Hi @adamsaghy Sorry about the confusion earlier. While cleaning up the PR I accidentally pushed a couple of unrelated files, so I first fixed that and then noticed your comment about rebasing. The PR has now been rebased onto the latest In addition to the original rounding fix, I made a few related improvements: 1. Fix for the rounded activation charge issue The actual fix is in new ShareAccountChargePaidBy(chargeTransaction, charge, charge.percentageOrAmount())It now uses the rounded transaction amount: new ShareAccountChargePaidBy(chargeTransaction, charge, amount)This ensures that the persisted I also added an integration test,
To support this test using the generated client, I added the missing accounting account fields to 2. Generated client improvements Since using the generated client for these commands is acceptable, I also exposed the missing request fields in:
After regenerating the client, I made use of these generated client requests for:
This makes the integration tests consistent with Fineract client usage instead of RestAssured. |
|
I went through the remaining failing CI job ( From what I observed, the reported breaking changes are due to the generated client schema updates that I made for:
I also noticed that the additional fields added to It looks like the failure is specifically because the OpenAPI contract changed for the existing request models. Could you please let me know how you would like me to proceed? |
bbc2558 to
fc80760
Compare
fc80760 to
745ae67
Compare
|
I have reverted the unrelated Swagger changes so this PR now contains only the changes relevant to charge rounding. I also rebased the branch on the latest develop and the CI checks are now passing. |
I am postponing to merge this till release of 1.15.0 is not finished |
Description
FINERACT-2284: Enforce charge rounding using Money rounding rules
This PR fixes charge amount handling across Fineract by ensuring that charge values are rounded using the application's configured currency rules before they are persisted or processed.
Previously, several charge flows stored or processed raw
BigDecimalvalues directly. As a result, charge amounts could bypass currency rounding settings such as:digitsAfterDecimalinMultiplesOfThis led to inconsistent behavior between charge calculations and other monetary operations that already rely on
Money.This PR standardizes charge amount handling by using
Moneyfor charge rounding across all supported charge types:In addition, if a charge amount rounds to zero after applying currency rules, the charge is no longer persisted or created.
Functional Changes
Loan Charges
Savings Account Charges
Money.Share Account Charges
Client Charges
API Documentation
Updated Swagger/OpenAPI schemas where required to improve request model documentation and examples.
Why This Change
Fineract already provides currency-specific monetary rules through
Money, including support for:digitsAfterDecimal)inMultiplesOf)Charges should follow the same monetary rules as all other financial amounts.
By enforcing rounding consistently:
Test Coverage
Added integration test coverage for all supported charge types:
LoanChargeRoundingTestSavingsAccountChargeRoundingTestShareAccountChargeRoundingTestClientChargeRoundingTestThe tests verify:
inMultiplesOfanddigitsAfterDecimalrounding is enforced.Verification
Executed the complete charge rounding integration test suite successfully.
Checklist
Please make sure these boxes are checked before submitting your pull request - thanks!
Your assigned reviewer(s) will follow our guidelines for code reviews.