function getByCompanyIDAndStationIDAndStatusAndEmployeeNumber($company_id, $station_id, $status_id, $employee_number, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if ($station_id == '') {
            return FALSE;
        }
        if ($status_id == '') {
            return FALSE;
        }
        if ($order == NULL) {
            $order = array('a.id' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        $sf = new StationFactory();
        $sugf = new StationUserGroupFactory();
        $sbf = new StationBranchFactory();
        $sdf = new StationDepartmentFactory();
        $siuf = new StationIncludeUserFactory();
        $seuf = new StationExcludeUserFactory();
        $uif = new UserIdentificationFactory();
        $ph = array('company_id' => $company_id, 'station_id' => $station_id, 'status_id' => $status_id, 'employee_number' => $employee_number);
        //Also include users with user_identifcation rows that have been *created* after the given date
        //so the first supervisor/admin enrolled on a timeclock is properly updated to lock the menu.
        $query = '
					select 	a.*
					from 	' . $this->getTable() . ' as a

					LEFT JOIN ' . $sf->getTable() . ' as z ON (1=1)
					where	a.company_id = ?
						AND z.id = ?
						AND a.status_id = ?
						AND a.employee_number = ?
						AND
							(
								(
									(
										(
											(
												z.user_group_selection_type_id = 10
													OR ( z.user_group_selection_type_id = 20 AND a.group_id in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE z.id = b.station_id ) )
													OR ( z.user_group_selection_type_id = 30 AND a.group_id not in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE z.id = b.station_id ) )
											)
											AND
											(
												z.branch_selection_type_id = 10
													OR ( z.branch_selection_type_id = 20 AND a.default_branch_id in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE z.id = c.station_id ) )
													OR ( z.branch_selection_type_id = 30 AND a.default_branch_id not in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE z.id = c.station_id ) )
											)
											AND
											(
												z.department_selection_type_id = 10
													OR ( z.department_selection_type_id = 20 AND a.default_department_id in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE z.id = d.station_id ) )
													OR ( z.department_selection_type_id = 30 AND a.default_department_id not in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE z.id = d.station_id ) )
											)
											AND a.id not in ( select f.user_id from ' . $seuf->getTable() . ' as f WHERE z.id = f.station_id )
										)
										OR a.id in ( select e.user_id from ' . $siuf->getTable() . ' as e WHERE z.id = e.station_id )
									)
								)
							)
							';
        $query .= '	AND ( a.deleted = 0 AND z.deleted = 0 )';
        //Debug::Arr($ph, 'Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }
    function getByCompanyIDAndStationIDAndStatusAndDate($company_id, $station_id, $status_id, $date = NULL, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if ($station_id == '') {
            return FALSE;
        }
        if ($status_id == '') {
            return FALSE;
        }
        if ($date == '') {
            $date = 0;
        }
        if ($order == NULL) {
            $order = array('a.last_name' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        $sf = new StationFactory();
        $sugf = new StationUserGroupFactory();
        $sbf = new StationBranchFactory();
        $sdf = new StationDepartmentFactory();
        $siuf = new StationIncludeUserFactory();
        $seuf = new StationExcludeUserFactory();
        $ph = array('company_id' => $company_id, 'station_id' => $station_id, 'status_id' => $status_id, 'date' => $date, 'date2' => $date);
        $query = '
					select 	a.*
					from 	' . $this->getTable() . ' as a,
							' . $sf->getTable() . ' as z
					where	a.company_id = ?
						AND z.id = ?
						AND a.status_id = ?
						AND
							(
								(
									(
										z.user_group_selection_type_id = 10
											OR ( z.user_group_selection_type_id = 20 AND a.group_id in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE z.id = b.station_id ) )
											OR ( z.user_group_selection_type_id = 30 AND a.group_id not in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE z.id = b.station_id ) )
									)
									AND
									(
										z.branch_selection_type_id = 10
											OR ( z.branch_selection_type_id = 20 AND a.default_branch_id in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE z.id = c.station_id ) )
											OR ( z.branch_selection_type_id = 30 AND a.default_branch_id not in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE z.id = c.station_id ) )
									)
									AND
									(
										z.department_selection_type_id = 10
											OR ( z.department_selection_type_id = 20 AND a.default_department_id in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE z.id = d.station_id ) )
											OR ( z.department_selection_type_id = 30 AND a.default_department_id not in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE z.id = d.station_id ) )
									)
									AND a.id not in ( select f.user_id from ' . $seuf->getTable() . ' as f WHERE z.id = f.station_id )
								)
								OR a.id in ( select e.user_id from ' . $siuf->getTable() . ' as e WHERE z.id = e.station_id )
							)
						AND	( a.created_date >= ? OR a.updated_date >= ? )
						AND ( a.deleted = 0 AND z.deleted = 0 )';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict);
        //Debug::text('Query: '. $query, __FILE__, __LINE__, __METHOD__, 10);
        if ($limit == NULL) {
            $this->rs = $this->db->Execute($query, $ph);
        } else {
            $this->rs = $this->db->PageExecute($query, $limit, $page, $ph);
        }
        return $this;
    }
    function getByUserIdAndStatusAndType($user_id, $status, $type, $order = NULL)
    {
        if ($user_id == '') {
            return FALSE;
        }
        if ($status == '') {
            return FALSE;
        }
        if ($type == '') {
            return FALSE;
        }
        $status_key = Option::getByValue($status, $this->getOptions('status'));
        if ($status_key !== FALSE) {
            $status = $status_key;
        }
        $type_key = Option::getByValue($type, $this->getOptions('type'));
        if ($type_key !== FALSE) {
            $type = $type_key;
        }
        $ulf = new UserListFactory();
        $ulf->getById($user_id);
        if ($ulf->getRecordCount() != 1) {
            return FALSE;
        }
        $sugf = new StationUserGroupFactory();
        $sbf = new StationBranchFactory();
        $sdf = new StationDepartmentFactory();
        $siuf = new StationIncludeUserFactory();
        $seuf = new StationExcludeUserFactory();
        $uf = new UserFactory();
        $ph = array('user_id_a' => $user_id, 'company_id' => $ulf->getCurrent()->getCompany(), 'status' => $status, 'type' => $type);
        $query = '
					select 	a.*
					from 	' . $this->getTable() . ' as a
						LEFT JOIN ' . $uf->getTable() . ' as z ON z.id = ?
					where a.company_id = ?
						AND a.status_id = ?
						AND a.type_id = ?
						AND
							(
								(
									(
										a.user_group_selection_type_id = 10
											OR ( a.user_group_selection_type_id = 20 AND z.group_id in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE a.id = b.station_id ) )
											OR ( a.user_group_selection_type_id = 30 AND z.group_id not in ( select b.group_id from ' . $sugf->getTable() . ' as b WHERE a.id = b.station_id ) )
									)
									AND
									(
										a.branch_selection_type_id = 10
											OR ( a.branch_selection_type_id = 20 AND z.default_branch_id in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE a.id = c.station_id ) )
											OR ( a.branch_selection_type_id = 30 AND z.default_branch_id not in ( select c.branch_id from ' . $sbf->getTable() . ' as c WHERE a.id = c.station_id ) )
									)
									AND
									(
										a.department_selection_type_id = 10
											OR ( a.department_selection_type_id = 20 AND z.default_department_id in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE a.id = d.station_id ) )
											OR ( a.department_selection_type_id = 30 AND z.default_department_id not in ( select d.department_id from ' . $sdf->getTable() . ' as d WHERE a.id = d.station_id ) )
									)
									AND z.id not in ( select f.user_id from ' . $seuf->getTable() . ' as f WHERE a.id = f.station_id )
								)
								OR z.id in ( select e.user_id from ' . $siuf->getTable() . ' as e WHERE a.id = e.station_id )
							)
						AND ( a.deleted = 0 AND z.deleted = 0 )
						ORDER BY lower(a.source) = \'any\' desc, lower(station_id) = \'any\' desc
						';
        //Try to order the SQL query to hit wildcard stations first.
        //$query .= $this->getSortSQL( $order, $strict );
        //Debug::text('Query: '. $query, __FILE__, __LINE__, __METHOD__, 10);
        //Debug::arr($ph, 'PH: ', __FILE__, __LINE__, __METHOD__, 10);
        $this->rs = $this->db->Execute($query, $ph);
        return $this;
    }
 function setIncludeUser($ids)
 {
     Debug::text('Setting IDs...', __FILE__, __LINE__, __METHOD__, 10);
     if (is_array($ids)) {
         $tmp_ids = array();
         if (!$this->isNew()) {
             //If needed, delete mappings first.
             $lf_a = new StationIncludeUserListFactory();
             $lf_a->getByStationId($this->getId());
             foreach ($lf_a as $obj) {
                 $id = $obj->getIncludeUser();
                 Debug::text('IncludeUser ID: ' . $obj->getIncludeUser() . ' ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10);
                 //Delete users that are not selected.
                 if (!in_array($id, $ids)) {
                     Debug::text('Deleting: ' . $id, __FILE__, __LINE__, __METHOD__, 10);
                     $obj->Delete();
                 } else {
                     //Save ID's that need to be updated.
                     Debug::text('NOT Deleting : ' . $id, __FILE__, __LINE__, __METHOD__, 10);
                     $tmp_ids[] = $id;
                 }
             }
             unset($id, $obj);
         }
         //Insert new mappings.
         $lf_b = new UserListFactory();
         foreach ($ids as $id) {
             if (isset($ids) and !in_array($id, $tmp_ids)) {
                 $f = new StationIncludeUserFactory();
                 $f->setStation($this->getId());
                 $f->setIncludeUser($id);
                 $obj = $lf_b->getById($id)->getCurrent();
                 if ($this->Validator->isTrue('include_user', $f->Validator->isValid(), TTi18n::gettext('Selected Employee is invalid') . ' (' . $obj->getFullName() . ')')) {
                     $f->save();
                 }
             }
         }
         return TRUE;
     }
     Debug::text('No IDs to set.', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }