ID; $common = new time_sheets_common(); if ($user_id == 0) { echo "You must be logged in to view this page."; return; } $common->show_datestuff(); echo "
"; $this->search_timesheet(); echo ""; $common->show_clients_on_retainer(1); echo "
"; } function search_timesheet_sc(){ return $this->search_timesheet(0); } function search_timesheet($display=1) { $user = wp_get_current_user(); $user_id = $user->ID; $common = new time_sheets_common(); $approval = new time_sheets_queue_approval(); $options = get_option('time_sheets'); $page = ''; if ($user_id == 0) { $page = $page . "You must be logged in to view this page."; return $page; } $start_date = isset($_GET['start_date']) ? $common->f_date($common->clean_from_db($_GET['start_date'])) : ""; $end_date = isset($_GET['end_date']) ? $common->f_date($common->clean_from_db($_GET['end_date'])) : ""; $include_completed = isset($_GET['include_completed']) ? $common->clean_from_db($_GET['include_completed']) : ""; if (isset($_GET['include_me'])) { $include_me = ' checked'; } else { IF (!isset($_GET['submit'])) { $include_me = ' checked'; } else { $include_me = ''; } } if (isset($_GET['show_all_templates'])) { $show_all_templates = ' checked'; } else { $show_all_templates = ''; } if (isset($_GET['filter_by_client'])) { $filter_by_client = ' checked'; } else { $filter_by_client = ''; } if (!isset($_GET['start_date'])) { $start_date = date('Y-m-d', strtotime("-1 Year")); $start_date = $common->f_date($start_date); $show_all_templates = ' checked'; } if (!isset($_GET['end_date'])) { $end_date = date('Y-m-d', strtotime("+1 Day")); $end_date = $common->f_date($end_date); } $v_start_date = date('Y-m-d', strtotime($start_date)); $v_end_date = date('Y-m-d', strtotime($end_date)); $page = $page . '
'; $page = $page . ""; $page = $page . ""; if (isset($options['allow_recurring_timesheets'])) { $page = $page . ""; } if ($approval->employee_approver_check('true') <> 0) { if (is_admin()) { $include_all=''; if (isset($_GET['include_all'])) { $include_all = 'checked'; } } } else { $include_all = 'disabled'; } $page = $page . ""; if ($approval->employee_approver_check()<> 0) { if (isset($_GET['include_all_team'])) { $include_team = ' checked'; } else { $include_team = ''; } $page = $page . ""; } else { $page = $page . ""; } $page = $page . ""; if (isset($options['hide_client_project'])) { #$Clients = $common->return_clients_for_user($timesheet_user_id); $Projects = $common->return_projects_for_user($user_id); if (sizeof($Projects)==1) { $page = $page . ""; } else { $page = $page . ""; $js_projects = $common->draw_clients_and_projects(0, 0, 0, '', (object) $_GET, $clientList); } } else { $page = $page . ""; $js_projects = $common->draw_clients_and_projects(0, 0, 0, '', (object) $_GET, $clientList); } $page = $page . $js_projects[1]; $page = $page . ""; $page = $page . "
Enter Range To Search:"; $page = $page . " to
Include My Timesheets
Show All My Templates
Include All Users
Include All Team Members
Select Team Member"; $users = $common->return_approvers_team_list($user_id, 1); $page = $page . "
Include Completed Timesheets
Filter by Client and Project
Filter by Client and Project
"; $page = $page . "
"; if ($approval->employee_approver_check()<> 0) { $page = $page . " "; } $page = $page . ""; if (is_admin()) { $page = $page . ""; } $page = $page . $this->run_timesheet_search($start_date, $end_date, $include_me); if (!isset($_GET['start_date']) || isset($options['allow_recurring_timesheets'])) { if (isset($_GET['show_all_templates'])) { $start_date = '0000-01-01'; $end_date = '9999-12-31'; } $page = $page . $this->run_timesheet_template_search($start_date, $end_date, $include_me); } if ($display==1) { echo $page; } else { return $page; } } function mark_timesheets_week_complete ($timesheet_id) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $entry = new time_sheets_entry(); $sql = "select ProjectId, total_hours from {$wpdb->prefix}timesheet where timesheet_id=%d"; $params=array(intval($timesheet_id)); $timesheet = $db->get_row($sql, $params); $sql = "update {$wpdb->prefix}timesheet set marked_complete_by=$user_id, marked_complete_date = CURDATE(), week_complete = 1 where timesheet_id=%d"; $params=array(intval($timesheet_id)); $db->query($sql, $params); $sql = "update {$wpdb->prefix}timesheet_client_projects set HoursUsed=HoursUsed+$timesheet->total_hours where ProjectId=%d"; $params=array($timesheet->ProjectId); $db->query($sql, $params); $entry->set_project_inactive_on_date($timesheet->ProjectId); $sql = "delete from {$wpdb->prefix}timesheet_reject_message_queue where timesheet_id = %d"; $params=array(intval($timesheet_id)); $db->query($sql, $params); #Archive the change $common->archive_records ('timesheet', 'timesheet_archive', "timesheet_id", intval($timesheet_id), '=', 'UPDATED'); $entry->email_on_submission($timesheet_id); } function run_timesheet_search_query($start_date, $end_date, $include_me, $timesheet_table) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $sql = "select t.*, c.ClientName client_name, cp.ProjectName, u.display_name, monday_nb+tuesday_nb+wednesday_nb+thursday_nb+friday_nb+saturday_nb+sunday_nb as total_nb from {$timesheet_table} t JOIN {$wpdb->prefix}timesheet_clients c ON t.ClientId = c.ClientId JOIN {$wpdb->prefix}timesheet_client_projects cp on t.ProjectId=cp.ProjectId join {$wpdb->users} u on t.user_id = u.ID where t.start_date between %s and %s"; if (!isset($_GET['include_completed'])) { $sql = "{$sql} and week_complete = 0"; } if (!isset($_GET['include_all'])) { if ($include_me != '') { $sql = $sql." and (t.user_id={$user_id} /*b1*/"; } }else { $sql = $sql." and (1=1"; } if (isset($_GET['include_all_team'])) { IF ($include_me == '') { $sql = $sql." and ("; } else { $sql = $sql." or "; } $sql = $sql." t.user_id in (select approvie_user_id from {$wpdb->prefix}timesheet_approvers_approvies where approver_user_id = {$user_id}) )"; } elseif (isset($_GET['team_member'])) { IF ($include_me == '') { $sql = $sql." and ("; } else { $sql = $sql." or "; } $sql = $sql." t.user_id = {$common->intval($_GET['team_member'])} /*b2*/ )"; } else { $sql = $sql." )"; } if (isset($_GET['filter_by_client'])) { if ($_GET['ClientId']) { $sql = $sql." and t.ClientId = {$common->intval($_GET['ClientId'])} "; } if ($_GET['ProjectId']) { $sql = $sql." and t.ProjectId = {$common->intval($_GET['ProjectId'])} "; } } $sql = "{$sql} order by t.start_date desc, c.ClientName asc, cp.ProjectName asc "; $params = array($common->mysql_date($start_date), $common->mysql_date($end_date)); $timesheets = $db->get_results($sql, $params); return $timesheets; } function update_bulk_loop($start_date, $end_date, $include_me) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $ids = ''; $timesheets = $this->run_timesheet_search_query($start_date, $end_date, $include_me, "{$wpdb->prefix}timesheet"); if ($timesheets) { foreach ($timesheets as $timesheet) { if (isset($_GET['week_complete_' . $timesheet->timesheet_id])) { $this->mark_timesheets_week_complete($timesheet->timesheet_id); $ids = $ids . $timesheet->timesheet_id . ', '; } } $ids = substr($ids,0,strlen($ids)-2); if ($ids=="") { return "

No timesheets were selected to be closed.

"; } else { return "

Timesheet(s) {$ids} have been marked complete.

"; } } } function use_template($template_id) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $start_date = date('Y-m-d'); $int_start_date = strtotime($start_date); if (date('N', strtotime($int_start_date) != 1)) { $int_start_date = strtotime('monday this week', $int_start_date); } $start_date = date('Y-m-d', $int_start_date); $sql = "insert into {$wpdb->prefix}timesheet (user_id, start_date, entered_date, client_name, project_name, monday_hours, tuesday_hours, wednesday_hours, thursday_hours, friday_hours, saturday_hours, sunday_hours, total_hours, monday_desc, tuesday_desc, wednesday_desc, thursday_desc, friday_desc, saturday_desc, sunday_desc, per_diem_days, hotel_charges, rental_car_charges, tolls, other_expenses, other_expenses_notes, week_complete, marked_complete_by, marked_complete_date, approved, approved_by, approved_date, invoiced, invoiced_by, invoiced_date, invoiceid, ClientId, mileage, EmbargoPendingProjectClose, project_complete, ProjectId, payrolled, payrolled_on, payrolled_by, flight_cost, isPerDiem, perdiem_city, taxi, monday_ot, tuesday_ot, wednesday_ot, thursday_ot, friday_ot, saturday_ot, sunday_ot, monday_nb, tuesday_nb, wednesday_nb, thursday_nb, friday_nb, saturday_nb, sunday_nb) select user_id, '{$start_date}', now(), client_name, project_name, monday_hours, tuesday_hours, wednesday_hours, thursday_hours, friday_hours, saturday_hours, sunday_hours, total_hours, monday_desc, tuesday_desc, wednesday_desc, thursday_desc, friday_desc, saturday_desc, sunday_desc, per_diem_days, hotel_charges, rental_car_charges, tolls, other_expenses, other_expenses_notes, week_complete, marked_complete_by, marked_complete_date, approved, approved_by, approved_date, invoiced, invoiced_by, invoiced_date, invoiceid, ClientId, mileage, EmbargoPendingProjectClose, project_complete, ProjectId, payrolled, payrolled_on, payrolled_by, flight_cost, isPerDiem, perdiem_city, taxi, monday_ot, tuesday_ot, wednesday_ot, thursday_ot, friday_ot, saturday_ot, sunday_ot, monday_nb, tuesday_nb, wednesday_nb, thursday_nb, friday_nb, saturday_nb, sunday_nb from {$wpdb->prefix}timesheet_scheduled where timesheet_id = %d"; $param = array($template_id); $db->query($sql, $param); $sql = "select max(timesheet_id) timesheet_id from {$wpdb->prefix}timesheet"; $new_timesheet = $db->get_row($sql); $common->archive_records ('timesheet', 'timesheet_archive', "timesheet_id", $new_timesheet->timesheet_id, '=', 'UPDATED'); } function use_templates($start_date, $end_date, $include_me) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $ids = ''; $timesheets = $this->run_timesheet_search_query($start_date, $end_date, $include_me, "{$wpdb->prefix}timesheet_scheduled"); if ($timesheets) { foreach ($timesheets as $timesheet) { if (isset($_GET['template_' . $timesheet->timesheet_id])) { $this->use_template($timesheet->timesheet_id); $ids = $ids . $timesheet->timesheet_id . ', '; } } $ids = substr($ids,0,strlen($ids)-2); if ($ids=="") { return "

No templates were selected to be used.

"; } else { return "

Template(s) {$ids} have been used.

"; } } } function delete_template($template_id) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $common->archive_records ('timesheet_scheduled', 'timesheet_scheduled_archive', "timesheet_id", $template_id, '=', 'DELETED'); $sql = "delete from {$wpdb->prefix}timesheet_scheduled where timesheet_id = %d"; $param = array($template_id); $db->query($sql, $param); } function delete_templates($start_date, $end_date, $include_me) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $ids = ''; $timesheets = $this->run_timesheet_search_query($start_date, $end_date, $include_me, "{$wpdb->prefix}timesheet_scheduled"); if ($timesheets) { foreach ($timesheets as $timesheet) { if (isset($_GET['template_' . $timesheet->timesheet_id])) { $this->delete_template($timesheet->timesheet_id); $ids = $ids . $timesheet->timesheet_id . ', '; } } $ids = substr($ids,0,strlen($ids)-2); if ($ids=="") { return "

No templates were selected to be deleted.

"; } else { return "

Template(s) {$ids} have been deleted.

"; } } } function run_timesheet_template_search ($start_date, $end_date, $include_me) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $page = ''; $timesheets = $this->run_timesheet_search_query($start_date, $end_date, $include_me, "{$wpdb->prefix}timesheet_scheduled"); if (strpos($_SERVER['REQUEST_URI'], 'admin.php') !== False) { $timesheet_url = "./admin.php?page=show_timesheet&"; } else { $timesheet_url = $options['rel_url_to_timesheet'] . '?'; } if ($timesheets) { $total_hours = 0; $total_nb = 0; $page = '
'; $page = $page . "  

"; $page = $page . ""; foreach ($timesheets as $timesheet) { $page = $page . ""; if (($timesheet->week_complete==0) && ($timesheet->user_id==$user_id)) { $page = $page . ""; } else { $page = $page . ""; } $page = $page . ""; $page = $page . ""; $total_hours = $total_hours + $timesheet->total_hours; $total_nb = $total_nb + $timesheet->total_nb; } $grand_total = $total_hours + $total_nb; $total_hours = number_format($total_hours, 2); $total_nb = number_format($total_nb, 2); $grand_total = number_format($grand_total, 2); $page = $page . ""; $page = $page . ""; $page = $page . "
 TemplateUserNext ExecutionClient NameProject NameBillable HoursNon-Billable Hours
   {$timesheet->timesheet_id}{$timesheet->display_name}{$common->f_date($timesheet->next_execution)}{$common->clean_from_db($timesheet->client_name)}{$common->clean_from_db($timesheet->ProjectName)}{$common->clean_from_db($timesheet->total_hours)}{$common->clean_from_db($timesheet->total_nb)}
 {$total_hours}{$total_nb}
 {$grand_total}
"; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; if (isset($_GET['include_all'])) { $page = $page . ""; } if (isset($_GET['include_all_team'])) { $page = $page . ""; } if (isset($_GET['include_completed'])) { $page = $page . ""; } if (isset($_GET['filter_by_client'])) { $page = $page . ""; } $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . "

"; } else { $page = "No time sheet templates found."; } return $page; } function run_timesheet_search($start_date, $end_date, $include_me) { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $page = ''; if (isset($_GET['bulk_action']) && $_GET['bulk_action']=='Mark Week Complete') { $message = $this->update_bulk_loop($start_date, $end_date, $include_me); $page = $page . $message; } //Doing the template work here instead of in the template function so that the bulk actions //take effect before the timesheet query is executed. if (isset($_GET['template_bulk_action']) && $_GET['template_bulk_action']=='Delete Template(s)') { $message = $this->delete_templates($start_date, $end_date, $include_me); $page = $page . $message; } if (isset($_GET['template_bulk_action']) && $_GET['template_bulk_action']=='Use Template(s)') { $message = $this->use_templates($start_date, $end_date, $include_me); $page = $page . $message; } $timesheets = $this->run_timesheet_search_query($start_date, $end_date, $include_me, "{$wpdb->prefix}timesheet"); if (strpos($_SERVER['REQUEST_URI'], 'admin.php') !== False) { $timesheet_url = "./admin.php?page=show_timesheet&"; } else { $timesheet_url = $options['rel_url_to_timesheet'] . '?'; } if ($timesheets) { $total_hours = 0; $total_nb = 0; $page = $page . '
'; $page = $page . "  

"; $page = $page . ""; foreach ($timesheets as $timesheet) { $page = $page . ""; if (($timesheet->week_complete==0) && ($timesheet->user_id==$user_id)) { $page = $page . ""; } else { $page = $page . ""; } $page = $page . ""; $total_hours = $total_hours + $timesheet->total_hours; $total_nb = $total_nb + $timesheet->total_nb; } $grand_total = $total_hours + $total_nb; $total_hours = number_format($total_hours, 2); $total_nb = number_format($total_nb, 2); $grand_total = number_format($grand_total, 2); $page = $page . ""; $page = $page . ""; $page = $page . "
 Time SheetUserApprovedWeek CompletedStart DateClient NameProject NameBillable HoursNon-Billable Hours
   {$timesheet->timesheet_id}{$timesheet->display_name}"; if ($timesheet->approved==1) { $page = $page . ""; } else { $page = $page . " "; } $page = $page . ""; if ($timesheet->marked_complete_by) { $page = $page . ""; } else { $page = $page . " "; } $page = $page . "{$common->f_date($timesheet->start_date)}{$common->clean_from_db($timesheet->client_name)}{$common->clean_from_db($timesheet->ProjectName)}{$common->clean_from_db($timesheet->total_hours)}{$common->clean_from_db($timesheet->total_nb)}
 {$total_hours}{$total_nb}
 {$grand_total}
"; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; if (isset($_GET['include_all'])) { $page = $page . ""; } if (isset($_GET['include_all_team'])) { $page = $page . ""; } if (isset($_GET['include_completed'])) { $page = $page . ""; } if (isset($_GET['filter_by_client'])) { $page = $page . ""; } $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . "

"; $page = $page . ""; } else { $page = $page . "
No time sheets found with the above search criteria."; } return $page; } }