do_timesheet_payroll(); } if ($_GET['page'] == "invoice_timesheet") { $queue_invoice->do_timesheet_invoicing(); } if ($_GET['page'] == "approve_timesheet") { $queue_approval->do_timesheet_approval_processing(); } } elseif ($_GET['action'] == "split2") { if ($_GET['page'] == "approve_timesheet") { $queue_approval->do_timesheet_split(); } } else { if ($_GET['page'] == "payroll_timesheet") { $queue_payroll->do_timesheet_rejectpayroll(); } if ($_GET['page'] == "invoice_timesheet") { $queue_invoice->do_timesheet_rejectinvoicing(); } } } } function email_on_submission($timesheet_id) { $options = get_option('time_sheets'); if (!$options['enable_email']) { return; } global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $user_id = get_current_user_id(); $sql = "select user_login from {$wpdb->prefix}timesheet t join {$wpdb->users} u on u.ID = t.user_id where t.timesheet_id = %d"; $params = array($timesheet_id); $user_login = $db->get_var($sql, $params); $sql = "select user_email, display_name from {$wpdb->users} u join {$wpdb->prefix}timesheet_approvers_approvies aa on u.ID = aa.approver_user_id and aa.approvie_user_id = {$user_id}"; $users = $db->get_results($sql); $sql = "select display_name from {$wpdb->users} u join {$wpdb->prefix}timesheet t on u.ID = t.user_id where t.timesheet_id = {$timesheet_id}"; $display_user = $db->get_var($sql); $subject = "There are time sheet(s) pending approval."; $body = "A time sheet has been entered by {$display_user} and is pending approval. It can be approved from the approval menu."; foreach ($users as $user) { $common->send_email ($user->user_email, $subject, $body); } } function enter_timesheet() { if ($_POST['action'] == 'save') { if ($this->validate_timesheet()=="true") { $timesheet_id = $this->save_timesheet(); } } $this->show_timesheet($timesheet_id); } function check_overages($timesheet_id) { global $wpdb; $db = new time_sheets_db(); $sql = "select MaxHours, HoursUsed from {$wpdb->prefix}timesheet_client_projects a join {$wpdb->prefix}timesheet b on a.ProjectId = b.ProjectId where timesheet_id=%d"; $params=array($timesheet_id); $project = $db->get_row($sql, $params); if ($project->HoursUsed > $project->MaxHours && $project->MaxHours<>0) { echo '

This project has run over on hours. Please inform management that a new SOW may be required.

'; } } function validate_timesheet() { $passed = true; if (!$_POST['start_date']) { echo '

The week start date is required.

'; $passed="false"; } if (!$_POST['ClientId']) { echo '

The client name is required.

'; $passed="false"; } if (!$_POST['ProjectId']) { echo '

A project is required.

'; $passed="false"; } return $passed; } function show_timesheet($timesheet_id=0) { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $main = new time_sheets_main(); $settings = new time_sheets_settings(); $options = get_option('time_sheets'); $common = new time_sheets_common(); $common->show_datestuff(); if ($user_id == 0) { echo "

You must be logged in to view this page.

"; return; } $queue_approval = new time_sheets_queue_approval(); if (!$timesheet_id) { if ($_POST['timesheet_id']) { $timesheet_id = $_POST['timesheet_id']; } else { $timesheet_id = $_GET['timesheet_id']; } } $this->check_overages(intval($timesheet_id)); $sql = "select * from {$wpdb->prefix}timesheet t join {$wpdb->users} u on t.user_id = u.ID left outer join {$wpdb->prefix}timesheet_client_projects tcp on t.ProjectId = tcp.ProjectId where timesheet_id=%d"; $params = array($timesheet_id); if ($timesheet_id) { $timesheet = $db->get_row($sql, $params); $timesheet_user = $timesheet->display_name; $timesheet_user_id = $timesheet->user_id; $timesheet_id_int = $timesheet_id; } else { #if ($_POST['submit']) { # $sql = "select * from {$wpdb->prefix}timesheet where user_id=%d and start_date = %s"; # $params = array($user_id, $_POST['start_date']); # $timesheet = $db->get_row($sql, $params); #} else { $timesheet = (object) $_POST; $timesheet_user_id = $user_id; #} $timesheet_id_int = 0; } echo "

Weekly Time Sheet

"; $disable_object=""; if ($timesheet->week_complete=='1') { $disable_object= " disabled"; } echo "
"; wp_nonce_field( 'timesheet_entry_'. $timesheet_id); if ($timesheet_user) { echo "Timesheet Number: {$timesheet_id}
"; echo "Timesheet For: {$timesheet_user}
"; } if (!$timesheet->start_date) { $defaultDate = get_user_option('timesheet_defaultdate', $user_id); if (!$defaultDate) { $defaultDate='Monday Last Week'; } $start_date = date('Y-m-d', strtotime($defaultDate)); $clientid = -1; } else { $start_date = date("Y-m-d", strtotime($timesheet->start_date)); $clientid = $timesheet->ClientId; } if (date('w', strtotime($start_date)) !=1) { echo '

This time sheet does not start on a Monday. Please change the date to Monday.

'; } echo ""; $clients = $db->get_results("select ClientName, tc.ClientId, r.BillOnProjectCompletion from {$wpdb->prefix}timesheet_clients tc join {$wpdb->prefix}timesheet_clients_users tcu ON tc.ClientId = tcu.ClientId join {$wpdb->prefix}timesheet_client_projects tcp on tc.ClientId = tcp.ClientId and tcp.Active=1 left outer join {$wpdb->prefix}timesheet_recurring_invoices_monthly r on tc.ClientId = r.client_id WHERE tcu.user_id = $user_id union select ClientName, tc1.ClientId, r.BillOnProjectCompletion from {$wpdb->prefix}timesheet_clients tc1 left outer join {$wpdb->prefix}timesheet_recurring_invoices_monthly r on tc1.ClientId = r.client_id where tc1.ClientId = $clientid union select ClientName, tc2.ClientId, r.BillOnProjectCompletion from {$wpdb->prefix}timesheet_clients tc2 left outer join {$wpdb->prefix}timesheet_recurring_invoices_monthly r on tc2.ClientId = r.client_id join {$wpdb->prefix}timesheet t on tc2.ClientId = t.ClientId where t.timesheet_id = $timesheet_id_int order by ClientName"); if (!$timesheet->ProjectId) { $ProjectId = -1; } else { $ProjectId = $timesheet->ProjectId; } $sql = "select tcp.ClientId, ProjectId, ProjectName, BillOnProjectCompletion from {$wpdb->prefix}timesheet_client_projects tcp join {$wpdb->prefix}timesheet_clients_users tcu on tcp.ClientId = tcu.ClientId where tcu.user_id = $user_id and tcp.Active=1 union select ClientId, ProjectId, ProjectName, BillOnProjectCompletion from {$wpdb->prefix}timesheet_client_projects where ProjectId = {$ProjectId} order by ProjectName "; $projects = $db->get_results($sql); foreach ($projects as $project) { $project->ProjectName = $common->clean_from_db($project->ProjectName); $clean_projects[] = $project; } $js_projects = json_encode($clean_projects); if ($options['hide_client_project'] && count($clients)==1 && count($projects)==1) { $client = $clients[0]; $project = $projects[0]; echo ""; $clientList = "{$clientList}\"{$client->ClientId}\", "; $clientList = "[$clientList \"-1\"]"; } else { echo ""; if ($clients) { echo ""; echo ""; } if ($timesheet->notes) { echo ""; } echo "
Week Start Date:
Client Name:"; } else { echo "New Client must be added."; } if ($queue_approval->employee_approver_check()!=0 && $disable_object=="") { echo "Add a Client"; } echo "
Project Name:"; if ($queue_approval->employee_approver_check()!=0 && $disable_object=="") { echo "Add a Project"; } echo "
Project Notes:
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
MondayTuesdayWednesdayThursdayFridaySaturdaySunday
{$common->add_days_to_date($start_date, 0, $holidays)}{$common->add_days_to_date($start_date, 1, $holidays)}{$common->add_days_to_date($start_date, 2, $holidays)}{$common->add_days_to_date($start_date, 3, $holidays)}{$common->add_days_to_date($start_date, 4, $holidays)}{$common->add_days_to_date($start_date, 5, $holidays)}{$common->add_days_to_date($start_date, 6, $holidays)}
Hours
"; if ($settings->user_can_enter_notes($timesheet_user_id, 'display_notes_toggle', 'display_notes_list')==1) { echo "Daily Notes"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
"; } if ($settings->user_can_enter_notes($timesheet_user_id, 'display_expenses_toggle', 'display_expenses_list')==1) { echo "Additional Information"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "

Per Diem CityFlight/Train CostsHotel ChargesRental Car ChargesTollsMileage
(Miles Driven)
Other Expenses
Other Expense Notes:
"; } echo ""; if (!$options['remove_embargo']) { echo ""; } echo "
Week Complete:week_complete=='1') { echo " checked disabled"; } echo ">
Project Complete:project_complete=='1') { echo " checked"; } echo "> (If this is available, all project workers must select this for client to be billed as client is only billed upon project completion.)
"; if ($timesheet->week_complete!='1') { echo "
"; } echo ""; echo ""; echo ""; echo "
"; echo " "; } function save_timesheet() { global $wpdb; $user_id = get_current_user_id(); if ($user_id == 0) { echo "

