Skip to content
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

Ensure the civicrm_financial_item records get created correctly #132

Merged

Conversation

omarabuhussein
Copy link
Contributor

@omarabuhussein omarabuhussein commented Apr 27, 2018

Before

For line items with tax amounts, two civicrm_financial_item records should be created, one for the line item total_amount and the other for the tax amount. Currently only one record for the tax amount get created.

After

IF a line item has a tax amount, then now two civicrm_financial_item records get created one for the total_amount and the other for the tax amount.

Example

If for example we configured the webform to create two memberships for contact 1, where one membership has tax on it where the other not, here is how currently it will look like in line items view page :

2018-04-27 12_42_17-pp test _ dmaster8

which is correct.

After submitting the webform, the following two line items will get created :

2018-04-27 13_48_01-_ untitled dmaster8ci_ovi8l buildkit_2 - query - navicat premium

which is also correct.

But if we look at civicrm_financial_item table then this is the only created records :

2018-04-27 13_48_24-_ untitled dmaster8ci_ovi8l buildkit_2 - query - navicat premium

as you can see only two civicrm_financial_item records are created, but 3 records should be created instead.

Here is how things looks like after applying this patch :

2018-04-27 13_53_28-_ untitled dmaster8ci_ovi8l buildkit_2 - query - navicat premium

2018-04-27 13_53_41-_ untitled dmaster8ci_ovi8l buildkit_2 - query - navicat premium

as u can see, the line items are still the same which is correct, but now 3 civicrm_financial_item records will be created.

Technical Details

This regressions was introucded in this PR : #105 , in this line https://github.com/colemanw/webform_civicrm/pull/105/files#diff-59be2e079a691088e15ab3a80e0640a0L2054

this code :

$result = CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult);
if (!is_null($this->tax_rate)) {
  $result = CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult, TRUE);
}

was replaced by this one :

$taxTrxnID = !empty($lineItemRecord->tax_amount);
CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult, $taxTrxnID);

which means if the line item has a tax amount, true will be passed as a 3rd parameter to CRM_Financial_BAO_FinancialItem::add() method which means only the tax civicrm_financial_item record will be created. I reverted the code to how it was before where there are two calls to CRM_Financial_BAO_FinancialItem::add() method, one to create the civicrm_financial_item record for the total_amount and the other for the tax amount.

@KarinG
Copy link
Collaborator

KarinG commented Apr 27, 2018

@mattwire - can you please have look?

@@ -2123,8 +2123,11 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {

$lineItemRecord = json_decode(json_encode($item), FALSE);
// Add financial_item and entity_financial_trxn
$taxTrxnID = !empty($lineItemRecord->tax_amount);
CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult, $taxTrxnID);
CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here to explain why we need to call CRM_Financial_BAO_FinancialItem::add twice (I understand now, but I didn't at the time as the code is not really clear on the CiviCRM side).

@mattwire
Copy link
Collaborator

@KarinG @omarabuhussein I've tested and confirmed that this fixes the described issue. If we can get a comment added to the changed code explaining why we have to call it twice then it's good to merge.

@KarinG
Copy link
Collaborator

KarinG commented Apr 28, 2018

Thanks Matt! Omar: if you can add that comment then I’ll merge this

@omarabuhussein omarabuhussein force-pushed the fix-finanncial-item-creations branch from 3c5b15d to 5b9d6ba Compare April 29, 2018 15:11
@omarabuhussein
Copy link
Contributor Author

Thanks @mattwire and @KarinG , I added a comment to explain that and squashed all the commits into one commit.

@KarinG
Copy link
Collaborator

KarinG commented May 1, 2018

Ok @mattwire ?

@mattwire
Copy link
Collaborator

mattwire commented May 1, 2018

@KarinG Yes, approved.

@KarinG
Copy link
Collaborator

KarinG commented May 1, 2018

Thank you all!

@KarinG KarinG merged commit 75446f2 into colemanw:7.x-4.x May 1, 2018
@omarabuhussein omarabuhussein deleted the fix-finanncial-item-creations branch May 1, 2018 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants