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.
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 . "";
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, 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
";
}
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);
}
}