employee_invoicer_check(); $approver=$queue_approval->employee_approver_check('false'); $client_managers = new time_sheets_client_managers(); echo "

"; IF (isset($_GET['ClientId'])) { echo ""; } elseif (isset($_POST['ClientId'])) { echo ""; } if (($client_managers->client_manager_check()==1) || ($approver==1)) { echo "   "; } if (($client_managers->client_manager_check()==1) || ($invoicer==1)) { echo " "; } if (($client_managers->client_manager_check()==1) || ($approver==1)) { echo "  "; } echo "
"; if (isset($_GET['menu'])) { if ($_GET['menu']=='New Client') { $this->add_client(); } if ($_GET['menu']=='Edit Client') { $this->add_client_users(); } if ($_GET['menu']=='Recurring Billing') { $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 = wp_get_current_user(); $user_id = $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 "

"; $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 ""; } echo "
Timesheet IDWeek StartingEmployeeHours InvoicedIs ApprovedIs InvoicedIs Embargoed
{$timesheet->timesheet_id} {$start_date} {$timesheet->display_name} {$timesheet->total_hours}

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 = wp_get_current_user(); $user_id = $user->ID; $common = new time_sheets_common(); if (isset($_POST['BillOnProjectCompletion'])) { $BillOnProjectCompletion = 1; } else { $BillOnProjectCompletion = 0; } if (isset($_POST['sub_action']) && $_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 (isset($_POST['enabled'])) { if ($client->client_id) { $sql = "update {$wpdb->prefix}timesheet_recurring_invoices_monthly SET MonthlyHours = %d, HourlyRate = %d, Notes = '%s', BillOnProjectCompletion = %d, reqular_interval=%d WHERE client_id = %d"; $parm = array(intval($_POST['MonthlyHours']), $_POST['HourlyRate'], sanitize_textarea_field($_POST['notes']), $BillOnProjectCompletion, intval($_POST['recurring_interval']), intval($_POST['ClientId'])); $db->query($sql, $parm); //archive $sql = "INSERT INTO {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive SELECT $user->ID, now(), 'UPDATED', client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly WHERE client_id = %d"; $parm = array(intval($_POST['ClientId'])); $db->query($sql, $parm); echo '

Client Updated.

'; } else { $db->query("insert into {$wpdb->prefix}timesheet_recurring_invoices_monthly (client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, reqular_interval ) values (%d, %d, %d, '%s', %d, %d)", array(intval($_POST['ClientId']), intval($_POST['MonthlyHours']), $_POST['HourlyRate'], sanitize_textarea_field($_POST['notes']), $BillOnProjectCompletion, intval($_POST['recurring_interval']))); //archive $sql = "INSERT INTO {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive SELECT $user->ID, now(), 'INSERTED', client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly WHERE client_id = %d"; $parm = array(intval($_POST['ClientId'])); $db->query($sql, $parm); echo '

Client Added.

'; } } if (!isset($_POST['enabled'])) { //archive $sql = "INSERT INTO {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive SELECT $user->ID, now(), 'DELETED', client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion FROM {$wpdb->prefix}timesheet_recurring_invoices_monthly WHERE client_id = %d"; $parm = array(intval($_POST['ClientId'])); $db->query($sql, $parm); $db->query("delete from {$wpdb->prefix}timesheet_recurring_invoices_monthly where client_id = %d", array(intval($_POST['ClientId']))); echo '

Client Removed.

'; } } echo "
"; echo "
Select Client:"; $this->GetClient(); #$clients = $db->get_results("select ClientName, tc.ClientId, im.client_id IsMonthly # from {$wpdb->prefix}timesheet_clients tc # left outer join {$wpdb->prefix}timesheet_recurring_invoices_monthly im on tc.clientid = im.client_id # order by ClientName"); #if ($clients) { # echo ""; #} echo "
"; echo ""; echo "
"; if (isset($_POST['action']) && $_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 "

"; echo ""; echo ""; $MonthlyHours = isset($client->MonthlyHours) ? $client->MonthlyHours : 0; $HourlyRate = isset($client->HourlyRate) ? $client->HourlyRate : 0; $Notes = isset($client->Notes) ? $client->Notes : ""; $recurring_interval = isset($client->reqular_interval) ? $client->reqular_interval : 1; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
Enabled:client_id)) { echo " checked"; } echo ">
Interval
Hours Included:
Hourly Rate:
Notes:
BillOnProjectCompletion) && $client->BillOnProjectCompletion == 1) { echo " checked"; } echo "> Bill On Project Completion
"; echo ""; echo ""; echo ""; echo ""; echo "
"; } } function add_client_users() { $db = new time_sheets_db(); $common = new time_sheets_common(); global $wpdb; $user = wp_get_current_user(); $user_id = $user->ID; if (isset($_POST['ClientId']) && isset($_POST['action']) && $_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 = "select * from {$wpdb->prefix}timesheet_clients_users where ClientId = %d and user_id = %d"; $parms = array(intval($_POST['ClientId']), $user->ID); unset($u); $u= $db->query($sql, $parms); //var_dump($u); if (!$u) { $sql = "insert into {$wpdb->prefix}timesheet_clients_users (ClientId, user_id) values (%d, %d)"; $db->query($sql, $parms); $sql = "insert into {$wpdb->prefix}timesheet_clients_users_archive select $user->ID, now(), 'INSERTED', ClientId, user_id FROM {$wpdb->prefix}timesheet_clients_users WHERE ClientId = %d and user_id = %d"; $db->query($sql, $parms); } } else { $sql = "insert into {$wpdb->prefix}timesheet_clients_users_archive select $user->ID, now(), 'DELETED', ClientId, user_id FROM {$wpdb->prefix}timesheet_clients_users WHERE ClientId = %d and user_id = %d"; $parms = array(intval($_POST['ClientId']), $user->ID); $db->query($sql, $parms); $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, sales_person_id = %d, technical_sales_person_id = %d where ClientId = %d"; $parms = array(sanitize_text_field($_POST['ClientName']), intval($_POST['sales_person_id']), intval($_POST['technical_sales_person_id']), intval($_POST['ClientId'])); $db->query($sql, $parms); $sql = "insert into {$wpdb->prefix}timesheet_clients_archive select $user->ID, now(), 'UPDATED', ClientId, ClientName, Active, FinalProjectEnd, sales_person_id, technical_sales_person_id FROM {$wpdb->prefix}timesheet_clients WHERE ClientId = %d"; $parms = array(intval($_POST['ClientId'])); $db->query($sql, $parms); var_dump ($sql); echo '

