";
global $wpdb;
$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('m');
if ($today != $daysinmonth) {
return;
}
if ($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);
}
if ($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 email_late_timesheets() {
global $wpdb;
$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;
$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';
$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}.