You must be logged in to view this page.

"; return; } if (!$_POST['ProjectId']) { echo '

No Project Was Selected. A project must be selected. If no projects are available contact your lead.

'; } check_admin_referer( 'timesheet_entry_'. $_POST['timesheet_id'] ); $db = new time_sheets_db(); $user_id = get_current_user_id(); $monday_hours = floatval($_POST['monday_hours']); $tuesday_hours = floatval($_POST['tuesday_hours']); $wednesday_hours = floatval($_POST['wednesday_hours']); $thursday_hours = floatval($_POST['thursday_hours']); $friday_hours = floatval($_POST['friday_hours']); $saturday_hours = floatval($_POST['saturday_hours']); $sunday_hours = floatval($_POST['sunday_hours']); $may_embargo = ($_POST['may_embargo'] == 1 )? 1 : 0 ; $project_complete = ($_POST['project_complete'] == 1 )? 1 : 0 ; $EmbargoPendingProjectClose = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d", array(intval($_POST['ClientId']))); $EmbargoPendingProjectClose2 = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_client_projects where ProjectId = %d", array(intval($_POST['ProjectId']))); if ($EmbargoPendingProjectClose2==1) { $EmbargoPendingProjectClose=1; } if ($EmbargoPendingProjectClose<>1) { $EmbargoPendingProjectClose=0; } if ($project_complete<>1) { $project_complete=0; } if (!$monday_hours) { $monday_hours=0; } if (!$tuesday_hours) { $tuesday_hours=0; } if (!$wednesday_hours) { $wednesday_hours=0; } if (!$thursday_hours) { $thursday_hours=0; } if (!$friday_hours) { $friday_hours=0; } if (!$saturday_hours) { $saturday_hours=0; } if (!$sunday_hours) { $sunday_hours=0; } $total_hours = $monday_hours + $tuesday_hours + $wednesday_hours + $thursday_hours + $friday_hours + $saturday_hours + $sunday_hours; if ($_POST['week_complete']==1) { $week_complete=1; } else { $week_complete=0; } $start_date = date("Y-m-d", strtotime($_POST['start_date'])); if ($_POST['timesheet_id']) { //UPDATE $timesheet_id = $_POST['timesheet_id']; $sql = "update {$wpdb->prefix}timesheet SET start_date=%s, ClientId=%d, ProjectId=%d, monday_hours=%s, tuesday_hours=%s, wednesday_hours=%s, thursday_hours=%s, friday_hours=%s, saturday_hours=%s, sunday_hours=%s, total_hours=%s, monday_desc=%s, tuesday_desc=%s, wednesday_desc=%s, thursday_desc=%s, friday_desc=%s, saturday_desc=%s, sunday_desc=%s, per_diem_days=%s, hotel_charges=%s, rental_car_charges=%s, tolls=%s, other_expenses=%s, other_expenses_notes=%s, week_complete=%d, mileage=%d, EmbargoPendingProjectClose=%d, project_complete=%d, flight_cost=%s, isPerDiem=%d, perdiem_city=%s WHERE timesheet_id=%d"; $params=array($start_date, intval($_POST['ClientId']), intval($_POST['ProjectId']), floatval($monday_hours), floatval($tuesday_hours), floatval($wednesday_hours), floatval($thursday_hours), floatval($friday_hours), floatval($saturday_hours), floatval($sunday_hours), $total_hours, sanitize_textarea_field($_POST['monday_desc']), sanitize_textarea_field($_POST['tuesday_desc']), sanitize_textarea_field($_POST['wednesday_desc']), sanitize_textarea_field($_POST['thursday_desc']), sanitize_textarea_field($_POST['friday_desc']), sanitize_textarea_field($_POST['saturday_desc']), sanitize_textarea_field($_POST['sunday_desc']), floatval($_POST['per_diem_days']), floatval($_POST['hotel_charges']), floatval($_POST['rental_car_charges']), floatval($_POST['tolls']), floatval($_POST['other_expenses']), sanitize_textarea_field($_POST['other_expenses_notes']), $week_complete, floatval($_POST['mileage']), $EmbargoPendingProjectClose, $project_complete, floatval($_POST['flight_cost']), intval($_POST['isPerDiem']), sanitize_text_field($_POST['perdiem_city']), intval($_POST['timesheet_id'])); $db->query($sql, $params); if ($week_complete==1) { $sql = "update {$wpdb->prefix}timesheet set marked_complete_by=$user_id, marked_complete_date = CURDATE() where timesheet_id=%d"; $params=array(intval($_POST['timesheet_id'])); $db->query($sql, $params); if ($EmbargoPendingProjectClose==0) { $this->email_on_submission(intval($_POST['timesheet_id'])); } $sql = "update {$wpdb->prefix}timesheet_client_projects set HoursUsed=HoursUsed+$total_hours where ProjectId=%d"; $params=array(intval($_POST['ProjectId'])); $db->query($sql, $params); } echo '

