Fixed warning of Division by zero in CiviReport
authorpratikshad <pratiksha.dubey@webaccess.co.in>
Fri, 16 Jan 2015 10:37:40 +0000 (16:07 +0530)
committerpratikshad <pratiksha.dubey@webaccess.co.in>
Fri, 16 Jan 2015 10:37:40 +0000 (16:07 +0530)
CRM/Report/Form/Contribute/Repeat.php

index 7d07560621757db94cc3e8b70a436d6eab47160a..6a5782519bea7a988e243983754725342d4aa7cb 100644 (file)
@@ -621,11 +621,11 @@ LEFT JOIN civicrm_temp_civireport_repeat2
     }
 
     $total_distinct_contacts = count($contact_sums);
-    $number_maintained = 0;
-    $number_upgraded = 0;
-    $number_downgraded = 0;
-    $number_new = 0;
-    $number_lapsed = 0;
+    $maintained = 0;
+    $upgraded = 0;
+    $downgraded = 0;
+    $new = 0;
+    $lapsed = 0;
 
     foreach ($contact_sums as $uid => $row) {
       if ($row['contribution1_total_amount_sum'] &&
@@ -634,49 +634,50 @@ LEFT JOIN civicrm_temp_civireport_repeat2
         $change = ($row['contribution1_total_amount_sum'] -
           $row['contribution2_total_amount_sum']);
         if ($change == 0) {
-          $number_maintained += 1;
+          $maintained += 1;
         }
         elseif ($change > 0) {
-          $number_upgraded += 1;
+          $upgraded += 1;
         }
         elseif ($change < 0) {
-          $number_downgraded += 1;
+          $downgraded += 1;
         }
       }
       elseif ($row['contribution1_total_amount_sum']) {
-        $number_new += 1;
+        $new += 1;
       }
       elseif ($row['contribution2_total_amount_sum']) {
-        $number_lapsed += 1;
+        $lapsed += 1;
       }
     }
 
     //calculate percentages from numbers
-    $percent_maintained = ($number_maintained / $total_distinct_contacts) * 100;
-    $percent_upgraded = ($number_upgraded / $total_distinct_contacts) * 100;
-    $percent_downgraded = ($number_downgraded / $total_distinct_contacts) * 100;
-    $percent_new = ($number_new / $total_distinct_contacts) * 100;
-    $percent_lapsed = ($number_lapsed / $total_distinct_contacts) * 100;
-
+    if (!empty($total_distinct_contacts)) {
+      $maintained = ($maintained / $total_distinct_contacts) * 100;
+      $upgraded = ($upgraded / $total_distinct_contacts) * 100;
+      $downgraded = ($downgraded / $total_distinct_contacts) * 100;
+      $new = ($new / $total_distinct_contacts) * 100;
+      $lapsed = ($lapsed / $total_distinct_contacts) * 100;
+    }
     //display percentages for new, lapsed, upgraded, downgraded, and maintained contributors
     $statistics['counts']['count_new'] = array(
-      'value' => $percent_new,
+      'value' => $new,
       'title' => '% New Donors',
     );
     $statistics['counts']['count_lapsed'] = array(
-      'value' => $percent_lapsed,
+      'value' => $lapsed,
       'title' => '% Lapsed Donors',
     );
     $statistics['counts']['count_upgraded'] = array(
-      'value' => $percent_upgraded,
+      'value' => $upgraded,
       'title' => '% Upgraded Donors',
     );
     $statistics['counts']['count_downgraded'] = array(
-      'value' => $percent_downgraded,
+      'value' => $downgraded,
       'title' => '% Downgraded Donors',
     );
     $statistics['counts']['count_maintained'] = array(
-      'value' => $percent_maintained,
+      'value' => $maintained,
       'title' => '% Maintained Donors',
     );