function createUserWage($user_id, $rate, $effective_date, $wage_group_id = 0)
 {
     $uwf = new UserWageFactory();
     $uwf->setUser($user_id);
     $uwf->setWageGroup($wage_group_id);
     $uwf->setType(10);
     $uwf->setWage($rate);
     //$uwf->setWeeklyTime( TTDate::parseTimeUnit( $wage_data['weekly_time'] ) );
     $uwf->setEffectiveDate($effective_date);
     if ($uwf->isValid()) {
         $insert_id = $uwf->Save();
         Debug::Text('User Wage ID: ' . $insert_id, __FILE__, __LINE__, __METHOD__, 10);
         return $insert_id;
     }
     Debug::Text('Failed Creating User Wage!', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }
    function getSearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        //Debug::Arr($order,'aOrder Data:', __FILE__, __LINE__, __METHOD__,10);
        $additional_order_fields = array('name', 'description', 'last_name', 'start_date', 'user_id');
        if ($order == NULL) {
            $order = array('c.start_date' => 'asc', 'cb.user_id' => 'desc', 'a.week' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_user_ids'];
        }
        //This is used by Flex Schedule Summary report.
        if (isset($filter_data['include_user_id'])) {
            $filter_data['id'] = $filter_data['include_user_id'];
        }
        if (isset($filter_data['include_user_ids'])) {
            $filter_data['id'] = $filter_data['include_user_ids'];
        }
        /*
        if ( isset($filter_data['user_status_ids']) ) {
        	$filter_data['status_id'] = $filter_data['user_status_ids'];
        }
        */
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['default_branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['default_branch_ids'];
        }
        if (isset($filter_data['default_department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['default_department_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['schedule_branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['schedule_department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['schedule_branch_ids'])) {
            $filter_data['schedule_branch_id'] = $filter_data['schedule_branch_ids'];
        }
        if (isset($filter_data['schedule_department_ids'])) {
            $filter_data['schedule_department_id'] = $filter_data['schedule_department_ids'];
        }
        if (isset($filter_data['exclude_job_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_job_ids'];
        }
        if (isset($filter_data['include_job_ids'])) {
            $filter_data['include_job_id'] = $filter_data['include_job_ids'];
        }
        if (isset($filter_data['job_group_ids'])) {
            $filter_data['job_group_id'] = $filter_data['job_group_ids'];
        }
        if (isset($filter_data['job_item_ids'])) {
            $filter_data['job_item_id'] = $filter_data['job_item_ids'];
        }
        //Debug::Arr($order,'bOrder Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        $uf = new UserFactory();
        $uwf = new UserWageFactory();
        $rscf = new RecurringScheduleControlFactory();
        $rsuf = new RecurringScheduleUserFactory();
        $rstcf = new RecurringScheduleTemplateControlFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        $apf = new AbsencePolicyFactory();
        $ppsuf = new PayPeriodScheduleUserFactory();
        $ppsf = new PayPeriodScheduleFactory();
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('filter_end_date' => $this->db->BindDate($filter_data['end_date']), 'company_id' => $company_id);
        $query = '
					select 	a.*,
							apf.type_id as absence_policy_type_id,
							apf.name as absence_policy,
							cb.user_id as user_id,

							CASE WHEN a.branch_id = -1 THEN d.default_branch_id ELSE a.branch_id END as schedule_branch_id,
							CASE WHEN a.branch_id = -1 THEN bf.name ELSE bfb.name END as schedule_branch,
							CASE WHEN a.department_id = -1 THEN d.default_department_id ELSE a.department_id END as schedule_department_id,
							CASE WHEN a.department_id = -1 THEN df.name ELSE dfb.name END as schedule_department,

							c.start_date as recurring_schedule_control_start_date,
							c.end_date as recurring_schedule_control_end_date,
							c.start_week as recurring_schedule_control_start_week,
							zz.max_week as max_week,
							( (((a.week-1)+zz.max_week-(c.start_week-1))%zz.max_week) + 1) as remapped_week,

							d.first_name as first_name,
							d.last_name as last_name,
							d.default_branch_id as default_branch_id,
							bf.name as default_branch,
							d.default_department_id as default_department_id,
							df.name as default_department,
							d.title_id as title_id,
							utf.name as title,
							d.group_id as group_id,
							ugf.name as "group",
							d.created_by as user_created_by,
							d.hire_date as hire_date,
							d.termination_date as termination_date,

							uw.id as user_wage_id,
							uw.hourly_rate as user_wage_hourly_rate,
							uw.effective_date as user_wage_effective_date,

							ppsf.shift_assigned_day_id as shift_assigned_day_id,

							c.created_by as recurring_schedule_control_created_by
							';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= ',
						x.name as job,
						x.status_id as job_status_id,
						x.manual_id as job_manual_id,
						x.branch_id as job_branch_id,
						x.department_id as job_department_id,
						x.group_id as job_group_id,

						y.name as job_item,
						y.manual_id as job_item_manual_id,
						y.group_id as job_item_group_id';
        }
        //Since when dealing with recurring schedules, we don't have a row for each specific date, so when determining wages
        //we can only use the last wage entered that is earlier than the filter end date.
        //Since in theory committed schedules will occur before todays date anyways, the accuracy won't be off too much unless
        //the end date they specify is really far in the future, and post dated wage entry is also made.
        $query .= '
					from 	' . $this->getTable() . ' as a
						LEFT JOIN ( select z.recurring_schedule_template_control_id, max(z.week) as max_week from recurring_schedule_template as z where deleted = 0 group by z.recurring_schedule_template_control_id ) as zz ON a.recurring_schedule_template_control_id = zz.recurring_schedule_template_control_id
						LEFT JOIN ' . $rstcf->getTable() . ' as b ON a.recurring_schedule_template_control_id = b.id
						LEFT JOIN ' . $rscf->getTable() . ' as c ON a.recurring_schedule_template_control_id = c.recurring_schedule_template_control_id
						LEFT JOIN ' . $rsuf->getTable() . ' as cb ON c.id = cb.recurring_schedule_control_id
						LEFT JOIN ' . $uf->getTable() . ' as d ON cb.user_id = d.id

						LEFT JOIN ' . $ppsuf->getTable() . ' as ppsuf ON d.id = ppsuf.user_id
						LEFT JOIN ' . $ppsf->getTable() . ' as ppsf ON ( ppsuf.pay_period_schedule_id = ppsf.id AND ppsf.deleted = 0 )

						LEFT JOIN ' . $bf->getTable() . ' as bf ON ( d.default_branch_id = bf.id AND bf.deleted = 0)
						LEFT JOIN ' . $bf->getTable() . ' as bfb ON ( a.branch_id = bfb.id AND bfb.deleted = 0)
						LEFT JOIN ' . $df->getTable() . ' as df ON ( d.default_department_id = df.id AND df.deleted = 0)
						LEFT JOIN ' . $df->getTable() . ' as dfb ON ( a.department_id = dfb.id AND dfb.deleted = 0)
						LEFT JOIN ' . $ugf->getTable() . ' as ugf ON ( d.group_id = ugf.id AND ugf.deleted = 0 )
						LEFT JOIN ' . $utf->getTable() . ' as utf ON ( d.title_id = utf.id AND utf.deleted = 0 )
						LEFT JOIN ' . $apf->getTable() . ' as apf ON ( a.absence_policy_id = apf.id AND apf.deleted = 0 )

						LEFT JOIN ' . $uwf->getTable() . ' as uw ON uw.id = (select uwb.id
																	from ' . $uwf->getTable() . ' as uwb
																	where uwb.user_id = cb.user_id
																		and uwb.effective_date <= ?
																		and uwb.deleted = 0
																		order by uwb.effective_date desc limit 1)

						';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as x ON a.job_id = x.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as y ON a.job_item_id = y.id';
        }
        $query .= ' where 	b.company_id = ?
					';
        if (isset($filter_data['permission_children_ids']) and isset($filter_data['permission_children_ids'][0]) and !in_array(-1, (array) $filter_data['permission_children_ids'])) {
            $query .= ' AND d.id in (' . $this->getListSQL($filter_data['permission_children_ids'], $ph) . ') ';
        }
        if (isset($filter_data['id']) and isset($filter_data['id'][0]) and !in_array(-1, (array) $filter_data['id'])) {
            $query .= ' AND d.id in (' . $this->getListSQL($filter_data['id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_id']) and isset($filter_data['exclude_id'][0]) and !in_array(-1, (array) $filter_data['exclude_id'])) {
            $query .= ' AND d.id not in (' . $this->getListSQL($filter_data['exclude_id'], $ph) . ') ';
        }
        if (isset($filter_data['user_status_id']) and isset($filter_data['user_status_id'][0]) and !in_array(-1, (array) $filter_data['user_status_id'])) {
            $query .= ' AND d.status_id in (' . $this->getListSQL($filter_data['user_status_id'], $ph) . ') ';
        }
        if (isset($filter_data['status_id']) and isset($filter_data['status_id'][0]) and !in_array(-1, (array) $filter_data['status_id'])) {
            $query .= ' AND a.status_id in (' . $this->getListSQL($filter_data['status_id'], $ph) . ') ';
        }
        if (isset($filter_data['group_id']) and isset($filter_data['group_id'][0]) and !in_array(-1, (array) $filter_data['group_id'])) {
            if (isset($filter_data['include_subgroups']) and (bool) $filter_data['include_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['group_id'], TRUE);
            }
            $query .= ' AND d.group_id in (' . $this->getListSQL($filter_data['group_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_branch_id']) and isset($filter_data['default_branch_id'][0]) and !in_array(-1, (array) $filter_data['default_branch_id'])) {
            $query .= ' AND d.default_branch_id in (' . $this->getListSQL($filter_data['default_branch_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_department_id']) and isset($filter_data['default_department_id'][0]) and !in_array(-1, (array) $filter_data['default_department_id'])) {
            $query .= ' AND d.default_department_id in (' . $this->getListSQL($filter_data['default_department_id'], $ph) . ') ';
        }
        if (isset($filter_data['schedule_branch_id']) and isset($filter_data['schedule_branch_id'][0]) and !in_array(-1, (array) $filter_data['schedule_branch_id'])) {
            $query .= ' AND ( a.branch_id in (' . $this->getListSQL($filter_data['schedule_branch_id'], $ph) . ') OR ( a.branch_id = -1 AND d.default_branch_id in (' . $this->getListSQL($filter_data['schedule_branch_id'], $ph) . ') ) )';
        }
        if (isset($filter_data['schedule_department_id']) and isset($filter_data['schedule_department_id'][0]) and !in_array(-1, (array) $filter_data['schedule_department_id'])) {
            $query .= ' AND ( a.department_id in (' . $this->getListSQL($filter_data['schedule_department_id'], $ph) . ') OR ( a.department_id = -1 AND d.default_department_id in (' . $this->getListSQL($filter_data['schedule_department_id'], $ph) . ') ) )';
        }
        if (isset($filter_data['title_id']) and isset($filter_data['title_id'][0]) and !in_array(-1, (array) $filter_data['title_id'])) {
            $query .= ' AND d.title_id in (' . $this->getListSQL($filter_data['title_id'], $ph) . ') ';
        }
        //Use the job_id in the schedule table so we can filter by '0' or No Job
        if (isset($filter_data['job_id']) and isset($filter_data['job_id'][0]) and !in_array(-1, (array) $filter_data['job_id'])) {
            $query .= ' AND a.job_id in (' . $this->getListSQL($filter_data['job_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_group_id']) and isset($filter_data['job_group_id'][0]) and !in_array(-1, (array) $filter_data['job_group_id'])) {
            if (isset($filter_data['include_job_subgroups']) and (bool) $filter_data['include_job_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['job_group_id'] = $uglf->getByCompanyIdAndGroupIdAndjob_subgroupsArray($company_id, $filter_data['job_group_id'], TRUE);
            }
            $query .= ' AND x.group_id in (' . $this->getListSQL($filter_data['job_group_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_item_id']) and isset($filter_data['job_item_id'][0]) and !in_array(-1, (array) $filter_data['job_item_id'])) {
            $query .= ' AND a.job_item_id in (' . $this->getListSQL($filter_data['job_item_id'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '' and isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $start_date_stamp = $this->db->BindDate($filter_data['start_date']);
            $end_date_stamp = $this->db->BindDate($filter_data['end_date']);
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $start_date_stamp;
            $ph[] = $end_date_stamp;
            $ph[] = $filter_data['end_date'];
            $ph[] = $filter_data['start_date'];
            $query .= ' AND (
								(c.start_date >= ? AND c.start_date <= ? AND c.end_date IS NULL )
								OR
								(c.start_date <= ? AND c.end_date IS NULL )
								OR
								(c.start_date <= ? AND c.end_date >= ? )
								OR
								(c.start_date >= ? AND c.end_date <= ? )
								OR
								(c.start_date >= ? AND c.start_date <= ? )
								OR
								(c.end_date >= ? AND c.end_date <= ? )
								OR
								(c.start_date <= ? AND c.end_date >= ? )
							)
							AND
							(
								( d.hire_date is NULL OR d.hire_date <= ? )
								AND
								( d.termination_date is NULL OR d.termination_date >= ? )
							)
						';
        }
        $query .= '
						AND ( a.deleted = 0 AND b.deleted = 0 AND c.deleted = 0 AND (d.deleted is NULL OR d.deleted = 0 ) )
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        //Debug::Arr($ph,' Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }
    function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        if (isset($filter_data['user_date_total_type_id'])) {
            $filter_data['type_id'] = $filter_data['user_date_total_type_id'];
        }
        $additional_order_fields = array('first_name', 'last_name', 'date_stamp', 'time_stamp', 'type_id', 'status_id', 'branch', 'department', 'default_branch', 'default_department', 'group', 'title');
        if ($order == NULL) {
            $order = array('c.date_stamp' => 'asc', 'a.status_id' => 'asc', 'a.type_id' => 'asc', 'a.total_time' => 'asc', 'a.status_id' => 'asc');
            $strict = FALSE;
        } else {
            $strict = FALSE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_user_ids'];
        }
        if (isset($filter_data['include_user_ids'])) {
            $filter_data['user_id'] = $filter_data['include_user_ids'];
        }
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['pay_period_ids'])) {
            $filter_data['pay_period_id'] = $filter_data['pay_period_ids'];
        }
        if (isset($filter_data['exclude_job_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_job_ids'];
        }
        if (isset($filter_data['include_job_ids'])) {
            $filter_data['include_job_id'] = $filter_data['include_job_ids'];
        }
        if (isset($filter_data['job_group_ids'])) {
            $filter_data['job_group_id'] = $filter_data['job_group_ids'];
        }
        if (isset($filter_data['job_item_ids'])) {
            $filter_data['job_item_id'] = $filter_data['job_item_ids'];
        }
        $uf = new UserFactory();
        $udf = new UserDateFactory();
        $uwf = new UserWageFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        $otpf = new OverTimePolicyFactory();
        $apf = new AbsencePolicyFactory();
        $ppf = new PremiumPolicyFactory();
        $mpf = new MealPolicyFactory();
        $bpf = new BreakPolicyFactory();
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('company_id' => $company_id);
        $query = '
					select
							a.id as id,
							a.user_date_id as user_date_id,
							a.type_id as type_id,
							a.status_id as status_id,
							a.punch_control_id as punch_control_id,

							a.over_time_policy_id as over_time_policy_id,
							otpf.name as over_time_policy,
							a.absence_policy_id as absence_policy_id,
							apf.name as absence_policy,
							apf.type_id as absence_policy_type_id,
							a.premium_policy_id as premium_policy_id,
							ppf.name as premium_policy,
							a.meal_policy_id as meal_policy_id,
							mpf.name as meal_policy,
							a.break_policy_id as break_policy_id,
							bpf.name as break_policy,

							a.start_time_stamp as start_time_stamp,
							a.end_time_stamp as end_time_stamp,

							a.override as override,

							a.branch_id as branch_id,
							j.name as branch,
							a.department_id as department_id,
							k.name as department,
							a.job_id as job_id,
							a.job_item_id as job_item_id,
							a.quantity as quantity,
							a.bad_quantity as bad_quantity,
							a.total_time as total_time,
							a.actual_total_time as actual_total_time,

							c.user_id as user_id,
							c.date_stamp as date_stamp,
							c.pay_period_id as pay_period_id,

							d.first_name as first_name,
							d.last_name as last_name,
							d.status_id as user_status_id,
							d.group_id as group_id,
							g.name as "group",
							d.title_id as title_id,
							h.name as title,
							d.default_branch_id as default_branch_id,
							e.name as default_branch,
							d.default_department_id as default_department_id,
							f.name as default_department,
							d.created_by as user_created_by,

							z.id as user_wage_id,
							z.effective_date as user_wage_effective_date ';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= ',
						x.name as job,
						x.name as job_name,
						x.status_id as job_status_id,
						x.manual_id as job_manual_id,
						x.branch_id as job_branch_id,
						x.department_id as job_department_id,
						x.group_id as job_group_id,
						y.name as job_item';
        }
        $query .= '
					from 	' . $this->getTable() . ' as a
							LEFT JOIN ' . $udf->getTable() . ' as c ON a.user_date_id = c.id
							LEFT JOIN ' . $uf->getTable() . ' as d ON c.user_id = d.id

							LEFT JOIN ' . $bf->getTable() . ' as e ON ( d.default_branch_id = e.id AND e.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as f ON ( d.default_department_id = f.id AND f.deleted = 0)
							LEFT JOIN ' . $ugf->getTable() . ' as g ON ( d.group_id = g.id AND g.deleted = 0 )
							LEFT JOIN ' . $utf->getTable() . ' as h ON ( d.title_id = h.id AND h.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as j ON ( a.branch_id = j.id AND j.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as k ON ( a.department_id = k.id AND k.deleted = 0)

							LEFT JOIN ' . $otpf->getTable() . ' as otpf ON ( a.over_time_policy_id > 0 AND a.over_time_policy_id = otpf.id AND otpf.deleted = 0 )
							LEFT JOIN ' . $apf->getTable() . ' as apf ON ( a.absence_policy_id > 0 AND a.absence_policy_id = apf.id AND apf.deleted = 0 )
							LEFT JOIN ' . $ppf->getTable() . ' as ppf ON ( a.premium_policy_id > 0 AND a.premium_policy_id = ppf.id AND ppf.deleted = 0 )
							LEFT JOIN ' . $mpf->getTable() . ' as mpf ON ( a.meal_policy_id > 0 AND a.meal_policy_id = mpf.id AND mpf.deleted = 0 )
							LEFT JOIN ' . $bpf->getTable() . ' as bpf ON ( a.break_policy_id > 0 AND a.break_policy_id = bpf.id AND bpf.deleted = 0 )

							LEFT JOIN ' . $uwf->getTable() . ' as z ON z.id = (select z.id
																		from ' . $uwf->getTable() . ' as z
																		where z.user_id = c.user_id
																			and z.effective_date <= c.date_stamp
																			and z.wage_group_id = 0
																			and z.deleted = 0
																			order by z.effective_date desc LiMiT 1)
					';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as x ON a.job_id = x.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as y ON a.job_item_id = y.id';
        }
        $query .= '	WHERE d.company_id = ?';
        $query .= isset($filter_data['permission_children_ids']) ? $this->getWhereClauseSQL('d.id', $filter_data['permission_children_ids'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['id']) ? $this->getWhereClauseSQL('a.id', $filter_data['id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_id']) ? $this->getWhereClauseSQL('d.id', $filter_data['exclude_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_id']) ? $this->getWhereClauseSQL('c.user_id', $filter_data['user_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_status_id']) ? $this->getWhereClauseSQL('d.status_id', $filter_data['user_status_id'], 'numeric_list', $ph) : NULL;
        if (isset($filter_data['include_subgroups']) and (bool) $filter_data['include_subgroups'] == TRUE) {
            $uglf = new UserGroupListFactory();
            $filter_data['user_group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['user_group_id'], TRUE);
        }
        $query .= isset($filter_data['user_group_id']) ? $this->getWhereClauseSQL('d.group_id', $filter_data['user_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_group']) ? $this->getWhereClauseSQL('g.name', $filter_data['user_group'], 'text', $ph) : NULL;
        $query .= isset($filter_data['group_id']) ? $this->getWhereClauseSQL('d.group_id', $filter_data['group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_branch_id']) ? $this->getWhereClauseSQL('d.default_branch_id', $filter_data['default_branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_department_id']) ? $this->getWhereClauseSQL('d.default_department_id', $filter_data['default_department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['title_id']) ? $this->getWhereClauseSQL('d.title_id', $filter_data['title_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['status_id']) ? $this->getWhereClauseSQL('a.status_id', $filter_data['status_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['type_id']) ? $this->getWhereClauseSQL('a.type_id', $filter_data['type_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['pay_period_id']) ? $this->getWhereClauseSQL('c.pay_period_id', $filter_data['pay_period_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['branch_id']) ? $this->getWhereClauseSQL('a.branch_id', $filter_data['branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['department_id']) ? $this->getWhereClauseSQL('a.department_id', $filter_data['department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_id']) ? $this->getWhereClauseSQL('a.job_id', $filter_data['job_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['include_job_id']) ? $this->getWhereClauseSQL('a.job_id', $filter_data['include_job_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_job_id']) ? $this->getWhereClauseSQL('a.job_id', $filter_data['exclude_job_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_group_id']) ? $this->getWhereClauseSQL('x.group_id', $filter_data['job_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_item_id']) ? $this->getWhereClauseSQL('a.job_item_id', $filter_data['job_item_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['include_job_item_id']) ? $this->getWhereClauseSQL('a.job_item_id', $filter_data['include_job_item_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_job_item_id']) ? $this->getWhereClauseSQL('a.job_item_id', $filter_data['exclude_job_item_id'], 'not_numeric_list', $ph) : NULL;
        /*
        		if ( isset($filter_data['permission_children_ids']) AND isset($filter_data['permission_children_ids'][0]) AND !in_array(-1, (array)$filter_data['permission_children_ids']) ) {
        			$query  .=	' AND d.id in ('. $this->getListSQL($filter_data['permission_children_ids'], $ph) .') ';
        		}
        		if ( isset($filter_data['id']) AND isset($filter_data['id'][0]) AND !in_array(-1, (array)$filter_data['id']) ) {
        			$query  .=	' AND a.id in ('. $this->getListSQL($filter_data['id'], $ph) .') ';
        		}
        		if ( isset($filter_data['exclude_id']) AND isset($filter_data['exclude_id'][0]) AND !in_array(-1, (array)$filter_data['exclude_id']) ) {
        			$query  .=	' AND d.id not in ('. $this->getListSQL($filter_data['exclude_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['user_id']) AND isset($filter_data['user_id'][0]) AND !in_array(-1, (array)$filter_data['user_id']) ) {
        			$query  .=	' AND c.user_id in ('. $this->getListSQL($filter_data['user_id'], $ph) .') ';
        		}
        
        		if ( isset($filter_data['user_status_id']) AND isset($filter_data['user_status_id'][0]) AND !in_array(-1, (array)$filter_data['user_status_id']) ) {
        			$query  .=	' AND d.status_id in ('. $this->getListSQL($filter_data['user_status_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['group_id']) AND isset($filter_data['group_id'][0]) AND !in_array(-1, (array)$filter_data['group_id']) ) {
        			if ( isset($filter_data['include_subgroups']) AND (bool)$filter_data['include_subgroups'] == TRUE ) {
        				$uglf = new UserGroupListFactory();
        				$filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray( $company_id, $filter_data['group_id'], TRUE);
        			}
        			$query  .=	' AND d.group_id in ('. $this->getListSQL($filter_data['group_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['default_branch_id']) AND isset($filter_data['default_branch_id'][0]) AND !in_array(-1, (array)$filter_data['default_branch_id']) ) {
        			$query  .=	' AND d.default_branch_id in ('. $this->getListSQL($filter_data['default_branch_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['default_department_id']) AND isset($filter_data['default_department_id'][0]) AND !in_array(-1, (array)$filter_data['default_department_id']) ) {
        			$query  .=	' AND d.default_department_id in ('. $this->getListSQL($filter_data['default_department_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['title_id']) AND isset($filter_data['title_id'][0]) AND !in_array(-1, (array)$filter_data['title_id']) ) {
        			$query  .=	' AND d.title_id in ('. $this->getListSQL($filter_data['title_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['branch_id']) AND isset($filter_data['branch_id'][0]) AND !in_array(-1, (array)$filter_data['branch_id']) ) {
        			$query  .=	' AND a.branch_id in ('. $this->getListSQL($filter_data['branch_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['department_id']) AND isset($filter_data['department_id'][0]) AND !in_array(-1, (array)$filter_data['department_id']) ) {
        			$query  .=	' AND a.department_id in ('. $this->getListSQL($filter_data['department_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['status_id']) AND isset($filter_data['status_id'][0]) AND !in_array(-1, (array)$filter_data['status_id']) ) {
        			$query  .=	' AND a.status_id in ('. $this->getListSQL($filter_data['status_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['type_id']) AND isset($filter_data['type_id'][0]) AND !in_array(-1, (array)$filter_data['type_id']) ) {
        			$query  .=	' AND a.type_id in ('. $this->getListSQL($filter_data['type_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['pay_period_id']) AND isset($filter_data['pay_period_id'][0]) AND !in_array(-1, (array)$filter_data['pay_period_id']) ) {
        			$query .= 	' AND c.pay_period_id in ('. $this->getListSQL($filter_data['pay_period_id'], $ph) .') ';
        		}
        
        
        		//Use the job_id in the punch_control table so we can filter by '0' or No Job
        		if ( isset($filter_data['include_job_id']) AND isset($filter_data['include_job_id'][0]) AND !in_array(-1, (array)$filter_data['include_job_id']) ) {
        			$query  .=	' AND a.job_id in ('. $this->getListSQL($filter_data['include_job_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['exclude_job_id']) AND isset($filter_data['exclude_job_id'][0]) AND !in_array(-1, (array)$filter_data['exclude_job_id']) ) {
        			$query  .=	' AND a.job_id not in ('. $this->getListSQL($filter_data['exclude_job_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['job_group_id']) AND isset($filter_data['job_group_id'][0]) AND !in_array(-1, (array)$filter_data['job_group_id']) ) {
        			if ( isset($filter_data['include_job_subgroups']) AND (bool)$filter_data['include_job_subgroups'] == TRUE ) {
        				$uglf = new UserGroupListFactory();
        				$filter_data['job_group_id'] = $uglf->getByCompanyIdAndGroupIdAndjob_subgroupsArray( $company_id, $filter_data['job_group_id'], TRUE);
        			}
        			$query  .=	' AND x.group_id in ('. $this->getListSQL($filter_data['job_group_id'], $ph) .') ';
        		}
        
        		if ( isset($filter_data['job_item_id']) AND isset($filter_data['job_item_id'][0]) AND !in_array(-1, (array)$filter_data['job_item_id']) ) {
        			$query  .=	' AND a.job_item_id in ('. $this->getListSQL($filter_data['job_item_id'], $ph) .') ';
        		}
        */
        if (isset($filter_data['date']) and trim($filter_data['date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['date']);
            $query .= ' AND c.date_stamp = ?';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND c.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND c.date_stamp <= ?';
        }
        $query .= '
						AND (a.deleted = 0 AND c.deleted = 0 AND d.deleted = 0)
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        //Debug::Arr($ph, 'Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        return $this;
    }
    function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        //$additional_order_fields = array('b.name', 'c.name', 'd.name', 'e.name');
        $additional_order_fields = array('first_name', 'last_name', 'date_stamp', 'time_stamp', 'type_id', 'status_id', 'branch', 'department', 'default_branch', 'default_department', 'group', 'title');
        $sort_column_aliases = array('status' => 'status_id', 'type' => 'type_id', 'first_name' => 'd.first_name', 'last_name' => 'd.last_name', 'station_station_id' => 'l.station_id', 'station_type' => 'l.type_id', 'station_source' => 'l.source', 'station_description' => 'l.description');
        $order = $this->getColumnsFromAliases($order, $sort_column_aliases);
        if ($order == NULL) {
            $order = array('c.pay_period_id' => 'asc', 'c.user_id' => 'asc', 'a.time_stamp' => 'asc', 'a.punch_control_id' => 'asc', 'a.status_id' => 'asc');
            $strict = FALSE;
        } else {
            $strict = FALSE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_user_ids'];
        }
        /*
        		if ( isset($filter_data['user_id']) ) {
        			$filter_data['id'] = $filter_data['user_id'];
        		}
        		if ( isset($filter_data['include_user_ids']) ) {
        			$filter_data['id'] = $filter_data['include_user_ids'];
        		}
        */
        if (isset($filter_data['user_status_ids'])) {
            $filter_data['user_status_id'] = $filter_data['user_status_ids'];
        }
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['punch_branch_ids'])) {
            $filter_data['branch_id'] = $filter_data['punch_branch_ids'];
        }
        if (isset($filter_data['punch_department_ids'])) {
            $filter_data['department_id'] = $filter_data['punch_department_ids'];
        }
        if (isset($filter_data['pay_period_ids'])) {
            $filter_data['pay_period_id'] = $filter_data['pay_period_ids'];
        }
        /*
        		if ( isset($filter_data['exclude_job_ids']) ) {
        			$filter_data['exclude_id'] = $filter_data['exclude_job_ids'];
        		}
        		if ( isset($filter_data['include_job_ids']) ) {
        			$filter_data['include_job_id'] = $filter_data['include_job_ids'];
        		}
        		if ( isset($filter_data['job_group_ids']) ) {
        			$filter_data['job_group_id'] = $filter_data['job_group_ids'];
        		}
        		if ( isset($filter_data['job_item_ids']) ) {
        			$filter_data['job_item_id'] = $filter_data['job_item_ids'];
        		}
        */
        $uf = new UserFactory();
        $udf = new UserDateFactory();
        $pcf = new PunchControlFactory();
        $uwf = new UserWageFactory();
        $sf = new StationFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('company_id' => $company_id);
        //Tainted: Determine if the punch was manually created (without punching in/out) or modified by someone other than the person who punched in/out.
        $query = '
					select
							a.id as id,
							a.punch_control_id as punch_control_id,
							a.type_id as type_id,
							a.status_id as status_id,
							a.time_stamp as time_stamp,
							a.actual_time_stamp as actual_time_stamp,
							a.original_time_stamp as original_time_stamp,
							a.longitude,
							a.latitude,
							a.transfer,

							a.created_by as created_by,
							a.created_date as created_date,
							a.updated_by as updated_by,
							a.updated_date as updated_date,

							b.user_date_id as user_date_id,
							b.branch_id as branch_id,
							j.name as branch,
							b.department_id as department_id,
							k.name as department,
							b.job_id as job_id,
							b.job_item_id as job_item_id,
							b.quantity as quantity,
							b.bad_quantity as bad_quantity,
							b.total_time as total_time,
							b.actual_total_time as actual_total_time,
							b.meal_policy_id as meal_policy_id,
							b.other_id1 as other_id1,
							b.other_id2 as other_id2,
							b.other_id3 as other_id3,
							b.other_id4 as other_id4,
							b.other_id5 as other_id5,
							b.note as note,

							c.user_id as user_id,
							c.date_stamp as date_stamp,
							c.pay_period_id as pay_period_id,

							CASE WHEN c.user_id != a.created_by OR a.created_by != a.updated_by OR ( a.created_by is NULL AND a.updated_by is NOT NULL ) THEN 1 ELSE 0 END as tainted,

							d.first_name as first_name,
							d.last_name as last_name,
							d.status_id as user_status_id,
							d.group_id as group_id,
							g.name as "group",
							d.title_id as title_id,
							h.name as title,
							d.default_branch_id as default_branch_id,
							e.name as default_branch,
							d.default_department_id as default_department_id,
							f.name as default_department,
							d.created_by as user_created_by,

							l.id as station_id,
							l.type_id as station_type_id,
							l.station_id as station_station_id,
							l.source as station_source,
							l.description as station_description,

							w.id as user_wage_id,
							w.effective_date as user_wage_effective_date,

							y.first_name as created_by_first_name,
							y.middle_name as created_by_middle_name,
							y.last_name as created_by_last_name,
							z.first_name as updated_by_first_name,
							z.middle_name as updated_by_middle_name,
							z.last_name as updated_by_last_name
							';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= ',
						r.name as job,
						r.name as job_name,
						r.status_id as job_status_id,
						r.manual_id as job_manual_id,
						r.branch_id as job_branch_id,
						r.department_id as job_department_id,
						r.group_id as job_group_id,
						s.name as job_item';
        }
        $query .= '
					from 	' . $this->getTable() . ' as a
							LEFT JOIN ' . $pcf->getTable() . ' as b ON a.punch_control_id = b.id
							LEFT JOIN ' . $udf->getTable() . ' as c ON b.user_date_id = c.id
							LEFT JOIN ' . $uf->getTable() . ' as d ON c.user_id = d.id

							LEFT JOIN ' . $bf->getTable() . ' as e ON ( d.default_branch_id = e.id AND e.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as f ON ( d.default_department_id = f.id AND f.deleted = 0)
							LEFT JOIN ' . $ugf->getTable() . ' as g ON ( d.group_id = g.id AND g.deleted = 0 )
							LEFT JOIN ' . $utf->getTable() . ' as h ON ( d.title_id = h.id AND h.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as j ON ( b.branch_id = j.id AND j.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as k ON ( b.department_id = k.id AND k.deleted = 0)

							LEFT JOIN ' . $sf->getTable() . ' as l ON ( a.station_id = l.id AND l.deleted = 0 )

							LEFT JOIN ' . $uwf->getTable() . ' as w ON w.id = (select w.id
																		from ' . $uwf->getTable() . ' as w
																		where w.user_id = c.user_id
																			and w.effective_date <= c.date_stamp
																			and w.deleted = 0
																			order by w.effective_date desc LiMiT 1)

							LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
							LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
					';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as r ON b.job_id = r.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as s ON b.job_item_id = s.id';
        }
        $query .= '	WHERE d.company_id = ?';
        $query .= isset($filter_data['permission_children_ids']) ? $this->getWhereClauseSQL('d.id', $filter_data['permission_children_ids'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['id']) ? $this->getWhereClauseSQL('a.id', $filter_data['id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_id']) ? $this->getWhereClauseSQL('d.id', $filter_data['exclude_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_id']) ? $this->getWhereClauseSQL('c.user_id', $filter_data['user_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_status_id']) ? $this->getWhereClauseSQL('d.status_id', $filter_data['user_status_id'], 'numeric_list', $ph) : NULL;
        if (isset($filter_data['include_subgroups']) and (bool) $filter_data['include_subgroups'] == TRUE) {
            $uglf = new UserGroupListFactory();
            $filter_data['user_group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['user_group_id'], TRUE);
        }
        $query .= isset($filter_data['user_group_id']) ? $this->getWhereClauseSQL('d.group_id', $filter_data['user_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_group']) ? $this->getWhereClauseSQL('g.name', $filter_data['user_group'], 'text', $ph) : NULL;
        $query .= isset($filter_data['group_id']) ? $this->getWhereClauseSQL('d.group_id', $filter_data['group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_branch_id']) ? $this->getWhereClauseSQL('d.default_branch_id', $filter_data['default_branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_department_id']) ? $this->getWhereClauseSQL('d.default_department_id', $filter_data['default_department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['title_id']) ? $this->getWhereClauseSQL('d.title_id', $filter_data['title_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['status_id']) ? $this->getWhereClauseSQL('a.status_id', $filter_data['status_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['type_id']) ? $this->getWhereClauseSQL('a.type_id', $filter_data['type_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['pay_period_id']) ? $this->getWhereClauseSQL('c.pay_period_id', $filter_data['pay_period_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['branch_id']) ? $this->getWhereClauseSQL('b.branch_id', $filter_data['branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['department_id']) ? $this->getWhereClauseSQL('b.department_id', $filter_data['department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_id']) ? $this->getWhereClauseSQL('b.job_id', $filter_data['job_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['include_job_id']) ? $this->getWhereClauseSQL('b.job_id', $filter_data['include_job_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_job_id']) ? $this->getWhereClauseSQL('b.job_id', $filter_data['exclude_job_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_group_id']) ? $this->getWhereClauseSQL('r.group_id', $filter_data['job_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_item_id']) ? $this->getWhereClauseSQL('b.job_item_id', $filter_data['job_item_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['include_job_item_id']) ? $this->getWhereClauseSQL('b.job_item_id', $filter_data['include_job_item_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_job_item_id']) ? $this->getWhereClauseSQL('b.job_item_id', $filter_data['exclude_job_item_id'], 'not_numeric_list', $ph) : NULL;
        /*
        		if ( isset($filter_data['permission_children_ids']) AND isset($filter_data['permission_children_ids'][0]) AND !in_array(-1, (array)$filter_data['permission_children_ids']) ) {
        			$query  .=	' AND d.id in ('. $this->getListSQL($filter_data['permission_children_ids'], $ph) .') ';
        		}
        		if ( isset($filter_data['id']) AND isset($filter_data['id'][0]) AND !in_array(-1, (array)$filter_data['id']) ) {
        			$query  .=	' AND a.id in ('. $this->getListSQL($filter_data['id'], $ph) .') ';
        		}
        		if ( isset($filter_data['exclude_id']) AND isset($filter_data['exclude_id'][0]) AND !in_array(-1, (array)$filter_data['exclude_id']) ) {
        			$query  .=	' AND d.id not in ('. $this->getListSQL($filter_data['exclude_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['user_id']) AND isset($filter_data['user_id'][0]) AND !in_array(-1, (array)$filter_data['user_id']) ) {
        			$query  .=	' AND c.user_id in ('. $this->getListSQL($filter_data['user_id'], $ph) .') ';
        		}
        
        		if ( isset($filter_data['user_status_id']) AND isset($filter_data['user_status_id'][0]) AND !in_array(-1, (array)$filter_data['user_status_id']) ) {
        			$query  .=	' AND d.status_id in ('. $this->getListSQL($filter_data['user_status_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['group_id']) AND isset($filter_data['group_id'][0]) AND !in_array(-1, (array)$filter_data['group_id']) ) {
        			if ( isset($filter_data['include_subgroups']) AND (bool)$filter_data['include_subgroups'] == TRUE ) {
        				$uglf = new UserGroupListFactory();
        				$filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray( $company_id, $filter_data['group_id'], TRUE);
        			}
        			$query  .=	' AND d.group_id in ('. $this->getListSQL($filter_data['group_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['default_branch_id']) AND isset($filter_data['default_branch_id'][0]) AND !in_array(-1, (array)$filter_data['default_branch_id']) ) {
        			$query  .=	' AND d.default_branch_id in ('. $this->getListSQL($filter_data['default_branch_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['default_department_id']) AND isset($filter_data['default_department_id'][0]) AND !in_array(-1, (array)$filter_data['default_department_id']) ) {
        			$query  .=	' AND d.default_department_id in ('. $this->getListSQL($filter_data['default_department_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['title_id']) AND isset($filter_data['title_id'][0]) AND !in_array(-1, (array)$filter_data['title_id']) ) {
        			$query  .=	' AND d.title_id in ('. $this->getListSQL($filter_data['title_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['branch_id']) AND isset($filter_data['branch_id'][0]) AND !in_array(-1, (array)$filter_data['branch_id']) ) {
        			$query  .=	' AND b.branch_id in ('. $this->getListSQL($filter_data['branch_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['department_id']) AND isset($filter_data['department_id'][0]) AND !in_array(-1, (array)$filter_data['department_id']) ) {
        			$query  .=	' AND b.department_id in ('. $this->getListSQL($filter_data['department_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['status_id']) AND isset($filter_data['status_id'][0]) AND !in_array(-1, (array)$filter_data['status_id']) ) {
        			$query  .=	' AND a.status_id in ('. $this->getListSQL($filter_data['status_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['type_id']) AND isset($filter_data['type_id'][0]) AND !in_array(-1, (array)$filter_data['type_id']) ) {
        			$query  .=	' AND a.type_id in ('. $this->getListSQL($filter_data['type_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['pay_period_id']) AND isset($filter_data['pay_period_id'][0]) AND !in_array(-1, (array)$filter_data['pay_period_id']) ) {
        			$query .= 	' AND c.pay_period_id in ('. $this->getListSQL($filter_data['pay_period_id'], $ph) .') ';
        		}
        
        
        		//Use the job_id in the punch_control table so we can filter by '0' or No Job
        		if ( isset($filter_data['include_job_id']) AND isset($filter_data['include_job_id'][0]) AND !in_array(-1, (array)$filter_data['include_job_id']) ) {
        			$query  .=	' AND b.job_id in ('. $this->getListSQL($filter_data['include_job_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['exclude_job_id']) AND isset($filter_data['exclude_job_id'][0]) AND !in_array(-1, (array)$filter_data['exclude_job_id']) ) {
        			$query  .=	' AND b.job_id not in ('. $this->getListSQL($filter_data['exclude_job_id'], $ph) .') ';
        		}
        		if ( isset($filter_data['job_group_id']) AND isset($filter_data['job_group_id'][0]) AND !in_array(-1, (array)$filter_data['job_group_id']) ) {
        			if ( isset($filter_data['include_job_subgroups']) AND (bool)$filter_data['include_job_subgroups'] == TRUE ) {
        				$uglf = new UserGroupListFactory();
        				$filter_data['job_group_id'] = $uglf->getByCompanyIdAndGroupIdAndjob_subgroupsArray( $company_id, $filter_data['job_group_id'], TRUE);
        			}
        			$query  .=	' AND r.group_id in ('. $this->getListSQL($filter_data['job_group_id'], $ph) .') ';
        		}
        
        		if ( isset($filter_data['job_item_id']) AND isset($filter_data['job_item_id'][0]) AND !in_array(-1, (array)$filter_data['job_item_id']) ) {
        			$query  .=	' AND b.job_item_id in ('. $this->getListSQL($filter_data['job_item_id'], $ph) .') ';
        		}
        */
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND c.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND c.date_stamp <= ?';
        }
        $query .= '
						AND (a.deleted = 0 AND b.deleted = 0 AND c.deleted = 0 AND d.deleted = 0)
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        //Debug::Arr($ph, 'Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }
    function getReportByStartDateAndEndDateAndUserIdListAndJobListAndJobItemList($start_date, $end_date, $user_ids, $job_ids, $job_item_ids, $order = NULL)
    {
        if ($user_ids == '') {
            Debug::Text('No User Ids: ', __FILE__, __LINE__, __METHOD__, 10);
            return FALSE;
        }
        if ($job_ids == '') {
            Debug::Text('No Job Ids: ', __FILE__, __LINE__, __METHOD__, 10);
            return FALSE;
        }
        if ($job_item_ids == '') {
            Debug::Text('No Job Item Ids: ', __FILE__, __LINE__, __METHOD__, 10);
            return FALSE;
        }
        if ($start_date == '') {
            Debug::Text('No Start Date: ', __FILE__, __LINE__, __METHOD__, 10);
            $start_date = 0;
        }
        if ($end_date == '') {
            Debug::Text('No End Date: ', __FILE__, __LINE__, __METHOD__, 10);
            $end_date = time();
        }
        //$order = array( 'b.pay_period_id' => 'asc', 'b.user_id' => 'asc' );
        //$order = array( 'z.last_name' => 'asc' );
        /*
        if ( $order == NULL ) {
        	$order = array( 'b.pay_period_id' => 'asc', 'b.user_id' => 'asc' );
        	$strict = FALSE;
        } else {
        	$strict = TRUE;
        }
        */
        $ulf = new UserListFactory();
        $udf = new UserDateFactory();
        $uwf = new UserWageFactory();
        $otpf = new OverTimePolicyFactory();
        $apf = new AbsencePolicyFactory();
        $ppf = new PremiumPolicyFactory();
        $ph = array('start_date' => $this->db->BindDate($start_date), 'end_date' => $this->db->BindDate($end_date));
        $query = '

					select  b.user_id as user_id,
							a.status_id as status_id,
							a.type_id as type_id,
							a.branch_id as branch_id,
							a.department_id as department_id,
							a.job_id as job_id,
							a.job_item_id as job_item_id,

							a.over_time_policy_id as over_time_policy_id,
							n.id as over_time_policy_wage_id,
							n.effective_date as over_time_policy_wage_effective_date,

							a.absence_policy_id as absence_policy_id,
							p.id as absence_policy_wage_id,
							p.effective_date as absence_policy_wage_effective_date,

							a.premium_policy_id as premium_policy_id,
							r.id as premium_policy_wage_id,
							r.effective_date as premium_policy_wage_effective_date,

							z.id as user_wage_id,
							z.effective_date as user_wage_effective_date,
							sum(total_time) as total_time,
							sum(actual_total_time) as actual_total_time,
							sum(quantity) as quantity,
							sum(bad_quantity) as bad_quantity
					from	' . $this->getTable() . ' as a
					LEFT JOIN ' . $udf->getTable() . ' as b ON a.user_date_id = b.id

					LEFT JOIN ' . $otpf->getTable() . ' as m ON a.over_time_policy_id = m.id
					LEFT JOIN ' . $uwf->getTable() . ' as n ON n.id = (select n.id
																		from ' . $uwf->getTable() . ' as n
																		where n.user_id = b.user_id
																			and n.wage_group_id = m.wage_group_id
																			and n.effective_date <= b.date_stamp
																			and n.deleted = 0
																			order by n.effective_date desc limit 1)

					LEFT JOIN ' . $apf->getTable() . ' as o ON a.absence_policy_id = o.id
					LEFT JOIN ' . $uwf->getTable() . ' as p ON p.id = (select p.id
																		from ' . $uwf->getTable() . ' as p
																		where p.user_id = b.user_id
																			and p.wage_group_id = o.wage_group_id
																			and p.effective_date <= b.date_stamp
																			and p.deleted = 0
																			order by p.effective_date desc limit 1)

					LEFT JOIN ' . $ppf->getTable() . ' as q ON a.premium_policy_id = q.id
					LEFT JOIN ' . $uwf->getTable() . ' as r ON r.id = (select r.id
																		from ' . $uwf->getTable() . ' as r
																		where r.user_id = b.user_id
																			and r.wage_group_id = q.wage_group_id
																			and r.effective_date <= b.date_stamp
																			and r.deleted = 0
																			order by r.effective_date desc limit 1)

					LEFT JOIN ' . $uwf->getTable() . ' as z ON z.id = (select z.id
																		from ' . $uwf->getTable() . ' as z
																		where z.user_id = b.user_id
																			and z.effective_date <= b.date_stamp
																			and z.deleted = 0
																			order by z.effective_date desc limit 1)
					where 	a.user_date_id = b.id
						AND b.date_stamp >= ?
						AND b.date_stamp <= ?
						AND b.user_id in (' . $this->getListSQL($user_ids, $ph) . ')
						AND a.job_id in (' . $this->getListSQL($job_ids, $ph) . ')
					';
        //AND a.job_item_id in ('. $this->getListSQL($job_item_ids, $ph) .')
        $filter_query = NULL;
        if ($job_item_ids != '' and isset($job_item_ids[0]) and !in_array(-1, $job_item_ids)) {
            $query .= ' AND a.job_item_id in (' . $this->getListSQL($job_item_ids, $ph) . ') ';
        }
        $query .= '
						AND a.status_id in (10,20,30)
						AND ( a.deleted = 0 AND b.deleted = 0)
					group by b.user_id,user_wage_id, user_wage_effective_date, over_time_policy_wage_id, over_time_policy_wage_effective_date, absence_policy_wage_id, absence_policy_wage_effective_date, premium_policy_wage_id, premium_policy_wage_effective_date, a.status_id, a.type_id, a.branch_id, a.department_id, a.job_id, a.job_item_id, a.over_time_policy_id, a.absence_policy_id, a.premium_policy_id
				';
        $query .= $this->getSortSQL($order, FALSE);
        $this->rs = $this->db->Execute($query, $ph);
        return $this;
    }
    function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        //$additional_order_fields = array('b.name', 'c.name', 'd.name', 'e.name');
        $additional_order_fields = array('first_name', 'last_name', 'date_stamp', 'time_stamp', 'type_id', 'status_id', 'branch', 'department', 'default_branch', 'default_department', 'group', 'title');
        if ($order == NULL) {
            $order = array('c.pay_period_id' => 'asc', 'c.user_id' => 'asc');
            $strict = FALSE;
        } else {
            $strict = FALSE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_user_ids'];
        }
        if (isset($filter_data['user_id'])) {
            $filter_data['id'] = $filter_data['user_id'];
        }
        if (isset($filter_data['include_user_ids'])) {
            $filter_data['id'] = $filter_data['include_user_ids'];
        }
        if (isset($filter_data['user_status_ids'])) {
            $filter_data['status_id'] = $filter_data['user_status_ids'];
        }
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['punch_branch_ids'])) {
            $filter_data['punch_branch_id'] = $filter_data['punch_branch_ids'];
        }
        if (isset($filter_data['punch_department_ids'])) {
            $filter_data['punch_department_id'] = $filter_data['punch_department_ids'];
        }
        if (isset($filter_data['exclude_job_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_job_ids'];
        }
        if (isset($filter_data['include_job_ids'])) {
            $filter_data['include_job_id'] = $filter_data['include_job_ids'];
        }
        if (isset($filter_data['job_group_ids'])) {
            $filter_data['job_group_id'] = $filter_data['job_group_ids'];
        }
        if (isset($filter_data['job_item_ids'])) {
            $filter_data['job_item_id'] = $filter_data['job_item_ids'];
        }
        $uf = new UserFactory();
        $udf = new UserDateFactory();
        $pcf = new PunchControlFactory();
        $uwf = new UserWageFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('company_id' => $company_id);
        $query = '
					select
							b.id as id,
							b.user_date_id as user_date_id,
							b.branch_id as branch_id,
							j.name as branch,
							b.department_id as department_id,
							k.name as department,
							b.job_id as job_id,
							x.name as job,
							b.job_item_id as job_item_id,
							y.name as job_item,
							b.quantity as quantity,
							b.bad_quantity as bad_quantity,
							b.total_time as total_time,
							b.actual_total_time as actual_total_time,
							b.meal_policy_id as meal_policy_id,
							b.other_id1 as other_id1,
							b.other_id2 as other_id2,
							b.other_id3 as other_id3,
							b.other_id4 as other_id4,
							b.other_id5 as other_id5,
							b.note as note,

							c.user_id as user_id,
							c.date_stamp as date_stamp,
							c.pay_period_id as pay_period_id,

							d.first_name as first_name,
							d.last_name as last_name,
							d.status_id as user_status_id,
							d.group_id as group_id,
							g.name as "group",
							d.title_id as title_id,
							h.name as title,
							d.default_branch_id as default_branch_id,
							e.name as default_branch,
							d.default_department_id as default_department_id,
							f.name as default_department,
							d.created_by as user_created_by,

							z.id as user_wage_id,
							z.effective_date as user_wage_effective_date ';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= ',
						x.name as job_name,
						x.status_id as job_status_id,
						x.manual_id as job_manual_id,
						x.branch_id as job_branch_id,
						x.department_id as job_department_id,
						x.group_id as job_group_id';
        }
        $query .= '
					from 	' . $this->getTable() . ' as b
							LEFT JOIN ' . $udf->getTable() . ' as c ON b.user_date_id = c.id
							LEFT JOIN ' . $uf->getTable() . ' as d ON c.user_id = d.id

							LEFT JOIN ' . $bf->getTable() . ' as e ON ( d.default_branch_id = e.id AND e.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as f ON ( d.default_department_id = f.id AND f.deleted = 0)
							LEFT JOIN ' . $ugf->getTable() . ' as g ON ( d.group_id = g.id AND g.deleted = 0 )
							LEFT JOIN ' . $utf->getTable() . ' as h ON ( d.title_id = h.id AND h.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as j ON ( b.branch_id = j.id AND j.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as k ON ( b.department_id = k.id AND k.deleted = 0)

							LEFT JOIN ' . $uwf->getTable() . ' as z ON z.id = (select z.id
																		from ' . $uwf->getTable() . ' as z
																		where z.user_id = c.user_id
																			and z.effective_date <= c.date_stamp
																			and z.deleted = 0
																			order by z.effective_date desc LiMiT 1)
					';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as x ON b.job_id = x.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as y ON b.job_item_id = y.id';
        }
        $query .= '	WHERE d.company_id = ?';
        if (isset($filter_data['permission_children_ids']) and isset($filter_data['permission_children_ids'][0]) and !in_array(-1, (array) $filter_data['permission_children_ids'])) {
            $query .= ' AND d.id in (' . $this->getListSQL($filter_data['permission_children_ids'], $ph) . ') ';
        }
        if (isset($filter_data['id']) and isset($filter_data['id'][0]) and !in_array(-1, (array) $filter_data['id'])) {
            $query .= ' AND b.id in (' . $this->getListSQL($filter_data['id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_id']) and isset($filter_data['exclude_id'][0]) and !in_array(-1, (array) $filter_data['exclude_id'])) {
            $query .= ' AND d.id not in (' . $this->getListSQL($filter_data['exclude_id'], $ph) . ') ';
        }
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND c.user_id in (' . $this->getListSQL($filter_data['user_id'], $ph) . ') ';
        }
        if (isset($filter_data['status_id']) and isset($filter_data['status_id'][0]) and !in_array(-1, (array) $filter_data['status_id'])) {
            $query .= ' AND d.status_id in (' . $this->getListSQL($filter_data['status_id'], $ph) . ') ';
        }
        if (isset($filter_data['group_id']) and isset($filter_data['group_id'][0]) and !in_array(-1, (array) $filter_data['group_id'])) {
            if (isset($filter_data['include_subgroups']) and (bool) $filter_data['include_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['group_id'], TRUE);
            }
            $query .= ' AND d.group_id in (' . $this->getListSQL($filter_data['group_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_branch_id']) and isset($filter_data['default_branch_id'][0]) and !in_array(-1, (array) $filter_data['default_branch_id'])) {
            $query .= ' AND d.default_branch_id in (' . $this->getListSQL($filter_data['default_branch_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_department_id']) and isset($filter_data['default_department_id'][0]) and !in_array(-1, (array) $filter_data['default_department_id'])) {
            $query .= ' AND d.default_department_id in (' . $this->getListSQL($filter_data['default_department_id'], $ph) . ') ';
        }
        if (isset($filter_data['title_id']) and isset($filter_data['title_id'][0]) and !in_array(-1, (array) $filter_data['title_id'])) {
            $query .= ' AND d.title_id in (' . $this->getListSQL($filter_data['title_id'], $ph) . ') ';
        }
        if (isset($filter_data['punch_branch_id']) and isset($filter_data['punch_branch_id'][0]) and !in_array(-1, (array) $filter_data['punch_branch_id'])) {
            $query .= ' AND b.branch_id in (' . $this->getListSQL($filter_data['punch_branch_id'], $ph) . ') ';
        }
        if (isset($filter_data['punch_department_id']) and isset($filter_data['punch_department_id'][0]) and !in_array(-1, (array) $filter_data['punch_department_id'])) {
            $query .= ' AND b.department_id in (' . $this->getListSQL($filter_data['punch_department_id'], $ph) . ') ';
        }
        if (isset($filter_data['pay_period_ids']) and isset($filter_data['pay_period_ids'][0]) and !in_array(-1, (array) $filter_data['pay_period_ids'])) {
            $query .= ' AND c.pay_period_id in (' . $this->getListSQL($filter_data['pay_period_ids'], $ph) . ') ';
        }
        //Use the job_id in the punch_control table so we can filter by '0' or No Job
        if (isset($filter_data['include_job_id']) and isset($filter_data['include_job_id'][0]) and !in_array(-1, (array) $filter_data['include_job_id'])) {
            $query .= ' AND b.job_id in (' . $this->getListSQL($filter_data['include_job_id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_job_id']) and isset($filter_data['exclude_job_id'][0]) and !in_array(-1, (array) $filter_data['exclude_job_id'])) {
            $query .= ' AND b.job_id not in (' . $this->getListSQL($filter_data['exclude_job_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_group_id']) and isset($filter_data['job_group_id'][0]) and !in_array(-1, (array) $filter_data['job_group_id'])) {
            if (isset($filter_data['include_job_subgroups']) and (bool) $filter_data['include_job_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['job_group_id'] = $uglf->getByCompanyIdAndGroupIdAndjob_subgroupsArray($company_id, $filter_data['job_group_id'], TRUE);
            }
            $query .= ' AND x.group_id in (' . $this->getListSQL($filter_data['job_group_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_item_id']) and isset($filter_data['job_item_id'][0]) and !in_array(-1, (array) $filter_data['job_item_id'])) {
            $query .= ' AND b.job_item_id in (' . $this->getListSQL($filter_data['job_item_id'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND c.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND c.date_stamp <= ?';
        }
        $query .= '
						AND ( b.deleted = 0 AND c.deleted = 0 AND d.deleted = 0 )
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }
Exemple #7
0
 echo " \t\t\tSuccess!\n";
 if (isset($mapped_row['wage_type_id']) and $mapped_row['wage_type_id'] != '' and isset($mapped_row['wage']) and $mapped_row['wage'] != '') {
     echo "    Importing User Wage Information...";
     //Import Salary information
     if (isset($mapped_row['wage_effective_date'])) {
         $wage_effective_date = Misc::importCallInputParseFunction('wage_effective_date', $mapped_row['wage_effective_date'], $filtered_import_map['wage_effective_date']['default_value'], $filtered_import_map['wage_effective_date']['parse_hint']);
     } else {
         $wage_effective_date = $uf->getHireDate();
     }
     $uwlf = new UserWageListFactory();
     $uwlf->getByUserIdAndStartDateAndEndDate($user_id, $wage_effective_date, $wage_effective_date);
     if ($uwlf->getRecordCount() == 1) {
         $uwf = $uwlf->getCurrent();
         echo "(U) ";
     } else {
         $uwf = new UserWageFactory();
     }
     $uwf->setUser($user_id);
     if (isset($mapped_row['wage_type_id']) and $mapped_row['wage_type_id'] != '') {
         $uwf->setType(Misc::importCallInputParseFunction('wage_type_id', $mapped_row['wage_type_id'], $filtered_import_map['wage_type_id']['default_value'], $filtered_import_map['wage_type_id']['parse_hint']));
     }
     if (isset($mapped_row['wage']) and $mapped_row['wage'] != '') {
         $uwf->setWage(Misc::importCallInputParseFunction('wage', $mapped_row['wage'], $filtered_import_map['wage']['default_value'], $mapped_row['wage_type_id']));
     }
     if ($uwf->getType() == 20) {
         if (isset($mapped_row['wage_weekly_time']) and $mapped_row['wage_weekly_time'] != '') {
             $uwf->setWeeklyTime(Misc::importCallInputParseFunction('wage_weekly_time', $mapped_row['wage_weekly_time'], $filtered_import_map['wage_weekly_time']['default_value'], $filtered_import_map['wage_weekly_time']['parse_hint']));
         }
     }
     if (isset($mapped_row['labor_burden_percent']) and $mapped_row['labor_burden_percent'] != '') {
         $uwf->setLaborBurdenPercent(Misc::importCallInputParseFunction('labor_burden_percent', $mapped_row['labor_burden_percent'], $filtered_import_map['labor_burden_percent']['default_value'], $filtered_import_map['labor_burden_percent']['parse_hint']));
Exemple #8
0
 function setUserDateTotalArray()
 {
     //Loop through unique UserDateTotal rows... Adding entries to pay stubs.
     $udtlf = TTnew('UserDateTotalListFactory');
     $udtlf->getByUserIdAndPayPeriodIdAndEndDate($this->getUser(), $this->getPayPeriod(), $this->getPayPeriodObject()->getEndDate());
     $dock_absence_time = 0;
     $paid_absence_time = 0;
     $dock_absence_amount = 0;
     $paid_absence_amount = 0;
     $prev_wage_effective_date = 0;
     if ($udtlf->getRecordCount() > 0) {
         foreach ($udtlf as $udt_obj) {
             Debug::text('User Total Row... Type: ' . $udt_obj->getType() . ' OverTime Policy ID: ' . $udt_obj->getOverTimePolicyID() . ' User Wage ID: ' . $udt_obj->getColumn('user_wage_id'), __FILE__, __LINE__, __METHOD__, 10);
             if ($udt_obj->getStatus() == 10 and $udt_obj->getType() == 20) {
                 //Regular Time
                 Debug::text('User Total Row... Regular Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10);
                 //Check if they are a salary user...
                 //Use WORKED time to calculate regular time. Not just regular time.
                 if (is_object($this->getUserWageObject($udt_obj->getColumn('user_wage_id'))) and $this->getUserWageObject($udt_obj->getColumn('user_wage_id'))->getType() != 10) {
                     //Salary
                     Debug::text('Strict Salary Wage: Reduce Regular Pay By: Dock Time: ' . $dock_absence_time . ' and Paid Absence: ' . $paid_absence_time, __FILE__, __LINE__, __METHOD__, 10);
                     if (isset($dock_absence_amount_arr[$udt_obj->getColumn('user_wage_id')])) {
                         $dock_absence_wage = $dock_absence_amount_arr[$udt_obj->getColumn('user_wage_id')];
                     } else {
                         $dock_absence_wage = 0;
                     }
                     if (isset($reduce_salary_absence_amount_arr[$udt_obj->getColumn('user_wage_id')])) {
                         $paid_absence_wage = $reduce_salary_absence_amount_arr[$udt_obj->getColumn('user_wage_id')];
                     } else {
                         $paid_absence_wage = 0;
                     }
                     Debug::text('Wage ID: ' . $udt_obj->getColumn('user_wage_id') . ' Dock Absence Wage: ' . $dock_absence_wage . ' Paid Absence Wage: ' . $paid_absence_wage, __FILE__, __LINE__, __METHOD__, 10);
                     $maximum_wage_salary = UserWageFactory::proRateSalary($this->getMaximumPayPeriodWage($udt_obj->getColumn('user_wage_id')), $this->getUserWageObject($udt_obj->getColumn('user_wage_id'))->getEffectiveDate(), $prev_wage_effective_date, $this->getPayPeriodObject()->getStartDate(), $this->getPayPeriodObject()->getEndDate(), $this->getUserObject()->getTerminationDate());
                     $prev_wage_effective_date = $this->getUserWageObject($udt_obj->getColumn('user_wage_id'))->getEffectiveDate();
                     $amount = bcsub($maximum_wage_salary, bcadd($dock_absence_wage, $paid_absence_wage));
                     $total_time = $udt_obj->getTotalTime();
                     //Dont minus dock/paid absence time. Because its already not included.
                     $rate = NULL;
                     $pay_stub_entry = $this->getPayStubEntryAccountLinkObject()->getRegularTime();
                     unset($dock_absence_wage, $paid_absence_wage);
                 } else {
                     //Hourly
                     Debug::text('Hourly or Hourly + Bonus Wage', __FILE__, __LINE__, __METHOD__, 10);
                     $pay_stub_entry = $this->getPayStubEntryAccountLinkObject()->getRegularTime();
                     $total_time = $udt_obj->getTotalTime();
                     $rate = $this->getHourlyRate($udt_obj->getColumn('user_wage_id'));
                     $amount = $this->getWage($udt_obj->getTotalTime(), $rate);
                 }
                 Debug::text('aPay Stub Entry Account ID: ' . $pay_stub_entry . ' Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
             } elseif ($udt_obj->getStatus() == 10 and $udt_obj->getType() == 30) {
                 //Overtime
                 Debug::text('User Total Row... Overtime Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10);
                 //Get overtime policy info. Allow negative rates so they withdraw from pay stub accounts.
                 if ($udt_obj->getOverTimePolicyObject()->getRate() != 0) {
                     Debug::text('Paid Overtime Time Policy... Rate: ' . $udt_obj->getOverTimePolicyObject()->getRate(), __FILE__, __LINE__, __METHOD__, 10);
                     $pay_stub_entry = $udt_obj->getOverTimePolicyObject()->getPayStubEntryAccountId();
                     $total_time = $udt_obj->getTotalTime();
                     $rate = bcmul($this->getHourlyRate($udt_obj->getColumn('over_time_policy_wage_id')), $udt_obj->getOverTimePolicyObject()->getRate());
                     $amount = $this->getWage($udt_obj->getTotalTime(), $rate);
                     Debug::text('bPay Stub Entry Account ID: ' . $pay_stub_entry . ' Amount: ' . $amount . ' Rate: ' . $rate, __FILE__, __LINE__, __METHOD__, 10);
                 } else {
                     Debug::text('NOT Paid Overtime Time Policy: ', __FILE__, __LINE__, __METHOD__, 10);
                 }
             } elseif ($udt_obj->getStatus() == 30 and $udt_obj->getType() == 10) {
                 //Absence
                 Debug::text('User Total Row... Absence Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10);
                 if (is_object($udt_obj->getAbsencePolicyObject()) and ($udt_obj->getAbsencePolicyObject()->getType() == 10 or $udt_obj->getAbsencePolicyObject()->getType() == 12) and $udt_obj->getAbsencePolicyObject()->getPayStubEntryAccountID() != '') {
                     //Paid
                     Debug::text('Paid Absence Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10);
                     $pay_stub_entry = (int) $udt_obj->getAbsencePolicyObject()->getPayStubEntryAccountID();
                     $total_time = $udt_obj->getTotalTime();
                     $rate = bcmul($this->getHourlyRate($udt_obj->getColumn('absence_policy_wage_id')), $udt_obj->getAbsencePolicyObject()->getRate());
                     $amount = $this->getWage($udt_obj->getTotalTime(), $rate);
                     //$rate = $this->getHourlyRate( $udt_obj->getColumn('absence_policy_wage_id') );
                     //$amount = $this->getWage( $udt_obj->getTotalTime(), $this->getHourlyRate( $udt_obj->getColumn('absence_policy_wage_id') ) );
                     //Debug::text('Paid Absence Info: '. $udt_obj->getTotalTime() , __FILE__, __LINE__, __METHOD__,10);
                     Debug::text('cPay Stub Entry Account ID: ' . $pay_stub_entry . ' Amount: ' . $amount . ' Rate: ' . $rate, __FILE__, __LINE__, __METHOD__, 10);
                     $paid_absence_time = bcadd($paid_absence_time, $udt_obj->getTotalTime());
                     $paid_absence_amount = bcadd($paid_absence_amount, $amount);
                     //Make sure we add the amount below. Incase there are two or more
                     //entries for a paid absence in the same user_wage_id on one pay stub.
                     if (!isset($paid_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')])) {
                         $paid_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')] = 0;
                     }
                     $paid_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')] = bcadd($paid_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')], $amount);
                     //Some paid absences are over and above employees salary, so we need to track them separately.
                     //So we only reduce the salary of the amount of regular paid absences, not "Paid (Above Salary)" absences.
                     if (!isset($reduce_salary_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')])) {
                         $reduce_salary_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')] = 0;
                     }
                     if ($udt_obj->getAbsencePolicyObject()->getType() == 10) {
                         $reduce_salary_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')] = bcadd($reduce_salary_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')], $amount);
                     }
                 } elseif (is_object($udt_obj->getAbsencePolicyObject()) and $udt_obj->getAbsencePolicyObject()->getType() == 30) {
                     $dock_absence_time = bcadd($dock_absence_time, $udt_obj->getTotalTime());
                     $rate = bcmul($this->getHourlyRate($udt_obj->getColumn('absence_policy_wage_id')), $udt_obj->getAbsencePolicyObject()->getRate());
                     $amount = $this->getWage($udt_obj->getTotalTime(), $rate);
                     //$amount = $this->getWage( $udt_obj->getTotalTime(), $this->getHourlyRate( $udt_obj->getColumn('absence_policy_wage_id') ) );
                     $dock_absence_amount = bcadd($dock_absence_amount, $amount);
                     $dock_absence_amount_arr[$udt_obj->getColumn('absence_policy_wage_id')] = $amount;
                     Debug::text('DOCK Absence Time.. Adding: ' . $udt_obj->getTotalTime() . ' Total: ' . $dock_absence_time . ' Rate: ' . $rate, __FILE__, __LINE__, __METHOD__, 10);
                     unset($rate);
                 }
             } elseif ($udt_obj->getStatus() == 10 and $udt_obj->getType() == 40) {
                 //Premium
                 Debug::text('User Total Row... Premium Time: ' . $udt_obj->getTotalTime(), __FILE__, __LINE__, __METHOD__, 10);
                 //Get premium policy info.
                 if (is_object($udt_obj->getPremiumPolicyObject()) and $udt_obj->getPremiumPolicyObject()->getRate() != 0) {
                     Debug::text('Paid Premium Time Policy... Rate: ' . $udt_obj->getPremiumPolicyObject()->getRate(), __FILE__, __LINE__, __METHOD__, 10);
                     $rate = $udt_obj->getPremiumPolicyObject()->getHourlyRate($this->getHourlyRate($udt_obj->getColumn('premium_policy_wage_id')));
                     $pay_stub_entry = $udt_obj->getPremiumPolicyObject()->getPayStubEntryAccountId();
                     $total_time = $udt_obj->getTotalTime();
                     $amount = $this->getWage($udt_obj->getTotalTime(), $rate);
                     Debug::text('dPay Stub Entry Account ID: ' . $pay_stub_entry . ' Amount: ' . $amount . ' Rate: ' . $rate, __FILE__, __LINE__, __METHOD__, 10);
                 } else {
                     Debug::text('NOT Paid Premium Time Policy: ', __FILE__, __LINE__, __METHOD__, 10);
                 }
             }
             if (isset($pay_stub_entry) and $pay_stub_entry != '') {
                 Debug::text('zPay Stub Entry Account ID: ' . $pay_stub_entry . ' Amount: ' . $amount, __FILE__, __LINE__, __METHOD__, 10);
                 $ret_arr['entries'][] = array('user_wage_id' => $udt_obj->getColumn('user_wage_id'), 'pay_stub_entry' => $pay_stub_entry, 'total_time' => $total_time, 'amount' => $amount, 'rate' => $rate);
             }
             unset($pay_stub_entry, $amount, $total_time, $rate);
         }
     } else {
         Debug::text('NO UserDate Total entries found.', __FILE__, __LINE__, __METHOD__, 10);
     }
     $ret_arr['other']['paid_absence_time'] = $paid_absence_time;
     $ret_arr['other']['dock_absence_time'] = $dock_absence_time;
     $ret_arr['other']['paid_absence_amount'] = $paid_absence_amount;
     $ret_arr['other']['dock_absence_amount'] = $dock_absence_amount;
     if (isset($ret_arr)) {
         Debug::Arr($ret_arr, 'UserDateTotal Array', __FILE__, __LINE__, __METHOD__, 10);
         return $this->user_date_total_arr = $ret_arr;
     }
     return FALSE;
 }
 function getUserLaborBurdenPercent($user_id)
 {
     global $current_company;
     if (!is_object($current_company)) {
         return FALSE;
     }
     if ($user_id == '') {
         return '0.00';
     }
     $retval = UserWageFactory::calculateLaborBurdenPercent($current_company->getId(), $user_id);
     if ($retval == '') {
         return '0.00';
     }
     return $retval;
 }
    function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        $additional_order_fields = array('schedule_policy_id', 'schedule_policy', 'first_name', 'last_name', 'user_status_id', 'group_id', 'group', 'title_id', 'title', 'default_branch_id', 'default_branch', 'default_department_id', 'default_department', 'total_time', 'date_stamp', 'pay_period_id');
        if ($order == NULL) {
            $order = array('c.pay_period_id' => 'asc', 'c.user_id' => 'asc', 'a.start_time' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_user_ids'];
        }
        if (isset($filter_data['include_user_ids'])) {
            $filter_data['id'] = $filter_data['include_user_ids'];
        }
        if (isset($filter_data['user_status_ids'])) {
            $filter_data['status_id'] = $filter_data['user_status_ids'];
        }
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['default_branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['default_branch_ids'];
        }
        if (isset($filter_data['default_department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['default_department_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['exclude_job_ids'])) {
            $filter_data['exclude_id'] = $filter_data['exclude_job_ids'];
        }
        if (isset($filter_data['include_job_ids'])) {
            $filter_data['include_job_id'] = $filter_data['include_job_ids'];
        }
        if (isset($filter_data['job_group_ids'])) {
            $filter_data['job_group_id'] = $filter_data['job_group_ids'];
        }
        if (isset($filter_data['job_item_ids'])) {
            $filter_data['job_item_id'] = $filter_data['job_item_ids'];
        }
        if (isset($filter_data['pay_period_ids'])) {
            $filter_data['pay_period_id'] = $filter_data['pay_period_ids'];
        }
        if (isset($filter_data['start_time'])) {
            $filter_data['start_date'] = $filter_data['start_time'];
        }
        if (isset($filter_data['end_time'])) {
            $filter_data['end_date'] = $filter_data['end_time'];
        }
        $spf = new SchedulePolicyFactory();
        $uf = new UserFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        $udf = new UserDateFactory();
        $uwf = new UserWageFactory();
        if (getTTProductEdition() == TT_PRODUCT_PROFESSIONAL) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('company_id' => $company_id);
        $query = '
					select
							a.id as id,
							a.id as schedule_id,
							a.status_id as status_id,
							a.start_time as start_time,
							a.end_time as end_time,

							a.user_date_id as user_date_id,
							a.branch_id as branch_id,
							j.name as branch,
							a.department_id as department_id,
							k.name as department,
							a.job_id as job_id,
							a.job_item_id as job_item_id,
							a.total_time as total_time,
							a.schedule_policy_id as schedule_policy_id,
							a.absence_policy_id as absence_policy_id,

							i.name as schedule_policy,

							c.user_id as user_id,
							c.date_stamp as date_stamp,
							c.pay_period_id as pay_period_id,

							d.first_name as first_name,
							d.last_name as last_name,
							d.status_id as user_status_id,
							d.group_id as group_id,
							g.name as group,
							d.title_id as title_id,
							h.name as title,
							d.default_branch_id as default_branch_id,
							e.name as default_branch,
							d.default_department_id as default_department_id,
							f.name as default_department,
							d.created_by as user_created_by,

							m.id as user_wage_id,
							m.effective_date as user_wage_effective_date,

							y.first_name as created_by_first_name,
							y.middle_name as created_by_middle_name,
							y.last_name as created_by_last_name,
							z.first_name as updated_by_first_name,
							z.middle_name as updated_by_middle_name,
							z.last_name as updated_by_last_name';
        if (getTTProductEdition() == TT_PRODUCT_PROFESSIONAL) {
            $query .= ',
						w.name as job_name,
						w.status_id as job_status_id,
						w.manual_id as job_manual_id,
						w.branch_id as job_branch_id,
						w.department_id as job_department_id,
						w.group_id as job_group_id';
        }
        $query .= '
					from 	' . $this->getTable() . ' as a
							LEFT JOIN ' . $spf->getTable() . ' as i ON a.schedule_policy_id = i.id
							LEFT JOIN ' . $udf->getTable() . ' as c ON a.user_date_id = c.id
							LEFT JOIN ' . $uf->getTable() . ' as d ON c.user_id = d.id

							LEFT JOIN ' . $bf->getTable() . ' as e ON ( d.default_branch_id = e.id AND e.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as f ON ( d.default_department_id = f.id AND f.deleted = 0)
							LEFT JOIN ' . $ugf->getTable() . ' as g ON ( d.group_id = g.id AND g.deleted = 0 )
							LEFT JOIN ' . $utf->getTable() . ' as h ON ( d.title_id = h.id AND h.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as j ON ( a.branch_id = j.id AND j.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as k ON ( a.department_id = k.id AND k.deleted = 0)

							LEFT JOIN ' . $uwf->getTable() . ' as m ON m.id = (select m.id
																		from ' . $uwf->getTable() . ' as m
																		where m.user_id = c.user_id
																			and m.effective_date <= c.date_stamp
																			and m.deleted = 0
																			order by m.effective_date desc limit 1)
					';
        if (getTTProductEdition() == TT_PRODUCT_PROFESSIONAL) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as w ON a.job_id = w.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as x ON a.job_item_id = x.id';
        }
        $query .= '
						LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
						LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
					WHERE d.company_id = ?';
        if (isset($filter_data['permission_children_ids']) and isset($filter_data['permission_children_ids'][0]) and !in_array(-1, (array) $filter_data['permission_children_ids'])) {
            $query .= ' AND d.id in (' . $this->getListSQL($filter_data['permission_children_ids'], $ph) . ') ';
        }
        if (isset($filter_data['id']) and isset($filter_data['id'][0]) and !in_array(-1, (array) $filter_data['id'])) {
            $query .= ' AND a.id in (' . $this->getListSQL($filter_data['id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_id']) and isset($filter_data['exclude_id'][0]) and !in_array(-1, (array) $filter_data['exclude_id'])) {
            $query .= ' AND d.id not in (' . $this->getListSQL($filter_data['exclude_id'], $ph) . ') ';
        }
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND c.user_id in (' . $this->getListSQL($filter_data['user_id'], $ph) . ') ';
        }
        if (isset($filter_data['user_status_id']) and isset($filter_data['user_status_id'][0]) and !in_array(-1, (array) $filter_data['user_status_id'])) {
            $query .= ' AND d.status_id in (' . $this->getListSQL($filter_data['user_status_id'], $ph) . ') ';
        }
        if (isset($filter_data['group_id']) and isset($filter_data['group_id'][0]) and !in_array(-1, (array) $filter_data['group_id'])) {
            if (isset($filter_data['include_subgroups']) and (bool) $filter_data['include_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['group_id'], TRUE);
            }
            $query .= ' AND d.group_id in (' . $this->getListSQL($filter_data['group_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_branch_id']) and isset($filter_data['default_branch_id'][0]) and !in_array(-1, (array) $filter_data['default_branch_id'])) {
            $query .= ' AND d.default_branch_id in (' . $this->getListSQL($filter_data['default_branch_id'], $ph) . ') ';
        }
        if (isset($filter_data['default_department_id']) and isset($filter_data['default_department_id'][0]) and !in_array(-1, (array) $filter_data['default_department_id'])) {
            $query .= ' AND d.default_department_id in (' . $this->getListSQL($filter_data['default_department_id'], $ph) . ') ';
        }
        if (isset($filter_data['title_id']) and isset($filter_data['title_id'][0]) and !in_array(-1, (array) $filter_data['title_id'])) {
            $query .= ' AND d.title_id in (' . $this->getListSQL($filter_data['title_id'], $ph) . ') ';
        }
        if (isset($filter_data['branch_id']) and isset($filter_data['branch_id'][0]) and !in_array(-1, (array) $filter_data['branch_id'])) {
            $query .= ' AND a.branch_id in (' . $this->getListSQL($filter_data['branch_id'], $ph) . ') ';
        }
        if (isset($filter_data['department_id']) and isset($filter_data['department_id'][0]) and !in_array(-1, (array) $filter_data['department_id'])) {
            $query .= ' AND a.department_id in (' . $this->getListSQL($filter_data['department_id'], $ph) . ') ';
        }
        if (isset($filter_data['status_id']) and isset($filter_data['status_id'][0]) and !in_array(-1, (array) $filter_data['status_id'])) {
            $query .= ' AND a.status_id in (' . $this->getListSQL($filter_data['status_id'], $ph) . ') ';
        }
        if (isset($filter_data['schedule_policy_id']) and isset($filter_data['schedule_policy_id'][0]) and !in_array(-1, (array) $filter_data['schedule_policy_id'])) {
            $query .= ' AND a.schedule_policy_id in (' . $this->getListSQL($filter_data['schedule_policy_id'], $ph) . ') ';
        }
        if (isset($filter_data['pay_period_id']) and isset($filter_data['pay_period_id'][0]) and !in_array(-1, (array) $filter_data['pay_period_id'])) {
            $query .= ' AND c.pay_period_id in (' . $this->getListSQL($filter_data['pay_period_id'], $ph) . ') ';
        }
        //Use the job_id in the schedule table so we can filter by '0' or No Job
        if (isset($filter_data['include_job_id']) and isset($filter_data['include_job_id'][0]) and !in_array(-1, (array) $filter_data['include_job_id'])) {
            $query .= ' AND a.job_id in (' . $this->getListSQL($filter_data['include_job_id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_job_id']) and isset($filter_data['exclude_job_id'][0]) and !in_array(-1, (array) $filter_data['exclude_job_id'])) {
            $query .= ' AND a.job_id not in (' . $this->getListSQL($filter_data['exclude_job_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_group_id']) and isset($filter_data['job_group_id'][0]) and !in_array(-1, (array) $filter_data['job_group_id'])) {
            if (isset($filter_data['include_job_subgroups']) and (bool) $filter_data['include_job_subgroups'] == TRUE) {
                $uglf = new UserGroupListFactory();
                $filter_data['job_group_id'] = $uglf->getByCompanyIdAndGroupIdAndjob_subgroupsArray($company_id, $filter_data['job_group_id'], TRUE);
            }
            $query .= ' AND w.group_id in (' . $this->getListSQL($filter_data['job_group_id'], $ph) . ') ';
        }
        if (isset($filter_data['job_item_id']) and isset($filter_data['job_item_id'][0]) and !in_array(-1, (array) $filter_data['job_item_id'])) {
            $query .= ' AND b.job_item_id in (' . $this->getListSQL($filter_data['job_item_id'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindTimeStamp($filter_data['start_date']);
            $query .= ' AND a.start_time >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindTimeStamp($filter_data['end_date']);
            $query .= ' AND a.start_time <= ?';
        }
        if (isset($filter_data['created_by']) and isset($filter_data['created_by'][0]) and !in_array(-1, (array) $filter_data['created_by'])) {
            $query .= ' AND a.created_by in (' . $this->getListSQL($filter_data['created_by'], $ph) . ') ';
        }
        if (isset($filter_data['updated_by']) and isset($filter_data['updated_by'][0]) and !in_array(-1, (array) $filter_data['updated_by'])) {
            $query .= ' AND a.updated_by in (' . $this->getListSQL($filter_data['updated_by'], $ph) . ') ';
        }
        $query .= '
						AND (a.deleted = 0 AND c.deleted = 0 AND d.deleted = 0)
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        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;
    }
}
$smarty->assign('title', TTi18n::gettext($title = 'Edit Employee Wage'));
// See index.php
/*
 * Get FORM variables
 */
extract(FormVariables::GetVariables(array('action', 'id', 'user_id', 'saved_search_id', 'wage_data')));
if (isset($wage_data)) {
    if ($wage_data['effective_date'] != '') {
        $wage_data['effective_date'] = TTDate::parseDateTime($wage_data['effective_date']);
    }
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$hlf = new HierarchyListFactory();
$permission_children_ids = $hlf->getHierarchyChildrenByCompanyIdAndUserIdAndObjectTypeID($current_company->getId(), $current_user->getId());
$uwf = new UserWageFactory();
$ulf = new UserListFactory();
$action = Misc::findSubmitButton();
$action = strtolower($action);
switch ($action) {
    case 'submit':
        Debug::Text('Submit!', __FILE__, __LINE__, __METHOD__, 10);
        $ulf->getByIdAndCompanyId($user_id, $current_company->getId());
        if ($ulf->getRecordCount() > 0) {
            $user_obj = $ulf->getCurrent();
            $is_owner = $permission->isOwner($user_obj->getCreatedBy(), $user_obj->getID());
            $is_child = $permission->isChild($user_obj->getId(), $permission_children_ids);
            if ($permission->Check('wage', 'edit') or $permission->Check('wage', 'edit_own') and $is_owner === TRUE or $permission->Check('wage', 'edit_child') and $is_child === TRUE) {
                $uwf->setId($wage_data['id']);
                $uwf->setUser($user_id);
                $uwf->setWageGroup($wage_data['wage_group_id']);
    function getScheduleSummaryReportByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        $additional_order_fields = array('schedule_policy_id', 'schedule_policy', 'first_name', 'last_name', 'user_status_id', 'group_id', 'group', 'title_id', 'title', 'default_branch_id', 'default_branch', 'default_department_id', 'default_department', 'total_time', 'date_stamp', 'pay_period_id');
        $sort_column_aliases = array('updated_date' => 'a.updated_date', 'created_date' => 'a.created_date');
        $order = $this->getColumnsFromAliases($order, $sort_column_aliases);
        if ($order == NULL) {
            $order = array('c.pay_period_id' => 'asc', 'c.user_id' => 'asc', 'a.start_time' => 'asc');
            $strict = FALSE;
        } else {
            $strict = TRUE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        if (isset($filter_data['exclude_user_ids'])) {
            $filter_data['exclude_user_id'] = $filter_data['exclude_user_ids'];
        }
        if (isset($filter_data['include_user_ids'])) {
            $filter_data['include_user_id'] = $filter_data['include_user_ids'];
        }
        if (isset($filter_data['user_status_ids'])) {
            $filter_data['user_status_id'] = $filter_data['user_status_ids'];
        }
        if (isset($filter_data['user_title_ids'])) {
            $filter_data['title_id'] = $filter_data['user_title_ids'];
        }
        if (isset($filter_data['group_ids'])) {
            $filter_data['group_id'] = $filter_data['group_ids'];
        }
        if (isset($filter_data['default_branch_ids'])) {
            $filter_data['default_branch_id'] = $filter_data['default_branch_ids'];
        }
        if (isset($filter_data['default_department_ids'])) {
            $filter_data['default_department_id'] = $filter_data['default_department_ids'];
        }
        if (isset($filter_data['branch_ids'])) {
            $filter_data['branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['department_id'] = $filter_data['department_ids'];
        }
        if (isset($filter_data['schedule_branch_id'])) {
            $filter_data['branch_id'] = $filter_data['schedule_branch_id'];
        }
        if (isset($filter_data['schedule_department_id'])) {
            $filter_data['department_id'] = $filter_data['schedule_department_id'];
        }
        if (isset($filter_data['exclude_job_ids'])) {
            $filter_data['exclude_job_id'] = $filter_data['exclude_job_ids'];
        }
        if (isset($filter_data['include_job_ids'])) {
            $filter_data['include_job_id'] = $filter_data['include_job_ids'];
        }
        if (isset($filter_data['job_group_ids'])) {
            $filter_data['job_group_id'] = $filter_data['job_group_ids'];
        }
        if (isset($filter_data['job_item_ids'])) {
            $filter_data['job_item_id'] = $filter_data['job_item_ids'];
        }
        if (isset($filter_data['pay_period_ids'])) {
            $filter_data['pay_period_id'] = $filter_data['pay_period_ids'];
        }
        if (isset($filter_data['start_time'])) {
            $filter_data['start_date'] = $filter_data['start_time'];
        }
        if (isset($filter_data['end_time'])) {
            $filter_data['end_date'] = $filter_data['end_time'];
        }
        $spf = new SchedulePolicyFactory();
        $apf = new AbsencePolicyFactory();
        $uf = new UserFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        $udf = new UserDateFactory();
        $ppf = new PayPeriodFactory();
        $uwf = new UserWageFactory();
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $jf = new JobFactory();
            $jif = new JobItemFactory();
        }
        $ph = array('company_id' => $company_id, 'company_id2' => $company_id);
        //"group" is a reserved word in MySQL.
        $query = '
					select
							a.id as id,
							a.id as schedule_id,
							a.status_id as status_id,
							a.start_time as start_time,
							a.end_time as end_time,

							a.user_date_id as user_date_id,
							a.branch_id as branch_id,
							j.name as branch,
							a.department_id as department_id,
							k.name as department,
							a.job_id as job_id,
							a.job_item_id as job_item_id,
							a.total_time as total_time,
							a.schedule_policy_id as schedule_policy_id,
							i.name as schedule_policy,

							a.note as note,

							a.absence_policy_id as absence_policy_id,
							apf.name as absence_policy,
							apf.type_id as absence_policy_type_id,

							c.user_id as user_id,
							c.date_stamp as date_stamp,
							ppf.id as pay_period_id,
							ppf.start_date as pay_period_start_date,
							ppf.end_date as pay_period_end_date,
							ppf.transaction_date as pay_period_transaction_date,

							c.pay_period_id as pay_period_id,

							d.first_name as first_name,
							d.last_name as last_name,
							d.status_id as user_status_id,
							d.group_id as group_id,
							g.name as "group",
							d.title_id as title_id,
							h.name as title,
							d.default_branch_id as default_branch_id,
							e.name as default_branch,
							d.default_department_id as default_department_id,
							f.name as default_department,
							d.created_by as user_created_by,

							m.id as user_wage_id,
							m.hourly_rate as user_wage_hourly_rate,
							m.labor_burden_percent as user_labor_burden_percent,
							m.effective_date as user_wage_effective_date,

							y.first_name as created_by_first_name,
							y.middle_name as created_by_middle_name,
							y.last_name as created_by_last_name,
							z.first_name as updated_by_first_name,
							z.middle_name as updated_by_middle_name,
							z.last_name as updated_by_last_name';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= ',
						w.name as job_name,
						w.status_id as job_status_id,
						w.manual_id as job_manual_id,
						w.branch_id as job_branch_id,
						w.department_id as job_department_id,
						w.group_id as job_group_id';
        }
        $query .= '
					from 	' . $this->getTable() . ' as a
							LEFT JOIN ' . $spf->getTable() . ' as i ON ( a.schedule_policy_id = i.id AND i.deleted = 0)
							LEFT JOIN ' . $apf->getTable() . ' as apf ON ( a.absence_policy_id = apf.id AND apf.deleted = 0)
							LEFT JOIN ' . $udf->getTable() . ' as c ON a.user_date_id = c.id
							LEFT JOIN ' . $ppf->getTable() . ' as ppf ON c.pay_period_id = ppf.id
							LEFT JOIN ' . $uf->getTable() . ' as d ON ( c.user_id = d.id AND d.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as e ON ( d.default_branch_id = e.id AND e.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as f ON ( d.default_department_id = f.id AND f.deleted = 0)
							LEFT JOIN ' . $ugf->getTable() . ' as g ON ( d.group_id = g.id AND g.deleted = 0 )
							LEFT JOIN ' . $utf->getTable() . ' as h ON ( d.title_id = h.id AND h.deleted = 0 )

							LEFT JOIN ' . $bf->getTable() . ' as j ON ( a.branch_id = j.id AND j.deleted = 0)
							LEFT JOIN ' . $df->getTable() . ' as k ON ( a.department_id = k.id AND k.deleted = 0)

							LEFT JOIN ' . $uwf->getTable() . ' as m ON m.id = (select m.id
																		from ' . $uwf->getTable() . ' as m
																		where m.user_id = c.user_id
																			and m.effective_date <= c.date_stamp
																			and m.deleted = 0
																			order by m.effective_date desc limit 1)
					';
        if (getTTProductEdition() >= TT_PRODUCT_CORPORATE) {
            $query .= '	LEFT JOIN ' . $jf->getTable() . ' as w ON a.job_id = w.id';
            $query .= '	LEFT JOIN ' . $jif->getTable() . ' as x ON a.job_item_id = x.id';
        }
        $query .= '
						LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
						LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
					WHERE ( d.company_id = ? OR a.company_id = ? )';
        $query .= isset($filter_data['permission_children_ids']) ? $this->getWhereClauseSQL('d.id', $filter_data['permission_children_ids'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['id']) ? $this->getWhereClauseSQL('a.id', $filter_data['id'], 'numeric_list', $ph) : NULL;
        //Need to include/exclude users based on c.user_id, as we need to support OPEN shifts and user_id=0 which can only happen in user_date table.
        $query .= isset($filter_data['include_user_id']) ? $this->getWhereClauseSQL('c.user_id', $filter_data['include_user_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_user_id']) ? $this->getWhereClauseSQL('c.user_id', $filter_data['exclude_user_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_status_id']) ? $this->getWhereClauseSQL('d.status_id', $filter_data['user_status_id'], 'numeric_list', $ph) : NULL;
        if (isset($filter_data['include_user_subgroups']) and (bool) $filter_data['include_user_subgroups'] == TRUE) {
            $uglf = new UserGroupListFactory();
            $filter_data['group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['group_id'], TRUE);
        }
        $query .= isset($filter_data['group_id']) ? $this->getWhereClauseSQL('d.group_id', $filter_data['group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_branch_id']) ? $this->getWhereClauseSQL('d.default_branch_id', $filter_data['default_branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_department_id']) ? $this->getWhereClauseSQL('d.default_department_id', $filter_data['default_department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['title_id']) ? $this->getWhereClauseSQL('d.title_id', $filter_data['title_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['branch_id']) ? $this->getWhereClauseSQL('a.branch_id', $filter_data['branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['department_id']) ? $this->getWhereClauseSQL('a.department_id', $filter_data['department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['status_id']) ? $this->getWhereClauseSQL('a.status_id', $filter_data['status_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['schedule_policy_id']) ? $this->getWhereClauseSQL('a.pay_period_id', $filter_data['schedule_policy_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['pay_period_id']) ? $this->getWhereClauseSQL('c.pay_period_id', $filter_data['pay_period_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['tag']) ? $this->getWhereClauseSQL('d.id', array('company_id' => $company_id, 'object_type_id' => 200, 'tag' => $filter_data['tag']), 'tag', $ph) : NULL;
        //Use the job_id in the schedule table so we can filter by '0' or No Job
        $query .= isset($filter_data['include_job_id']) ? $this->getWhereClauseSQL('a.job_id', $filter_data['include_job_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_job_id']) ? $this->getWhereClauseSQL('a.job_id', $filter_data['exclude_job_id'], 'not_numeric_list', $ph) : NULL;
        if (isset($filter_data['include_job_subgroups']) and (bool) $filter_data['include_job_subgroups'] == TRUE) {
            $jglf = new JobGroupListFactory();
            $filter_data['job_group_id'] = $jglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['job_group_id'], TRUE);
        }
        $query .= isset($filter_data['job_group_id']) ? $this->getWhereClauseSQL('w.group_id', $filter_data['job_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['job_item_id']) ? $this->getWhereClauseSQL('a.job_item_id', $filter_data['job_item_id'], 'numeric_list', $ph) : NULL;
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindTimeStamp($filter_data['start_date']);
            $query .= ' AND a.start_time >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindTimeStamp($filter_data['end_date']);
            $query .= ' AND a.start_time <= ?';
        }
        $query .= isset($filter_data['created_by']) ? $this->getWhereClauseSQL(array('a.created_by', 'y.first_name', 'y.last_name'), $filter_data['created_by'], 'user_id_or_name', $ph) : NULL;
        $query .= isset($filter_data['updated_by']) ? $this->getWhereClauseSQL(array('a.updated_by', 'z.first_name', 'z.last_name'), $filter_data['updated_by'], 'user_id_or_name', $ph) : NULL;
        $query .= '
						AND (a.deleted = 0 AND c.deleted = 0 )
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        //Debug::Arr($ph,'Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }