do_timesheet_payroll(); } if ($page == "invoice_timesheet") { $queue_invoice->do_timesheet_invoicing(); } if ($page == "approve_timesheet") { $queue_approval->do_timesheet_approval_processing(); } } elseif ($action == "split2") { if ($page == "approve_timesheet") { $queue_approval->do_timesheet_split(); } } else { if ($page == "payroll_timesheet") { $queue_payroll->do_timesheet_rejectpayroll(); } if ($page == "invoice_timesheet") { $queue_invoice->do_timesheet_rejectinvoicing(); } #Disable this #if ($page == "show_timesheet") { # if ($this->validate_timesheet()=="true") { # $timesheet_id = $this->save_timesheet(); # return $timesheet_id; # } #$timesheet_id = $this->save_timesheet(); #} } } } function email_on_submission($timesheet_id) { $options = get_option('time_sheets'); $admin_url = get_admin_url(); if (!isset($options['enable_email'])) { return; } global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $user = wp_get_current_user(); $user_id = $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); $http = !empty($_SERVER['HTTPS']) ? "https" : "http"; $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_sc() { #enable this if (isset($_POST['action']) && $_POST['action'] == 'save') { # if ($this->validate_timesheet()=="true") { # $timesheet_id = $this->save_timesheet(); # # } } else { $timesheet_id = (isset($_GET['timesheet_id']) ? intval($_GET['timesheet_id']) : ""); } #global $wpdb; #if ($_POST['submit'] == 'Save Timesheet') { # $db = new time_sheets_db(); # $user_id = get_current_user_id(); # if ($_POST['timesheet_id']) { # $timesheet_id = $_POST['timesheet_id']; # } else { # # $sql = "select max(timesheet_id) # from {$wpdb->prefix}timesheet # where user_id={$user_id}"; # # $timesheet_id = $db->get_var($sql); # } #} #echo $timesheet_id; $page = $this->show_timesheet($timesheet_id, 0); return $page; } function enter_timesheet() { #enable this #if ($_POST['action'] == 'save') { # if ($this->validate_timesheet()=="true") { # $timesheet_id = $this->save_timesheet(); # } #} #echo "test1;"; global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; if ($_POST['submit'] == 'Save Timesheet' && isset($timesheet_id)) { if ($_POST['timesheet_id']) { $timesheet_id = $_POST['timesheet_id']; } else { $sql = "select max(timesheet_id) from {$wpdb->prefix}timesheet where user_id={$user_id}"; $timesheet_id = $db->get_var($sql); } } $this->show_timesheet($timesheet_id); } function check_overages($timesheet_id, $send=0) { global $wpdb; $options = get_option('time_sheets'); $project_trigger_percent = (isset($options['project_trigger_percent'])) ? $options['project_trigger_percent'] : 80; $email_address_on_project_over = (isset($options['email_address_on_project_over'])) ? $options['email_address_on_project_over'] : 'noreply@nothing'; if (!is_numeric($project_trigger_percent)) { $project_trigger_percent = 100; } $db = new time_sheets_db(); $common = new time_sheets_common(); $sql = "select a.MaxHours, a.HoursUsed, a.ProjectName, tc.ClientName, tc.ClientId, a.IsRetainer, trim.MonthlyHours, b.week_complete, a.max_monthly_bucket from {$wpdb->prefix}timesheet_client_projects a join {$wpdb->prefix}timesheet b on a.ProjectId = b.ProjectId join {$wpdb->prefix}timesheet_clients tc on a.ClientId = tc.ClientId left outer join {$wpdb->prefix}timesheet_recurring_invoices_monthly trim on a.ClientId = trim.client_id where timesheet_id=%d"; $params=array($timesheet_id); $project = $db->get_row($sql, $params); $send = 1; if (isset($project) ) { $MaxHours = $project->MaxHours; $HoursUsed = $project->HoursUsed; $RealHoursUsed = $project->HoursUsed; if ($project->IsRetainer==1) { $LastMonthMaxHours = $project->MaxHours-$project->MonthlyHours; $HoursUsed = $project->HoursUsed - $LastMonthMaxHours; $MaxHours = $project->MonthlyHours; } if ($project->week_complete==1) { $send=0; } if ($send==1 && isset($options['enable_email']) && $email_address_on_project_over <> "" && $project->IsRetainer==0 && $project->MaxHours<>0) { $Sales = "The sales team has been notified that this project may require additional hours."; } else { $Sales = "Contact the Sales team is assistance is needed."; } if ($HoursUsed > ($project_trigger_percent/100)*$MaxHours && $MaxHours && !isset($project->max_monthly_bucket)) { echo "

The project '{$project->ProjectName}' for '{$project->ClientName}' has used {$project_trigger_percent}% or more of its hours ({$RealHoursUsed} hours used). Be sure that the client approves of hours over the project max of {$project->MaxHours} hours. {$Sales}

"; if ($send==1 && $email_address_on_project_over <> "" && $project->IsRetainer==0 && $project->MaxHours<>0) { $common->send_email($options['email_address_on_project_over'], "Project '{$project->ProjectName}' for '{$project->ClientName}' has used {$project_trigger_percent}% of its hours", "The project '{$project->ProjectName}' for '{$project->ClientName}' has used {$project_trigger_percent}% of its hours.

If the project is not wrapping up, work with the team member working on this project to ensure any needed approvals from the client are received and/or a new SOW is written.

If the project is wrapping up, any post project work should be prepped.

(Sent by the time sheet system)

-----------------------------------------------------------

"); } } } } function validate_timesheet(&$output='') { $passed = true; $output = ''; if (!isset($_POST['start_date'])) { $output = $output . '

The week start date is required.

'; $passed="false"; } if (!isset($_POST['ClientId']) || (isset($_POST['ClientId']) && $_POST['ClientId'] == -2)) { $output = $output . '

The client name is required.

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

A project is required.

'; $passed="false"; } return $passed; } function int_to_weekday($week_starts, $days_to_inc) { $value = $week_starts+$days_to_inc; if ($value > 6) { $value = $value-7; } if ($value==0) { $weekday = 'Sunday'; }elseif ($value==1) { $weekday = 'Monday'; }elseif ($value==2) { $weekday = 'Tuesday'; }elseif ($value==3) { $weekday = 'Wednesday'; }elseif ($value==4) { $weekday = 'Thursday'; }elseif ($value==5) { $weekday = 'Friday'; }elseif ($value==6) { $weekday = 'Saturday'; } return $weekday; } function request_client_access_save($client_id) { global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $sql = "select ClientName from {$wpdb->prefix}timesheet_clients where ClientId = %d"; $params = array($client_id); $client = $db->get_row($sql, $params); $sql = "select uuid() as uuid"; $uuid = $db->get_row($sql); $sql = "insert into {$wpdb->prefix}timesheet_client_users_approval_queue (request_id, request_created, user_id, client_id, status) values (%s, now(), %d, %d, 1)"; $params = array($uuid->uuid, $user_id, $client_id); $db->query($sql, $params); $sql = "insert into {$wpdb->prefix}timesheet_client_users_approval_queue_log (request_id, log_created, status, user_id) values (%s, now(), 1, %d)"; $params = array($uuid->uuid, $user_id); $db->query($sql, $params); if (!isset($options['enable_requesting_access_self_approve'])) { $http = !empty($_SERVER['HTTPS']) ? "https" : "http"; $subject = "User {$user->display_name} is requesting access to a client"; $body = "The user '{$user->display_name}' is requesting access to the client '{$client->ClientName}'. uuid}'>Approve Request uuid}'>Deny Request."; $sql = "select user_email from {$wpdb->prefix}users u join {$wpdb->prefix}timesheet_manage_client_users mcu on u.ID = mcu.user_id"; $users = $db->get_results($sql); if ($users) { foreach ($users as $user) { $common->send_email($user->user_email, $subject, $body ); } } return "

