function getByPolicyGroupUserIdAndTypeId($user_id, $type_id, $where = NULL, $order = NULL)
    {
        if ($user_id == '') {
            return FALSE;
        }
        if ($type_id == '') {
            return FALSE;
        }
        if ($order == NULL) {
            $order = array('d.punch_type_id' => 'desc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        $pgf = new PolicyGroupFactory();
        $pguf = new PolicyGroupUserFactory();
        $cgmf = new CompanyGenericMapFactory();
        $ripf = new RoundIntervalPolicyFactory();
        $punch_type_relation_options = $ripf->getOptions('punch_type_relation');
        if (isset($punch_type_relation_options[$type_id])) {
            $punch_type_ids = $punch_type_relation_options[$type_id];
            $punch_type_ids[] = $type_id;
        } else {
            return FALSE;
        }
        $ph = array('user_id' => $user_id);
        $query = '
					select 	d.*
					from 	' . $pguf->getTable() . ' as a,
							' . $pgf->getTable() . ' as b,
							' . $cgmf->getTable() . ' as c,
							' . $this->getTable() . ' as d
					where 	a.policy_group_id = b.id
						AND ( b.id = c.object_id AND b.company_id = c.company_id AND c.object_type_id = 130 )
						AND c.map_id = d.id
						AND a.user_id = ?
						AND d.punch_type_id in ( ' . $this->getListSQL($punch_type_ids, $ph) . ')
						AND ( b.deleted = 0 AND d.deleted = 0 )
						';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict);
        $query .= ' LIMIT 1';
        $this->rs = $this->db->Execute($query, $ph);
        return $this;
    }
 function createRoundingPolicy($company_id, $type)
 {
     $ripf = new RoundIntervalPolicyFactory();
     $ripf->setCompany($company_id);
     switch ($type) {
         case 10:
             //In
             $ripf->setName('5min');
             $ripf->setPunchType(40);
             //In
             $ripf->setRoundType(30);
             //Up
             $ripf->setInterval(60 * 5);
             //5mins
             $ripf->setGrace(60 * 3);
             //3min
             $ripf->setStrict(FALSE);
             break;
         case 20:
             //Out
             $ripf->setName('5min');
             $ripf->setPunchType(50);
             //In
             $ripf->setRoundType(10);
             //Down
             $ripf->setInterval(60 * 5);
             //5mins
             $ripf->setGrace(60 * 3);
             //3min
             $ripf->setStrict(FALSE);
             break;
         case 30:
             //Day total
             $ripf->setName('15min');
             $ripf->setPunchType(120);
             //In
             $ripf->setRoundType(10);
             //Down
             $ripf->setInterval(60 * 15);
             //15mins
             $ripf->setGrace(60 * 3);
             //3min
             $ripf->setStrict(FALSE);
             break;
         case 40:
             //Lunch total
             $ripf->setName('15min');
             $ripf->setPunchType(100);
             //In
             $ripf->setRoundType(10);
             //Down
             $ripf->setInterval(60 * 15);
             //15mins
             $ripf->setGrace(60 * 3);
             //3min
             $ripf->setStrict(FALSE);
             break;
         case 50:
             //Break total
             $ripf->setName('15min');
             $ripf->setPunchType(110);
             //In
             $ripf->setRoundType(10);
             //Down
             $ripf->setInterval(60 * 15);
             //15mins
             $ripf->setGrace(60 * 3);
             //3min
             $ripf->setStrict(FALSE);
             break;
     }
     if ($ripf->isValid()) {
         $insert_id = $ripf->Save();
         Debug::Text('Rounding Policy ID: ' . $insert_id, __FILE__, __LINE__, __METHOD__, 10);
         return $insert_id;
     }
     Debug::Text('Failed Creating Rounding Policy!', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }
require_once Environment::getBasePath() . 'includes/Interface.inc.php';
if (!$permission->Check('round_policy', 'enabled') or !($permission->Check('round_policy', 'edit') or $permission->Check('round_policy', 'edit_own'))) {
    $permission->Redirect(FALSE);
    //Redirect
}
$smarty->assign('title', TTi18n::gettext($title = 'Edit Rounding Policy'));
// See index.php
/*
 * Get FORM variables
 */
extract(FormVariables::GetVariables(array('action', 'id', 'data')));
if (isset($data['interval'])) {
    $data['interval'] = TTDate::parseTimeUnit($data['interval']);
    $data['grace'] = TTDate::parseTimeUnit($data['grace']);
}
$ripf = new RoundIntervalPolicyFactory();
$action = Misc::findSubmitButton();
$action = strtolower($action);
switch ($action) {
    case 'submit':
        Debug::Text('Submit!', __FILE__, __LINE__, __METHOD__, 10);
        $ripf->setId($data['id']);
        $ripf->setCompany($current_company->getId());
        $ripf->setName($data['name']);
        $ripf->setPunchType($data['punch_type_id']);
        $ripf->setRoundType($data['round_type_id']);
        $ripf->setInterval($data['interval']);
        $ripf->setGrace($data['grace']);
        if (isset($data['strict'])) {
            $ripf->setStrict(TRUE);
        } else {