CRM-16481
authorzorgalina <zorgalina@gmail.com>
Sat, 25 Jun 2016 07:05:06 +0000 (00:05 -0700)
committerzorgalina <zorgalina@gmail.com>
Sat, 25 Jun 2016 07:05:06 +0000 (00:05 -0700)
CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php
CRM/Event/Cart/Form/Checkout/Payment.php

index 536f9f7cadfad4eb66e0a779d5e6ae7ddc59ca4b..ffe3b7f56386f3bde5a844022be91b63bac1ac29 100644 (file)
@@ -208,6 +208,7 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_
       $defaults += $form->setDefaultValues();
       //Set price defaults if any
       foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
+        $event_id = $event_in_cart->event_id;
         $price_set_id = CRM_Event_BAO_Event::usesPriceSet($event_in_cart->event_id);
         if ($price_set_id) {
           $price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE, TRUE);
index a41bec3ad4fa72a92f79bf4ac6b541abd1b87564..61d997d4bc9de644e3a62d163857c45804eb96b5 100644 (file)
@@ -263,14 +263,14 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       $price_set = $price_sets[$price_set_id];
       $price_set_amount = array();
       CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount);
-      $cost = $event_price_values['amount'];
-
       $discountCode = $this->_price_values['discountcode'];
       if (!empty($discountCode)) {
-        $discounted_event_ids = _cividiscount_get_discounted_event_ids();
-        if (in_array($event_in_cart->event_id, $discounted_event_ids)) {
-          $this->apply_discount($discountCode, $price_set_amount, $cost);
+        $ret=$this->apply_discount($discountCode, $price_set_amount, $cost, $event_in_cart->event_id);
+        if ($ret == FALSE) {
+          $cost = $event_price_values['amount'];
         }
+      } else {
+        $cost = $event_price_values['amount'];
       }
       // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
       // function to get correct amount level consistently. Remove setting of the amount level in
@@ -744,7 +744,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
    * @param array &$price_set_amount
    * @param &$cost
    */
-  protected function apply_discount($discountCode, &$price_set_amount, &$cost) {
+  protected function apply_discount($discountCode, &$price_set_amount, &$cost, &$event_id) {
     //need better way to determine if cividiscount installed
     $autoDiscount = array();
     $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'";
@@ -756,12 +756,23 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     }
     $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids();
     $discounts = _cividiscount_get_discounts();
+    $events = array();
+    $stat = FALSE;
     foreach ($discounts as $key => $discountValue) {
       if ($key == $discountCode) {
+        $events = CRM_Utils_Array::value('events',$discountValue);
+        $evt_ids = implode(",",$events);
+        if ($evt_ids == "0" || strpos($evt_ids, $event_id)){
+          $event_match = TRUE;
+        }
         //check priceset is_active
-        $today = date('Y-m-d');
-        $diff1 = date_diff(date_create($today), date_create($discountValue['active_on']));
-        if ($diff1->days > 0) {
+        if ($discountValue['active_on'] != NULL) {
+          $today = date('Y-m-d');
+          $diff1 = date_diff(date_create($today), date_create($discountValue['active_on']));
+          if ($diff1->days > 0) {
+            $active1 = TRUE;
+         }
+        } else {
           $active1 = TRUE;
         }
         if ($discountValue['expire_on'] != NULL) {
@@ -774,17 +785,20 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
           $active2 = TRUE;
         }
       }
-      if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == TRUE) {
+      if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == TRUE && $event_match == TRUE) {
         foreach ($price_set_amount as $key => $price) {
           if (array_search($price['price_field_value_id'], $discounted_priceset_ids) != NULL) {
             $discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, "USD");
             $price_set_amount[$key]['line_total'] = $discounted[0];
             $cost += $discounted[0];
             $price_set_amount[$key]['label'] = $discounted[1];
+         } else {
+            $cost += $price['line_total'];
           }
         }
+        $stat = TRUE;
       }
     }
+    return $stat;
   }
-
 }