Access to the requested client has been submitted. You will be contacted with the status of the request. The request ID is {$uuid->uuid}.

"; } else { $access = $this->request_client_access_approve($uuid->uuid); return "

Access to the requested client has been completed

"; } } function request_client_access_approve($request_id) { global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $user = wp_get_current_user(); $user_id = $user->ID; $sql = "select * from {$wpdb->prefix}timesheet_client_users_approval_queue where request_id = %s and status = 1"; $params = array($request_id); $request = $db->get_row($sql, $params); if ($request) { $sql = "select count(*) ct from {$wpdb->prefix}timesheet_clients_users where ClientId = {$request->client_id} and user_id = {$request->user_id}"; $access_count = $db->get_row($sql); if ($access_count->ct == 0) { $sql = "insert into {$wpdb->prefix}timesheet_clients_users (ClientId, user_id) values ({$request->client_id}, {$request->user_id})"; $db->query($sql); $sql = "insert into {$wpdb->prefix}timesheet_client_users_approval_queue_log (request_id, log_created, status, user_id) values ({$request->request_id}, now(), 2, {$user_id}"; $db->query($sql); $sql = "update {$wpdb->prefix}timesheet_client_users_approval_queue set status = 2, approved_by = %d, approved_on = now() where request_id = %s"; $params = array($user_id, $request->request_id); $db->query($sql, $params); $sql = "select user_email from {$wpdb->prefix}users where ID = {$request->user_id}"; $user = $db->get_row($sql); $sql = "select ClientName from {$wpdb->prefix}timesheet_clients where ClientId = %d"; $params = array($request->client_id); $client = $db->get_row($sql, $params); $subject = "Your access request for the client {$client->ClientName} has been approved"; $body = "Your access request for the client {$client->ClientName} has been approved"; $common->send_email($user->user_email, $subject, $body); return "

Request {$request->request_id} has been approved and completed.

"; } else { $sql = "insert into {$wpdb->prefix}timesheet_client_users_approval_queue_log (request_id, log_created, status, user_id) values ({$request->request_id}, now(), 3, {$user_id}"; $db->query($sql); $sql = "update {$wpdb->prefix}timesheet_client_users_approval_queue set status = 3, approved_by = %d, approved_on = now() where request_id = %s"; $params = array($user_id, $request->request_id); $db->query($sql, $params); return "

Request {$request->request_id} has been completed. The user already has access to this client.

"; } } else { return "

This request can not be found, or has been completed.

"; } } function request_client_access_deny($request_id) { global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $user = wp_get_current_user(); $user_id = $user->ID; $sql = "select * from {$wpdb->prefix}timesheet_client_users_approval_queue where request_id = %s and status = 1"; $params = array($request_id); $request = $db->get_row($sql, $params); if ($request) { $sql = "insert into {$wpdb->prefix}timesheet_client_users_approval_queue_log (request_id, log_created, status, user_id) values ({$request->request_id}, now(), 3, {$user_id}"; $db->query($sql); $sql = "update {$wpdb->prefix}timesheet_client_users_approval_queue set status = 3, approved_by = %d, approved_on = now() where request_id = %s"; $params = array($user_id, $request->request_id); $db->query($sql, $params); $sql = "select user_email from {$wpdb->prefix}users where ID = {$request->user_id}"; $user = $db->row($sql); $sql = "select ClientName from {$wpdb->prefix}timesheet_clients where ClientId = %d"; $params = array($request->client_id); $client = $db->get_row($sql, $params); $subject = "Your access request for the client {$client->ClientName} has been denied"; $body = "Your access request for the client {$client->ClientName} has been denied. Please contact the management team for more information."; $common->send_email($user->user_email, $subject, $body); return "

Request {$request->request_id} has been canceled.

"; } else { return "

This request can not be found, or has been completed.

"; } } function request_client_access() { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $page = ''; if (isset($_GET['clientid'])) { $page = $page . $this->request_client_access_save(intval($_GET['clientid'])); } $sql = "select distinct ClientName, c.ClientId from {$wpdb->prefix}timesheet_clients c join {$wpdb->prefix}timesheet_client_projects cp on c.ClientId = cp.ClientId and cp.Active = 1 where c.ClientId not in (select ClientId from {$wpdb->prefix}timesheet_clients_users where user_id = {$user_id}) order by ClientName"; $clients = $db->get_results($sql); $page = $page . "

Select a client from the list which you need access to: "; if ($clients) { $page = $page . "

"; } else { $page = $page . "

There are no clients entered."; } $page = $page . "

"; return $page; } function show_timesheet($timesheet_id=0, $show=1) { global $wpdb; #global $timesheet_id; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; //$main = new time_sheets_main(); $settings = new time_sheets_settings(); $options = get_option('time_sheets'); $common = new time_sheets_common(); $allow_overtime = isset($options['allow_overtime']) ? $options['allow_overtime'] : ""; $currency_char = isset($options['currency_char']) ? $options['currency_char'] : ""; $show_overtime_when_empty = isset($options['show_overtime_when_empty']) ? 1 : 0; $enable_non_billable_time = isset($options['enable_non_billable_time']) ? 1 : 0; $show_non_billable_when_empty = isset($options['show_non_billable_when_empty']) ? 1 : 0; $page = ''; if (isset($_GET['subpage'])) { if ($_GET['subpage'] == 'request_client_access') { $page = $page . $this->request_client_access(); } if ($_GET['subpage'] == 'approve_request') { $page = $page . $this->request_client_access_approve($_GET['request_id']); } if ($_GET['subpage'] == 'deny_request') { $page = $page . $this->request_client_access_deny($_GET['request_id']); } if ($show==1) { echo $page; return; } else { return $page; } } if (isset($_GET['is_template']) || isset($_POST['is_template'])) { $table_name = "{$wpdb->prefix}timesheet_scheduled"; $is_template = True; } else { $table_name = "{$wpdb->prefix}timesheet"; $is_template = False; } $common->show_datestuff(); $queue_approval = new time_sheets_queue_approval(); $queue_invoice = new time_sheets_queue_invoice(); $queue_payroll = new time_sheets_queue_payroll(); $folder = plugins_url(); if ($user_id == 0) { $page = $page . "

You must be logged in to view this page.

"; return $page; } if (isset($_POST['action']) && $_POST['action'] == 'save') { if ($this->validate_timesheet($page1)=="true") { $timesheet_id = $this->save_timesheet(); } else { $page = $page . $page1; $timesheet_id = -1; } } if (!$timesheet_id || $timesheet_id == 0 || $timesheet_id == "") { if (isset($_POST['timesheet_id']) || isset($_GET['timesheet_id']) || isset($_GET['time_sheet_id']) ) { if (isset($_POST['timesheet_id'])) { $timesheet_id = intval($_POST['timesheet_id']); } elseif (isset($_GET['timesheet_id'])) { $timesheet_id = intval($_GET['timesheet_id']); } elseif (isset($_GET['time_sheet_id'])) { $timesheet_id = intval($_GET['time_sheet_id']); } } else { if (isset($_POST['submit']) && $_POST['submit'] == 'Save Timesheet') { $sql = "select max(timesheet_id) from {$table_name} where user_id={$user_id}"; $timesheet_id = $db->get_var($sql); } } } if (!$is_template) { $page = $page . $this->check_overages(intval($timesheet_id), 1); } if (isset($_POST['action']) && ($_POST['action']=="comment_to_timesheet")) { $timesheet_id_int = intval($_POST['timesheet_id']); $comment = $common->esc_textarea($_POST['comment']); $timesheet_action = intval($_POST['timesheet_action']); check_admin_referer('timesheet_edit_timesheet'); if (!$comment && $timesheet_rejected=1) { $page = $page . "
Time sheets can not be rejected without a comment.
"; } else { $this->save_audit_comment($timesheet_id, $comment, $timesheet_action); } } $sql = "select t.*, u.* ,tcp.*, cs.display_name cs_display_name, cps.display_name cps_display_name, tcs.display_name tcs_display_name, tcps.display_name tcps_display_name"; if (isset($options['allow_money_based_retainers'])) { $sql = $sql . ", et.name as 'TitleName', et2.name as 'TitleNameOverride'"; } $sql = $sql . " from {$table_name} 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 JOIN {$wpdb->prefix}timesheet_client_projects cp ON t.ProjectId = cp.ProjectId JOIN {$wpdb->prefix}timesheet_clients c ON cp.ClientId = c.ClientId left outer join {$wpdb->users} cs on c.sales_person_id = cs.ID left outer join {$wpdb->users} cps on cp.sales_person_id = cps.ID left outer join {$wpdb->users} tcs on c.technical_sales_person_id = tcs.ID left outer join {$wpdb->users} tcps on cp.technical_sales_person_id = tcps.ID "; if (isset($options['allow_money_based_retainers']) ){ $sql = $sql . "left outer join {$wpdb->prefix}timesheet_employee_title_join etj on u.ID = etj.user_id left outer join {$wpdb->prefix}timesheet_customer_project_employee_title_override peto on peto.user_id = u.ID and peto.project_id = t.ProjectId left outer join {$wpdb->prefix}timesheet_employee_titles et on etj.title_id = et.title_id left outer join {$wpdb->prefix}timesheet_employee_titles et2 on peto.title_id = et2.title_id"; } $sql = $sql . " 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; if ((!$queue_approval->employee_approver_check('true') && !$queue_invoice->employee_invoicer_check() && !$queue_payroll->employee_payroll_check() ) && $timesheet_user_id <> $user_id) { $page = $page . "Only supervisors can view other employees time sheets."; return; } } else { $timesheet = (object) $_POST; $timesheet_user_id = $user_id; $timesheet_id_int = 0; } $page = $page . "

Weekly Time Sheet"; if ($is_template) { $page = $page . " Template"; } $page = $page . "

"; $disable_object=""; if (isset($timesheet->week_complete) && ($timesheet->week_complete=='1')) { $disable_object= " disabled"; } $page = $page . "
"; //class='ws-validate' $nonce = wp_create_nonce('timesheet_edit_timesheet'); $page = $page . ""; if ($timesheet_id_int == 0) { $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; } $week_starts = (isset($options['week_starts'])) ? $options['week_starts'] : 0; if (date('w', strtotime($start_date)) != $week_starts) { $page = $page . "

This time sheet does not start on a {$this->int_to_weekday($options['week_starts'], 0)}. Please change the date to a {$this->int_to_weekday($options['week_starts'], 0)}.

"; } $page = $page . ""; if (isset($timesheet_user)) { $page = $page . ""; $page = $page . ""; if (isset($options['allow_money_based_retainers']) && (isset($timesheet->week_complete) && ($timesheet->week_complete=='1'))){ $page = $page . ""; } } if ((isset($timesheet->invoiceid)) && ($timesheet->invoiceid != 0)) { $page = $page . ""; } $page = $page . ""; //
data-date-inline-picker='false' data-date-open-on-focus='true'
if ($timesheet_id_int == 0) { $ProjectId = -1; $clientid = 0; } else { $ProjectId = $timesheet->ProjectId; $clientid = $timesheet->ClientId; } if (isset($options['hide_client_project'])) { #$Clients = $common->return_clients_for_user($timesheet_user_id); $Projects = $common->return_projects_for_user($timesheet_user_id); if (sizeof($Projects)==1) { $page = $page . ""; } else { $js_projects = $common->draw_clients_and_projects($clientid, $ProjectId, $timesheet_id_int, $disable_object, $timesheet, $clientList); } } else { $js_projects = $common->draw_clients_and_projects($clientid, $ProjectId, $timesheet_id_int, $disable_object, $timesheet, $clientList); } $page = $page . $js_projects[1]; if (isset($timesheet->notes)) { $page = $page . ""; } if (isset($timesheet->MaxHours) && $timesheet->MaxHours <> 0) { $page = $page . ""; } if (isset($options['sales_override']) && $options['sales_override']=='project') { if (isset($timesheet->cps_display_name)) { $page = $page . ""; } if (isset($timesheet->tcps_display_name)) { $page = $page . ""; } } else { if (isset($timesheet->cs_display_name)) { $page = $page . ""; } if (isset($timesheet->tcs_display_name)) { $page = $page . ""; } } $page = $page . "
" . (($is_template == true)?"Template":"Timesheet") . " Number:{$timesheet_id}
" . (($is_template == true)?"Template":"Timesheet") . " For:{$timesheet_user}
Employee Title"; if (isset($timesheet->TitleNameOverride)) { $page = $page . " for Project"; } $page = $page . ":"; if (isset($timesheet->TitleNameOverride)) { $page = $page . $common->clean_from_db($timesheet->TitleNameOverride); } else { $page = $page . $common->clean_from_db($timesheet->TitleName); } $page = $page . "
Invoice ID:{$timesheet->invoiceid}
Week Start Date:
Project Notes:
Maximum Project Hours:{$timesheet->MaxHours}
Sales Person:{$timesheet->cps_display_name}
Technical Sales Person:{$timesheet->tcps_display_name}
Sales Person:{$timesheet->cs_display_name}
Technical Sales Person:{$timesheet->tcs_display_name}
"; $week_starts = (isset($options['week_starts'])) ? $options['week_starts'] : 0; $page = $page . ""; $monday_hours = isset($timesheet->monday_hours) ? $timesheet->monday_hours : '0'; $tuesday_hours = isset($timesheet->tuesday_hours) ? $timesheet->tuesday_hours : '0'; $wednesday_hours = isset($timesheet->wednesday_hours) ? $timesheet->wednesday_hours : '0'; $thursday_hours = isset($timesheet->thursday_hours) ? $timesheet->thursday_hours : '0'; $friday_hours = isset($timesheet->friday_hours) ? $timesheet->friday_hours : '0'; $saturday_hours = isset($timesheet->saturday_hours) ? $timesheet->saturday_hours : '0'; $sunday_hours = isset($timesheet->sunday_hours) ? $timesheet->sunday_hours : '0'; $monday_ot = isset($timesheet->monday_ot) ? $timesheet->monday_ot : '0'; $tuesday_ot = isset($timesheet->tuesday_ot) ? $timesheet->tuesday_ot : '0'; $wednesday_ot = isset($timesheet->wednesday_ot) ? $timesheet->wednesday_ot : '0'; $thursday_ot = isset($timesheet->thursday_ot) ? $timesheet->thursday_ot : '0'; $friday_ot = isset($timesheet->friday_ot) ? $timesheet->friday_ot : '0'; $saturday_ot = isset($timesheet->saturday_ot) ? $timesheet->saturday_ot : '0'; $sunday_ot = isset($timesheet->sunday_ot) ? $timesheet->sunday_ot : '0'; $monday_nb = isset($timesheet->monday_nb) ? $timesheet->monday_nb : '0'; $tuesday_nb = isset($timesheet->tuesday_nb) ? $timesheet->tuesday_nb : '0'; $wednesday_nb = isset($timesheet->wednesday_nb) ? $timesheet->wednesday_nb : '0'; $thursday_nb = isset($timesheet->thursday_nb) ? $timesheet->thursday_nb : '0'; $friday_nb = isset($timesheet->friday_nb) ? $timesheet->friday_nb : '0'; $saturday_nb = isset($timesheet->saturday_nb) ? $timesheet->saturday_nb : '0'; $sunday_nb = isset($timesheet->sunday_nb) ? $timesheet->sunday_nb : '0'; $total_ot = $monday_ot+$tuesday_ot+$wednesday_ot+$thursday_ot+$friday_ot+$saturday_ot+$sunday_ot; $total_nb = $monday_nb+$tuesday_nb+$wednesday_nb+$thursday_nb+$friday_nb+$saturday_nb+$sunday_nb; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $hide_ot = 0; $hide_nb = 0; //if ($show_overtime_when_empty==1 && isset($timesheet->week_complete) && $timesheet->week_complete=='1' && $total_ot==0) { // $hide_ot = 1; //} if (isset($timesheet->week_complete) && $timesheet->week_complete=='1') { //Week is complete if ($show_overtime_when_empty==0 && $total_ot == 0) { $hide_ot = 1; } if ($show_non_billable_when_empty==0 && $total_nb == 0) { $hide_nb = 1; } } else { //Week is not complete if ($enable_non_billable_time==0) { $hide_nb = 1; } } if (($allow_overtime) && ($hide_ot == 0)) { $monday_ot_style = ($monday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $tuesday_ot_style = ($tuesday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $wednesday_ot_style = ($wednesday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $thursday_ot_style = ($thursday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $friday_ot_style = ($friday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $saturday_ot_style = ($saturday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $sunday_ot_style = ($sunday_ot!=0 && $timesheet->week_complete=='1') ? "style='background-color:Tomato;'" : ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; } if ($hide_nb == 0) { $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; } $page = $page ."
{$this->int_to_weekday($week_starts, 0)}{$this->int_to_weekday($week_starts, 1)}{$this->int_to_weekday($week_starts, 2)}{$this->int_to_weekday($week_starts, 3)}{$this->int_to_weekday($week_starts, 4)}{$this->int_to_weekday($week_starts, 5)}{$this->int_to_weekday($week_starts, 6)}
Hours
Overtime
Non-Billable Time
"; $monday_desc = isset($timesheet->monday_desc) ? $common->esc_textarea($timesheet->monday_desc) : ''; $tuesday_desc = isset($timesheet->tuesday_desc) ? $common->esc_textarea($timesheet->tuesday_desc) : ''; $wednesday_desc = isset($timesheet->wednesday_desc) ? $common->esc_textarea($timesheet->wednesday_desc) : ''; $thursday_desc = isset($timesheet->thursday_desc) ? $common->esc_textarea($timesheet->thursday_desc) : ''; $friday_desc = isset($timesheet->friday_desc) ? $common->esc_textarea($timesheet->friday_desc) : ''; $saturday_desc = isset($timesheet->saturday_desc) ? $common->esc_textarea($timesheet->saturday_desc) : ''; $sunday_desc = isset($timesheet->sunday_desc) ? $common->esc_textarea($timesheet->sunday_desc) : ''; if ($settings->user_can_enter_notes($timesheet_user_id, 'display_notes_toggle', 'display_notes_list')==1) { $page = $page . "
Daily Notes"; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . "
{$this->int_to_weekday($options['week_starts'], 0)}
{$this->int_to_weekday($options['week_starts'], 1)}
{$this->int_to_weekday($options['week_starts'], 2)}
{$this->int_to_weekday($options['week_starts'], 3)}
{$this->int_to_weekday($options['week_starts'], 4)}
{$this->int_to_weekday($options['week_starts'], 5)}
{$this->int_to_weekday($options['week_starts'], 6)}
"; } $per_diem = ($timesheet_id_int!=0 ? $common->is_match($timesheet->isPerDiem, '1', ' selected') : ''); $actual = ($timesheet_id_int!=0 ? $common->is_match($timesheet->isPerDiem, '0', ' selected') : ''); if ($settings->user_can_enter_notes($timesheet_user_id, 'display_expenses_toggle', 'display_expenses_list')==1) { $page = $page . "Additional Information"; $page = $page . ""; $per_diem_days = (isset($timesheet->per_diem_days) ? $timesheet->per_diem_days : ''); $per_diem_city = (isset($timesheet->perdiem_city) ? $timesheet->perdiem_city : ''); $flight_cost = (isset($timesheet->flight_cost) ? $timesheet->flight_cost : ''); $hotel_charges = (isset($timesheet->hotel_charges) ? $timesheet->hotel_charges : ''); $rental_car_charges = (isset($timesheet->rental_car_charges) ? $timesheet->rental_car_charges : ''); $taxi = (isset($timesheet->taxi) ? $timesheet->taxi : ''); $tolls = (isset($timesheet->tolls) ? $timesheet->tolls : ''); $mileage = (isset($timesheet->mileage) ? $timesheet->mileage : ''); $other_expenses = (isset($timesheet->other_expenses) ? $timesheet->other_expenses : ''); $other_expenses_notes = (isset($timesheet->other_expenses_notes) ? $timesheet->other_expenses_notes : ''); $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . "

Per Diem CityFlight/Train CostsHotel ChargesRental Car ChargesTaxi/RideshareTollsMileage
({$options['distance_metric']} Driven)
Other Expenses
{$currency_char}{$currency_char}{$currency_char}{$currency_char}{$currency_char}{$currency_char}
Other Expense Notes:
"; } $page = $page . ""; if (!isset($options['remove_embargo'])) { $page = $page . ""; } //Recuring Timesheet if (isset($options['allow_recurring_timesheets']) && ($is_template==true || $timesheet_id_int==0)) { $page = $page . ""; $page = $page . ""; } $page = $page . "
Week Complete:week_complete) && $timesheet->week_complete=='1') { $page = $page . " checked disabled"; } $page = $page . ">
Project Complete:project_complete) && $timesheet->project_complete=='1') { $page = $page . " checked"; } $page = $page . "> (If this is available, all project workers must select this for client to be billed as client is only billed upon project completion.)
Make this a recurring timesheet?
Interval
Next Execution next_execution))) . "' disabled>
Expires On expires_on))) . "' disabled> (optional)
delete_after_expiration==1)?'checked':'') . "> Delete template after expiration?
"; //if (!$is_template) { if ($timesheet_id_int==0 || $timesheet->week_complete=='0') { if (isset($timesheet->user_id)) { if ($timesheet->user_id == $user_id) { $page = $page . "
"; } } else { $page = $page . "
"; } } //} $page = $page . ""; if ($timesheet_id != "") { $page = $page . ""; if ($is_template == true) { $page = $page . ""; } } $page = $page . ""; $page = $page . "
"; if (isset($timesheet->week_complete) && $timesheet->week_complete=='1' && !isset($options['hide_notes'])){ //Comments that are being audited for time sheets that are closed. $page = $page . "

