do_timesheet_payroll(); } if ($_GET['page'] == "invoice_timesheet") { $entry->do_timesheet_invoicing(); } if ($_GET['page'] == "approve_timesheet") { $this->do_timesheet_approval_processing(); } } elseif ($_GET['action'] == "split2") { if ($_GET['page'] == "approve_timesheet") { $this->do_timesheet_split(); } } else { if ($_GET['page'] == "payroll_timesheet") { $entry->do_timesheet_rejectpayroll(); } if ($_GET['page'] == "invoice_timesheet") { $entry->do_timesheet_rejectinvoicing(); } } } } function do_timesheet_approval_processing() { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $timesheets = $this->return_approval_list(); if ($timesheets) { foreach ($timesheets as $timesheet) { $objname = "sheet_{$timesheet->timesheet_id}"; $value = $_GET[$objname]; if ($value == "approve") { $this->do_timesheet_approval($timesheet->timesheet_id); } if ($value == "reject") { $this->do_timesheet_rejectapproval($timesheet->timesheet_id); } } } echo "

Timesheet(s) updated.

"; } function email_on_submission($timesheet_id) { $options = get_option('time_sheets'); if (!$options['enable_email']) { return; } global $wpdb; $db = new time_sheets_db(); $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) { $this->send_email ($user->user_email, $subject, $body); } } function email_on_approval($timesheet_id) { $options = get_option('time_sheets'); if (!$options['enable_email']) { return; } global $wpdb; $db = new time_sheets_db(); $sql = "select display_name 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 from {$wpdb->users} u join {$wpdb->prefix}timesheet_invoicers a on u.ID = a.user_id"; $users = $db->get_results($sql); $subject = "There are time sheet(s) pending invoicing."; $body = "A time sheet has been entered by {$user_login} which is approved and needs to be invoiced to the client.. It can be viewed and invoiced from the invoicing menu."; foreach ($users as $user) { $this->send_email ($user->user_email, $subject, $body); } } function send_email ($to, $subject, $body) { global $wpdb; $options = get_option('time_sheets'); $db = new time_sheets_db(); if($options['enable_email']) { $sql = "insert into {$wpdb->prefix}timesheet_emailqueue (send_to, send_from_email, send_from_name, subject, message_body, entered_on) values (%s, %s, %s, %s, %s, now())"; $parms = array($to, $options['email_from'], $options['email_name'], $subject, $body); $db->query($sql, $parms); if ($options['show_email_notice']) { echo "

Email to {$to} queued.

"; } } } function enter_timesheet() { if ($_POST['action'] == 'save') { if ($this->validate_timesheet()==true) { $this->save_timesheet(); } } $this->show_timesheet(); } function do_timesheet_split() { global $wpdb; $db = new time_sheets_db(); $sql = "SELECT GROUP_CONCAT(distinct COLUMN_NAME) columns FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_NAME`='{$wpdb->prefix}timesheet' and COLUMN_NAME not in ('timesheet_id', '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', 'mileage', 'flight_cost')"; $cols = $db->get_var($sql); if ($_GET['new_start_date'] ==1) { $cols = "{$cols}, tuesday_hours, wednesday_hours, thursday_hours, friday_hours, saturday_hours, sunday_hours, tuesday_desc, wednesday_desc, thursday_desc, friday_desc, saturday_desc, sunday_desc"; $update_cols = "tuesday_hours = 0, wednesday_hours=0, thursday_hours=0, friday_hours=0, saturday_hours=0, sunday_hours=0, tuesday_desc='', wednesday_desc='', thursday_desc='', friday_desc='', saturday_desc='', sunday_desc=''"; } if ($_GET['new_start_date'] ==2) { $cols = "{$cols}, wednesday_hours, thursday_hours, friday_hours, saturday_hours, sunday_hours, wednesday_desc, thursday_desc, friday_desc, saturday_desc, sunday_desc"; $update_cols = "wednesday_hours=0, thursday_hours=0, friday_hours=0, saturday_hours=0, sunday_hours=0, wednesday_desc='', thursday_desc='', friday_desc='', saturday_desc='', sunday_desc=''"; } if ($_GET['new_start_date'] ==3) { $cols = "{$cols}, thursday_hours, friday_hours, saturday_hours, sunday_hours, thursday_desc, friday_desc, saturday_desc, sunday_desc"; $update_cols = "thursday_hours=0, friday_hours=0, saturday_hours=0, sunday_hours=0, thursday_desc='', friday_desc='', saturday_desc='', sunday_desc=''"; } if ($_GET['new_start_date'] ==4) { $cols = "{$cols}, friday_hours, saturday_hours, sunday_hours, friday_desc, saturday_desc, sunday_desc"; $update_cols = "friday_hours=0, saturday_hours=0, sunday_hours=0, friday_desc='', saturday_desc='', sunday_desc=''"; } if ($_GET['new_start_date'] ==5) { $cols = "{$cols}, saturday_hours, sunday_hours, saturday_desc, sunday_desc"; $update_cols = "saturday_hours=0, sunday_hours=0, saturday_desc='', sunday_desc=''"; } if ($_GET['new_start_date'] ==6) { $cols = "{$cols}, sunday_hours, sunday_desc"; $update_cols = "sunday_hours=0, sunday_desc=''"; } $sql = "INSERT INTO {$wpdb->prefix}timesheet ({$cols}) SELECT {$cols} FROM {$wpdb->prefix}timesheet WHERE timesheet_id = %d"; $parms = array($_GET['timesheet_id']); $db->query($sql, $parms); $sql = "UPDATE {$wpdb->prefix}timesheet set {$update_cols} WHERE timesheet_id = %d"; $db->query($sql, $parms); echo '

Timesheet has been split.

'; } function split_timesheet(){ global $wpdb; $db = new time_sheets_db(); $sql = "select start_date from {$wpdb->prefix}timesheet where timesheet_id = %d"; $parms = array($_GET['timesheet_id']); $start_date = $db->get_var($sql, $parms); echo "

Date selected below and later will be moved to a new timesheet. Expenses will be left on the origional timesheet.
Select date to split timesheet:
"; } function approve_timesheet(){ if ($_GET['action']=='preapprove') { $this->show_timesheet(); } elseif ($_GET['action']=='split') { $this->split_timesheet(); } else { echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
Show Embargoed Entries
Show Retainers
Hide Non-Retainers
"; echo "
"; } } function show_invoicing_list() { global $wpdb; $db = new time_sheets_db(); $main = new time_sheets_main(); $approver = $main->employee_approver_check(); $timesheets = $this->return_invoicing_list(); $folder = plugins_url(); if ($_GET['show_retainers']) { $retainers_parm = "&show_retainers=checked"; } if ($_GET['hide_nonretainers']) { $retainers_parm = $retainers_parm . "&hide_nonretainers=checked"; } if ($timesheets) { echo "
"; $this->show_approval_list(); echo ""; $this->show_clients_on_retainer(); echo "
"; if ($approver<>0) { echo ""; } echo ""; foreach ($timesheets as $timesheet) { echo ""; if ($approver<>0) { echo ""; } $invoice_id = $timesheet->invoiceid; if ($invoice_id == 0) { $invoice_id = ''; } echo ""; } echo "
TimesheetRejectViewCompletedInvoice NumberUserStart DateFirst Billed DayClientProject Name
{$timesheet->timesheet_id}{$this->replace(' ', ' ', $timesheet->display_name)}{$this->f_date($timesheet->start_date)}{$this->f_date($timesheet->first_billed_day)}{$timesheet->client_name}{$timesheet->project_name}

"; if ($_GET['show_retainers']) { echo ""; } if ($_GET['hide_nonretainers']) { echo ""; } echo ""; } else { echo "No time sheets to invoice."; } } function remove_br($string) { $string = str_replace("\'", "'", $string); return nl2br($string); } function show_payroll_list() { global $wpdb; $db = new time_sheets_db(); $main = new time_sheets_main(); $approver = $main->employee_approver_check(); $options = get_option('time_sheets'); $timesheets = $this->return_payroll_list(); $folder = plugins_url(); if ($timesheets) { echo "
"; if ($approver<>0) { echo ""; } echo ""; if ($options['mileage']) { echo ""; } if ($options['per_diem']) { echo ""; } if ($options['flight_cost']) { echo ""; } if ($options['hotel']) { echo ""; } if ($options['rental_car']) { echo ""; } if ($options['tolls']) { echo ""; } if ($options['other_expenses']) { echo ""; } echo ""; foreach ($timesheets as $timesheet) { echo ""; if ($approver<>0) { echo ""; } echo ""; if ($options['mileage']) { echo ""; } if ($options['per_diem']) { echo ""; } if ($options['flight_cost']) { echo ""; } if ($options['hotel']) { echo ""; } if ($options['rental_car']) { echo ""; } if ($options['tolls']) { echo ""; } if ($options['other_expenses']) { echo ""; } echo ""; } echo "
TimesheetRejectViewCompletedUserStart DateClientProject NameMileagePer Diem DaysFlight/Train CostHotel ChargesRental CarTollsOther Expenses
{$timesheet->timesheet_id}{$this->replace(' ', ' ', $timesheet->display_name)}{$this->f_date($timesheet->start_date)}{$timesheet->client_name}{$timesheet->project_name}{$timesheet->mileage}{$timesheet->per_diem_days}{$timesheet->flight_cost}{$timesheet->hotel_charges}{$timesheet->rental_car_charges}{$timesheet->tolls}{$timesheet->other_expenses}

"; } else { echo "No time sheets to process for payroll."; } } function invoice_timesheet(){ if ($_GET['action']=='preinvoice') { $this->show_timesheet(); } else { echo "
"; echo ""; echo ""; echo ""; echo ""; echo "
Show Retainers
Hide Non-Retainers
"; echo "
"; } } function payroll_timesheet(){ if ($_GET['action']=='preinvoice') { $this->show_timesheet(); } else { echo "
"; $this->show_invoicing_list(); echo ""; $this->show_clients_on_retainer(); echo "
"; } } function show_clients_on_retainer() { global $wpdb; $db = new time_sheets_db(); $sql = "select ClientName, im.MonthlyHours, im.HourlyRate, im.Notes from {$wpdb->prefix}timesheet_clients tc inner join {$wpdb->prefix}timesheet_recurring_invoices_monthly im on tc.clientid = im.client_id where im.MonthlyHours <> 0 order by ClientName"; $clients = $db->get_results($sql); if ($clients) { echo "
"; $this->show_payroll_list(); echo "
"; foreach ($clients as $client) { echo ""; } echo "
Client NameNumber of HoursHourly Rate on RetainerNotes
{$client->ClientName}{$client->MonthlyHours}$ {$client->HourlyRate}{$this->remove_br($client->Notes)}
"; } $sql = "select ClientName, im.MonthlyHours, im.HourlyRate, im.Notes from {$wpdb->prefix}timesheet_clients tc inner join {$wpdb->prefix}timesheet_recurring_invoices_monthly im on tc.clientid = im.client_id where im.MonthlyHours = 0 order by ClientName"; $clients = $db->get_results($sql); if ($clients) { echo "
"; foreach ($clients as $client) { echo ""; } echo "
Client NameNotes
{$client->ClientName}{$this->remove_br($client->Notes)}
"; } $sql = "select ClientName, ProjectName, p.notes from {$wpdb->prefix}timesheet_client_projects p join {$wpdb->prefix}timesheet_clients c on p.ClientId = c.ClientId where p.notes <> '' and p.Active = 1 order by ClientName, ProjectName"; $clients = $db->get_results($sql); if ($clients) { echo "
"; foreach ($clients as $client) { echo ""; } echo "
Client NameProject NameNotes
{$client->ClientName}{$client->ProjectName}{$this->remove_br($client->notes)}
"; } } function do_timesheet_rejectapproval($timesheet_id) { global $wpdb; $db = new time_sheets_db(); $sql = "update {$wpdb->prefix}timesheet set marked_complete_by=NULL, marked_complete_date = NULL, week_complete=0 where timesheet_id=%d"; $params=array($timesheet_id); $db->query($sql, $params); $subject = "Your timesheet(s) need to be reviewed and updated"; $body = "Timesheet {$timesheet_id} needs to be reviewed and updated"; $sql = "select user_email from {$wpdb->users} u join {$wpdb->prefix}timesheet t ON u.ID = t.user_id where t.timesheet_id = %d"; $email = $db->get_var($sql, $params); $this->send_email ($email, $subject, $body); } function do_timesheet_rejectinvoicing() { global $wpdb; $db = new time_sheets_db(); $sql = "update {$wpdb->prefix}timesheet set approved_by=NULL, approved_date = NULL, approved=0 where timesheet_id=%d"; $params=array($_GET['timesheet_id']); $db->query($sql, $params); echo '

Timesheet has been rejected.

'; } function do_timesheet_rejectpayroll() { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $sql = "update {$wpdb->prefix}timesheet set invoiced = 0, invoiced_by = NULL, invoiced_date = NULL where timesheet_id = %d"; $params = array($_GET['timesheet_id']); $db->query($sql, $params); echo '

Timesheet has been rejected.

'; } function return_payroll_list() { global $wpdb; $db = new time_sheets_db(); $sql = "select t.timesheet_id, u.user_login, t.start_date, c.ClientName client_name, cp.ProjectName project_name, t.invoiceid, cp.notes, t.per_diem_days, t.hotel_charges, t.rental_car_charges, t.tolls, t.other_expenses, t.mileage, t.flight_cost, u.display_name 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 join {$wpdb->users} u on t.user_id = u.ID where t.invoiced = 1 and payrolled = 0 order by t.start_date"; $timesheets = $db->get_results($sql); return $timesheets; } function return_approval_list() { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $sql = "select t.timesheet_id, u.user_login, t.start_date, c.ClientName client_name, cp.ProjectName project_name, EmbargoPendingProjectClose, cp.notes, u.display_name, date_add(t.start_date, INTERVAL CASE WHEN monday_hours != 0 then 0 when tuesday_hours != 0 then 1 when wednesday_hours != 0 then 2 when thursday_hours != 0 then 3 when friday_hours != 0 then 4 when saturday_hours != 0 then 5 when sunday_hours != 0 then 6 else 0 end DAY) first_billed_day 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 join {$wpdb->users} u on t.user_id = u.ID join {$wpdb->prefix}timesheet_approvers_approvies aa ON aa.approvie_user_id = u.ID join {$wpdb->prefix}timesheet_approvers ta on aa.approver_user_id = ta.user_id and (ta.user_id = {$user_id} OR (ta.backup_user_id = {$user_id} and ta.backup_expires_on > now())) where t.week_complete = 1 and approved = 0 "; if (!$_GET['show_embargoed']) { $sql = "$sql and (EmbargoPendingProjectClose = 0 or EmbargoPendingProjectClose IS NULL)"; } $IsRetainer = "-1"; if ($_GET['show_retainers']) { $IsRetainer = $IsRetainer . ", 1"; } if (!$_GET['hide_nonretainers']) { $IsRetainer = $IsRetainer . ", 0"; } $sql = $sql . " and cp.ProjectId in (select ProjectId from {$wpdb->prefix}timesheet_client_projects where IsRetainer in ({$IsRetainer})) order by t.start_date"; $timesheets = $db->get_results($sql); return $timesheets; } function return_invoicing_list() { global $wpdb; $db = new time_sheets_db(); $IsRetainer = "-1"; if ($_GET['show_retainers']) { $IsRetainer = $IsRetainer . ", 1"; } if (!$_GET['hide_nonretainers']) { $IsRetainer = $IsRetainer . ", 0"; } $sql = "select t.timesheet_id, u.user_login, t.start_date, c.ClientName client_name, cp.ProjectName project_name, t.invoiceid, cp.notes, u.display_name, date_add(t.start_date, INTERVAL CASE WHEN monday_hours != 0 then 0 when tuesday_hours != 0 then 1 when wednesday_hours != 0 then 2 when thursday_hours != 0 then 3 when friday_hours != 0 then 4 when saturday_hours != 0 then 5 when sunday_hours != 0 then 6 else 0 end DAY) first_billed_day 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 join {$wpdb->users} u on t.user_id = u.ID where t.invoiced = 0 and approved = 1 and cp.ProjectId in (select ProjectId from {$wpdb->prefix}timesheet_client_projects where IsRetainer in ({$IsRetainer})) order by c.ClientName, t.start_date"; $timesheets = $db->get_results($sql); return $timesheets; } function do_timesheet_invoicing(){ global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $options = get_option('time_sheets'); $timesheets = $this->return_invoicing_list(); foreach ($timesheets as $timesheet) { $valuename = "timesheet_{$timesheet->timesheet_id}_invoice"; $invoiceid = $_GET[$valuename]; $valuename = "timesheet_{$timesheet->timesheet_id}"; if ($_GET[$valuename] == 1) { $invoiced = 1; } else { $invoiced = 0; } $payrolled = 1; //Default payrolled to 1, reset to 0 if needed if ($timesheet->always_payroll != "") { $payrolled = 0; } if ($options['mileage'] && $timesheet->mileage!=0) { $payrolled = 0; } if ($options['per_diem'] && $timesheet->per_diem_days != 0) { $payrolled = 0; } if ($options['flight_cost'] && $timesheet->flight_cost != 0) { $payrolled = 0; } if ($options['hotel'] && $timesheet->hotel_costs != 0) { $payrolled = 0; } if ($options['rental_car'] && $timesheet->rental_car_costs!=0) { $payrolled = 0; } if ($options['tolls'] && $timesheet->tolls!=0) { $payrolled = 0; } if ($options['other_expenses'] && $timesheet->other_expenses!=0) { $payrolled = 0; } $sql = "update {$wpdb->prefix}timesheet set invoiced= {$invoiced}, invoiced_by = {$user_id}, invoiced_date = CURDATE(), invoiceid = %d, payrolled = {$payrolled} where timesheet_id = {$timesheet->timesheet_id}"; $params = array($invoiceid); $db->query($sql, $params); } echo '

Timesheets have been updated.

'; } function do_timesheet_payroll(){ global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $timesheets = $this->return_approval_list(); foreach ($timesheets as $timesheet) { $valuename = "timesheet_{$timesheet->timesheet_id}"; if ($_GET[$valuename] == 1) { $sql = "update {$wpdb->prefix}timesheet set payrolled= 1, payrolled_by = {$user_id}, payrolled_on = CURDATE() where timesheet_id = {$timesheet->timesheet_id}"; $db->query($sql); } } echo '

Timesheet(s) have been updated.

'; } function do_timesheet_approval($timesheet_id){ global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $sql = "update {$wpdb->prefix}timesheet set approved = 1, approved_by = $user_id, approved_date = CURDATE() where timesheet_id = %d"; $params = array($timesheet_id); $db->query($sql, $params); $this->email_on_approval($timesheet_id); $this->check_overages($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 show_approval_list() { global $wpdb; $db = new time_sheets_db(); $user_id = get_current_user_id(); $timesheets = $this->return_approval_list(); $folder = plugins_url(); if ($_GET['show_embargoed']) { $embargod_parm = "&show_embargoed=1"; } if ($_GET['show_retainers']) { $retainers_parm = "&show_retainers=checked"; } if ($_GET['hide_nonretainers']) { $retainers_parm = $retainers_parm . "&hide_nonretainers=checked"; } if ($timesheets) { echo "
"; if ($_GET['show_embargoed']) { echo ""; } echo ""; foreach ($timesheets as $timesheet) { echo ""; if ($_GET['show_embargoed']) { echo ""; } echo ""; } echo "
TimesheetViewApproveRejectSplitEmbargoedUserStart DateFirst Billed DateClientProject Name
{$timesheet->timesheet_id}"; if ($timesheet->EmbargoPendingProjectClose==1) { echo ""; } else { echo ""; } echo "{$this->replace(' ', ' ', $timesheet->display_name)}{$this->f_date($timesheet->start_date)}{$this->f_date($timesheet->first_billed_day)}{$timesheet->client_name}{$timesheet->project_name}

"; if ($_GET['show_retainers']) { echo ""; } if ($_GET['hide_nonretainers']) { echo ""; } echo "
"; } else { echo "No time sheets to approve."; } } function f_date($date) { return date("Y-m-d", strtotime($date)); } 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['monday_hours'] =='' || $_POST['tuesday_hours'] =='' || $_POST['wednesday_hours'] =='' || $_POST['thursday_hours'] =='' || $_POST['friday_hours'] =='' || $_POST['saturday_hours'] =='' || $_POST['sunday_hours'] =='') { // echo '

Hours for each day are required.

'; // $passed=false; //} return $passed; } function add_days_to_date($idate, $days, $holidays) { $date = new DateTime($idate); date_add($date, date_interval_create_from_date_string("{$days} days")); $leading_color = ""; $trailing_color = ""; if ($holidays) { foreach ($holidays as $dt) { foreach ($dt as $daykey) { if (date('m-d',strtotime((string)$daykey))==date_format($date, 'm-d')) { $leading_color=""; $trailing_color=""; } } } } $date = date_format($date, 'm-d'); $return = "{$leading_color} {$date} {$trailing_color}"; return $return; } function show_timesheet() { 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'); if ($_POST['timesheet_id']) { $timesheet_id = $_POST['timesheet_id']; } else { $timesheet_id = $_GET['timesheet_id']; } $this->check_overages($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; } 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; #} } $folder = plugins_url( '' , __FILE__ ); echo "

Weekly Time Sheet

"; $disable_object=""; if ($timesheet->week_complete=='1') { $disable_object= " disabled"; } echo "
"; 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; } 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 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 order by ClientName"); echo ""; if ($clients) { echo ""; 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); $js_projects = json_encode($projects); echo ""; if ($timesheet->notes) { echo ""; } echo "
Week Start Date:
Client Name:"; } else { echo "New Client must be added."; } if ($main->employee_approver_check()!=0 && $disable_object=="") { echo "Add a Client"; } echo "
Project Name:"; if ($main->employee_approver_check()!=0 && $disable_object=="") { echo "Add a Project"; } echo "
Project Notes:{$this->remove_br($timesheet->notes)}
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
MondayTuesdayWednesdayThursdayFridaySaturdaySunday
{$this->add_days_to_date($start_date, 0, $holidays)}{$this->add_days_to_date($start_date, 1, $holidays)}{$this->add_days_to_date($start_date, 2, $holidays)}{$this->add_days_to_date($start_date, 3, $holidays)}{$this->add_days_to_date($start_date, 4, $holidays)}{$this->add_days_to_date($start_date, 5, $holidays)}{$this->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 ""; 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->user_id==$user_id || !$timesheet->user_id) { if ($timesheet->week_complete!='1') { echo "
timesheet_id) { echo " disabled"; } echo ">"; if ($_POST['action']=='save' && !$timesheet->timesheet_id) { $timesheet_id = $db->get_var("select max(timesheet_id) from {$wpdb->prefix}timesheet where user_id={$user_id}"); echo "
Time Sheet must be reloaded to be updated."; } } //} echo ""; echo ""; echo ""; echo "
"; echo " "; } function save_timesheet() { global $wpdb; if (!$_POST['ProjectId']) { echo '

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

'; } $db = new time_sheets_db(); $user_id = get_current_user_id(); $monday_hours = $_POST['monday_hours']; $tuesday_hours = $_POST['tuesday_hours']; $wednesday_hours = $_POST['wednesday_hours']; $thursday_hours = $_POST['thursday_hours']; $friday_hours = $_POST['friday_hours']; $saturday_hours = $_POST['saturday_hours']; $sunday_hours = $_POST['sunday_hours']; $may_embargo = $_POST['may_embargo']; $project_complete = $_POST['project_complete']; $EmbargoPendingProjectClose = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d", array($_POST['ClientId'])); $EmbargoPendingProjectClose2 = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_client_projects where ProjectId = %d", array($_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 $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, $_POST['ClientId'], $_POST['ProjectId'], $monday_hours, $tuesday_hours, $wednesday_hours, $thursday_hours, $friday_hours, $saturday_hours, $sunday_hours, $total_hours, $_POST['monday_desc'], $_POST['tuesday_desc'], $_POST['wednesday_desc'], $_POST['thursday_desc'], $_POST['friday_desc'], $_POST['saturday_desc'], $_POST['sunday_desc'], $_POST['per_diem_days'], $_POST['hotel_charges'], $_POST['rental_car_charges'], $_POST['tolls'], $_POST['other_expenses'], $_POST['other_expenses_notes'], $week_complete, $_POST['mileage'], $EmbargoPendingProjectClose, $project_complete, $_POST['flight_cost'], $_POST['isPerDiem'], $_POST['perdiem_city'], $_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($_POST['timesheet_id']); $db->query($sql, $params); if ($EmbargoPendingProjectClose==0) { $this->email_on_submission($_POST['timesheet_id']); } $sql = "update {$wpdb->prefix}timesheet_client_projects set HoursUsed=HoursUsed+$total_hours where ProjectId=%d"; $params=array($_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, $_POST['ClientId'], $_POST['ProjectId'], $monday_hours, $tuesday_hours, $wednesday_hours, $thursday_hours, $friday_hours, $saturday_hours, $sunday_hours, $total_hours, $_POST['monday_desc'], $_POST['tuesday_desc'], $_POST['wednesday_desc'], $_POST['thursday_desc'], $_POST['friday_desc'], $_POST['saturday_desc'], $_POST['sunday_desc'], $_POST['per_diem_days'], $_POST['hotel_charges'], $_POST['rental_car_charges'], $_POST['tolls'], $_POST['other_expenses'], $_POST['other_expenses_notes'], $week_complete, $_POST['mileage'], $EmbargoPendingProjectClose, $project_complete, $_POST['flight_cost'], $_POST['isPerDiem'], $_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($_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($_POST['ProjectId']); $db->query($sql, $params); echo "
Your timesheets for this client have been sent for processing.

"; $this->email_on_submission($timesheet_id); } } $sql = "select * from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d and MonthlyHours <> 0"; $parm = array($_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($_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.

"; } } } function search_timesheet() { $start_date = $_GET['start_date']; $end_date = $_GET['end_date']; if ($start_date=='') { $start_date = date('Y-m-d', strtotime("-1 Year")); } if ($end_date=='') { $end_date = date('Y-m-d', strtotime("+1 Day")); } 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(); $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, $start_date, $end_date); $timesheets = $db->get_results($sql, $params); if ($timesheets) { $folder = plugins_url(); 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 ""; echo date("Y-m-d", strtotime($timesheet->start_date)); echo "{$timesheet->client_name}{$timesheet->ProjectName}"; } echo "
"; } else { echo "No timesheets found matching those dates."; } } function is_match($v1, $v2, $return, $debug=0) { if ($debug==1) { var_dump($v1); var_dump($v2); } if (trim($v1)==trim($v2)) { return $return; } else { return ""; } } function replace($search, $replace, $value) { return str_replace($search, $replace, $value); } }