function getTimesheetDetailReportByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        //$order = array( 'b.pay_period_id' => 'asc', 'b.user_id' => 'asc' );
        //$order = array( 'b.pay_period_id' => 'asc','uf.last_name' => 'asc', 'b.date_stamp' => 'asc' );
        /*
        if ( $order == NULL ) {
        	$order = array( 'b.pay_period_id' => 'asc', 'b.user_id' => 'asc' );
        	$strict = FALSE;
        } else {
        	$strict = TRUE;
        }
        */
        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['branch_ids'])) {
            $filter_data['branch_id'] = $filter_data['branch_ids'];
        }
        if (isset($filter_data['department_ids'])) {
            $filter_data['department_id'] = $filter_data['department_ids'];
        }
        $uf = new UserFactory();
        $udf = new UserDateFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ppf_b = new PayPeriodFactory();
        $uwf = new UserWageFactory();
        $pcf = new PunchControlFactory();
        $pf = new PunchFactory();
        $otpf = new OverTimePolicyFactory();
        $apf = new AbsencePolicyFactory();
        $ppf = new PremiumPolicyFactory();
        $ph = array('company_id' => $company_id);
        //Make it so employees with 0 hours still show up!! Very important!
        //Order dock hours first, so it can be deducted from regular time.
        //Show Min/Max punches based on day/branch/department, so we can split reports out day/branch/department and still show
        //  when the employee punched in/out for each.
        $query = '
					select
							b.user_id as user_id,
							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,
							b.date_stamp as date_stamp,
							bf.name as branch,
							df.name as department,
							a.status_id as status_id,
							a.type_id as type_id,

							a.over_time_policy_id as over_time_policy_id,
							a.absence_policy_id as absence_policy_id,
							a.premium_policy_id as premium_policy_id,

							z.hourly_rate as hourly_rate,
							z.labor_burden_percent as labor_burden_percent,

							min_punch.time_stamp as min_punch_time_stamp,
							max_punch.time_stamp as max_punch_time_stamp,
							sum(total_time) as total_time,
							sum(actual_total_time) as actual_total_time
					from	' . $this->getTable() . ' as a
					LEFT JOIN ' . $udf->getTable() . ' as b ON a.user_date_id = b.id
					LEFT JOIN ' . $uf->getTable() . ' as uf ON b.user_id = uf.id

					LEFT JOIN ' . $bf->getTable() . ' as bf ON a.branch_id = bf.id
					LEFT JOIN ' . $df->getTable() . ' as df ON a.department_id = df.id

					LEFT JOIN ' . $ppf_b->getTable() . ' as ppf ON b.pay_period_id = ppf.id

					LEFT JOIN ' . $otpf->getTable() . ' as m ON (a.over_time_policy_id = m.id AND m.deleted = 0)
					LEFT JOIN ' . $apf->getTable() . ' as o ON (a.absence_policy_id = o.id AND o.deleted = 0)
					LEFT JOIN ' . $ppf->getTable() . ' as q ON (a.premium_policy_id = q.id AND q.deleted = 0)

					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.wage_group_id = (CASE WHEN a.over_time_policy_id > 0 THEN m.wage_group_id
																									ELSE
																										CASE WHEN a.absence_policy_id > 0 THEN o.wage_group_id ELSE
																											CASE WHEN a.premium_policy_id > 0 THEN q.wage_group_id ELSE 0 END
																										END
																									END)
																			and z.deleted = 0
																			order by z.effective_date desc limit 1)

					LEFT JOIN ' . $pf->getTable() . ' as min_punch ON min_punch.id = (
																		select pf_a.id
																		from ' . $pf->getTable() . ' as pf_a
																		LEFT JOIN ' . $pcf->getTable() . ' as pcf_a ON pf_a.punch_control_id = pcf_a.id
																		WHERE pcf_a.user_date_id = a.user_date_id
																			AND pcf_a.branch_id = a.branch_id
																			AND pcf_a.department_id = a.department_id
																			AND pf_a.status_id = 10
																		ORDER BY pf_a.time_stamp ASC
																		LIMIT 1
																		)

					LEFT JOIN ' . $pf->getTable() . ' as max_punch ON max_punch.id = (
																		select pf_a.id
																		from ' . $pf->getTable() . ' as pf_a
																		LEFT JOIN ' . $pcf->getTable() . ' as pcf_a ON pf_a.punch_control_id = pcf_a.id
																		WHERE pcf_a.user_date_id = a.user_date_id
																			AND pcf_a.branch_id = a.branch_id
																			AND pcf_a.department_id = a.department_id
																			AND pf_a.status_id = 20
																		ORDER BY pf_a.time_stamp DESC
																		LIMIT 1
																		)

					where 	uf.company_id = ? ';
        $query .= isset($filter_data['permission_children_ids']) ? $this->getWhereClauseSQL('uf.id', $filter_data['permission_children_ids'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['include_user_id']) ? $this->getWhereClauseSQL('uf.id', $filter_data['include_user_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['exclude_user_id']) ? $this->getWhereClauseSQL('uf.id', $filter_data['exclude_user_id'], 'not_numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_status_id']) ? $this->getWhereClauseSQL('uf.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['user_group_id'] = $uglf->getByCompanyIdAndGroupIdAndSubGroupsArray($company_id, $filter_data['user_group_id'], TRUE);
        }
        $query .= isset($filter_data['user_group_id']) ? $this->getWhereClauseSQL('uf.group_id', $filter_data['user_group_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_branch_id']) ? $this->getWhereClauseSQL('uf.default_branch_id', $filter_data['default_branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['default_department_id']) ? $this->getWhereClauseSQL('uf.default_department_id', $filter_data['default_department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['user_title_id']) ? $this->getWhereClauseSQL('uf.title_id', $filter_data['user_title_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['punch_branch_id']) ? $this->getWhereClauseSQL('a.branch_id', $filter_data['punch_branch_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['punch_department_id']) ? $this->getWhereClauseSQL('a.department_id', $filter_data['punch_department_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['pay_period_id']) ? $this->getWhereClauseSQL('b.pay_period_id', $filter_data['pay_period_id'], 'numeric_list', $ph) : NULL;
        $query .= isset($filter_data['tag']) ? $this->getWhereClauseSQL('uf.id', array('company_id' => $company_id, 'object_type_id' => 200, 'tag' => $filter_data['tag']), 'tag', $ph) : NULL;
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND b.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND b.date_stamp <= ?';
        }
        //This isn't needed as it lists every status: AND a.status_id in (10,20,30)
        $query .= '
						AND ( a.deleted = 0 AND b.deleted = 0 )
					group by b.user_id, ppf.id, ppf.start_date, ppf.end_date, ppf.transaction_date, bf.name, df.name, b.date_stamp, z.hourly_rate, z.labor_burden_percent, a.status_id, a.type_id, a.over_time_policy_id, a.absence_policy_id, a.premium_policy_id, min_punch.time_stamp, max_punch.time_stamp
					';
        $query .= $this->getSortSQL($order, FALSE);
        $this->ExecuteSQL($query, $ph);
        //Debug::Arr($ph, 'Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        return $this;
    }
    function getDayReportByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        //$order = array( 'b.pay_period_id' => 'asc', 'b.user_id' => 'asc' );
        $order = array('tmp.pay_period_id' => 'asc', 'z.last_name' => 'asc', 'tmp.date_stamp' => '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();
        $pcf = new PunchControlFactory();
        $pf = new PunchFactory();
        $otpf = new OverTimePolicyFactory();
        $apf = new AbsencePolicyFactory();
        $ppf = new PremiumPolicyFactory();
        $ph = array();
        //Make it so employees with 0 hours still show up!! Very important!
        //Order dock hours first, so it can be deducted from regular time.
        $query = '
				select z.id, tmp.*
				from ' . $ulf->getTable() . ' as z
				LEFT JOIN

					( select
							b.user_id,
							b.pay_period_id as pay_period_id,
							b.date_stamp as date_stamp,
							a.branch_id as branch_id,
							a.department_id as department_id,
							a.status_id as status_id,
							a.type_id as type_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,
							tmp2.min_punch_time_stamp as min_punch_time_stamp,
							tmp2.max_punch_time_stamp as max_punch_time_stamp,
							sum(total_Time) as total_time,
							sum(actual_total_Time) as actual_total_time
					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)
					LEFT JOIN (
						select tmp3.id, min(tmp3.min_punch_time_stamp) as min_punch_time_stamp, max(tmp3.max_punch_time_stamp) as max_punch_time_stamp from (
							select tmp2_a.id,
								CASE WHEN tmp2_c.status_id = 10 THEN min(tmp2_c.time_stamp) ELSE NULL END as min_punch_time_stamp,
								CASE WHEN tmp2_c.status_id = 20 THEN max(tmp2_c.time_stamp) ELSE NULL END as max_punch_time_stamp
								from ' . $udf->getTable() . ' as tmp2_a
								LEFT JOIN ' . $pcf->getTable() . ' as tmp2_b ON tmp2_a.id = tmp2_b.user_date_id
								LEFT JOIN ' . $pf->getTable() . ' as tmp2_c ON tmp2_b.id = tmp2_c.punch_control_id
								WHERE 1=1 ';
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND tmp2_a.user_id in (' . $this->getListSQL($filter_data['user_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 tmp2_a.pay_period_id in (' . $this->getListSQL($filter_data['pay_period_ids'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND tmp2_a.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND tmp2_a.date_stamp <= ?';
        }
        $query .= '
									AND tmp2_c.time_stamp is not null
									AND ( tmp2_a.deleted = 0 AND tmp2_b.deleted = 0 AND tmp2_c.deleted = 0 )
								group by tmp2_a.id, tmp2_c.status_id
							) as tmp3 group by tmp3.id
					) as tmp2 ON b.id = tmp2.id

					where 	1=1 ';
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND b.user_id in (' . $this->getListSQL($filter_data['user_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 b.pay_period_id in (' . $this->getListSQL($filter_data['pay_period_ids'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND b.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND b.date_stamp <= ?';
        }
        $ph[] = $company_id;
        $query .= '
						AND a.status_id in (10,20,30)
						AND ( a.deleted = 0 AND b.deleted = 0 )
					group by b.user_id, b.pay_period_id, a.branch_id, a.department_id, b.date_stamp, 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.over_time_policy_id, a.absence_policy_id, a.premium_policy_id, tmp2.min_punch_time_stamp, tmp2.max_punch_time_stamp
					) as tmp ON z.id = tmp.user_id
				WHERE z.company_id = ? ';
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND z.id in (' . $this->getListSQL($filter_data['user_id'], $ph) . ') ';
        }
        $query .= ' AND z.deleted = 0 ';
        $query .= $this->getSortSQL($order, FALSE);
        $this->rs = $this->db->Execute($query, $ph);
        return $this;
    }
    function getByPunchId($punch_id, $order = NULL)
    {
        if ($punch_id == '') {
            return FALSE;
        }
        $pf = new PunchFactory();
        $ph = array('punch_id' => (int) $punch_id);
        $query = '
					select 	a.*
					from	' . $this->getTable() . ' as a,
							' . $pf->getTable() . ' as b
					where 	a.id = b.punch_control_id
						AND b.id = ?
						AND ( a.deleted = 0 AND b.deleted=0 )
					';
        $query .= $this->getSortSQL($order);
        $this->ExecuteSQL($query, $ph);
        return $this;
    }
Esempio n. 4
0
 //Debug::setVerbosity(11);
 $init_progress_bar = TRUE;
 if ($init_progress_bar == TRUE) {
     InitProgressBar();
     $init_progress_bar = FALSE;
 }
 $progress_bar->setValue(0);
 $progress_bar->display();
 //This will be slightly off depending on which days of the week they choose.
 $total_punches = count($filter_user_id) * TTDate::getDays($data['end_punch_full_time_stamp'] - $data['start_punch_full_time_stamp']);
 Debug::Text('Total Punches: ' . $total_punches . ' Users: ' . count($filter_user_id) . ' Days: ' . TTDate::getDays($data['end_punch_full_time_stamp'] - $data['start_punch_full_time_stamp']), __FILE__, __LINE__, __METHOD__, 10);
 $pcf = new PunchControlFactory();
 $pf = new PunchFactory();
 $ulf = new UserListFactory();
 $pf->StartTransaction();
 TTLog::addEntry($current_user->getId(), 500, 'Mass Punch: Time: ' . TTDate::getDate('TIME', $data['start_punch_full_time_stamp']) . ' Total Employees: ' . count($filter_user_id) . ' Total Days: ' . round(TTDate::getDays($data['end_punch_full_time_stamp'] - $data['start_punch_full_time_stamp'])), $current_user->getId(), $pf->getTable());
 $time_stamp = $data['start_punch_full_time_stamp'];
 $x = 0;
 while ($time_stamp <= $data['end_punch_full_time_stamp']) {
     if (isset($data['dow'][TTDate::getDayOfWeek($time_stamp)]) and $data['dow'][TTDate::getDayOfWeek($time_stamp)] == 1) {
         foreach ($filter_user_id as $user_id) {
             $ulf->getByIdAndCompanyId($user_id, $current_company->getId());
             if ($ulf->getRecordCount() == 1) {
                 $user_obj = $ulf->getCurrent();
                 $user_generic_status_label = $user_obj->getFullName(TRUE) . ' @ ' . TTDate::getDate('DATE+TIME', $time_stamp);
             } else {
                 $user_obj = NULL;
                 $user_generic_status_label = 'N/A @ ' . TTDate::getDate('DATE+TIME', $time_stamp);
             }
             $pcf = new PunchControlFactory();
             $pf = new PunchFactory();
    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_group_id'])) {
            $filter_data['group_id'] = $filter_data['user_group_id'];
        }
        if (isset($filter_data['user_title_id'])) {
            $filter_data['title_id'] = $filter_data['user_title_id'];
        }
        if (isset($filter_data['include_user_id'])) {
            $filter_data['user_id'] = $filter_data['include_user_id'];
        }
        if (isset($filter_data['exception_policy_severity_id'])) {
            $filter_data['severity_id'] = $filter_data['exception_policy_severity_id'];
        }
        $additional_order_fields = array('d.name', 'e.name', 'f.name', 'g.name', 'h.status_id', 'i.severity_id', 'i.type_id', 'c.first_name', 'c.last_name', 'c.country', 'c.province', 'b.date_stamp', 'pgf.name', 'pscf.name', 'ppsf.name');
        $sort_column_aliases = array('status' => 'status_id', 'type' => 'type_id');
        $order = $this->getColumnsFromAliases($order, $sort_column_aliases);
        if ($order == NULL) {
            //$order = array( 'status_id' => 'asc', 'last_name' => 'asc', 'first_name' => 'asc', 'middle_name' => 'asc');
            $order = array('i.severity_id' => 'desc', 'c.last_name' => 'asc', 'b.date_stamp' => 'asc', 'i.type_id' => 'asc');
            $strict = FALSE;
        } else {
            //Do order by column conversions, because if we include these columns in the SQL
            //query, they contaminate the data array.
            if (isset($order['default_branch'])) {
                $order['d.name'] = $order['default_branch'];
                unset($order['default_branch']);
            }
            if (isset($order['default_department'])) {
                $order['e.name'] = $order['default_department'];
                unset($order['default_department']);
            }
            if (isset($order['user_group'])) {
                $order['f.name'] = $order['user_group'];
                unset($order['user_group']);
            }
            if (isset($order['title'])) {
                $order['g.name'] = $order['title'];
                unset($order['title']);
            }
            if (isset($order['exception_policy_type_id'])) {
                $order['i.type_id'] = $order['exception_policy_type_id'];
                unset($order['exception_policy_type_id']);
            }
            if (isset($order['severity_id'])) {
                $order['i.severity_id'] = $order['severity_id'];
                unset($order['severity_id']);
            }
            if (isset($order['severity'])) {
                $order['i.severity_id'] = $order['severity'];
                unset($order['severity']);
            }
            if (isset($order['exception_policy_type'])) {
                $order['i.type_id'] = $order['exception_policy_type'];
                unset($order['exception_policy_type']);
            }
            if (isset($order['exception_policy_type_id'])) {
                $order['i.type_id'] = $order['exception_policy_type_id'];
                unset($order['exception_policy_type_id']);
            }
            if (isset($order['first_name'])) {
                $order['c.first_name'] = $order['first_name'];
                unset($order['first_name']);
            }
            if (isset($order['last_name'])) {
                $order['c.last_name'] = $order['last_name'];
                unset($order['last_name']);
            }
            if (isset($order['country'])) {
                $order['c.country'] = $order['country'];
                unset($order['country']);
            }
            if (isset($order['province'])) {
                $order['c.province'] = $order['province'];
                unset($order['province']);
            }
            if (isset($order['date_stamp'])) {
                $order['b.date_stamp'] = $order['date_stamp'];
                unset($order['date_stamp']);
            }
            if (isset($order['policy_group'])) {
                $order['pgf.name'] = $order['policy_group'];
                unset($order['policy_group']);
            }
            if (isset($order['permission_group'])) {
                $order['pscf.name'] = $order['permission_group'];
                unset($order['permission_group']);
            }
            if (isset($order['pay_period_schedule'])) {
                $order['ppsf.name'] = $order['pay_period_schedule'];
                unset($order['pay_period_schedule']);
            }
            //Always sort by last name,first name after other columns
            if (!isset($order['c.last_name'])) {
                $order['c.last_name'] = 'asc';
            }
            if (!isset($order['c.first_name'])) {
                $order['c.first_name'] = 'asc';
            }
            if (!isset($order['b.date_stamp'])) {
                $order['b.date_stamp'] = 'asc';
            }
            if (!isset($order['i.severity_id'])) {
                $order['i.severity_id'] = 'desc';
            }
            $strict = TRUE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        $udf = new UserDateFactory();
        $uf = new UserFactory();
        $bf = new BranchFactory();
        $df = new DepartmentFactory();
        $ugf = new UserGroupFactory();
        $utf = new UserTitleFactory();
        $ppf = new PayPeriodFactory();
        $ppsf = new PayPeriodScheduleFactory();
        $epf = new ExceptionPolicyFactory();
        $epcf = new ExceptionPolicyControlFactory();
        $pguf = new PolicyGroupUserFactory();
        $pgf = new PolicyGroupFactory();
        $pf = new PunchFactory();
        $pcf = new PunchControlFactory();
        $pscf = new PermissionControlFactory();
        $puf = new PermissionUserFactory();
        $ph = array('company_id' => $company_id);
        $query = '
					select 	a.*,
							b.date_stamp as date_stamp,
							b.pay_period_id as pay_period_id,
							h.pay_period_schedule_id as pay_period_schedule_id,
							i.severity_id as severity_id,
							i.type_id as exception_policy_type_id,
							b.user_id as user_id,
                            h.start_date as pay_period_start_date,
							h.end_date as pay_period_end_date,
							h.transaction_date as pay_period_transaction_date,
							c.first_name as first_name,
							c.last_name as last_name,
                            c.country as country,
                            c.province as province,
							c.status_id as user_status_id,
							c.group_id as group_id,
							f.name as "group",
							c.title_id as title_id,
							g.name as title,
							c.default_branch_id as default_branch_id,
							d.name as default_branch,
							c.default_department_id as default_department_id,
							e.name as default_department,

							pcf.branch_id as branch_id,
							bf.name as branch,
							pcf.department_id as department_id,
							df.name as department,
                            pgf.name as policy_group,
                            pscf.name as permission_group,
                            ppsf.name as pay_period_schedule,

							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
					from 	' . $this->getTable() . ' as a
						LEFT JOIN ' . $pf->getTable() . ' as pf ON ( a.punch_id IS NOT NULL AND a.punch_id = pf.id AND pf.deleted = 0)
						LEFT JOIN ' . $pcf->getTable() . ' as pcf ON ( ( ( pf.id IS NOT NULL AND pf.punch_control_id = pcf.id ) OR ( a.punch_control_id is NOT NULL AND a.punch_control_id = pcf.id ) ) AND pcf.deleted = 0)
						LEFT JOIN ' . $bf->getTable() . ' as bf ON pcf.branch_id = bf.id
						LEFT JOIN ' . $df->getTable() . ' as df ON pcf.department_id = df.id

						LEFT JOIN ' . $udf->getTable() . ' as b ON a.user_date_id = b.id
						LEFT JOIN ' . $uf->getTable() . ' as c ON b.user_id = c.id
						LEFT JOIN ' . $bf->getTable() . ' as d ON c.default_branch_id = d.id
						LEFT JOIN ' . $df->getTable() . ' as e ON c.default_department_id = e.id
						LEFT JOIN ' . $ugf->getTable() . ' as f ON c.group_id = f.id
						LEFT JOIN ' . $utf->getTable() . ' as g ON c.title_id = g.id
						LEFT JOIN ' . $ppf->getTable() . ' as h ON b.pay_period_id = h.id
                        LEFT JOIN ' . $ppsf->getTable() . ' as ppsf ON ppsf.id = h.pay_period_schedule_id
						LEFT JOIN ' . $epf->getTable() . ' as i ON a.exception_policy_id = i.id
                        LEFT JOIN ' . $epcf->getTable() . ' as epcf ON epcf.id = i.exception_policy_control_id
						LEFT JOIN ' . $pguf->getTable() . ' as pguf ON b.user_id = pguf.user_id
                        LEFT JOIN ' . $pgf->getTable() . ' as pgf ON pguf.policy_group_id = pgf.id
                        LEFT JOIN ' . $puf->getTable() . ' as puf ON c.id = puf.user_id
                        LEFT JOIN ' . $pscf->getTable() . ' as pscf ON pscf.id = puf.permission_control_id
						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	c.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 c.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['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND c.id in (' . $this->getListSQL($filter_data['user_id'], $ph) . ') ';
        }
        $query .= isset($filter_data['exclude_user_id']) ? $this->getWhereClauseSQL('c.id', $filter_data['exclude_user_id'], 'not_numeric_list', $ph) : NULL;
        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 c.status_id in (' . $this->getListSQL($filter_data['user_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['severity_id']) and isset($filter_data['severity_id'][0]) and !in_array(-1, (array) $filter_data['severity_id'])) {
            $query .= ' AND i.severity_id in (' . $this->getListSQL($filter_data['severity_id'], $ph) . ') ';
        }
        if (isset($filter_data['exception_policy_type_id']) and isset($filter_data['exception_policy_type_id'][0]) and !in_array(-1, (array) $filter_data['exception_policy_type_id'])) {
            $query .= ' AND i.type_id in (' . $this->getListSQL($filter_data['exception_policy_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 b.pay_period_id in (' . $this->getListSQL($filter_data['pay_period_id'], $ph) . ') ';
        }
        if (isset($filter_data['pay_period_status_id']) and isset($filter_data['pay_period_status_id'][0]) and !in_array(-1, (array) $filter_data['pay_period_status_id'])) {
            $query .= ' AND h.status_id in (' . $this->getListSQL($filter_data['pay_period_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 c.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 c.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 c.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 c.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 pcf.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 pcf.department_id in (' . $this->getListSQL($filter_data['department_id'], $ph) . ') ';
        }
        if (isset($filter_data['start_date']) and trim($filter_data['start_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['start_date']);
            $query .= ' AND b.date_stamp >= ?';
        }
        if (isset($filter_data['end_date']) and trim($filter_data['end_date']) != '') {
            $ph[] = $this->db->BindDate($filter_data['end_date']);
            $query .= ' AND b.date_stamp <= ?';
        }
        $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;
        //Make sure we accept exception rows assign to pay_period_id = 0 (no pay period), as this can happen when punches exist in the future.
        $query .= '
						AND ( a.deleted = 0 AND b.deleted = 0 AND c.deleted = 0 AND pgf.deleted = 0 AND ( h.deleted = 0 OR h.deleted is NULL ) )
					';
        $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 getMidDayExceptionsByStartDateAndEndDateAndPayPeriodStatus($start_date, $end_date, $pay_period_status_id)
    {
        if ($start_date == '') {
            return FALSE;
        }
        if ($end_date == '') {
            return FALSE;
        }
        if ($pay_period_status_id == '') {
            return FALSE;
        }
        $epf = new ExceptionPolicyFactory();
        $ef = new ExceptionFactory();
        $epcf = new ExceptionPolicyControlFactory();
        $pgf = new PolicyGroupFactory();
        $pguf = new PolicyGroupUserFactory();
        $uf = new UserFactory();
        $cf = new CompanyFactory();
        $udf = new UserDateFactory();
        $sf = new ScheduleFactory();
        $pcf = new PunchControlFactory();
        $pf = new PunchFactory();
        $ppf = new PayPeriodFactory();
        $current_epoch = time();
        if (strncmp($this->db->databaseType, 'mysql', 5) == 0) {
            $to_timestamp_sql = 'FROM_UNIXTIME';
        } else {
            $to_timestamp_sql = 'to_timestamp';
        }
        $ph = array('current_time1' => $this->db->BindTimeStamp($current_epoch), 'current_time2' => $this->db->BindTimeStamp($current_epoch), 'current_epoch1' => $current_epoch, 'start_date' => $this->db->BindDate($start_date), 'end_date' => $this->db->BindDate($end_date));
        //Exceptions that need to be calculated in the middle of the day:
        //Definitely: In Late, Out Late, Missed CheckIn
        //Possible: Over Daily Scheduled Time, Over Weekly Scheduled Time, Over Daily Time, Over Weekly Time, Long Lunch (can't run this fast enough), Long Break (can't run this fast enough),
        //Optimize calcQuickExceptions:
        // Loop through exception policies where In Late/Out Late/Missed CheckIn are enabled.
        // Loop through ACTIVE users assigned to these exceptions policies.
        // Only find days that are scheduled AND ( NO punch after schedule start time OR NO punch after schedule end time )
        //      For Missed CheckIn they do not need to be scheduled.
        // Exclude days that already have the exceptions triggered on them (?) (What about split shifts?)
        //	- Just exclude exceptions not assigned to punch/punch_control_id, if there is more than one in the day I don't think it helps much anyways.
        //
        //Currently Over Weekly/Daily time exceptions are only triggered on a Out punch.
        $query = '	select distinct udf.*
					FROM ' . $epf->getTable() . ' as epf
					LEFT JOIN ' . $epcf->getTable() . ' as epcf ON ( epf.exception_policy_control_id = epcf.id )
					LEFT JOIN ' . $pgf->getTable() . ' as pgf ON ( epcf.id = pgf.exception_policy_control_id )
					LEFT JOIN ' . $pguf->getTable() . ' as pguf ON ( pgf.id = pguf.policy_group_id )
					LEFT JOIN ' . $uf->getTable() . ' as uf ON ( pguf.user_id = uf.id )
					LEFT JOIN ' . $cf->getTable() . ' as cf ON ( uf.company_id = cf.id )
					LEFT JOIN ' . $udf->getTable() . ' as udf ON ( uf.id = udf.user_id )
					LEFT JOIN ' . $ppf->getTable() . ' as ppf ON ( ppf.id = udf.pay_period_id )
					LEFT JOIN ' . $ef->getTable() . ' as ef ON ( udf.id = ef.user_date_id AND ef.exception_policy_id = epf.id AND ef.type_id != 5 )
					LEFT JOIN ' . $sf->getTable() . ' as sf ON ( udf.id = sf.user_date_id AND ( sf.start_time <= ? OR sf.end_time <= ? ) )
					LEFT JOIN ' . $pcf->getTable() . ' as pcf ON ( udf.id = pcf.user_date_id AND pcf.deleted = 0 )
					LEFT JOIN ' . $pf->getTable() . ' as pf ON 	(
																pcf.id = pf.punch_control_id AND pf.deleted = 0
																AND (
																		( epf.type_id = \'S4\' AND ( pf.time_stamp >= sf.start_time OR pf.time_stamp <= sf.end_time ) )
																		OR
																		( epf.type_id = \'S6\' AND ( pf.time_stamp >= sf.end_time ) )
																		OR
																		( epf.type_id = \'C1\' AND ( pf.status_id = 10 AND pf.time_stamp <= ' . $to_timestamp_sql . '(?-epf.grace) ) )
																	)
																)
					WHERE ( epf.type_id in (\'S4\',\'S6\', \'C1\') AND epf.active = 1 )
						AND ( uf.status_id = 10 AND cf.status_id != 30 )
						AND ( udf.date_stamp >= ? AND udf.date_stamp <= ? )
						AND ppf.status_id in (' . $this->getListSQL($pay_period_status_id, $ph) . ')
						AND ( ( ( epf.type_id in (\'S4\',\'S6\') AND ( sf.id IS NOT NULL AND sf.deleted = 0 ) AND pf.id IS NULL ) OR epf.type_id = \'C1\' ) AND ef.id IS NULL  )
						AND ( epf.deleted = 0 AND epcf.deleted = 0 AND pgf.deleted = 0 AND uf.deleted = 0 AND cf.deleted = 0 AND udf.deleted = 0 )
				';
        //Don't check deleted = 0 on PCF/PF tables, as we need to check IS NULL on them instead.
        //Debug::Arr($ph, 'Query: '. $query, __FILE__, __LINE__, __METHOD__, 10);
        $this->ExecuteSQL($query, $ph);
        return $this;
    }