employee_invoicer_check();
$approver=$queue_approval->employee_approver_check('false');
echo "
";
if ($_GET['menu']=='New Client') {
$this->add_client();
}
if ($_GET['menu']=='Edit Client') {
$this->add_client_users();
}
if ($_GET['menu']=='Setup Monthlies') {
$this->setup_monthlies();
}
if ($_GET['menu']=='New Project') {
$this->NewProject();
}
if ($_GET['menu']=='Edit Project') {
$this->EditProject();
}
if ($_GET['menu']=='view_timesheets_for_project') {
$this->view_timesheets_for_project();
}
}
function view_timesheets_for_project(){
$db = new time_sheets_db();
global $wpdb;
$user_id = get_current_user_id();
$sql = "select t.timesheet_id, t.start_date, t.total_hours, invoiced, approved, EmbargoPendingProjectClose, u.display_name
from {$wpdb->prefix}timesheet t
join {$wpdb->users} u on t.user_id = u.ID
where ProjectId = %d
order by start_date, display_name";
$parms = array(intval($_GET['ProjectId']));
$timesheets = $db->get_results($sql, $parms);
if ($timesheets) {
echo "| Timesheet ID | Week Starting | Employee | Hours Invoiced | Is Approved | Is Invoiced | Is Embargoed |
";
$totalhours = 0;
foreach ($timesheets as $timesheet) {
if ($timesheet->invoiced=="1") {
$is_invoiced=plugins_url( 'check.png' , __FILE__);
} else {
$is_invoiced=plugins_url( 'x.png' , __FILE__);
}
if ($timesheet->approved=="1") {
$is_approved=plugins_url( 'check.png' , __FILE__);
} else {
$is_approved= plugins_url( 'x.png' , __FILE__) ;
}
if ($timesheet->EmbargoPendingProjectClose=="1") {
$is_Embargoed= plugins_url( 'check.png' , __FILE__) ;
} else {
$is_Embargoed= plugins_url( 'x.png' , __FILE__);
}
$totalhours = $totalhours + $timesheet->total_hours;
$start_date = date('Y-m-d', strtotime($timesheet->start_date));
echo "| {$timesheet->timesheet_id} |
{$start_date} |
{$timesheet->display_name} |
{$timesheet->total_hours} |
 |
 |
 |
";
}
echo "
Total Hours To Date: {$totalhours}";
} else {
echo "There are no timesheets for this project at this time.";
}
}
function setup_monthlies() {
$db = new time_sheets_db();
global $wpdb;
$user_id = get_current_user_id();
$common = new time_sheets_common();
if ($_POST['BillOnProjectCompletion']) {
$BillOnProjectCompletion = 1;
} else {
$BillOnProjectCompletion = 0;
}
if ($_POST['sub_action']=='update_client') {
$sql = "select * from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d";
$parms = array($_POST['ClientId']);
$client = $db->get_row($sql, $parms);
if ($_POST['enabled']) {
if ($client->client_id) {
$sql = "update {$wpdb->prefix}timesheet_recurring_invoices_monthly
SET MonthlyHours = %d, HourlyRate = %d, Notes = '%s', BillOnProjectCompletion = %d
WHERE client_id = %d";
$parm = array(intval($_POST['MonthlyHours']), $_POST['HourlyRate'], sanitize_text_field($_POST['notes']), $BillOnProjectCompletion, intval($_POST['ClientId']));
$db->query($sql, $parm);
echo '';
} else {
$db->query("insert into {$wpdb->prefix}timesheet_recurring_invoices_monthly
(client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion )
values (%d, %d, %d, '%s', %d)", array(intval($_POST['ClientId']), intval($_POST['MonthlyHours']), $_POST['HourlyRate'], sanitize_text_field($_POST['notes']), $BillOnProjectCompletion));
echo '';
}
}
if (!$_POST['enabled']) {
$db->query("delete from {$wpdb->prefix}timesheet_recurring_invoices_monthly
where client_id = %d", array(intval($_POST['ClientId'])));
echo '';
}
}
echo "";
if ($_POST['action']=='select_client') {
$sql = "select * from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d";
$parms = array(intval($_POST['ClientId']));
$client = $db->get_row($sql, $parms);
echo "";
}
}
function add_client_users() {
$db = new time_sheets_db();
global $wpdb;
$user_id = get_current_user_id();
if ($_POST['ClientId'] && $_POST['action']=='update_client1') {
$sql = "select ID
from {$wpdb->users} u";
$users = $db->get_results($sql);
foreach ($users as $user) {
$id = "user_{$user->ID}";
if ($_POST[$id]) {
$sql = "insert into {$wpdb->prefix}timesheet_clients_users
(ClientId, user_id)
values (%d, %d)";
$db->query($sql, array(intval($_POST['ClientId']), $user->ID));
} else {
$sql = "delete from {$wpdb->prefix}timesheet_clients_users
where ClientId = %d and user_id = %d";
$db->query($sql, array(intval($_POST['ClientId']), $user->ID));
}
}
$sql = "update {$wpdb->prefix}timesheet_clients set ClientName = %s where ClientId = %d";
$parms = array(sanitize_text_field($_POST['ClientName']), intval($_POST['ClientId']));
$db->query($sql, $parms);
echo '';
}
echo "";
if ($_POST['ClientId']) {
$sql = "select u.user_login, u.ID, tcu.user_id cu, u.display_name
from {$wpdb->users} u
left outer join {$wpdb->prefix}timesheet_clients_users tcu on u.id = tcu.user_id
and tcu.ClientId = %d
order by u.display_name";
$users = $db->get_results($sql, array(intval($_POST['ClientId'])));
$sql = "select * from {$wpdb->prefix}timesheet_clients where ClientId = %d";
$client = $db->get_row($sql, array(intval($_POST['ClientId'])));
echo "";
}
}
function add_client() {
global $wpdb;
$db = new time_sheets_db();
$user_id = get_current_user_id();
if ($_POST['action']=='save_client' && $_POST['ClientName'] != '') {
$ClientId=$db->get_var("select ClientId from {$wpdb->prefix}timesheet_clients where ClientName=%s", array(sanitize_text_field($_POST['ClientName'])));
if (!$ClientId) {
$db->query("insert into {$wpdb->prefix}timesheet_clients
(ClientName, Active)
values
(%s, 1)", array(sanitize_text_field($_POST['ClientName'])));
$ClientId=$db->get_var("select ClientId from {$wpdb->prefix}timesheet_clients where ClientName=%s", array(sanitize_text_field($_POST['ClientName'])));
$db->query("insert into {$wpdb->prefix}timesheet_clients_users
(ClientId, user_id)
values
($ClientId, $user_id)");
echo '';
} else {
$db->query("insert into {$wpdb->prefix}timesheet_clients_users
(ClientId, user_id)
values
($ClientId, $user_id)");
echo 'Client already exists.
Access granted to client.
';
}
}
echo "