Skip to content

Commit

Permalink
CRM-14653 civicrm#1 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CiviCRM committed Jun 10, 2014
1 parent 5e80405 commit 9d49fe0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
11 changes: 4 additions & 7 deletions CRM/CiviDiscount/DiscountCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ function getDiscounts() {
if(!$this->is_display_field_mode) {
$this->filterDiscountsByContact();
}
return $this->discounts;
return $this->entity_discounts;
}

/**
* filter this discounts according to entity
*/
function filterDiscountByEntity() {
$this->setEntityDiscounts();
$this->discounts = array_intersect_key($this->discounts, $this->entity_discounts);
}

/**
Expand Down Expand Up @@ -147,7 +146,7 @@ function isShowDiscountCodeField() {
if (!$this->getEntityHasDiscounts()) {
return FALSE;
}
if(!empty($this->entity_discounts) && $this->entity_discounts != $this->discounts) {
if(!empty($this->entity_discounts)) {
return TRUE;
}
}
Expand All @@ -170,7 +169,7 @@ function isAutoDiscount() {
function setEntityDiscounts() {
$this->entity_discounts = array();
foreach ($this->discounts as $discount_id => $discount) {
if($this->checkDiscountsByEntity($discount, $this->entity, $this->entity_id, 'filters')) {
if ($this->checkDiscountsByEntity($discount, $this->entity, $this->entity_id, 'filters')) {
$this->entity_discounts[$discount_id] = $discount;
}
}
Expand All @@ -197,9 +196,7 @@ function checkDiscountsByEntity($discount, $entity, $id, $type, $additionalFilte
if(empty($discount[$type][$entity])) {
return TRUE;
}
if(array_keys($discount[$type][$entity]) == array('id')) {
return in_array($id, $discount[$type][$entity]['id']);
}

$params = $discount[$type][$entity] + array_merge(array(
'options' => array('limit' => 999999999), 'return' => 'id'
), $additionalFilter);
Expand Down
28 changes: 18 additions & 10 deletions cividiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ function cividiscount_civicrm_buildForm($fname, &$form) {
'CRM_Event_Form_Registration_Register',
//'CRM_Event_Form_Registration_AdditionalParticipant'
))) {
$discountCalculator = new CRM_CiviDiscount_DiscountCalculator('event', $form->getVar('_eventId'), NULL, NULL, TRUE);
$contact_id = _cividiscount_get_form_contact_id($form);
$discountCalculator = new CRM_CiviDiscount_DiscountCalculator('event', $form->getVar('_eventId'), $contact_id, NULL, TRUE);
$addDiscountField = $discountCalculator->isShowDiscountCodeField();

}
elseif ($fname == 'CRM_Contribute_Form_Contribution_Main') {
$ids = _cividiscount_get_discounted_membership_ids();
Expand Down Expand Up @@ -304,8 +306,8 @@ function cividiscount_civicrm_buildAmount($pagetype, &$form, &$amounts) {
$form->set('_discountInfo', NULL);
$dicountCalculater = new CRM_CiviDiscount_DiscountCalculator($pagetype, $eid, $contact_id, $code, FALSE);
$discounts = $dicountCalculater->getDiscounts();
if(!empty($code) && empty($discounts)) {
$form->set( 'discountCodeErrorMsg', ts('The discount code you entered is invalid.'));
if (!empty($code) && empty($discounts)) {
$form->set( 'discountCodeErrorMsg', ts('The discount code you entered is invalid.'));
}

if (empty($discounts)) {
Expand Down Expand Up @@ -348,12 +350,18 @@ function cividiscount_civicrm_buildAmount($pagetype, &$form, &$amounts) {
$priceFields = isset($discount['pricesets']) ? $discount['pricesets'] : array();
//@todo - check that we can still exclude building events here- the original code only did the build against
// the first discount which wasn't working
if ($pagetype != 'event' && empty($priceFields)) {
// filter only valid membership types that have discount
foreach($priceSetInfo as $pfID => $priceFieldValues) {
if (!empty($priceFieldValues['membership_type_id']) &&
in_array($priceFieldValues['membership_type_id'], CRM_Utils_Array::value('memberships', $discount, array()))) {
$priceFields[$pfID] = $pfID;
if (empty($priceFields) && !empty($code)) {
if ($pagetype == 'event') {
$applyToAllLineItems = TRUE;
$discounts[$key]['pricesets'] = array_combine(array_keys($priceSetInfo), array_keys($priceSetInfo));
}
else {
// filter only valid membership types that have discount
foreach($priceSetInfo as $pfID => $priceFieldValues) {
if (!empty($priceFieldValues['membership_type_id']) &&
in_array($priceFieldValues['membership_type_id'], CRM_Utils_Array::value('memberships', $discount, array()))) {
$priceFields[$pfID] = $pfID;
}
}
}
}
Expand All @@ -369,7 +377,7 @@ function cividiscount_civicrm_buildAmount($pagetype, &$form, &$amounts) {
}

foreach ($fee['options'] as $option_id => &$option) {
if (CRM_Utils_Array::value($option['id'], $priceFields)) {
if (!empty($applyToAllLineItems) || CRM_Utils_Array::value($option['id'], $priceFields)) {
$originalLabel = $originalAmounts[$fee_id]['options'][$option_id]['label'];
$originalAmount = (integer) $originalAmounts[$fee_id]['options'][$option_id]['amount'];
list($amount, $label) =
Expand Down

0 comments on commit 9d49fe0

Please sign in to comment.