From 1e3401f3f61f69b0a45886c8c2ad571f12a7bc80 Mon Sep 17 00:00:00 2001 From: zorgalina Date: Fri, 8 May 2015 22:49:05 -0700 Subject: [PATCH] CRM-16481 - Apply discounts if any; line_item is updated Because multiple events are processed and the form isn't modeled on registerEvent, it was necessary to manually invoke discount methods; normally the hook buildAmount would do it - perhaps it'd be better to rewrite this page using buildAmount. note: Line_item update has been added in this version's post_process already Conflicts: CRM/Event/Cart/Form/Checkout/Payment.php --- .../Form/Checkout/ParticipantsAndPrices.php | 15 +++- CRM/Event/Cart/Form/Checkout/Payment.php | 82 +++++++++++++++++-- CRM/Event/Cart/Form/MerParticipant.php | 2 +- 3 files changed, 87 insertions(+), 12 deletions(-) diff --git a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php index 52aabc8e6e..2de18fbccc 100644 --- a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php +++ b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php @@ -7,6 +7,9 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ public $price_fields_for_event; public $_values = NULL; + /** + * Pre process function. + */ public function preProcess() { parent::preProcess(); @@ -17,6 +20,9 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ } } + /** + * Build quick form. + */ public function buildQuickForm() { $this->price_fields_for_event = array(); foreach ($this->cart->get_main_event_participants() as $participant) { @@ -28,7 +34,6 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ } //If events in cart have discounts the textbox for discount code will be displayed at the top, as long as this //form name is added to cividiscount - //$this->addElement('text', 'discountcode', ts('If you have a discount code, enter it here')); $this->assign('events_in_carts', $this->cart->get_main_events_in_carts()); $this->assign('price_fields_for_event', $this->price_fields_for_event); $this->addButtons( @@ -52,9 +57,11 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ } /** - * @param $contact + * Get the primary emil for the contact. + * + * @param CRM_Contact_BAO_Contact $contact * - * @return null + * @return string */ public static function primary_email_from_contact($contact) { foreach ($contact->email as $email) { @@ -217,7 +224,7 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ } } } - } + } return $defaults; } diff --git a/CRM/Event/Cart/Form/Checkout/Payment.php b/CRM/Event/Cart/Form/Checkout/Payment.php index 4a61f11a24..3c8d8050cf 100644 --- a/CRM/Event/Cart/Form/Checkout/Payment.php +++ b/CRM/Event/Cart/Form/Checkout/Payment.php @@ -18,9 +18,11 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { public $pay_later_receipt; /** + * Register a participant. + * * @param array $params - * @param $participant - * @param $event + * @param CRM_Event_BAO_Participant $participant + * @param CRM_Event_BAO_Event $event * * @return mixed */ @@ -85,7 +87,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { 'contribution_id' => $params['contributionID'], ); $ids = array(); - $paymentParticpant = CRM_Event_BAO_ParticipantPayment::create($payment_params, $ids); + CRM_Event_BAO_ParticipantPayment::create($payment_params, $ids); } $transaction->commit(); @@ -128,6 +130,9 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { return $participant; } + /** + * Build payment fields. + */ public function buildPaymentFields() { $payment_processor_id = NULL; $can_pay_later = TRUE; @@ -173,6 +178,9 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { } } + /** + * Build QuickForm. + */ public function buildQuickForm() { $this->line_items = array(); @@ -235,8 +243,10 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { } /** - * @param $event_in_cart - * @param null $class + * Process line item for event. + * + * @param bool $event_in_cart + * @param string $class */ public function process_event_line_item(&$event_in_cart, $class = NULL) { $cost = 0; @@ -254,6 +264,14 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $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); + } + } // @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 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest @@ -273,8 +291,10 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { } /** - * @param $event_in_cart - * @param null $class + * Add line item. + * + * @param CRM_Event_BAO_Event $event_in_cart + * @param string $class */ public function add_line_item($event_in_cart, $class = NULL) { $amount = 0; @@ -719,4 +739,52 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { return $defaults; } + /** + * @param $discountCode + * @param array &$price_set_amount + * @param &$cost + */ + function apply_discount($discountCode, &$price_set_amount, &$cost) { + //need better way to determine if cividiscount installed + $autoDiscount = array(); + $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'"; + $dao = CRM_Core_DAO::executeQuery($sql, ''); + while ($dao->fetch()) { + if ($dao->is_active != '1') { + return; + } + } + $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids(); + $discounts = _cividiscount_get_discounts(); + foreach ($discounts as $key => $discountValue) { + if ($key == $discountCode) { + //check priceset is_active + $today = date('Y-m-d'); + $diff1 = date_diff(date_create($today), date_create($discountValue['active_on'])); + if ($diff1->days > 0) { + $active1 = TRUE; + } + if ($discountValue['expire_on'] != NULL) { + $diff2 = date_diff(date_create($today), date_create($discountValue['expire_on'])); + if ($diff2->days > 0) { + $active2 = TRUE; + } + } + else { + $active2 = TRUE; + } + } + if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == 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]; + } + } + } + } + } + } diff --git a/CRM/Event/Cart/Form/MerParticipant.php b/CRM/Event/Cart/Form/MerParticipant.php index 10eb520be6..b618e78860 100644 --- a/CRM/Event/Cart/Form/MerParticipant.php +++ b/CRM/Event/Cart/Form/MerParticipant.php @@ -46,7 +46,7 @@ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { * * @return array */ - public function get_profile_groups($event_id) { + public static function get_profile_groups($event_id) { $ufJoinParams = array( 'entity_table' => 'civicrm_event', 'module' => 'CiviEvent', -- 2.25.1