Client Updated.

'; } echo "
"; echo "
Client Name"; $this->GetClient(); #$clients = $db->get_results("select ClientName, tc.ClientId, tc.sales_person_id # from {$wpdb->prefix}timesheet_clients tc # order by ClientName"); #if ($clients) { # echo ""; #} echo "
"; echo ""; echo "
"; if (isset($_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 "
"; echo "
"; echo "
Client Name:
Sales Person"; $users1 = $common->return_employee_list(); echo "
Technical Sales Person"; echo "

Employees working on this client
"; foreach ($users as $user) { echo ""; } echo "
 User NameUser
cu) { echo " checked"; } echo ">{$user->user_login}{$user->display_name}
"; echo "
"; echo ""; echo ""; echo "
"; } } function add_client() { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $common = new time_sheets_common(); if (isset($_POST['action']) && $_POST['action']=='save_client' && isset($_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, sales_person_id, technical_sales_person_id) values (%s, 1, %d, %d)", array(sanitize_text_field($_POST['ClientName']), intval($_POST['sales_person_id']), intval($_POST['technical_sales_person_id']))); $sql = "insert into {$wpdb->prefix}timesheet_clients_archive select $user->ID, now(), 'INSERTED', ClientId, ClientName, Active, FinalProjectEnd, sales_person_id, technical_sales_person_id FROM {$wpdb->prefix}timesheet_clients WHERE ClientName = %s"; $parms = array(sanitize_text_field($_POST['ClientName'])); $db->query($sql, $parms); $ClientId=$db->get_var("select ClientId from {$wpdb->prefix}timesheet_clients where ClientName=%s", array(sanitize_text_field($_POST['ClientName']))); $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)"; $parms = array($ClientId, $user->ID); $db->query($sql, $parms); $sql = "insert into {$wpdb->prefix}timesheet_clients_users_archive select $user->ID, now(), 'INSERTED', ClientId, user_id FROM {$wpdb->prefix}timesheet_clients_users WHERE ClientId = %d and user_id = %d"; $parms = array($ClientId, $user->ID); $db->query($sql, $parms); } } echo '

Client Added.

'; } else { $db->query("insert into {$wpdb->prefix}timesheet_clients_users (ClientId, user_id) values ($ClientId, $user_id)"); $sql = "insert into {$wpdb->prefix}timesheet_clients_users_archive select $user->ID, now(), 'INSERTED', ClientId, user_id FROM {$wpdb->prefix}timesheet_clients_users WHERE ClientId = %d and user_id = %d"; $parms = array($ClientId, $user_id); $db->query($sql, $parms); echo '

Client already exists.
Access granted to client.

'; } } echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; } function NewProject() { global $wpdb; $db = new time_sheets_db(); $options = get_option('time_sheets'); $common = new time_sheets_common(); if (isset($_GET['Active'])) { $Active = 1; } $IsRetainer = ''; $BillOnProjectCompletion = ''; $MaxHours = 0; $flat_rate = ""; $close_on_completion = ""; if (isset($_GET['action']) && $_GET['action']=='Save Project'){ $this->SaveNewProject(); if (isset($_GET['IsRetainer'])) { $IsRetainer = ' checked'; } if ($Active==1) { $Active = ' checked'; } if (isset($_GET['BillOnProjectCompletion'])) { $BillOnProjectCompletion = ' checked'; } if (isset($_GET['MaxHours'])) { $MaxHours = intval($_GET['MaxHours']); } if (isset($_GET['flat_rate'])) { $flat_rate = " checked"; } if (isset($_GET['close_on_completion']) || isset($options['default_auto_close'])) { $close_on_completion = " checked"; } } else { $Active = ' checked'; $close_on_completion = " checked"; } $ProjectName = isset($_GET['ProjectName']) ? $common->esc_textarea($_GET['ProjectName']) : ""; $PONumber = isset($_GET['po_number']) ? $common->esc_textarea($_GET['po_number']) : ""; echo "
Client Name
Sales Person"; $users = $common->return_employee_list(); echo "
Technical Sales Person"; echo "
Employees working on this client
"; $sql = "select u.user_login, u.ID, u.display_name from {$wpdb->users} u order by u.display_name"; $users = $db->get_results($sql); echo ""; foreach ($users as $user) { echo ""; } echo "
 User NameUser
{$user->user_login}{$user->display_name}
"; echo "
"; echo ""; echo ""; if (!isset($options['remove_embargo'])) { echo " "; } echo ""; echo ""; $Notes = isset($_GET['Notes']) ? $common->esc_textarea($_GET['notes']) : ""; echo "
Select Client:"; $this->GetClient(); echo "
Project Name:
PO Number:
Sales Person:"; $users = $common->return_employee_list(); echo "
Technical Sales Person:"; echo "
Is Retainer Project
Max Project Hours:
Active / Visable
Bill at end of project
Flat rate billing project Bypasses invoicing queue for this project
Auto close project when project is out of hours
Notes:
"; echo ""; echo "
"; } function SaveNewProject() { if ($this->ValidateProject(true)==false) { return; } global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); if ($_GET['IsRetainer']) { $IsRetainer=1; } else { $IsRetainer=0; } if ($_GET['MaxHours']) { $MaxHours = intval($_GET['MaxHours']); } else { $MaxHours = 0; } if ($_GET['Active']) { $Active = 1; } else { $Active = 0; } if ($_GET['flat_rate']) { $flat_rate = 1; } else { $flat_rate = 0; } if ($_GET['BillOnProjectCompletion']) { $BillOnProjectCompletion = 1; } else { $BillOnProjectCompletion = 0; } if ($_GET['close_on_completion']==1) { $close_on_completion = "1"; } else { $close_on_completion = "0"; } $sql = "INSERT INTO {$wpdb->prefix}timesheet_client_projects (ClientId, ProjectName, IsRetainer, MaxHours, HoursUsed, Active, Notes, BillOnProjectCompletion, flat_rate, po_number, sales_person_id, technical_sales_person_id, close_on_completion) values (%d, %s, %d, %d, %d, %d, %s, %d, %d, %s, %d, %d, %d)"; $parms = array(intval($_GET['ClientId']), sanitize_text_field($_GET['ProjectName']), $IsRetainer, $MaxHours, 0, $Active, sanitize_textarea_field($_GET['notes']), $BillOnProjectCompletion, $flat_rate, sanitize_text_field($_GET['po_number']), intval($_GET['sales_person_id']), intval($_GET['technical_sales_person_id']), $close_on_completion); $db->query($sql, $parms); $sql = "insert into {$wpdb->prefix}timesheet_client_projects_archive select $user->ID, now(), 'INSERTED', 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 ClientId = %d and ProjectName = %s"; $parms = array(intval($_GET['ClientId']), sanitize_text_field($_GET['ProjectName'])); $db->query($sql, $parms); echo '

Project added.

'; } function ValidateProject($existsCheck) { global $wpdb; $db = new time_sheets_db(); $valid = true; if (!$_GET['ClientId']) { echo '

The selected client is invalid.

'; $valid=false; } if (!$_GET['ProjectName']) { echo '

The name of the project is required.

'; $valid=false; } if ($_GET['IsRetainer']) { if ($_GET['ProjectId']) { $sql = "select * from {$wpdb->prefix}timesheet_client_projects where ClientId=%d and IsRetainer=1 and ProjectId<>%d"; $parms = array(intval($_GET['ClientId']), intval($_GET['ProjectId'])); } else { $sql = "select * from {$wpdb->prefix}timesheet_client_projects where ClientId=%d and IsRetainer=1"; $parms = array(intval($_GET['ClientId'])); } $project = $db->get_row($sql, $parms); if ($project) { echo "

There is already a retainer project for this client. Change the retainer status for '{$project->ProjectName}' to correct this.

