closed_timesheet_menu();
if (isset($_POST['filled_out'])) {
$this->closed_timesheet_search();
}
}
function closed_timesheet_search() {
global $wpdb;
$db = new time_sheets_db();
$common = new time_sheets_common();
$start_date = isset($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : "";
$end_date = isset($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : "";
$ClientId = isset($_POST['ClientId']) ? intval($_POST['ClientId']) : "";
$ProjectId = isset($_POST['ProjectId']) ? intval($_POST['ProjectId']) : "";
$invoice_number = isset($_POST['invoice_number']) ? intval($_POST['invoice_number']) : "";
$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : "";
$allow_invoice_comment_snipit = get_user_option('allow_invoice_comment_snipit', $user_id);
$options = get_option('time_sheets');
$Projects = $common->return_projects_for_user(-1);
if ($ClientId == -2) {
unset($ClientId);
}
if ($ProjectId == -2) {
unset($ProjectId);
}
$sql = "select t.timesheet_id, u.user_login, t.start_date, c.ClientName client_name, cp.ProjectName project_name, t.invoiceid, cp.notes, u.display_name, cp.po_number,
date_add(t.start_date, INTERVAL CASE WHEN monday_hours != 0 then 0 when tuesday_hours != 0 then 1 when wednesday_hours != 0 then 2 when thursday_hours != 0 then 3 when friday_hours != 0 then 4 when saturday_hours != 0 then 5 when sunday_hours != 0 then 6 else 0 end DAY) first_billed_day, cs.display_name cs_display_name, cps.display_name cps_display_name, tcs.display_name tcs_display_name, tcps.display_name tcps_display_name, t.total_hours, t.invoiceid, t.approved, t.marked_complete_by, t.other_expenses_notes
from {$wpdb->prefix}timesheet t
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.sales_person_id = tcs.ID
left outer join {$wpdb->users} tcps on cp.sales_person_id = tcps.ID
join {$wpdb->users} u on t.user_id = u.ID
where approved = 1
and t.invoiced = 1";
if ($start_date) {
$sql = $sql . " and t.start_date > '{$start_date}'";
}
if ($end_date) {
$sql = $sql . " and t.start_date <= '{$end_date}'";
}
if ($ClientId) {
$sql = $sql . " and c.ClientId = {$ClientId}";
}
if ($ProjectId) {
$sql = $sql . " and t.ProjectId = {$ProjectId}";
}
if ($invoice_number) {
$sql = $sql . " and t.invoiceid = {$invoice_number}";
}
if ($user_id) {
$sql = $sql . " and t.user_id = {$user_id}";
}
$timesheets = $db->get_results($sql);
if (!$timesheets) {
echo "No matching time sheets found.
";
}
echo "
| Timesheet | View | User | Approved | Week Completed | Invoice Number | Start Date | First Billed Date | ";
if ((isset($options['hide_client_project']) && sizeof($Projects) != 1) || !isset($options['hide_client_project'])) {
echo "Client | Project Name | ";
}
echo "Total Hours | ";
if ($allow_invoice_comment_snipit) {
echo "Notes | ";
}
echo "
";
foreach ($timesheets as $timesheet) {
echo "{$timesheet->timesheet_id} |
 |
{$timesheet->display_name} | ";
if ($timesheet->approved==1) {
echo " ";
} else {
echo " ";
}
echo " | ";
if ($timesheet->marked_complete_by) {
echo " ";
} else {
echo " ";
}
echo " | {$timesheet->invoiceid} | {$common->f_date($timesheet->start_date)} | {$common->f_date($timesheet->first_billed_day)} | ";
if ((isset($options['hide_client_project']) && sizeof($Projects) != 1) || !isset($options['hide_client_project'])) {
echo "{$common->clean_from_db($timesheet->client_name)} | {$common->clean_from_db($timesheet->project_name)} | ";
}
echo "{$timesheet->total_hours} | ";
if ($allow_invoice_comment_snipit) {
echo "";
echo substr($common->esc_textarea($timesheet->other_expenses_notes), 0, 20);
if (strlen($common->esc_textarea($timesheet->other_expenses_notes)) > 20) {
echo " ...";
}
echo " | ";
}
echo "";
}
echo "
";
}
function closed_timesheet_menu() {
global $wpdb;
$common = new time_sheets_common();
$db = new time_sheets_db();
$start_date = isset($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : "";
$end_date = isset($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : "";
$ClientId = isset($_POST['ClientId']) ? intval($_POST['ClientId']) : "";
$ProjectId = isset($_POST['ClientId']) ? intval($_POST['ClientId']) : "";
$invoice_number = isset($_POST['invoice_number']) ? intval($_POST['invoice_number']) : "";
$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : "";
$options = get_option('time_sheets');
$Projects = $common->return_projects_for_user(-1);
$users = $common->return_employee_list();
if (!$ClientId) {
$ClientId = -1;
}
if (!$ProjectId) {
$ProjectId = -1;
}
echo "Search for Invoiced Time Sheets
";
echo "";
echo "
";
}
function show_invoicing_list() {
global $wpdb;
$db = new time_sheets_db();
$main = new time_sheets_main();
$queue_approval = new time_sheets_queue_approval();
$approver = $queue_approval->employee_approver_check();
$common = new time_sheets_common();
$user = wp_get_current_user();
$user_id = $user->ID;
$options = get_option('time_sheets');
$timesheets = $this->return_invoicing_list();
$allow_show_record_invoicing = get_user_option('time_sheets_invoicing_allow_show_record_invoicing', $user_id);
$currency_char = $options['currency_char'];
$retainers_parm = "";
if (isset($_GET['show_retainers'])) {
$retainers_parm = "&show_retainers=checked";
}
if (isset($_GET['hide_nonretainers'])) {
$retainers_parm = $retainers_parm . "&hide_nonretainers=checked";
}
if ($timesheets) {
echo "";
} else {
echo "No time sheets to invoice.";
}
}
function invoice_timesheet(){
$entry = new time_sheets_entry();
$common = new time_sheets_common();
if (isset($_GET['action']) && $_GET['action']=='preinvoice') {
$entry->show_timesheet();
} else {
$show_retainers = isset($_GET['show_retainers']) ? " checked" : "";
$hide_nonretainers = isset($_GET['hide_nonretainers']) ? " checked" : "";
echo "
";
echo "
| ";
$this->show_invoicing_list();
echo " | ";
$common->show_clients_on_retainer();
echo " |
";
}
}
function do_timesheet_rejectinvoicing() {
global $wpdb;
$db = new time_sheets_db();
$user = wp_get_current_user();
$options = get_option('time_sheets');
if ($options['queue_order']=='parallel') {
echo 'This timesheet may be in the payroll queue and may need to be adjusted.
';
}
$sql = "update {$wpdb->prefix}timesheet
set approved_by=NULL,
approved_date = NULL,
approved=0
where timesheet_id=%d";
$params=array(intval($_GET['timesheet_id']));
$sql = "insert into {$wpdb->prefix}timesheet_archive
select $user->ID, now(), 'UPDATED', timesheet_id, user_id, start_date, entered_date, client_name, project_name, 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, marked_complete_by, marked_complete_date, approved, approved_by, approved_date, invoiced, invoiced_by, invoiced_date, invoiceid, ClientId, mileage, EmbargoPendingProjectClose, project_complete, ProjectId, payrolled, payrolled_on, payrolled_by, flight_cost, isPerDiem, perdiem_city, taxi, monday_ot, tuesday_ot, wednesday_ot, thursday_ot, friday_ot, saturday_ot, sunday_ot
FROM {$wpdb->prefix}timesheet
where timesheet_id = %d";
$params = array(intval($_GET['timesheet_id']));
$db->query($sql, $params);
$db->query($sql, $params);
echo 'Timesheet has been rejected.
';
}
function return_invoicing_list() {
global $wpdb;
$user = wp_get_current_user();
$user_id = $user->ID;
$db = new time_sheets_db();
$IsRetainer = "-1";
if (isset($_GET['show_retainers'])) {
$IsRetainer = $IsRetainer . ", 1";
}
if (!isset($_GET['hide_nonretainers'])) {
$IsRetainer = $IsRetainer . ", 0";
}
$primary_sort_col = get_user_option('time_sheets_invoicing_primary_sort_col', $user_id);
$primary_sort_order = get_user_option('time_sheets_invoicing_primary_sort_order', $user_id);
$secondary_sort_col = get_user_option('time_sheets_invoicing_secondary_sort_col', $user_id);
$secondary_sort_order = get_user_option('time_sheets_invoicing_secondary_sort_order', $user_id);
if ($primary_sort_col=='') {
$primary_sort_col = 'c.ClientName';
}
if ($primary_sort_order=='') {
$primary_sort_order='asc';
}
if ($secondary_sort_col=='') {
$secondary_sort_col = 't.start_date';
}
if ($secondary_sort_order=='') {
$secondary_sort_order='asc';
}
$sql = "select t.timesheet_id, u.user_login, t.start_date, c.ClientName client_name, cp.ProjectName project_name, t.invoiceid, cp.notes, u.display_name, cp.po_number,
date_add(t.start_date, INTERVAL CASE WHEN monday_hours != 0 then 0 when tuesday_hours != 0 then 1 when wednesday_hours != 0 then 2 when thursday_hours != 0 then 3 when friday_hours != 0 then 4 when saturday_hours != 0 then 5 when sunday_hours != 0 then 6 else 0 end DAY) first_billed_day, cs.display_name cs_display_name, cps.display_name cps_display_name, tcs.display_name tcs_display_name, tcps.display_name tcps_display_name, date_add(t.start_date, INTERVAL CASE WHEN sunday_hours != 0 then 6 when saturday_hours != 0 then 5 when friday_hours != 0 then 4 when thursday_hours != 0 then 3 when wednesday_hours != 0 then 2 when tuesday_hours != 0 then 1 when monday_hours != 0 then 0 else 0 end DAY) last_day_billed, total_hours, (hotel_charges+rental_car_charges+tolls+other_expenses+taxi) as expenses
from {$wpdb->prefix}timesheet t
JOIN {$wpdb->prefix}timesheet_clients c ON t.ClientId = c.ClientId
JOIN {$wpdb->prefix}timesheet_client_projects cp ON t.ProjectId = cp.ProjectId
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.sales_person_id = tcs.ID
left outer join {$wpdb->users} tcps on cp.sales_person_id = tcps.ID
join {$wpdb->users} u on t.user_id = u.ID
where t.invoiced = 0
and approved = 1
and cp.ProjectId in (select ProjectId from {$wpdb->prefix}timesheet_client_projects where IsRetainer in ({$IsRetainer}))
order by {$primary_sort_col} {$primary_sort_order}, {$secondary_sort_col} {$secondary_sort_order} ";
$timesheets = $db->get_results($sql);
return $timesheets;
}
function should_be_payrolled($timesheet_id) {
global $wpdb;
$db = new time_sheets_db();
$user = wp_get_current_user();
$user_id = $user->ID;
$options = get_option('time_sheets');
$sql = "select * from {$wpdb->prefix}timesheet where timesheet_id = {$timesheet_id}";
$timesheet = $db->get_row($sql);
$sql = "select * from {$wpdb->prefix}timesheet_employee_always_to_payroll where user_id = {$timesheet->user_id}";
$always_payrol = $db->get_row($sql);
$payrolled = 1; //Default payrolled to 1, reset to 0 if needed
if (isset($always_payrol)) {
$payrolled = 0;
}
if (isset($options['mileage']) && $timesheet->mileage!=0) {
$payrolled = 0;
}
if (isset($options['per_diem']) && $timesheet->per_diem_days != 0) {
$payrolled = 0;
}
if (isset($options['flight_cost']) && $timesheet->flight_cost != 0) {
$payrolled = 0;
}
if (isset($options['hotel']) && $timesheet->hotel_costs != 0) {
$payrolled = 0;
}
if (isset($options['rental_car']) && $timesheet->rental_car_costs!=0) {
$payrolled = 0;
}
if (isset($options['taxi']) && $timesheet->taxi!=0) {
$payrolled = 0;
}
if (isset($options['tolls']) && $timesheet->tolls!=0) {
$payrolled = 0;
}
if (isset($options['other_expenses']) && $timesheet->other_expenses!=0) {
$payrolled = 0;
}
return $payrolled;
}
function do_timesheet_invoicing_one_timesheet($timesheet_id, $invoiced, $invoiced_by, $invoice_id) {
global $wpdb;
$db = new time_sheets_db();
$user = wp_get_current_user();
$user_id = $user->ID;
$options = get_option('time_sheets');
if ($options['queue_order']!='parallel') {
$payrolled = $this->should_be_payrolled($timesheet_id);
$sql = "update {$wpdb->prefix}timesheet
set payrolled = {$payrolled}
where timesheet_id = {$timesheet_id}";
}
$sql = "update {$wpdb->prefix}timesheet
set invoiced= {$invoiced},
invoiced_by = {$user_id},
invoiced_date = CURDATE(),
invoiceid = {$invoice_id}
where timesheet_id = {$timesheet_id}";
$db->query($sql);
$sql = "insert into {$wpdb->prefix}timesheet_archive
select $user->ID, now(), 'UPDATED', timesheet_id, user_id, start_date, entered_date, client_name, project_name, 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, marked_complete_by, marked_complete_date, approved, approved_by, approved_date, invoiced, invoiced_by, invoiced_date, invoiceid, ClientId, mileage, EmbargoPendingProjectClose, project_complete, ProjectId, payrolled, payrolled_on, payrolled_by, flight_cost, isPerDiem, perdiem_city, taxi, monday_ot, tuesday_ot, wednesday_ot, thursday_ot, friday_ot, saturday_ot, sunday_ot
FROM {$wpdb->prefix}timesheet
where timesheet_id = %d";
$params = array($timesheet_id);
$db->query($sql, $params);
}
function do_timesheet_invoicing(){
global $wpdb;
$db = new time_sheets_db();
$user = wp_get_current_user();
$user_id = $user->ID;
$options = get_option('time_sheets');
check_admin_referer( 'timesheet_invoice_processing' );
$timesheets = $this->return_invoicing_list();
foreach ($timesheets as $timesheet) {
$valuename = "timesheet_{$timesheet->timesheet_id}_invoice";
$invoiceid = intval($_POST[$valuename]);
$valuename = "timesheet_{$timesheet->timesheet_id}";
if (isset($_POST[$valuename]) && $_POST[$valuename] == 1) {
$invoiced = 1;
} else {
$invoiced = 0;
}
$this->do_timesheet_invoicing_one_timesheet($timesheet->timesheet_id, $invoiced, $user_id, $invoiceid);
}
echo 'Timesheets have been updated.
';
}
function count_pending_invoice() {
global $wpdb;
global $vcount_pending_invoicing;
$user = wp_get_current_user();
$user_id = $user->ID;
$db = new time_sheets_db();
if (!isset($vcount_pending_invoicing)) {
$sql = "select sum(case when tcp.IsRetainer=1 then 1 else 0 end) Retainer, sum(case when tcp.IsRetainer=0 then 1 else 0 end) NotRetainer
from {$wpdb->prefix}timesheet t
join {$wpdb->prefix}timesheet_client_projects tcp on t.ProjectId = tcp.ProjectId
where t.approved = 1 and t.invoiced = 0";
$count = $db->get_row($sql);
$vcount_pending_invoicing = $count;
} else {
$count = $vcount_pending_invoicing;
}
return $count;
}
function count_users_open_invoice() {
global $wpdb;
$user = wp_get_current_user();
$user_id = $user->ID;
$db = new time_sheets_db();
$sql = "select count(*)
from {$wpdb->prefix}timesheet t
join {$wpdb->prefix}timesheet_client_projects tcp on t.ProjectId = tcp.ProjectId
where t.week_complete = 0 and t.user_id = {$user_id}";
$count = $db->get_var($sql);
return $count;
}
function employee_invoicer_check () {
global $wpdb;
global $invoicer_count;
$user = wp_get_current_user();
$user_id = $user->ID;
$db = new time_sheets_db();
if (!isset($invoicer_count)) {
$sql = "select count(*)
from {$wpdb->prefix}timesheet_invoicers t
where user_id = {$user_id}";
$count = $db->get_var($sql);
$invoicer_count = $count;
} else {
$count = $invoicer_count;
}
return $count;
}
}