function preProcess()
 {
     $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE);
     $oid = CRM_Utils_Request::retrieve('oid', 'Positive', $this, FALSE);
     if ($oid) {
         $this->_id = CRM_Utils_Request::retrieve('oid', 'Positive', $this, FALSE);
     } else {
         $this->assign('hide_contact', TRUE);
         $this->_id = $cid;
     }
     if (!CRM_Utils_Rule::positiveInteger($this->_id)) {
         CRM_Core_Error::fatal('We need a valid discount ID for view');
     }
     $this->assign('id', $this->_id);
     $defaults = array();
     $params = array('id' => $this->_id);
     require_once 'CRM/CiviDiscount/BAO/Item.php';
     CRM_CiviDiscount_BAO_Item::retrieve($params, $defaults);
     require_once 'CRM/CiviDiscount/BAO/Track.php';
     if ($cid) {
         $rows = CRM_CiviDiscount_BAO_Track::getUsageByContact($this->_id);
     } else {
         $rows = CRM_CiviDiscount_BAO_Track::getUsageByOrg($this->_id);
     }
     $this->assign('rows', $rows);
     $this->assign('code_details', $defaults);
     $this->ajaxResponse['tabCount'] = count($rows);
     if (!empty($defaults['code'])) {
         CRM_Utils_System::setTitle($defaults['code']);
     }
 }
/**
 * For participant and member delete, decrement the code usage value since
 * they are no longer using the code.
 * When a contact is deleted, we should also delete their tracking info/usage.
 * When removing participant (and additional) from events, also delete their tracking info/usage.
 */
function cividiscount_civicrm_pre($op, $name, $id, &$obj)
{
    if ($op == 'delete') {
        if (in_array($name, array('Individual', 'Household', 'Organization'))) {
            $result = _cividiscount_get_item_by_track(null, null, $id);
        } elseif ($name == 'Participant') {
            if (($result = _cividiscount_get_participant($id)) && ($contactid = $result['contact_id'])) {
                $result = _cividiscount_get_item_by_track('civicrm_participant', $id, $contactid);
            }
        } else {
            if ($name == 'Membership') {
                if (($result = _cividiscount_get_membership($id)) && ($contactid = $result['contact_id'])) {
                    $result = _cividiscount_get_item_by_track('civicrm_membership', $id, $contactid);
                }
            } else {
                return false;
            }
        }
        if (!empty($result)) {
            foreach ($result as $value) {
                if (!empty($value['item_id'])) {
                    CRM_CiviDiscount_BAO_Item::decrementUsage($value['item_id']);
                }
                if (!empty($value['id'])) {
                    CRM_CiviDiscount_BAO_Track::del($value['id']);
                }
            }
        }
    }
}
 static function getUsageByCode($id)
 {
     return CRM_CiviDiscount_BAO_Track::getUsage($id, NULL, NULL);
 }
 function preProcess()
 {
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
     require_once 'CRM/Utils/Rule.php';
     if (!CRM_Utils_Rule::positiveInteger($this->_id)) {
         CRM_Core_Error::fatal(ts('We need a valid discount ID for view'));
     }
     $this->assign('id', $this->_id);
     $defaults = array();
     $params = array('id' => $this->_id);
     require_once 'CRM/CiviDiscount/BAO/Item.php';
     CRM_CiviDiscount_BAO_Item::retrieve($params, $defaults);
     require_once 'CRM/CiviDiscount/BAO/Track.php';
     $rows = CRM_CiviDiscount_BAO_Track::getUsageByCode($this->_id);
     $this->assign('rows', $rows);
     $this->assign('code_details', $defaults);
     CRM_Utils_System::setTitle($defaults['code']);
 }