/**
  * Add the membership log record.
  *
  * @param array $params
  *   Values to use in create.
  *
  * @return CRM_CiviDiscount_DAO_Track
  */
 public static function create($params)
 {
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'DiscountTrack', CRM_Utils_Array::value('id', $params), $params);
     $dao = new CRM_CiviDiscount_DAO_Track();
     $dao->copyValues($params);
     $dao->save();
     if ($hook == 'create') {
         CRM_CiviDiscount_BAO_Item::incrementUsage($dao->item_id);
     }
     CRM_Utils_Hook::post($hook, 'DiscountTrack', $dao->id, $dao);
     return $dao;
 }
/**
 * 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();
        }
    }
}