Timesheet updated.

'; } else { //INSERT $sql = "insert into {$wpdb->prefix}timesheet (user_id, start_date, entered_date, ClientId, ProjectId, 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, approved, invoiced, mileage, EmbargoPendingProjectClose, project_complete, flight_cost, isPerDiem, perdiem_city) values (%d, %s, now(), %d, %d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, 0, 0, %d, %d, %d, %s, %d, %s)"; $params=array($user_id, $start_date, intval($_POST['ClientId']), intval($_POST['ProjectId']), floatval($monday_hours), floatval($tuesday_hours), floatval($wednesday_hours), floatval($thursday_hours), floatval($friday_hours), floatval($saturday_hours), floatval($sunday_hours), $total_hours, sanitize_textarea_field($_POST['monday_desc']), sanitize_textarea_field($_POST['tuesday_desc']), sanitize_textarea_field($_POST['wednesday_desc']), sanitize_textarea_field($_POST['thursday_desc']), sanitize_textarea_field($_POST['friday_desc']), sanitize_textarea_field($_POST['saturday_desc']), sanitize_textarea_field($_POST['sunday_desc']), floatval($_POST['per_diem_days']), floatval($_POST['hotel_charges']), floatval($_POST['rental_car_charges']), floatval($_POST['tolls']), floatval($_POST['other_expenses']), sanitize_textarea_field($_POST['other_expenses_notes']), $week_complete, $_POST['mileage'], $EmbargoPendingProjectClose, $project_complete, floatval($_POST['flight_cost']), floatval($_POST['isPerDiem']), sanitize_text_field($_POST['perdiem_city'])); $db->query($sql, $params); $sql = "select max(timesheet_id) from {$wpdb->prefix}timesheet where user_id={$user_id}"; $timesheet_id = $db->get_var($sql); $this->check_overages($timesheet_id); if ($week_complete==1) { $sql = "update {$wpdb->prefix}timesheet set marked_complete_by=$user_id, marked_complete_date = CURDATE() where timesheet_id=$timesheet_id"; $db->query($sql); if ($EmbargoPendingProjectClose==0) { $this->email_on_submission($timesheet_id); } $sql = "update {$wpdb->prefix}timesheet_client_projects set HoursUsed=HoursUsed+$total_hours where ProjectId=%d"; $params=array(intval($_POST['ProjectId'])); $db->query($sql, $params); } echo '