"; $valid=false; } } if ($existsCheck==true) { $sql = "select count(*) from {$wpdb->prefix}timesheet_client_projects where ClientId=%d and ProjectName=%s"; $params = array(intval($_GET['ClientId']), intval($_GET['ProjectName'])); $ct = $db->get_var($sql, $params); if ($ct!=0) { echo '

This project already exists for this client.

'; $valid=false; } } return $valid; } function EditProject() { global $wpdb; $db = new time_sheets_db(); $common = new time_sheets_common(); $options = get_option('time_sheets'); if (isset($_GET['subaction']) && $_GET['subaction']=='Save Project') { $this->SaveExistingProject(); } $this->GetProjects(); echo " "; if (isset($_GET['action']) && $_GET['action']=='Select Project') { $project = $db->get_row("select * from {$wpdb->prefix}timesheet_client_projects tcp where ProjectId=%d order by ProjectName", array(intval($_GET['ProjectId']))); $IsRetainer = ''; $DisableHours = ''; $Active = ''; $BillOnProjectCompletion = ''; $flat_rate = ''; $close_on_completion = ''; if ($project->IsRetainer==1) { $IsRetainer=" checked"; $DisableHours=" disabled"; } if ($project->Active==1) { $Active=" checked"; } if ($project->BillOnProjectCompletion==1) { $BillOnProjectCompletion = " checked"; } if ($project->flat_rate==1) { $flat_rate = " checked"; } if ($project->close_on_completion==1) { $close_on_completion = " checked"; } $hoursused = $project->MaxHours-$project->HoursUsed; echo ""; echo ""; echo " "; if (!isset($options['remove_embargo'])) { echo " "; } echo "
Select Client:"; $this->GetClient(); echo "
Project Name:
Project Name:
PO Number:
Sales Person:"; $users = $common->return_employee_list(); echo "
Technical Sales Person:"; echo "
Is Retainer Project
Max Project Hours:
Hours Used:{$project->HoursUsed}
Hours Remaining:{$hoursused}
Bill on project completion
Active / Visable
Flat rate billing project Bypasses invoicing queue for this project
Auto close project when project is out of hours
Notes:
"; if ($DisableHours) { echo ""; } echo " "; } } function SaveExistingProject() { if ($this->ValidateProject(false)==false) { return; } global $wpdb; $user = wp_get_current_user(); $db = new time_sheets_db(); if ($_GET['IsRetainer']) { $IsRetainer=1; } else { $IsRetainer=0; } if ($_GET['MaxHours']) { $MaxHours = intval($_GET['MaxHours']); } else { $MaxHours = 0; } if ($_GET['Active']) { $Active = 1; } else { $Active = 0; } if ($_GET['BillOnProjectCompletion']) { $BillOnProjectCompletion = 1; } else { $BillOnProjectCompletion = 0; } if ($_GET['flat_rate']) { $flat_rate = 1; } else { $flat_rate = 0; } if ($_GET['close_on_completion']==1) { $close_on_completion = "1"; } else { $close_on_completion = "0"; } $sql = "UPDATE {$wpdb->prefix}timesheet_client_projects SET ClientId=%d, ProjectName=%s, IsRetainer=%d, MaxHours=%d, Active=%d, Notes=%s, BillOnProjectCompletion=%d, flat_rate = %d, po_number = %s, sales_person_id = %d, technical_sales_person_id = %d, close_on_completion = %d WHERE ProjectId=%d"; $parms = array(intval($_GET['ClientId']), sanitize_text_field($_GET['ProjectName']), $IsRetainer, $MaxHours, $Active, sanitize_textarea_field($_GET['notes']), $BillOnProjectCompletion, $flat_rate, sanitize_text_field($_GET['po_number']), intval($_GET['sales_person_id']), intval($_GET['technical_sales_person_id']), $close_on_completion, intval($_GET['ProjectId']) ); $db->query($sql, $parms); $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"; $parms = array(intval($_GET['ProjectId'])); $db->query($sql, $parms); echo '

Project updated.

'; } function GetClient() { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $common = new time_sheets_common(); $clients = $db->get_results(" select ClientName, tc1.ClientId from {$wpdb->prefix}timesheet_clients tc1 order by ClientName"); echo ""; } function GetProjects() { global $wpdb; $db = new time_sheets_db(); $user = wp_get_current_user(); $user_id = $user->ID; $common = new time_sheets_common(); $projects = $db->get_results("select tcp.ClientId, ProjectId, concat(ProjectName, case when Active = 0 then ' (Inactive)' else ' (Active)' end) as ProjectName from {$wpdb->prefix}timesheet_client_projects tcp order by Active desc, ProjectName"); foreach ($projects as $project) { $project->ProjectName = $common->clean_from_db($project->ProjectName); $clean_projects[] = $project; } $js_projects = json_encode($clean_projects); echo ""; } }