/**
 * Implementation of hook_civicrm_postProcess()
 *
 * Record information about a discount use.
 */
function cividiscount_civicrm_postProcess($class, &$form)
{
    if (!in_array($class, array('CRM_Contribute_Form_Contribution_Confirm', 'CRM_Event_Form_Participant', 'CRM_Event_Form_Registration_Confirm', 'CRM_Member_Form_Membership', 'CRM_Member_Form_MembershipRenewal', 'CRM_Event_Form_ParticipantFeeSelection'))) {
        return;
    }
    $discountInfo = $form->get('_discountInfo');
    if (!$discountInfo) {
        return;
    }
    $discount = $discountInfo['discount'];
    $params = $form->getVar('_params');
    $term = $discount['discount_term'];
    $discountParams = array('item_id' => $discount['id'], 'description' => CRM_Utils_Array::value('amount_level', $params) . " " . CRM_Utils_Array::value('description', $params), 'contribution_id' => CRM_Utils_Array::value('contributionID', $params));
    // Online event registration.
    // Note that CRM_Event_Form_Registration_Register is an intermediate form.
    // CRM_Event_Form_Registration_Confirm completes the transaction.
    if ($class == 'CRM_Event_Form_Registration_Confirm') {
        _cividiscount_consume_discount_code_for_online_event($form->getVar('_participantIDS'), $discountParams);
    } elseif ($class == 'CRM_Contribute_Form_Contribution_Confirm') {
        // Note that CRM_Contribute_Form_Contribution_Main is an intermediate form.
        // CRM_Contribute_Form_Contribution_Confirm completes the transaction.
        _cividiscount_consume_discount_code_for_online_contribution($params, $discountParams, $discount['memberships']);
        // set end date for discounted online membership signups
        $membershipID = $params['membershipID'];
        _cividiscount_set_end_date_for_membership($membershipID, $term);
    } else {
        $contribution_id = NULL;
        // Offline event registration.
        if (in_array($class, array('CRM_Event_Form_Participant', 'CRM_Event_Form_ParticipantFeeSelection'))) {
            if ($class == 'CRM_Event_Form_ParticipantFeeSelection') {
                $discountParams['entity_id'] = $entity_id = $form->getVar('_participantId');
            } else {
                $discountParams['entity_id'] = $entity_id = $form->getVar('_id');
            }
            $participant_payment = _cividiscount_get_participant_payment($entity_id);
            $discountParams['contribution_id'] = $participant_payment['contribution_id'];
            $discountParams['entity_table'] = 'civicrm_participant';
            $participant = _cividiscount_get_participant($entity_id);
            $discountParams['contact_id'] = $participant['contact_id'];
        } elseif (in_array($class, array('CRM_Member_Form_Membership', 'CRM_Member_Form_MembershipRenewal'))) {
            // @todo check whether this uses price sets in submit & hence can use same
            // code as the online section and test whether code is decremented when a price set is used.
            $membership_types = $form->getVar('_memTypeSelected');
            $membership_type = isset($membership_types[0]) ? $membership_types[0] : NULL;
            if (!$membership_type) {
                $membership_type = $form->getVar('_memType');
            }
            // Check to make sure the discount actually applied to this membership.
            if (!CRM_Utils_Array::value($membership_type, $discount['memberships'])) {
                return;
            }
            $discountParams['entity_table'] = 'civicrm_membership';
            $discountParams['entity_id'] = $entity_id = $form->getVar('_id');
            $membership_payment = _cividiscount_get_membership_payment($entity_id);
            $discountParams['contribution_id'] = $membership_payment['contribution_id'];
            $membership = _cividiscount_get_membership($entity_id);
            $discountParams['contact_id'] = $membership['contact_id'];
            // set end date for discounted offline membership signups
            $membershipID = $discountParams['entity_id'];
            _cividiscount_set_end_date_for_membership($membershipID, $term);
        } else {
            $discountParams['entity_table'] = 'civicrm_contribution';
            $discountParams['entity_id'] = $contribution_id;
        }
        civicrm_api3('DiscountTrack', 'create', $discountParams);
    }
}
/**
 * Implementation of hook_civicrm_postProcess()
 *
 * Record information about a discount use.
 */
