-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dev/financial#34 Fix line allocation in Payment.create #14763
Conversation
(Standard links)
|
dda2e58
to
ac78942
Compare
Test fail relates but from the one I looked at I think the test might be setting it up wrong |
This is currently blocked on clarifying correct behaviour when line items are overpaid - from #14408 (comment) I've encountered a specific issue with the financial items - namely what should happen when the payment exceeds the line items total. This can happen when This means that the line item total is less than the contribution total There are 3 options
The former is locked in & tested in UpdatePaymentTest whereas the tests @monishdeb wrote for dev/core#310 assume the latter is the case One for @JoeMurray @monishdeb @pradpnayak to have thoughts on... |
ac78942
to
db7a8dd
Compare
@eileenmcnaughton Now that the related PRs are merged, what can I do to move this PR forward? |
@kcristiano definite progress - I still have questions on whether this is right & i should change the failing test to match it - I annotated the test here #15143 Fundamentally the question is 'what is the point of the financial items' - I had understood them as being about linking payments to the things they are paying. However, as we see in that test it is allocating the intention to pay and SOME of the payments - I feel like I still need some guidance from @JoeMurray & @lcdservices as to what we are seeking to achieve here - I did get some but when I tried to act off it I still found gaps between what I felt like they were saying and what I was seeing 'locked in' |
Thanks Eileen. I will do a run test and see what happens in the use cases that have given us issues. |
So here's my test result. I have configured a price-set for Contribution with 3 price fields as Text -$1, radio (a-$1, b-$2,c-$3), checkbox (c1-$10, c2-$20) and this is how it behaves on partial payment and refund. PARTIAL PAYMENT:
REFUND
Here it should create the 4 FI entries with respective proportionate amount (ID between 189 to 192) Result - Wrong behavior |
Thanks @monishdeb I'll take a look as well, but the data you have shows we haven't gotten this solved yet |
@monishdeb this PR doesn't touch what happens in the refund - they use separate code - so I'd like to worry about that as a follow up. A more specific blocking issue for this PR is that I can't get the api_v3_PaymentTest::testUpdatePayment to pass without changing assumptions - either in that test or in this PR (other fails are not notices etc) I added some comments here but basically this PR assumes that all PAYMENTS are allocated via financial items to line items. The test assumes that intended payments are allocated (ie accounts receivable transactions). If that is correct it complicates things but it also means I fundamentally don't understand what's going on / what's being achieved here & need that documented before this can be resolved. The complexity of the test is how it sets up the original transaction - it is done with some magic code that handles a part payment at the time of creating the contribution - I don't know where the UI calls it & I discourage the approach & encourage creating a pending payment and THEN adding a payment (partial or otherwise) |
CRM/Financial/BAO/Payment.php
Outdated
$entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [ | ||
'entity_table' => 'civicrm_financial_item', | ||
'entity_id' => ['IN' => array_keys($financialItems)], | ||
])['values']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eileenmcnaughton not all data stored in EntityFinancialTrxn are stored as paid some are pending incase of 'Accounts receivable' so probably you may need to join civicrm_financial_trxn table to check is_payment = 1 (i.e 'financial_trxn_id.is_payment' => 1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set limit => 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - well spotted - fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm. is_payment is also set true for refunds. I'm going to play around with output on different use cases.
CRM/Financial/BAO/Payment.php
Outdated
$financialItems = civicrm_api3('FinancialItem', 'get', [ | ||
'entity_id' => $lineItemID, | ||
'entity_table' => 'civicrm_line_item', | ||
'options' => ['sort' => 'id DESC'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set limit => 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - fixed
2d691b8
to
d902e31
Compare
@pradpnayak thanks for your input - making the change you suggested seems to have solved my updatePaymentTest problem. It DOES mean we now wind up with 4 EntityFinancialTrxns in the end rather than 3 - I'm assuming you are saying that is a good thing. If this passes tests can you or @kcristiano or @monishdeb do some tests to check the (slightly changed) results. Note the handling of refunds is unchanged and out of scope |
Jenkins test this please |
The latest patch doesn't change any behavior, and matches with my earlier test results, where it resolves the partial payment financial handling and refund is still broken. But then it's not in the current scope of this PR so I am happy with the code fix. Am afraid there will be related test failures because now we are not proportionately assigning amounts to line-items in case of partial payment. Also, it seems bookeeping report is not displaying the result correctly. @eileenmcnaughton have you checked the bookeeping report why the query is behaving strangely after this change? |
@monishdeb no -- this change DOES change something - ie I added this line https://github.com/civicrm/civicrm-core/pull/14763/files#diff-8967f6e57d6209aa277ded6512413608R613 On @pradpnayak's suggestion Neither " Am afraid there will be related test failures because now we are not proportionately assigning amounts to line-items in case of partial payment" or "Also, it seems bookeeping report is not displaying the result correctly" should be the case - can you elaborate? |
@eileenmcnaughton here's a screencast which show the financial adjustments in bookeeping report after doing partial payment. You will find Financial Type is missing against the adjusted amount $4. In addition it will also cause some issue in Financial Batch Export. @kcristiano can you please help me to provide results of batch export for partial payment? |
@monishdeb is this different to before I made that last change suggesed by Pradeep? Can you see what entry is missing / wrong to cause it? |
OK - I see that without the change recommended by @pradpnayak but not with it. However, I still see 2 test fails & am still stuck on what the logic should be. It's probably better discussed over on #15143 though as @pradpnayak has already commented there & resolving that should clarify what needs to happen here |
@eileenmcnaughton I went through the same test issues and some oddity in financial batch export result. In order to fulfill every requirement, I need to follow back the proportional line-item allocation when there is a partial or refund payment. And this PR #13114 has all the answers. I was wondering if we can use the logic in this patch to adjust the financial records. |
Gawd - I had my head around this when I did it but it's months ago now - trying to get my head into it again |
Yeah :( Should I submit a new PR where I will cherry-pick your commits and then change the code adopt the #13114 changes? which was earlier tested by @kcristiano @pradpnayak |
@kcristiano yeah keep this open a little longer. I don't like people using the PR queue as their personal to do list & keeping this open kinda flies in the face of that - but on the other hand this is still active & high priority (even if it doesn't feel like it). I'm going to try a bit more on the docs - the issue is really that with docs being poor shared understanding / expectations are poor. Unfortunately I think in some ways some community issues a couple of years dampened docs efforts & related comms & this is in some ways a fallout |
I do not want to spam here (as you want to pause this issue) but would https://lab.civicrm.org/dev/core/issues/375 be related? Also a consequence of different financial type line items. Apologies but if this is totally something different. In that case I am glad to delete this comment. |
@magnolia61 I commented at https://lab.civicrm.org/dev/core/issues/375 and will take a look, it does look similar. |
@kcristiano @JoeMurray I've added a section on financial items here civicrm/civicrm-dev-docs#719 - I think it makes them relatively clear (I hope) - it also finally documents the schema issue that has been causing us problems - I'll add a gitlab for that too |
d902e31
to
8881981
Compare
I've just rebased this - and tested the following steps as described by @monishdeb above
The outcome actually looks OK to me The code has changed quite a bit in the meantime which could explain the difference (I did also use the code functionality not the lineitemeditor to change the line item) @kcristiano my feeling is that if your testing shows this as fixing a problem without creating a new one we should merge it based on your testing. If it fixes a problem but leaves another one outstanding then we should create a follow up with instructions to replicate. This DOES lock in the fix with a test (originally written by @monishdeb ) |
@eileenmcnaughton Thanks for the work. Test failure looks related but I did a number or On the demo setup (buildkit master) i had the same results you did. I also took the patch and applied to 5.18.4 and used a DB that has rather complex events (11 different Financial Types across over 25 line items). The only issue I found is that when adding the second payment the received date changed on the original item. As many of our clients use cash basis accounting, they rely on the receive date. This is an improvement. As far as steps to replicate the change in receive date, the only difference is on the initial registration, use a date in the past and then when changing the selections use the current date. |
argh the tests - now I remember - will have to revist them |
8881981
to
4f8f2d2
Compare
After ongoing issues resolving civicrm#14763 I have concluded the underlying issue on the failing tests is the test setup. Specifically the use of 'partial_amount_to_pay' does not create the correct underlying entities - it 'sort of' creates a payment without linking it to the financial items. These parameters are part of our first attempt at partial payments. I am removing them from the test here but will deprecate later from other places in the code
After re-digging I've concluded the tests are flawed - in not using the order api & instead using the 'partial_amount_to_pay' parameter they are miscreating the initial order #15620 |
After ongoing issues resolving civicrm#14763 I have concluded the underlying issue on the failing tests is the test setup. Specifically the use of 'partial_amount_to_pay' does not create the correct underlying entities - it 'sort of' creates a payment without linking it to the financial items. These parameters are part of our first attempt at partial payments. I am removing them from the test here but will deprecate later from other places in the code
This is the other cause of failures #15621 |
Further docs updates here civicrm/civicrm-dev-docs#722 |
After ongoing issues resolving civicrm#14763 I have concluded the underlying issue on the failing tests is the test setup. Specifically the use of 'partial_amount_to_pay' does not create the correct underlying entities - it 'sort of' creates a payment without linking it to the financial items. These parameters are part of our first attempt at partial payments. I am removing them from the test here but will deprecate later from other places in the code
Cast result of getContributionBalance to float to match comment block. Without this it is possible for it to return 'null' which is not useful / in keeping. I did a bit of an audit & did not find places where this would cause an === comparison to fail
4f8f2d2
to
d985845
Compare
d985845
to
4f6e08d
Compare
OK - the current blocker on this is the fact the Update is not correctly creating the entity_financial_trxn records for the negative payment / cancel & the tests were missing that because of the way the tests were written :-( I've added #15630 which will fix the cancel action but not address the refund action as yet |
test this please |
@kcristiano so this is finally passing! I think it's OK to merge this if we figure out the remaining gaps & log issues over on financial - https://lab.civicrm.org/dev/financial/issues I think the gaps are
|
@eileenmcnaughton I agree this is ready to merge. I ran another set of tests and this is a big improvement. I agree the gaps are:
I'll open an issue with these items. Image A |
@kcristiano thanks I'll merge this & we'll look at the others - 3 in the list seems like the highest priority - can you write steps to replicate |
Steps to replicate in https://lab.civicrm.org/dev/financial/issues/94 (fixed link) |
link goes the wrong place - https://lab.civicrm.org/dev/financial/issues/94 |
Overview
Fixes the Payment.create api to correctly allocate payments to the right line items.
Note that this is not exposed in the UI without merging #14408 (which would make the additional payment form call payment.create and it does not apply to payments that update the status from Pending to Completed without this #14673
However, I think it gets us close to having the right code in this path.
Before
After
As above but only one EntityFinancialTrxn record is created when the Payment.create api is used to add the payment
Technical Details
This fix takes the approach of determining what % of the outstanding balance has been paid and then for each line item determinine the outstanding balance and multiplying it by that ration to assign 'allocation' field in the line item array. This is the amount used to allocate the line item between financial items. In the above example 100% of the balance is paid so 100%
of the line item is allocated
I don't hold any illusions this is the end of the line for locking down line item allocation. Once Merged I think we should
Comments
@monishdeb @kcristiano