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

CRM-21445 Fix divide by zero with contribution page discount #11292

Closed

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Nov 17, 2017

Overview

When you apply a 100% discount you cannot submit the contribution page:
Fix divide by zero when amount is 0.




@eileenmcnaughton
Copy link
Contributor

test this please

@eileenmcnaughton
Copy link
Contributor

I'm clear that the first part of this (the changes to CRM/Contribute/BAO/Contribution.php) make sense as it adds a conditional around a piece of code that would be attempting to divide by zero if the conditional were false. I need to spend a little more time looking at the latter one

@eileenmcnaughton
Copy link
Contributor

@mattwire I just did the following test on master

  1. install civi discount
  2. set up a discount code that gives 100% discount on General membership
  3. edit default membership form to only have pay later option
  4. register using my 100% discount code.

The only error I observed was a notice about function signature on getCurrency (which I will fix).

Can you recheck?

@mattwire mattwire force-pushed the CRM-21446_contribution_page_crashes branch 2 times, most recently from a3ea2e2 to a87fb55 Compare December 2, 2017 11:52
@mattwire
Copy link
Contributor Author

mattwire commented Dec 2, 2017

@eileenmcnaughton Definitely still an issue for me. Testing again here I think you need to have more than one payment processor enabled on the contribution page to see the error. I've just updated the patch for latest master which removes a couple of lines as #11286 is now merged.

@mattwire
Copy link
Contributor Author

mattwire commented Dec 2, 2017

jenkins test this please

@eileenmcnaughton
Copy link
Contributor

test this please

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Dec 2, 2017

OK - now I have replicated it - I found that the code you are removing is to cope with the situation where there is a separate membership payment configured. I think it's required when contribution = 0 & membership has a value or vise versa. I think something like this might work - although have got myself in a muddle as to replication so will walk away for a bit

--- a/CRM/Contribute/Form/Contribution/Main.php
+++ b/CRM/Contribute/Form/Contribution/Main.php
@@ -1211,6 +1211,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     else {
       $this->set('amount', $params['amount']);
     }
+    $this->set('form_total_amount', $params['amount'] + $params['separate_amount']);
 
     // generate and set an invoiceID for this transaction
     $invoiceID = md5(uniqid(rand(), TRUE));
diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php
index 3328b28..7ff67ed 100644
--- a/CRM/Contribute/Form/ContributionBase.php
+++ b/CRM/Contribute/Form/ContributionBase.php
@@ -464,6 +464,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->_defaults = array();
 
     $this->_amount = $this->get('amount');
+    $this->formTotalAmount = $this->get('form_total_amount');
 
     //CRM-6907
     $config = CRM_Core_Config::singleton();
@@ -566,15 +567,9 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
     //fix for CRM-3767
     $isMonetary = FALSE;
-    if ($this->_amount > 0.0) {
+    if ($this->formTotalAmount > 0.0) {
       $isMonetary = TRUE;
     }
-    elseif (!empty($this->_params['selectMembership'])) {
-      $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
-      if ($memFee > 0.0) {
-        $isMonetary = TRUE;
-      }
-    }

@eileenmcnaughton
Copy link
Contributor

Just following on from this - I feel like maybe rather than formTotalAmount it should be $form->amounts = array('total_amount' => x, 'first_payment_amount' => y, 'second_payment_amount' => z) or similar

@eileenmcnaughton
Copy link
Contributor

@mattwire I'm going to put up an alternative for the changes to CRM/Contribute/Form/ContributionBase.php but I'd like to merge the changes to BAO_Contribution since they make sense (if I can get a clean PR on those)

@mattwire mattwire force-pushed the CRM-21446_contribution_page_crashes branch from a87fb55 to 8dc9d46 Compare January 19, 2018 05:23
@mattwire mattwire changed the title WIP CRM-21446 contribution page crashes CRM-21446 contribution page crashes Jan 19, 2018
@mattwire
Copy link
Contributor Author

Hey @eileenmcnaughton I've just pushed an updated PR that just has the changes to BAO_Contribution

@eileenmcnaughton
Copy link
Contributor

Thanks @mattwire - can you update the PR description?

@mattwire mattwire changed the title CRM-21446 contribution page crashes CRM-21446 Fix divide by zero with contribution page discount Jan 20, 2018
@mattwire mattwire changed the title CRM-21446 Fix divide by zero with contribution page discount CRM-21436 Fix divide by zero with contribution page discount Jan 20, 2018
@mattwire mattwire changed the title CRM-21436 Fix divide by zero with contribution page discount CRM-21445 Fix divide by zero with contribution page discount Jan 20, 2018
@mattwire
Copy link
Contributor Author

@eileenmcnaughton PR description updated

@mattwire
Copy link
Contributor Author

Jenkins test this please

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Jan 20, 2018

Thanks @mattwire -the jenkins error is

PHP Files:

  • CRM/Contribute/BAO/Contribution.php
    ===============================[ php -l ]===============================
    PHP Parse error: syntax error, unexpected '(float)' (double) (T_DOUBLE_CAST) in CRM/Contribute/BAO/Contribution.php on line 5694
    Errors parsing CRM/Contribute/BAO/Contribution.php
    ===============================[ phpcs ]===============================

Maybe casting first & then the if will cheer it up?

$eftParams['amount'] = CRM_Contribute_BAO_Contribution_Utils::formatAmount($paid);
civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
$contributionTotalAmount = (float) $entityParams['contribution_total_amount'];
if (!empty($contributionTotalAmount)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@mattwire we need to create proportional entry in civicrm_entity_financial_trxn table even though the contribution total is 0.00 with civicrm_entity_financial_trxn.amount as 0.00. If there are no such entries then there would be problem while assigning transactions in financial batch or hopefully in quickbook reports as this transaction won't be listed because of missing entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pradnayak Ok. Happy to go with your PR if that's the case

@pradpnayak
Copy link
Contributor

@pradpnayak
Copy link
Contributor

I closed mine :(
Should i reopen it?

@mattwire
Copy link
Contributor Author

@pradpnayak Yes please. as yours keeps the financial_trxn stuff in

@eileenmcnaughton
Copy link
Contributor

@mattwire @pradpnayak so we are closing this one?

@mattwire
Copy link
Contributor Author

mattwire commented Feb 2, 2018

Closing in favour of #11601

@mattwire mattwire closed this Feb 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants