<?php
//////////////////////////////////////////////////////////////////////////////////
//	This script sets each task that is set to be completed daily back to Due//
//	Each Day at Midnight. 						        //
//									        //
//	Script Checks for tasks not completed and marks them as overdue before	//
//	Processing checks that have been completed.				//
//										//
//	Last Modified on: 3/21/2017						//
//	Expo Version 0.1							//
/////////////////////////////////////////////////////////////////////////////////

//include SQL connection information

include ('.sql_con.php');

//Daily

//Set any task not completed to overdue before we set any completed to due today
$daily_update_overdue_tasks="UPDATE `TASK` SET JOB_STATUS_ID = '4' WHERE SCHEDULE_ID = '1' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 DAY) AND JOB_STATUS_ID NOT IN ('1', '3', '4','8') AND TASK_STATUS_ID = '1'";
		mysql_query($daily_update_overdue_tasks)or die ('Failed to update Overdue Tasks</br>'.mysql_error());


//Set any task completed yesterday to Due today
$daily_set_tasks_due="UPDATE `TASK` SET JOB_STATUS_ID = '5' WHERE SCHEDULE_ID = '1' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 DAY) AND JOB_STATUS_ID NOT IN ('4','8') AND TASK_STATUS_ID = '1'";
		mysql_query($daily_set_tasks_due)or die ('Failed to Set tasks due</br>'.mysql_error());

//weekly scheduling

//set task not completed in 8 days to overdue
$weekly_update_overdue_tasks="UPDATE `TASK` SET JOB_STATUS_ID = '4' WHERE SCHEDULE_ID = '2' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 7 DAY) AND JOB_STATUS_ID NOT IN ('1', '3', '4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($weekly_update_overdue_tasks)or die ('Failed to update Overdue Tasks</br>'.mysql_error());


//Set any task completed Last week to Due today
$weekly_set_tasks_due="UPDATE `TASK` SET JOB_STATUS_ID = '5' WHERE SCHEDULE_ID = '2' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 7 DAY) AND JOB_STATUS_ID NOT IN ('4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($weekly_set_tasks_due)or die ('Failed to Set tasks due</br>'.mysql_error());


//Monthly Scheduling

//set task not completed in 30 days to overdue
$monthly_update_overdue_tasks="UPDATE `TASK` SET JOB_STATUS_ID = '4' WHERE SCHEDULE_ID = '3' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 30 DAY) AND JOB_STATUS_ID NOT IN ('1', '3', '4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($monthly_update_overdue_tasks)or die ('Failed to update Overdue Tasks</br>'.mysql_error());


//Set any task completed Last Month to Due today
$monthly_set_tasks_due="UPDATE `TASK` SET JOB_STATUS_ID = '5' WHERE SCHEDULE_ID = '3' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 30 DAY) AND JOB_STATUS_ID NOT IN ('4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($monthly_set_tasks_due)or die ('Failed to Set tasks due</br>'.mysql_error());


//Hourly

//set task not completed in 1 Hour to overdue
$hourly_update_overdue_tasks="UPDATE `TASK` SET JOB_STATUS_ID = '4' WHERE SCHEDULE_ID = '4' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 Hour) AND JOB_STATUS_ID NOT IN ('1', '3', '4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($hourly_update_overdue_tasks)or die ('Failed to update Overdue Tasks</br>'.mysql_error());


//Set any task completed Last Hour to Due today
$hourly_set_tasks_due="UPDATE `TASK` SET JOB_STATUS_ID = '5' WHERE SCHEDULE_ID = '4' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 Hour) AND JOB_STATUS_ID NOT IN ('4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($hourly_set_tasks_due)or die ('Failed to Set tasks due</br>'.mysql_error());


//Annually

//set task not completed in 1 Year to overdue
$annually_update_overdue_tasks="UPDATE `TASK` SET JOB_STATUS_ID = '4' WHERE SCHEDULE_ID = '5' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 Year) AND JOB_STATUS_ID NOT IN ('1', '3', '4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($annually_update_overdue_tasks)or die ('Failed to update Overdue Tasks</br>'.mysql_error());


//Set any task completed Last Year to Due today
$annually_set_tasks_due="UPDATE `TASK` SET JOB_STATUS_ID = '5' WHERE SCHEDULE_ID = '4' AND LAST_COMPLETED < DATE_SUB(NOW(), INTERVAL 1 Year) AND JOB_STATUS_ID NOT IN ('4','8') AND TASK_STATUS_ID = '1'";
                mysql_query($annually_set_tasks_due)or die ('Failed to Set tasks due</br>'.mysql_error());


mysql_close($con);
?>