Time Sheet Comments

"; $nonce = wp_create_nonce('timesheet_edit_timesheet'); $page = $page . ""; $page = $page . ""; $page = $page . "
Timesheet Action:
Comment:
"; if ( ($queue_approval->employee_approver_check('true') || $queue_invoice->employee_invoicer_check() || $queue_payroll->employee_payroll_check() ) || ($timesheet_user_id == $user_id && $timesheet->approved==0 && isset($options['allow_users_to_reject']))) { if ( ($timesheet->invoiced==0) ) { $page = $page . "
"; } else { $page = $page . "

Comments can not be saved for an already invoiced timesheet.

"; } } $page = $page . ""; $page = $page . ""; $page = $page . ""; $page = $page . "
"; } $sql = "select tca.entered_at, tca.comment, tca.timesheet_rejected, u.display_name, timesheet_rejected as timesheet_action from {$wpdb->prefix}timesheet_comment_audit tca join {$wpdb->users} u on tca.entered_by = u.ID where timesheet_id=%d order by entered_at desc"; $params = array($timesheet_id); $comments = $db->get_results($sql,$params); if ($comments) { $page = $page . "
Entered Notes
"; foreach ($comments as $comment) { $page = $page . ""; } $page = $page . "
Date EnteredEntered ByCommentSent Email
{$comment->entered_at}{$comment->display_name}{$common->esc_textarea($comment->comment)}"; if ($comment->timesheet_action!=3) { $page = $page . ""; } else { $page = $page . ""; } $page = $page . "
"; } $page = $page . " "; if ($show==1) { echo $page; } else { return $page; } } function save_audit_comment($timesheet_id, $comment, $timesheet_action) { global $wpdb; $user = wp_get_current_user(); $user_id = $user->ID; $db = new time_sheets_db; $common = new time_sheets_common(); if ($timesheet_action==1) { $approval = new time_sheets_queue_approval; $approval->do_timesheet_rejectapproval($timesheet_id,$comment); } $sql = "insert into {$wpdb->prefix}timesheet_comment_audit (timesheet_id, entered_by, entered_at, comment, timesheet_rejected) values (%d, %d, now(), %s, %d)"; $params = array($timesheet_id, $user_id, $comment, $timesheet_action); $db->query($sql, $params); if ($timesheet_action != 3) { $sql = "select distinct display_name from {$wpdb->users} u join {$wpdb->prefix}timesheet t on u.ID = t.user_id where t.timesheet_id = %d"; $params = array($timesheet_id); $timesheet_user = $db->get_row($sql, $params); $sql = "select distinct user_email from {$wpdb->users} u join {$wpdb->prefix}timesheet t on u.ID = t.user_id where t.timesheet_id = %d union select user_email from {$wpdb->users} u join {$wpdb->prefix}timesheet_invoicers i on u.ID = i.user_id union select user_email from {$wpdb->users} u join {$wpdb->prefix}timesheet_approvers a on u.ID = a.user_id "; $params = array($timesheet_id); $to = $db->get_results($sql, $params); $http = !empty($_SERVER['HTTPS']) ? "https" : "http"; $stat = $timesheet_action==1 ? " rejected this time sheet with the comment: " : " entered the comment: "; $subject = "Comment on Time Sheet {$timesheet_id}"; $body = $user->display_name . $stat . $comment . "
For the time sheet " . $timesheet_id . " entered by " . $timesheet_user->display_name . ".

