do_timesheet_payroll();
}
if ($_GET['page'] == "invoice_timesheet") {
$queue_invoice->do_timesheet_invoicing();
}
if ($_GET['page'] == "approve_timesheet") {
$queue_approval->do_timesheet_approval_processing();
}
} elseif ($_GET['action'] == "split2") {
if ($_GET['page'] == "approve_timesheet") {
$queue_approval->do_timesheet_split();
}
} else {
if ($_GET['page'] == "payroll_timesheet") {
$queue_payroll->do_timesheet_rejectpayroll();
}
if ($_GET['page'] == "invoice_timesheet") {
$queue_invoice->do_timesheet_rejectinvoicing();
}
}
}
}
function email_on_submission($timesheet_id) {
$options = get_option('time_sheets');
if (!$options['enable_email']) {
return;
}
global $wpdb;
$db = new time_sheets_db();
$common = new time_sheets_common();
$user_id = get_current_user_id();
$sql = "select user_login
from {$wpdb->prefix}timesheet t
join {$wpdb->users} u on u.ID = t.user_id
where t.timesheet_id = %d";
$params = array($timesheet_id);
$user_login = $db->get_var($sql, $params);
$sql = "select user_email, display_name
from {$wpdb->users} u
join {$wpdb->prefix}timesheet_approvers_approvies aa on u.ID = aa.approver_user_id
and aa.approvie_user_id = {$user_id}";
$users = $db->get_results($sql);
$sql = "select display_name
from {$wpdb->users} u
join {$wpdb->prefix}timesheet t on u.ID = t.user_id
where t.timesheet_id = {$timesheet_id}";
$display_user = $db->get_var($sql);
$subject = "There are time sheet(s) pending approval.";
$body = "A time sheet has been entered by {$display_user} and is pending approval.
It can be approved from the approval menu.";
foreach ($users as $user) {
$common->send_email ($user->user_email, $subject, $body);
}
}
function enter_timesheet() {
if ($_POST['action'] == 'save') {
if ($this->validate_timesheet()=="true") {
$timesheet_id = $this->save_timesheet();
}
}
$this->show_timesheet($timesheet_id);
}
function check_overages($timesheet_id) {
global $wpdb;
$db = new time_sheets_db();
$sql = "select MaxHours, HoursUsed, ProjectName
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 "
The project {$project->ProjectName} has run over on hours. Please inform management that a new SOW may be required.
";
}
}
function validate_timesheet() {
$passed = true;
if (!$_POST['start_date']) {
echo 'The week start date is required.
';
$passed="false";
}
if (!$_POST['ClientId']) {
echo 'The client name is required.
';
$passed="false";
}
if (!$_POST['ProjectId']) {
echo '';
$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 show_timesheet($timesheet_id=0) {
global $wpdb;
$db = new time_sheets_db();
$user_id = get_current_user_id();
$main = new time_sheets_main();
$settings = new time_sheets_settings();
$options = get_option('time_sheets');
$common = new time_sheets_common();
$common->show_datestuff();
$queue_approval = new time_sheets_queue_approval();
if ($user_id == 0) {
echo "You must be logged in to view this page.
";
return;
}
if (!$timesheet_id) {
if ($_POST['timesheet_id']) {
$timesheet_id = $_POST['timesheet_id'];
} else {
$timesheet_id = $_GET['timesheet_id'];
}
}
$this->check_overages(intval($timesheet_id));
$sql = "select * from {$wpdb->prefix}timesheet t join {$wpdb->users} u on t.user_id = u.ID left outer join {$wpdb->prefix}timesheet_client_projects tcp on t.ProjectId = tcp.ProjectId where timesheet_id=%d";
$params = array($timesheet_id);
if ($timesheet_id) {
$timesheet = $db->get_row($sql, $params);
$timesheet_user = $timesheet->display_name;
$timesheet_user_id = $timesheet->user_id;
$timesheet_id_int = $timesheet_id;
if (!$queue_approval->employee_approver_check('true') && $timesheet_user_id <> $user_id) {
echo "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;
}
echo "
Weekly Time Sheet
";
$disable_object="";
if ($timesheet->week_complete=='1') {
$disable_object= " disabled";
}
echo "";
echo "
";
}
function save_timesheet() {
global $wpdb;
$user_id = get_current_user_id();
if ($user_id == 0) {
echo "You must be logged in to view this page.
";
return;
}
if (!$_POST['ProjectId']) {
echo 'No Project Was Selected. A project must be selected. If no projects are available contact your lead.
';
return;
}
check_admin_referer( 'timesheet_entry_'. $_POST['timesheet_id'] );
$db = new time_sheets_db();
$user_id = get_current_user_id();
$monday_hours = floatval($_POST['monday_hours']);
$tuesday_hours = floatval($_POST['tuesday_hours']);
$wednesday_hours = floatval($_POST['wednesday_hours']);
$thursday_hours = floatval($_POST['thursday_hours']);
$friday_hours = floatval($_POST['friday_hours']);
$saturday_hours = floatval($_POST['saturday_hours']);
$sunday_hours = floatval($_POST['sunday_hours']);
if ($_POST['timesheet_id']) {
$sql = "select user_id from {$wpdb->prefix}timesheet 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;
}
} else {
echo 'You appear to be updating a timesheet which does not exist.
';
return;
}
}
$may_embargo = ($_POST['may_embargo'] == 1 )? 1 : 0 ;
$project_complete = ($_POST['project_complete'] == 1 )? 1 : 0 ;
$EmbargoPendingProjectClose = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d", array(intval($_POST['ClientId'])));
$EmbargoPendingProjectClose2 = $db->get_var("select BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_client_projects where ProjectId = %d", array(intval($_POST['ProjectId'])));
if ($EmbargoPendingProjectClose2==1) {
$EmbargoPendingProjectClose=1;
}
if ($EmbargoPendingProjectClose<>1) {
$EmbargoPendingProjectClose=0;
}
if ($project_complete<>1) {
$project_complete=0;
}
if (!$monday_hours) {
$monday_hours=0;
}
if (!$tuesday_hours) {
$tuesday_hours=0;
}
if (!$wednesday_hours) {
$wednesday_hours=0;
}
if (!$thursday_hours) {
$thursday_hours=0;
}
if (!$friday_hours) {
$friday_hours=0;
}
if (!$saturday_hours) {
$saturday_hours=0;
}
if (!$sunday_hours) {
$sunday_hours=0;
}
$total_hours = $monday_hours + $tuesday_hours + $wednesday_hours + $thursday_hours + $friday_hours + $saturday_hours + $sunday_hours;
if ($_POST['week_complete']==1) {
$week_complete=1;
} else {
$week_complete=0;
}
$start_date = date("Y-m-d", strtotime($_POST['start_date']));
if ($_POST['timesheet_id']) { //UPDATE
$timesheet_id = $_POST['timesheet_id'];
$sql = "update {$wpdb->prefix}timesheet
SET start_date=%s,
ClientId=%d,
ProjectId=%d,
monday_hours=%s,
tuesday_hours=%s,
wednesday_hours=%s,
thursday_hours=%s,
friday_hours=%s,
saturday_hours=%s,
sunday_hours=%s,
total_hours=%s,
monday_desc=%s,
tuesday_desc=%s,
wednesday_desc=%s,
thursday_desc=%s,
friday_desc=%s,
saturday_desc=%s,
sunday_desc=%s,
per_diem_days=%s,
hotel_charges=%s,
rental_car_charges=%s,
tolls=%s,
other_expenses=%s,
other_expenses_notes=%s,
week_complete=%d,
mileage=%d,
EmbargoPendingProjectClose=%d,
project_complete=%d,
flight_cost=%s,
isPerDiem=%d,
perdiem_city=%s
WHERE timesheet_id=%d";
$params=array($start_date, intval($_POST['ClientId']), intval($_POST['ProjectId']), floatval($monday_hours), floatval($tuesday_hours), floatval($wednesday_hours), floatval($thursday_hours), floatval($friday_hours), floatval($saturday_hours), floatval($sunday_hours), $total_hours, sanitize_textarea_field($_POST['monday_desc']), sanitize_textarea_field($_POST['tuesday_desc']), sanitize_textarea_field($_POST['wednesday_desc']), sanitize_textarea_field($_POST['thursday_desc']), sanitize_textarea_field($_POST['friday_desc']), sanitize_textarea_field($_POST['saturday_desc']), sanitize_textarea_field($_POST['sunday_desc']), floatval($_POST['per_diem_days']), floatval($_POST['hotel_charges']), floatval($_POST['rental_car_charges']), floatval($_POST['tolls']), floatval($_POST['other_expenses']), sanitize_textarea_field($_POST['other_expenses_notes']), $week_complete, floatval($_POST['mileage']), $EmbargoPendingProjectClose, $project_complete, floatval($_POST['flight_cost']), intval($_POST['isPerDiem']), sanitize_text_field($_POST['perdiem_city']), intval($_POST['timesheet_id']));
$db->query($sql, $params);
if ($week_complete==1) {
$sql = "update {$wpdb->prefix}timesheet
set marked_complete_by=$user_id,
marked_complete_date = CURDATE()
where timesheet_id=%d";
$params=array(intval($_POST['timesheet_id']));
$db->query($sql, $params);
if ($EmbargoPendingProjectClose==0) {
$this->email_on_submission(intval($_POST['timesheet_id']));
}
$sql = "update {$wpdb->prefix}timesheet_client_projects
set HoursUsed=HoursUsed+$total_hours
where ProjectId=%d";
$params=array(intval($_POST['ProjectId']));
$db->query($sql, $params);
}
echo '';
} else { //INSERT
$sql = "insert into {$wpdb->prefix}timesheet (user_id, start_date, entered_date, ClientId, ProjectId, monday_hours, tuesday_hours, wednesday_hours, thursday_hours, friday_hours, saturday_hours, sunday_hours, total_hours, monday_desc, tuesday_desc, wednesday_desc, thursday_desc, friday_desc, saturday_desc, sunday_desc, per_diem_days, hotel_charges, rental_car_charges, tolls, other_expenses, other_expenses_notes, week_complete, approved, invoiced, mileage, EmbargoPendingProjectClose, project_complete, flight_cost, isPerDiem, perdiem_city) values (%d, %s, now(), %d, %d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, 0, 0, %d, %d, %d, %s, %d, %s)";
$params=array($user_id, $start_date, intval($_POST['ClientId']), intval($_POST['ProjectId']), floatval($monday_hours), floatval($tuesday_hours), floatval($wednesday_hours), floatval($thursday_hours), floatval($friday_hours), floatval($saturday_hours), floatval($sunday_hours), $total_hours, sanitize_textarea_field($_POST['monday_desc']), sanitize_textarea_field($_POST['tuesday_desc']), sanitize_textarea_field($_POST['wednesday_desc']), sanitize_textarea_field($_POST['thursday_desc']), sanitize_textarea_field($_POST['friday_desc']), sanitize_textarea_field($_POST['saturday_desc']), sanitize_textarea_field($_POST['sunday_desc']), floatval($_POST['per_diem_days']), floatval($_POST['hotel_charges']), floatval($_POST['rental_car_charges']), floatval($_POST['tolls']), floatval($_POST['other_expenses']), sanitize_textarea_field($_POST['other_expenses_notes']), $week_complete, $_POST['mileage'], $EmbargoPendingProjectClose, $project_complete, floatval($_POST['flight_cost']), floatval($_POST['isPerDiem']), sanitize_text_field($_POST['perdiem_city']));
$db->query($sql, $params);
$sql = "select max(timesheet_id)
from {$wpdb->prefix}timesheet
where user_id={$user_id}";
$timesheet_id = $db->get_var($sql);
$this->check_overages($timesheet_id);
if ($week_complete==1) {
$sql = "update {$wpdb->prefix}timesheet
set marked_complete_by=$user_id,
marked_complete_date = CURDATE()
where timesheet_id=$timesheet_id";
$db->query($sql);
if ($EmbargoPendingProjectClose==0) {
$this->email_on_submission($timesheet_id);
}
$sql = "update {$wpdb->prefix}timesheet_client_projects
set HoursUsed=HoursUsed+$total_hours
where ProjectId=%d";
$params=array(intval($_POST['ProjectId']));
$db->query($sql, $params);
}
echo '';
}
if ($may_embargo==1) {
if ($project_complete==1 && $week_complete==1) {
$sql = "update {$wpdb->prefix}timesheet
SET EmbargoPendingProjectClose = 0
WHERE ProjectId=%d AND EmbargoPendingProjectClose = 1";
$params = array(intval($_POST['ProjectId']));
$db->query($sql, $params);
echo "Your timesheets for this client have been sent for processing.
";
$this->email_on_submission(intval($timesheet_id));
}
}
$sql = "select * from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d and MonthlyHours <> 0";
$parm = array(intval($_POST['ClientId']));
$client = $db->get_row($sql, $parm);
if ($client) {
$sql = "select sum(total_hours) total_hours from {$wpdb->prefix}timesheet where ClientId = %d and YEAR(start_date) = YEAR(NOW()) AND MONTH(start_date)=MONTH(NOW())";
$parm = array(intval($_POST['ClientId']));
$month_hours = $db->get_var($sql, $parm);
if ($month_hours > $client->MonthlyHours) {
echo "Client may be over their retainer hours for this month. Please ensure that client approves of hours over their monthly retainer of {$client->MonthlyHours} hours if they do not have an active SOW in place.
";
}
}
return $timesheet_id;
}
}