prefix}timesheet_customer_project_employee_title_override where expiration_date <= now()";
$db->query($sql);
}
function queue_use_templates() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$myDash = new time_sheets_mydashboard();
$common = new time_sheets_common();
$options = get_option('time_sheets');
if (!isset($options['allow_recurring_timesheets'])) {
return 0;
}
$sql = "select timesheet_id, expires_on, delete_after_expiration, next_execution, frequency from {$wpdb->prefix}timesheet_scheduled where next_execution <= now()";
$templates = $db->get_results($sql);
if ($templates) {
foreach ($templates as $template) {
#Use the template
$myDash->use_template($template->timesheet_id);
if ($template->frequency=='w') {
$sql = "update {$wpdb->prefix}timesheet_scheduled set next_execution = date_add(next_execution, INTERVAL 7 DAY) where timesheet_id = {$template->timesheet_id}";
} elseif ($template->frequency == 'm') {
$sql = "update {$wpdb->prefix}timesheet_scheduled set next_execution = date_add(next_execution, INTERVAL 1 MONTH) where timesheet_id = {$template->timesheet_id}";
} elseif ($template->frequency == 'q') {
$sql = "update {$wpdb->prefix}timesheet_scheduled set next_execution = date_add(next_execution, INTERVAL 1 QUARTER) where timesheet_id = {$template->timesheet_id}";
} elseif ($template->frequency == 'a') {
$sql = "update {$wpdb->prefix}timesheet_scheduled set next_execution = date_add(next_execution, INTERVAL 1 YEAR) where timesheet_id = {$template->timesheet_id}";
}
$db->query($sql);
$common->archive_records ('timesheet_scheduled', 'timesheet_scheduled_archive', "timesheet_id", $template->timesheet_id, '=', 'UPDATED');
if (($template->expires_on < date('Y-m-d')) && ($template->delete_after_expiration == 1)) {
echo "
{$template->timesheet_id} wants to be deleted";
$myDash->delete_template($template->timesheet_id);
}
}
}
}
function queue_process_client_projects_changequeue() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$common = new time_sheets_common();
$options = get_option('time_sheets');
$sql = "update {$wpdb->prefix}timesheet_client_projects tcp
inner join {$wpdb->prefix}timesheet_client_projects_changequeue tcpc on tcp.ProjectId = tcpc.ProjectId
set tcp.ProjectName = tcpc.ProjectName,
tcp.IsRetainer = tcpc.IsRetainer,
tcp.MaxHours = tcpc.MaxHours,
tcp.Active = tcpc.Active,
tcp.notes = tcpc.notes,
tcp.BillOnProjectCompletion = tcpc.BillOnProjectCompletion,
tcp.flat_rate = tcpc.flat_rate,
tcp.po_number = tcpc.po_number,
tcp.sales_person_id = tcpc.sales_person_id,
tcp.technical_sales_person_id = tcpc.technical_sales_person_id,
tcp.close_on_completion = tcpc.close_on_completion,
tcp.retainer_id = tcpc.retainer_id,
tcp.hours_included = tcpc.hours_included,
tcp.hourly_rate = tcpc.hourly_rate,
tcp.max_monthly_bucket = tcpc.max_monthly_bucket
where tcpc.process_date <= date(now()) and tcpc.is_deleted = 0 and is_processed = 0";
$db->query($sql);
$common->archive_records ('timesheet_client_projects', 'timesheet_client_projects_archive', "ProjectId", "(select ProjectId from {$wpdb->prefix}timesheet_client_projects_changequeue tcpc where tcpc.process_date <= date(now()) and tcpc.is_deleted = 0 and is_processed = 0)", 'IN', 'UPDATED');
echo "Updated projects
";
if (isset($options['allow_money_based_retainers'])) {
$sql = "update {$wpdb->prefix}timesheet_project_employee_titles pet
inner join {$wpdb->prefix}timesheet_client_projects_changequeue tcpc on pet.project_id = tcpc.ProjectId
inner join {$wpdb->prefix}timesheet_project_employee_titles_changequeue petc on petc.queue_id = tcpc.queue_id
and pet.title_id = petc.title_id
set pet.hourly_rate = petc.hourly_rate
where tcpc.process_date <= date(now()) and tcpc.is_deleted = 0 and is_processed = 0";
$db->query($sql);
echo "Updated project title rates
";
}
$common->archive_records ('timesheet_client_projects_changequeue', 'timesheet_client_projects_changequeue_archive', "process_date <= date(now()) and is_deleted = 0 and is_processed", "0", '=', 'QUEUED');
echo "Updated archive
";
$sql = "update {$wpdb->prefix}timesheet_client_projects_changequeue tcpc
set is_processed = 1
where tcpc.process_date <= date(now()) and tcpc.is_deleted = 0 and tcpc.is_processed = 0";
$db->query($sql);
$common->archive_records ('timesheet_client_projects_changequeue', 'timesheet_client_projects_changequeue_archive', "process_date <= date(now()) and is_deleted = 0 and is_processed", "0", '=', 'UPDATED');
echo "Updated queue
";
}
function queue_process_scheduled_recurring_changes() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$common = new time_sheets_common();
$user = wp_get_current_user();
$user_id = $user->ID;
//process updates
$sql = "update {$wpdb->prefix}timesheet_recurring_invoices_monthly b
inner join {$wpdb->prefix}timesheet_recurring_invoices_monthly_schedule a on b.client_id = a.client_id
set b.MonthlyHours = a.MonthlyHours,
b.HourlyRate = a.HourlyRate,
b.Notes = a.Notes,
b.BillOnProjectCompletion = a.BillOnProjectCompletion,
b.reqular_interval = a.regular_interval
where date(a.active_date) = date(now())";
$db->query($sql);
//Archive the updated
$sql = "insert into {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive
(created_by, created_on, client_id, action, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, reqular_interval)
select {$user_id}, now(), client_id, 'CRON UPD', MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, regular_interval
from {$wpdb->prefix}timesheet_recurring_invoices_monthly_schedule a
where client_id in (select client_id from {$wpdb->prefix}timesheet_recurring_invoices_monthly)
and date(a.active_date) = date(now())
and client_id not in (select client_id from {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive where date(created_on) = date(now()) and created_by = {$user_id})";
$db->query($sql);
//Archive the inserts, but need to archive before the actual inserts
$sql = "insert into {$wpdb->prefix}timesheet_recurring_invoices_monthly_archive
(created_by, created_on, client_id, action, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, reqular_interval)
select {$user_id}, now(), client_id, 'CRON INS', MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, regular_interval
from {$wpdb->prefix}timesheet_recurring_invoices_monthly_schedule a
where client_id not in (select client_id from {$wpdb->prefix}timesheet_recurring_invoices_monthly)
and date(a.active_date) = date(now())";
$db->query($sql);
//Add records for new recurring billings
$sql = "insert into {$wpdb->prefix}timesheet_recurring_invoices_monthly
(client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, reqular_interval)
select client_id, MonthlyHours, HourlyRate, Notes, BillOnProjectCompletion, regular_interval
from {$wpdb->prefix}timesheet_recurring_invoices_monthly_schedule a
where client_id not in (select client_id from {$wpdb->prefix}timesheet_recurring_invoices_monthly)
and date(a.active_date) = date(now())";
$db->query($sql);
}
function queue_rejected_reminders() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$common = new time_sheets_common();
$user = wp_get_current_user();
$user_id = $user->ID;
$http = !empty($_SERVER['HTTPS']) ? "https" : "http";
$sql = "select u.user_email, t.timesheet_id, r.display_name rejected_by, u.display_name owner_name, r.user_email rejected_by_email
from {$wpdb->prefix}timesheet_reject_message_queue trmq
join {$wpdb->prefix}timesheet t on trmq.timesheet_id = t.timesheet_id
join {$wpdb->users} r on trmq.rejected_by = r.ID
join {$wpdb->users} u on t.user_id = u.ID
where trmq.email_reminder_on <= now()";
$timesheets = $db->get_results($sql);
if ($timesheets) {
foreach ($timesheets as $timesheet) {
$subject = "Reminder to update a time sheet and resubmit it";
$body = "The time sheet timesheet_id . "'>" . $timesheet->timesheet_id . " (owned by {$timesheet->owner_name}) was rejected by {$timesheet->rejected_by} and needs to be updated and marked as 'Week Complete'.
";
$common->send_email($timesheet->user_email, $subject, $body);
$common->send_email($timesheet->rejected_by_email, $subject, $body);
$sql = "update {$wpdb->prefix}timesheet_reject_message_queue
set email_reminder_on = date_add(now(), interval 3 day)
where timesheet_id = {$timesheet->timesheet_id}";
$db->query($sql);
$sql = "insert into {$wpdb->prefix}timesheet_reject_message_queue_archive
(created_by, created_on, action, timesheet_id, rejected_by, rejected_on, email_reminder_on, notify_approved_on)
select $user->ID, now(), 'UPDATED', timesheet_id, rejected_by, rejected_on, email_reminder_on, notify_approved_on
FROM {$wpdb->prefix}timesheet_reject_message_queue
WHERE timesheet_id= {$timesheet->timesheet_id}";
$db->query($sql);
}
} else {
echo "Nothing to do.
";
}
}
function queue_rejected_admin_messages() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$common = new time_sheets_common();
$user = wp_get_current_user();
$user_id = $user->ID;
$http = !empty($_SERVER['HTTPS']) ? "https" : "http";
$sql = "select u.user_email, t.timesheet_id, r.display_name rejected_by, u.display_name owner_name, r.user_email rejected_by_email, u.user_email
from {$wpdb->prefix}timesheet_reject_message_queue trmq
join {$wpdb->prefix}timesheet t on trmq.timesheet_id = t.timesheet_id
join {$wpdb->users} r on trmq.rejected_by = r.ID
join {$wpdb->users} u on t.user_id = u.ID
where trmq.notify_approved_on <= now()";
$timesheets = $db->get_results($sql);
if (isset($timesheets)) {
foreach ($timesheets as $timesheet) {
$subject = "Time sheet has not been updated after reminder";
$body = "The time sheet timesheet_id . "'>" . $timesheet->timesheet_id . " (owned by {$timesheet->owner_name}) was rejected by {$timesheet->rejected_by} and has not been corrected and resubmitted. The owner of the timesheet needs to be contacted.
";
$common->send_email($timesheet->rejected_by_email, $subject, $body);
$sql = "update {$wpdb->prefix}timesheet_reject_message_queue
set notify_approved_on = date_add(now(), interval 7 day)
where timesheet_id = {$timesheet->timesheet_id}";
$db->query($sql);
$sql = "insert into {$wpdb->prefix}timesheet_reject_message_queue_archive
(created_by, created_on, action, timesheet_id, rejected_by, rejected_on, email_reminder_on, notify_approved_on)
select $user->ID, now(), 'UPDATED', timesheet_id, rejected_by, rejected_on, email_reminder_on, notify_approved_on
FROM {$wpdb->prefix}timesheet_reject_message_queue
WHERE timesheet_id= {$timesheet->timesheet_id}";
$db->query($sql);
}
} else {
echo "Nothing to do.
";
}
}
function disable_projects() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$sql = "UPDATE {$wpdb->prefix}timesheet_client_projects
set Active = 0
WHERE ProjectId IN (SELECT project_id FROM {$wpdb->prefix}timesheet_client_project_autoclose WHERE close_date < now())";
var_dump($sql);
$db->query($sql);
echo "
";
$sql = "DELETE FROM {$wpdb->prefix}timesheet_client_project_autoclose
WHERE close_date < now()";
var_dump($sql);
$db->query($sql);
}
function email_retainers_due() {
//echo "starting email_retainers_due
";
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$options = get_option('time_sheets');
$entry = new time_sheets_entry();
$db = new time_sheets_db();
$common = new time_sheets_common();
$daysinmonth = date('t');
$today = date('d');
if ($today != $daysinmonth) {
echo "This procedure has exited. It only runs on the last day of the month.";
return;
}
if (isset($options['update_retainer_hours'])) {
//echo "Fixing retainer hours avaialble.
";
$sql = "update {$wpdb->prefix}timesheet_client_projects tcp
set tcp.MaxHours = tcp.HoursUsed + (select MonthlyHours from {$wpdb->prefix}timesheet_recurring_invoices_monthly rim where tcp.ClientId = rim.client_id)
where tcp.IsRetainer = 1
order by tcp.ProjectId";
var_dump($sql);
$db->query($sql);
$common->archive_records('timesheet_client_projects', 'timesheet_client_projects_archive', 'IsRetainer', '1');
}
if (isset($options['email_retainer_due'])) {
$sql = "select DISTINCT u.user_email
from {$wpdb->prefix}timesheet t
join {$wpdb->users} u on t.user_id = u.ID
join {$wpdb->prefix}timesheet_client_projects tcp on t.ProjectId = tcp.ProjectId
and tcp.IsRetainer = 1
WHERE start_date > DATE_ADD(now(), INTERVAL -60 DAY)";
$timesheets = $db->get_results($sql);
if ($timesheets) {
$subject = "Retainers for this month are due";
$body = "Timesheets for your retainer clients are due today for the prior month. Please submit them by tonight so invoices can be processed in the morning for last month.";
foreach ($timesheets as $timesheet) {
$common->send_email ($timesheet->user_email, $subject, $body);
}
}
$sql = "SELECT DISTINCT u.user_email
FROM {$wpdb->users} u
JOIN {$wpdb->prefix}timesheet_approvers ta on u.ID = ta.user_id
UNION
SELECT DISTINCT u.user_email
FROM {$wpdb->users} u
JOIN {$wpdb->prefix}timesheet_invoicers ti on u.ID = ti.user_id";
$users = $db->get_results($sql);
if ($users) {
$subject = "Monthly retainer time sheets will be ready soon";
$body = "Monthly time sheets should be ready for processing tomorrow morning. This is your reminder to process them.";
foreach ($users as $user) {
$common->send_email($user->user_email, $subject, $body);
}
}
}
}
function truncate_email_queue_archive() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$db = new time_sheets_db();
$sql = "truncate table {$wpdb->prefix}timesheet_emailqueue_archive";
$db->query($sql);
}
function email_late_timesheets() {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$options = get_option('time_sheets');
if (date('w') != $options['day_of_week_timesheet_reminders']) {
return;
}
$entry = new time_sheets_entry();
$common = new time_sheets_common();
$db = new time_sheets_db();
if ($options['email_late_timesheets']) {
$sql = "select t.timesheet_id, u.user_email, DATE_ADD(start_date, INTERVAL 14 DAY)
from {$wpdb->prefix}timesheet t
join {$wpdb->users} u on t.user_id = u.ID
where start_date > DATE_ADD(now(), INTERVAL -14 DAY)
and week_complete = 0";
$timesheets = $db->get_results($sql);
if ($timesheets) {
foreach ($timesheets as $timesheet) {
$subject = "Timesheet pending completion";
$body = "Timesheet {$timesheet->timesheet_id} has not been marked as completed.
";
$common->send_email ($timesheet->user_email, $subject, $body);
}
}
}
}
function process_email($all = NULL) {
global $wpdb;
if ( defined( 'TIMESHEETS_SKIP_CRON' ) && true ) {
echo "Timesheet Cron is disabled";
return;
}
$options = get_option('time_sheets');
$db = new time_sheets_db();
if ($all) {
$sql = "select max(entered_on) entered_on from {$wpdb->prefix}timesheet_emailqueue";
} else {
$sql = "select max(entered_on) entered_on from {$wpdb->prefix}timesheet_emailqueue where entered_on not between date_add(now(),interval -5 minute) and now()";
}
$anything = $db->get_var ($sql);
if (is_null($anything)) {
echo "nothing to do";
return;
}
if ($anything) {
$sql = "select send_to, send_from_email, send_from_name, subject, count(*) ct
from {$wpdb->prefix}timesheet_emailqueue
group by send_to, send_from_email, send_from_name, subject";
$groups = $db->get_results($sql);
foreach ($groups as $group) {
$sql = "select email_id, message_body
from {$wpdb->prefix}timesheet_emailqueue
where send_to = %s and send_from_email = %s and send_from_name = %s and subject = %s
order by email_id";
$parms = array($group->send_to, $group->send_from_email, $group->send_from_name, $group->subject);
$rows = $db->get_results($sql, $parms);
$message_body = "";
$ids = "-1";
foreach ($rows as $row) {
$message_body = "{$message_body}
{$row->message_body}";
$ids = "{$ids}, {$row->email_id}";
}
$header[] = "From: {$group->send_from_name} <{$group->send_from_email}>";
$header[] = 'content-type: text/html';
$message_body = $message_body . "
"; $success = wp_mail($group->send_to, $group->subject, $message_body, $header); if ($success==false) { echo "
Error sending email to {$group->send_to}.
Email sent to {$group->send_to}.
Error sending email to {$to}
Email sent to {$to}
"; #if ($rows->records !== 0 || $rows->records !== -1) { if ($rows != 0) { $sql = "select user_email from {$wpdb->users} u join {$wpdb->prefix}timesheet_approvers a on u.ID = a.user_id"; $users = $db->get_results($sql); $subject = "{$IntervalName} retainers have been entered into the timesheet system."; $body = "{$IntervalName} retainers have been entered into the timesheet system and need to be processed. These retainer hour's need to be zeroed out as well.
There were {$rows} invoices entered.
The retainers can be viewed and invoiced from the invoicing menu."; if ($options['enable_email']) { foreach ($users as $user) { $common->send_email ($user->user_email, $subject, $body); } } } } }