"; foreach ($to as $a_to) { $common->send_email($a_to->user_email, $subject, $body); } } } function save_timesheet() { global $wpdb; #global $timesheet_id; $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $common = new time_sheets_common(); $ot_multiplier = isset($options['ot_multiplier']) ? floatval($options['ot_multiplier']) : 1; if ((!isset($_POST['is_template']) && !isset($_POST['template_id']))) { $table_name = "timesheet"; $archive_name = "timesheet_archive"; } else { $table_name = "timesheet_scheduled"; $archive_name = "timesheet_scheduled_archive"; } if ($ot_multiplier==0) { $ot_multiplier = 1; } if ($user_id == 0) { echo "

You must be logged in to view this page.

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

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

'; return -1; } if (isset($_POST['timesheet_id'])) { $timesheet_id = intval($_POST['timesheet_id']); } else { $timesheet_id = ''; } $nonce = check_admin_referer('timesheet_edit_timesheet'); $db = new time_sheets_db(); $monday_hours = isset($_POST['monday_hours']) ? floatval($_POST['monday_hours']) : 0; $tuesday_hours = isset($_POST['tuesday_hours']) ? floatval($_POST['tuesday_hours']) : 0; $wednesday_hours = isset($_POST['wednesday_hours']) ? floatval($_POST['wednesday_hours']) : 0; $thursday_hours = isset($_POST['thursday_hours']) ? floatval($_POST['thursday_hours']) : 0; $friday_hours = isset($_POST['friday_hours']) ? floatval($_POST['friday_hours']) : 0; $saturday_hours = isset($_POST['saturday_hours']) ? floatval($_POST['saturday_hours']) : 0; $sunday_hours = isset($_POST['sunday_hours']) ? floatval($_POST['sunday_hours']) : 0; $monday_ot = isset($_POST['monday_ot']) ? floatval($_POST['monday_ot']) : 0; $tuesday_ot = isset($_POST['tuesday_ot']) ? floatval($_POST['tuesday_ot']) : 0; $wednesday_ot = isset($_POST['wednesday_ot']) ? floatval($_POST['wednesday_ot']) : 0; $thursday_ot = isset($_POST['thursday_ot']) ? floatval($_POST['thursday_ot']) : 0; $friday_ot = isset($_POST['friday_ot']) ? floatval($_POST['friday_ot']) : 0; $saturday_ot = isset($_POST['saturday_ot']) ? floatval($_POST['saturday_ot']) : 0; $sunday_ot = isset($_POST['saturday_ot']) ? floatval($_POST['sunday_ot']) : 0; $monday_nb = isset($_POST['monday_nb']) ? floatval($_POST['monday_nb']) : 0; $tuesday_nb = isset($_POST['tuesday_nb']) ? floatval($_POST['tuesday_nb']) : 0; $wednesday_nb = isset($_POST['wednesday_nb']) ? floatval($_POST['wednesday_nb']) : 0; $thursday_nb = isset($_POST['thursday_nb']) ? floatval($_POST['thursday_nb']) : 0; $friday_nb = isset($_POST['friday_nb']) ? floatval($_POST['friday_nb']) : 0; $saturday_nb = isset($_POST['saturday_nb']) ? floatval($_POST['saturday_nb']) : 0; $sunday_nb = isset($_POST['saturday_nb']) ? floatval($_POST['sunday_nb']) : 0; $start_date = date("Y-m-d", strtotime($_POST['start_date'])); if (isset($_POST['timesheet_id'])) { $sql = "select user_id from {$wpdb->prefix}{$table_name} where timesheet_id = %d"; $parm = array(intval($_POST['timesheet_id'])); $timesheet = $db->get_row($sql, $parm); if ($timesheet) { //This is an update. Make sure the person updating is this person. if ($timesheet->user_id != $user_id) { echo '