function cividiscount_civicrm_postProcess($class, &$form)
{
    if (!in_array($class, array('CRM_Contribute_Form_Contribution_Confirm', 'CRM_Event_Form_Participant', 'CRM_Event_Form_Registration_Confirm', 'CRM_Member_Form_Membership', 'CRM_Member_Form_MembershipRenewal', 'CRM_Event_Form_ParticipantFeeSelection'))) {
        return;
    }
    $discountInfo = $form->get('_discountInfo');
    if (!$discountInfo) {
        return;
    }
    $ts = CRM_Utils_Time::getTime();
    $discount = $discountInfo['discount'];
    $params = $form->getVar('_params');
    $description = CRM_Utils_Array::value('amount_level', $params);
    // Online event registration.
    // Note that CRM_Event_Form_Registration_Register is an intermediate form.
    // CRM_Event_Form_Registration_Confirm completes the transaction.
    if ($class == 'CRM_Event_Form_Registration_Confirm') {
        $pids = $form->getVar('_participantIDS');
        // if multiple participant discount is not enabled then only use primary participant info for discount
        // and ignore additional participants
        if (!_cividiscount_allow_multiple()) {
            $pids = array($pids[0]);
        }
        foreach ($pids as $pid) {
            $participant = _cividiscount_get_participant($pid);
            $contact_id = $participant['contact_id'];
            $participant_payment = _cividiscount_get_participant_payment($pid);
            $contribution_id = $participant_payment['contribution_id'];
            CRM_CiviDiscount_BAO_Item::incrementUsage($discount['id']);
            $track = new CRM_CiviDiscount_DAO_Track();
            $track->item_id = $discount['id'];
            $track->contact_id = $contact_id;
            $track->contribution_id = $contribution_id;
            $track->entity_table = 'civicrm_participant';
            $track->entity_id = $pid;
            $track->used_date = $ts;
            $track->description = $description;
            $track->save();
        }
        // Online membership.
        // Note that CRM_Contribute_Form_Contribution_Main is an intermediate
        // form - CRM_Contribute_Form_Contribution_Confirm completes the
        // transaction.
    } else {
        if ($class == 'CRM_Contribute_Form_Contribution_Confirm') {
            $membership_type = $params['selectMembership'];
            $membershipId = $params['membershipID'];
            if (!is_array($membership_type)) {
                $membership_type = array($membership_type);
            }
            $discount_membership_matches = array_intersect($membership_type, $discount['memberships']);
            // check to make sure the discount actually applied to this membership.
            if (empty($discount_membership_matches) || !$membershipId) {
                return;
            }
            $description = CRM_Utils_Array::value('description', $params);
            $membership = _cividiscount_get_membership($membershipId);
            $contact_id = $membership['contact_id'];
            $membership_payment = _cividiscount_get_membership_payment($membershipId);
            $contribution_id = $membership_payment['contribution_id'];
            CRM_CiviDiscount_BAO_Item::incrementUsage($discount['id']);
            $track = new CRM_CiviDiscount_DAO_Track();
            $track->item_id = $discount['id'];
            $track->contact_id = $contact_id;
            $track->contribution_id = $contribution_id;
            $track->entity_table = 'civicrm_membership';
            $track->entity_id = $membershipId;
            $track->used_date = $ts;
            $track->description = $description;
            $track->save();
        } else {
            $contribution_id = NULL;
            // Offline event registration.
            if (in_array($class, array('CRM_Event_Form_Participant', 'CRM_Event_Form_ParticipantFeeSelection'))) {
                if ($class == 'CRM_Event_Form_ParticipantFeeSelection') {
                    $entity_id = $form->getVar('_participantId');
                } else {
                    $entity_id = $form->getVar('_id');
                }
                $participant_payment = _cividiscount_get_participant_payment($entity_id);
                $contribution_id = $participant_payment['contribution_id'];
                $entity_table = 'civicrm_participant';
                $participant = _cividiscount_get_participant($entity_id);
                $contact_id = $participant['contact_id'];
            } elseif (in_array($class, array('CRM_Member_Form_Membership', 'CRM_Member_Form_MembershipRenewal'))) {
                $membership_types = $form->getVar('_memTypeSelected');
                $membership_type = isset($membership_types[0]) ? $membership_types[0] : NULL;
                if (!$membership_type) {
                    $membership_type = $form->getVar('_memType');
                }
                // Check to make sure the discount actually applied to this membership.
                if (!CRM_Utils_Array::value($membership_type, $discount['memberships'])) {
                    return;
                }
                $entity_table = 'civicrm_membership';
                $entity_id = $form->getVar('_id');
                $membership_payment = _cividiscount_get_membership_payment($entity_id);
                $contribution_id = $membership_payment['contribution_id'];
                $description = CRM_Utils_Array::value('description', $params);
                $membership = _cividiscount_get_membership($entity_id);
                $contact_id = $membership['contact_id'];
            } else {
                $entity_table = 'civicrm_contribution';
                $entity_id = $contribution_id;
            }
            CRM_CiviDiscount_BAO_Item::incrementUsage($discount['id']);
            $track = new CRM_CiviDiscount_DAO_Track();
            $track->item_id = $discount['id'];
            $track->contact_id = $contact_id;
            $track->contribution_id = $contribution_id;
            $track->entity_table = $entity_table;
            $track->entity_id = $entity_id;
            $track->used_date = $ts;
            $track->description = $description;
            $track->save();
        }
    }
}