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();
}
#$timesheet_id = $this->save_timesheet();
}
}
}
}
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 = 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);
$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 ($_POST['action'] == 'save') {
if ($this->validate_timesheet()=="true") {
$timesheet_id = $this->save_timesheet();
}
} else {
$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') {
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 = $options['project_trigger_percent'];
$email_address_on_project_over = $options['email_address_on_project_over'];
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
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 ($project) {
$MaxHours = $project->MaxHours;
$HoursUsed = $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) {
echo "
The project '{$project->ProjectName}' for '{$project->ClientName}' has used {$project_trigger_percent}% of its hours. Be sure that the client approves of hours over the project max of {$project->MaxHours} hours. {$Sales}
";
if ($send==1 && isset($options['enable_email']) && $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.
Time sheets can not be rejected without a comment.
";
} else {
$this->save_audit_comment($timesheet_id, $comment, $timesheet_rejected);
}
}
$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;
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 {
#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;
}
$page = $page . "
Weekly Time Sheet
";
$disable_object="";
if (isset($timesheet->week_complete) && ($timesheet->week_complete=='1')) {
$disable_object= " disabled";
}
$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
";
}
$sql = "select tca.entered_at, tca.comment, tca.timesheet_rejected, u.display_name
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
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
$sql = "INSERT INTO {$wpdb->prefix}timesheet_client_projects_archive
SELECT $user_id, now(), 'UPDATED', ProjectId, ClientId, ProjectName, IsRetainer, MaxHours, HoursUsed, Active, notes, BillOnProjectCompletion, flat_rate, po_number, sales_person_id, technical_sales_person_id, close_on_completion
FROM {$wpdb->prefix}timesheet_client_projects
WHERE ProjectId=%d";
$params = array(intval($_POST['ProjectId']));
$db->query($sql, $params);
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 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);
}
}