You are attempting this submit a timesheet for a different user. This is not allowed.

'; return -1; } } else { echo '

You appear to be updating a timesheet which does not exist.

'; return -1; } $sql = "select count(*) ct from {$wpdb->prefix}{$table_name} where user_id = %d and ProjectId = %d and start_date = %s and timesheet_id <> %d"; $params = array($user_id, intval($_POST['ProjectId']), $start_date, intval($_POST['timesheet_id'])); $record_count = $db->get_var($sql, $params); if ($record_count!=0) { if (isset($options['error_if_date_already_used'])) { echo '

You already have entered a time sheet for this project for this week. This time sheet was NOT saved.

'; $_POST['submit'] == 'blank'; return -1; } else { echo "

You already have entered a time sheet for this project for this week.

"; } } } else { //Only do for new timesheets $sql = "select count(*) ct from {$wpdb->prefix}{$table_name} where user_id = %d and ProjectId = %d and start_date = %s"; $params = array($user_id, intval($_POST['ProjectId']), $start_date); $record_count = $db->get_var($sql, $params); if ($record_count!=0) { if (isset($options['error_if_date_already_used'])) { echo '

You already have entered a time sheet for this project for this week. This time sheet was NOT saved.

'; $_POST['submit'] == 'blank'; return -1; } else { echo "