Timesheet saved.

'; } if ($may_embargo==1) { if ($project_complete==1 && $week_complete==1) { $sql = "update {$wpdb->prefix}timesheet SET EmbargoPendingProjectClose = 0 WHERE ProjectId=%d AND EmbargoPendingProjectClose = 1"; $params = array(intval($_POST['ProjectId'])); $db->query($sql, $params); echo "
Your timesheets for this client have been sent for processing.

"; $this->email_on_submission(intval($timesheet_id)); } } $sql = "select * from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d and MonthlyHours <> 0"; $parm = array(intval($_POST['ClientId'])); $client = $db->get_row($sql, $parm); if ($client) { $sql = "select sum(total_hours) total_hours from {$wpdb->prefix}timesheet where ClientId = %d and YEAR(start_date) = YEAR(NOW()) AND MONTH(start_date)=MONTH(NOW())"; $parm = array(intval($_POST['ClientId'])); $month_hours = $db->get_var($sql, $parm); if ($month_hours > $client->MonthlyHours) { echo "

Client may be over their retainer hours for this month. Please ensure that client approves of hours over their monthly retainer of {$client->MonthlyHours} hours if they do not have an active SOW in place.

"; } } return $timesheet_id; } function search_timesheet() { $user_id = get_current_user_id(); $common = new time_sheets_common(); if ($user_id == 0) { echo "You must be logged in to view this page."; return; } $common->show_datestuff(); $start_date = $common->f_date($common->clean_from_db($_GET['start_date'])); $end_date = $common->f_date($common->clean_from_db($_GET['end_date'])); $include_completed = $common->clean_from_db($_GET['include_completed']); if ($common->clean_from_db($_GET['start_date'])=='') { $start_date = date('Y-m-d', strtotime("-1 Year")); $start_date = $common->f_date($start_date); } if ($common->clean_from_db($_GET['end_date'])=='') { $end_date = date('Y-m-d', strtotime("+1 Day")); $end_date = $common->f_date($end_date); } echo '
'; echo "Enter Range To Search: "; echo ""; echo " to "; echo ""; echo "
Include Completed Timesheets"; echo "
"; echo ""; echo "
"; $this->run_timesheet_search($start_date, $end_date); } function run_timesheet_search($start_date, $end_date) { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $options = get_option('time_sheets'); $common = new time_sheets_common(); $sql = "select t.timesheet_id, t.approved, t.start_date, c.ClientName client_name, cp.ProjectName, t.marked_complete_by from {$wpdb->prefix}timesheet t JOIN {$wpdb->prefix}timesheet_clients c ON t.ClientId = c.ClientId JOIN {$wpdb->prefix}timesheet_client_projects cp on t.ProjectId=cp.ProjectId where t.user_id=%d and t.start_date between %s and %s"; if (!$_GET['include_completed']) { $sql = "{$sql} and week_complete = 0"; } $sql = "{$sql} order by t.start_date desc, c.ClientName asc, cp.ProjectName asc"; $params = array($user_id, $common->mysql_date($start_date), $common->mysql_date($end_date)); $timesheets = $db->get_results($sql, $params); if (strpos($_SERVER['REQUEST_URI'], 'admin.php') !== False) { $timesheet_url = "./admin.php?page=enter_timesheet&"; } else { $timesheet_url = $options['rel_url_to_timesheet'] . '?'; } if ($timesheets) { echo ""; foreach ($timesheets as $timesheet) { echo "
Time SheetApprovedWeek CompletedStart DateClient NameProject Name
{$timesheet->timesheet_id}"; if ($timesheet->approved==1) { echo ""; } else { echo " "; } echo ""; if ($timesheet->marked_complete_by) { echo ""; } else { echo " "; } echo "{$common->f_date($timesheet->start_date)}{$common->clean_from_db($timesheet->client_name)}{$common->clean_from_db($timesheet->ProjectName)}"; } echo "
"; } else { echo "No timesheets found matching those dates."; } } }