You already have entered a time sheet for this project for this week.

"; } } } $may_embargo = isset($_POST['may_embargo'])? 1 : 0 ; $project_complete = isset($_POST['project_complete'])? 1 : 0 ; $week_complete = isset($_POST['week_complete']) ? 1 : 0; //if ($_POST['week_complete']==1) { // $week_complete=1; //} else { // $week_complete=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; } $total_hours = $monday_hours + $tuesday_hours + $wednesday_hours + $thursday_hours + $friday_hours + $saturday_hours + $sunday_hours; $total_hours = $total_hours + (($monday_ot + $tuesday_ot + $wednesday_ot + $thursday_ot + $friday_ot + $saturday_ot + $sunday_ot)*$ot_multiplier); if (isset($_POST['timesheet_id'])) { //UPDATE $timesheet_id = $_POST['timesheet_id']; $sql = "update {$wpdb->prefix}{$table_name} 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, taxi=%s, monday_ot=%s, tuesday_ot=%s, wednesday_ot=%s, thursday_ot=%s, friday_ot=%s, saturday_ot=%s, sunday_ot=%s, monday_nb=%s, tuesday_nb=%s, wednesday_nb=%s, thursday_nb=%s, friday_nb=%s, saturday_nb=%s, sunday_nb=%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, (isset($_POST['monday_desc'])) ? sanitize_textarea_field($_POST['monday_desc']) : "", (isset($_POST['tuesday_desc'])) ? sanitize_textarea_field($_POST['tuesday_desc']) : "", (isset($_POST['wednesday_desc'])) ? sanitize_textarea_field($_POST['wednesday_desc']) : "", (isset($_POST['thursday_desc'])) ? sanitize_textarea_field($_POST['thursday_desc']) : "", (isset($_POST['friday_desc'])) ? sanitize_textarea_field($_POST['friday_desc']) : "", (isset($_POST['saturday_desc'])) ? sanitize_textarea_field($_POST['saturday_desc']) : "", (isset($_POST['sunday_desc'])) ? sanitize_textarea_field($_POST['sunday_desc']) : "", (isset($_POST['per_diem_days'])) ? floatval($_POST['per_diem_days']) : 0, (isset($_POST['hotel_charges'])) ? floatval($_POST['hotel_charges']) : 0, (isset($_POST['rental_car_charges'])) ? floatval($_POST['rental_car_charges']) : 0, (isset($_POST['tolls'])) ? floatval($_POST['tolls']) : 0, (isset($_POST['other_expenses'])) ? floatval($_POST['other_expenses']) : 0, (isset($_POST['other_expenses_notes'])) ? sanitize_textarea_field($_POST['other_expenses_notes']) : "", $week_complete, (isset($_POST['mileage'])) ? floatval($_POST['mileage']) : 0, $EmbargoPendingProjectClose, $project_complete, (isset($_POST['flight_cost'])) ? floatval($_POST['flight_cost']) : 0, (isset($_POST['isPerDiem'])) ? intval($_POST['isPerDiem']) : 0, (isset($_POST['perdiem_city'])) ? sanitize_text_field($_POST['perdiem_city']) : "", (isset($_POST['taxi'])) ? floatval($_POST['taxi']) : 0, floatval($monday_ot), floatval($tuesday_ot), floatval($wednesday_ot), floatval($thursday_ot), floatval($friday_ot), floatval($saturday_ot), floatval($sunday_ot), floatval($monday_nb), floatval($tuesday_nb), floatval($wednesday_nb), floatval($thursday_nb), floatval($friday_nb), floatval($saturday_nb), floatval($sunday_nb), intval($_POST['timesheet_id'])); $db->query($sql, $params); if ($week_complete==1) { $sql = "update {$wpdb->prefix}{$table_name} set marked_complete_by=$user_id, marked_complete_date = CURDATE() where timesheet_id=%d"; $params=array(intval($_POST['timesheet_id'])); if (isset($options['allow_recurring_timesheets'])) { $this->update_retainer_usage($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); $this->set_project_inactive_on_date(intval($_POST['ProjectId'])); $sql = "delete from {$wpdb->prefix}timesheet_reject_message_queue where timesheet_id = %d"; $params=array(intval($_POST['timesheet_id'])); $db->query($sql, $params); } echo '

Timesheet updated.

'; } else { //INSERT $sql = "insert into {$wpdb->prefix}{$table_name} (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, 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) 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, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %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, (isset($_POST['monday_desc'])) ? sanitize_textarea_field($_POST['monday_desc']) : "", (isset($_POST['tuesday_desc'])) ? sanitize_textarea_field($_POST['tuesday_desc']) : "", (isset($_POST['wednesday_desc'])) ? sanitize_textarea_field($_POST['wednesday_desc']) : "", (isset($_POST['thursday_desc'])) ? sanitize_textarea_field($_POST['thursday_desc']) : "", (isset($_POST['friday_desc'])) ? sanitize_textarea_field($_POST['friday_desc']) : "", (isset($_POST['saturday_desc'])) ? sanitize_textarea_field($_POST['saturday_desc']) : "", (isset($_POST['sunday_desc'])) ? sanitize_textarea_field($_POST['sunday_desc']) : "", (isset($_POST['per_diem_days'])) ? floatval($_POST['per_diem_days']) : 0, (isset($_POST['hotel_charges'])) ? floatval($_POST['hotel_charges']) : 0, (isset($_POST['rental_car_charges'])) ? floatval($_POST['rental_car_charges']) : 0, (isset($_POST['tolls'])) ? floatval($_POST['tolls']) : 0, (isset($_POST['other_expenses'])) ? floatval($_POST['other_expenses']) : 0, (isset($_POST['other_expenses_notes'])) ? sanitize_textarea_field($_POST['other_expenses_notes']) : "", $week_complete, (isset($_POST['mileage'])) ? $_POST['mileage'] : 0, $EmbargoPendingProjectClose, $project_complete, (isset($_POST['flight_cost'])) ? floatval($_POST['flight_cost']) : 0, (isset($_POST['isPerDiem'])) ? floatval($_POST['isPerDiem']) : 0, (isset($_POST['perdiem_city'])) ? sanitize_text_field($_POST['perdiem_city']) : "", (isset($_POST['taxi'])) ? floatval($_POST['taxi']) : 0, floatval($monday_ot), floatval($tuesday_ot), floatval($wednesday_ot), floatval($thursday_ot), floatval($friday_ot), floatval($saturday_ot), floatval($sunday_ot), floatval($monday_nb), floatval($tuesday_nb), floatval($wednesday_nb), floatval($thursday_nb), floatval($friday_nb), floatval($saturday_nb), floatval($sunday_nb)); $db->query($sql, $params); $sql = "select max(timesheet_id) from {$wpdb->prefix}{$table_name} where user_id={$user_id}"; $timesheet_id = $db->get_var($sql); #$this->check_overages($timesheet_id, 1); if ($week_complete==1 && !isset($_POST['is_template'])) { $sql = "update {$wpdb->prefix}timesheet set marked_complete_by=$user_id, marked_complete_date = CURDATE() where timesheet_id=$timesheet_id"; if (isset($options['allow_recurring_timesheets'])) { $this->update_retainer_usage($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); $this->set_project_inactive_on_date(intval($_POST['ProjectId'])); } echo "

" . ((isset($_POST['is_template']) || isset($_POST['template_id']) )?'Template':'Timesheet') . " saved.

"; } if (isset($_POST['is_template']) || isset($_POST['template_id']) ) { //If this is a template if (isset($_POST['interval'])) { if ($_POST['interval'] == 'w') { $interval = 'w'; } elseif ($_POST['interval'] == 'm') { $interval = 'm'; } elseif ($_POST['interval'] == 'q') { $interval = 'q'; } elseif ($_POST['interval'] == 'a') { $interval = 'a'; } } if (isset($_POST['delete_after_expiration'])) { $delete_after_expiration = 1; } else { $delete_after_expiration = 0; } $sql = "update {$wpdb->prefix}{$table_name} set frequency = %s, next_execution = %s, expires_on = %s, delete_after_expiration = %d where timesheet_id = %d"; $params = array($interval, $_POST['next_execution'], $_POST['expires'], $delete_after_expiration, $timesheet_id); $db->query($sql, $params); } #Archive the change if (isset($_POST['timesheet_id'])) { //UPDATE $common->archive_records ($table_name, $archive_name, 'timesheet_id', intval($_POST['timesheet_id'])); } else { //INSERT $common->archive_records ($table_name, $archive_name, 'timesheet_id', intval($timesheet_id), '=', 'INSERT'); } 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); //Archive the change $common->archive_records ('timesheet', 'timesheet_archive', 'EmbargoPendingProjectClose = 1 AND ProjectId', intval($_POST['ProjectId'])); echo "

Your timesheets for this client have been sent for processing.

"; $sql = "update {$wpdb->prefix}timesheet_client_projects set Active=0 where ProjectId=%d"; $params = array(intval($_POST['ProjectId'])); $db->query($sql, $params); //Archive the change $common->archive_records ('timesheet_client_projects', 'timesheet_client_projects_archive', 'ProjectId', intval($_POST['ProjectId'])); echo "

This project has been marked as closed.

"; $this->email_on_submission(intval($timesheet_id)); } } #$sql = "select * from {$wpdb->prefix}timesheet_client_projects where project_id = %d"; #$parm = array(intval($_POST['ClientId'])); #$client = $db->get_row($sql, $parm); return $timesheet_id; } function update_retainer_usage($timesheet_id) { global $wpdb; $common = new time_sheets_common(); $user = wp_get_current_user(); $user_id = $user->ID; $options = get_option('time_sheets'); $page = ''; $db = new time_sheets_db(); $sql = "select * from {$wpdb->prefix}timesheet where timesheet_id = %d"; $params = array($timesheet_id); $timesheet = $db->get_row($sql, $params); $sql = "select * from {$wpdb->prefix}timesheet_client_projects where ProjectId = {$timesheet->ProjectId}"; $project = $db->get_row($sql); if (!isset($project->retainer_id) || $project->retainer_id==0) { return 0; } $total_hours = $common->get_timesheets_total_hours($timesheet_id); $rate = $common->get_employee_hourly_rate($user_id, $timesheet->ProjectId); $sql = "select * from {$wpdb->prefix}timesheet_client_projects where ProjectId = {$timesheet->ProjectId}"; $project = $db->get_row($sql); $retainer_id = $project->retainer_id; $start_date = $common->get_retainer_interval_startdate ($retainer_id, $timesheet->start_date, $timesheet->ProjectId); if ($rate) { $timesheet_amount = $total_hours*$rate; $sql = "insert into {$wpdb->prefix}timesheet_project_retainer_money_usage (project_id, frequency_start_date, retainer_id, max_frequency_bucket, used_amount) values ({$timesheet->ProjectId}, '{$start_date}', {$project->retainer_id}, {$project->max_monthly_bucket}, {$timesheet_amount}) ON DUPLICATE KEY UPDATE used_amount=used_amount+{$timesheet_amount}"; $db->query($sql); $sql = "select * from {$wpdb->prefix}timesheet_project_retainer_money_usage prmu join {$wpdb->prefix}timesheet_client_projects tcp ON tcp.ProjectId = prmu.project_id join {$wpdb->prefix}timesheet_clients tc on tcp.ClientId = tc.ClientId where project_id={$timesheet->ProjectId} and frequency_start_date = '{$start_date}'"; $retainer = $db->get_row($sql); if ($retainer) { if ($retainer->max_frequency_bucket < $retainer->used_amount*$options['project_trigger_percent'] ){ if (isset($options["email_address_on_project_over"])) { $common->send_email($options["email_address_on_project_over"], "Project {$retainer->ProjectName} for the client {$retainer->ClientName} is close to over budget", "Project {$retainer->ProjectName} for the client {$retainer->ClientName} has gone over {$options['project_trigger_percent']}% of the budget.

The client needs to be contacted to resolve the issue.

--The Time Sheet System"); echo "

Project has gone over {$options['project_trigger_percent']}% allowed amount for this interval. The sales team has been notified.

"; } else { echo "

Project has gone over {$options['project_trigger_percent']}% of the allowed amount for this interval. Please contact your sales team for assistance.

"; } } } else { echo "

Unable to write project usage values for this project. Contact your administrator.

"; } } else { echo "

No mapping between project and employee created. Contact administrator for assistance.

"; } } function set_project_inactive_on_date($project_id) { global $wpdb; $user = wp_get_current_user(); $user_id = $user->ID; $db = new time_sheets_db(); $common = new time_sheets_common(); $sql = "select MaxHours, IsRetainer FROM {$wpdb->prefix}timesheet_client_projects WHERE ProjectId = %d"; $params = array($project_id); $project = $db->get_row($sql, $params); if ($project->IsRetainer==1) { #The project is a retainer, do not close it return; } if ($project->MaxHours==0) { #If the project has no max number of hours, skip it return; } $sql = "select sum(total_hours) total_hours FROM {$wpdb->prefix}timesheet WHERE ProjectId = %d and week_complete = 1"; $params = array($project_id); $timesheets = $db->get_row($sql, $params); if ($timesheets->total_hours < $project->MaxHours) { #The project hasn't run out of hours so there's nothing to do yet return; } $date = new DateTime(); // Modify the date it contains $date->modify('next tuesday'); $nextTuesday = $date->format('Y-m-d'); $sql = "delete from {$wpdb->prefix}timesheet_client_project_autoclose where project_id = %d"; $params = array($project_id); $db->query($sql, $params); $sql = "insert into {$wpdb->prefix}timesheet_client_project_autoclose (project_id, close_date) values (%d, %s)"; $params = array($project_id, $nextTuesday); $db->query($sql, $params); } }