<?php
require_once (PATH_TABLES . "activity/TaskScheduleTable.php") ;
require_once (PATH_MODELS . "activity/TaskPlanScheduleClass.php") ;
require_once (PATH_MODELS . "activity/TaskPlanParticipantClass.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class TaskScheduleClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= TaskScheduleTable::C_TABLE ;
		$this->fldid 	= TaskScheduleTable::C_SCHEDULE_ID ;
	}
	function __destruct() {
	}

    function getSchedulebyPlanId($id){
        $sql = "SELECT * FROM " . TaskScheduleTable::C_TABLE . " ";
        $sql .= " WHERE ".TaskScheduleTable::C_ID." = " . $id . " ";

        $rows = $this->db->getTable($sql);

        return $rows;
    }
    
    function getTaskSchedules($taskID, $doUserFilter = -1, $resultFilter = -1, $startDateFilter = "", $endDateFilter = ""){
        $sql = "SELECT S.* FROM " . TaskScheduleTable::C_TABLE . " S ";
		$sql .= "LEFT OUTER JOIN " . TaskPlanParticipantTable::C_TABLE . " P ON (P." . TaskPlanParticipantTable::C_ID . " = S." . TaskPlanParticipantTable::C_ID . " and P." . TaskPlanParticipantTable::C_PARTICIPANT_TYPE . " = 0)";
        $filter = "WHERE S." . TaskScheduleTable::C_ID . " = " . $taskID . " ";
		
		if($doUserFilter != -1){
			$filter .= "AND P." . TaskPlanParticipantTable::C_USER_ID . " = " . $doUserFilter . " ";
		}
		
		if($resultFilter != -1)
			$filter .= "AND S." . TaskScheduleTable::C_RESULT . " = " . $resultFilter . " ";
		
		if($startDateFilter != "" && $endDateFilter != "")
			$filter .= "AND " . TaskScheduleTable::C_RESULT . " = " . $resultFilter . " ";
        
        $rows = $this->db->getTable($sql . $filter);
        
        return $rows;
    }
    
    function getTaskSchedule($taskID, $taskScheduleID){
        $sql = "SELECT * FROM " . TaskScheduleTable::C_TABLE . " ";
        $filter = "WHERE " . TaskScheduleTable::C_ID . " = " . $taskID . " ";
        $filter = "AND " . TaskScheduleTable::C_SCHEDULE_ID . " = " . $taskScheduleID . " ";
        
        $rows = $this->db->getTable($sql . $filter);
        
        return $rows;
    }
	
    function deleteTaskSchedule($taskID, $taskScheduleID = 0, $resultFilter = -1){
		try {
			$sql = "DELETE FROM " . TaskScheduleTable::C_TABLE . " ";
			$filter = "WHERE " . TaskScheduleTable::C_ID . " = " . $taskID . " ";
			
			if($taskScheduleID != 0)
				$filter .= "AND " . TaskScheduleTable::C_SCHEDULE_ID . " = " . $taskScheduleID . " ";
			
			if($resultFilter != -1)
				$filter .= "AND " . TaskScheduleTable::C_RESULT . " = " . $resultFilter . " ";
			
			$this->db->executeSql($sql . $filter);
			
		} catch (Exception $e) {
			Log::write('[TaskPlanSchedule]' . $e->getMessage());
			return false;
		}
        
        return true;
    }

    function getTaskScheduleList($jobnumber='', $fromdate, $todate, $customer=0,$status=-1){
        $sql = "SELECT T.* ,TP.*,TPP.* FROM " . TaskScheduleTable::C_TABLE . " T ";
        $sql .= " LEFT OUTER JOIN ".TaskPlanTable::C_TABLE." TP ON ( TP.".TaskPlanTable::C_ID." = T.".TaskScheduleTable::C_ID.") ";
        $sql .= " LEFT OUTER JOIN ".TaskPlanParticipantTable::C_TABLE." TPP ON ( TPP.".TaskPlanParticipantTable::C_ID." = T.".TaskScheduleTable::C_ID.") ";
        $sql .= "LEFT JOIN " . JobCentreTable::C_TABLE . " J ON (J.".JobCentreTable::C_ID." = TP.".TaskPlanTable::C_JOB_ID.")";
        $sql .= " WHERE (TPP.".TaskPlanParticipantTable::C_USER_ID." = " . $_SESSION[SE_USERID] . " ";
        $sql .= " OR TP.".TaskPlanTable::C_CREATE_BY." = " . $_SESSION[SE_USERID] . ") ";
        $sql .= "AND cast(T." . TaskScheduleTable::C_TIME . " AS date) BETWEEN '" . $fromdate . "' AND '" . $todate . "' ";
        /*$sql .= " WHERE TP.".TaskPlanParticipantTable::C_PARTICIPANT_TYPE." = ".ParticipantType::Assign." AND TP.".TaskPlanParticipantTable::C_USER_ID ." = " . $_SESSION[SE_USERID] . " ";*/

        if($jobnumber != ''){
            $sql .= " AND J." . JobCentreTable::C_JOB_NUMBER . " like '%" . $jobnumber . "%' ";
        }
        if($customer != 0){
            $sql .= " AND J." . JobCentreTable::C_CUST_ID . " = " . $customer . "";
        }
        if($status != -1){
            $sql .= " AND T." . TaskScheduleTable::C_RESULT . " = " . $status . " ";
        }
      
        $rows = $this->db->getTable($sql);
        
        
        return $rows;
    }

    function getTaskScheduleListByJobCentre($jobCentreId){
        $sql = "SELECT T.* ,TP.*,TPP.* FROM " . TaskScheduleTable::C_TABLE . " T ";
        $sql .= " LEFT OUTER JOIN ".TaskPlanTable::C_TABLE." TP ON ( TP.".TaskPlanTable::C_ID." = T.".TaskScheduleTable::C_ID.") ";
        $sql .= " LEFT OUTER JOIN ".TaskPlanParticipantTable::C_TABLE." TPP ON ( TPP.".TaskPlanParticipantTable::C_ID." = T.".TaskScheduleTable::C_ID.") ";
        $sql .= " WHERE TP.".TaskPlanTable::C_JOB_ID." = " . $jobCentreId . " ";
         
        $rows = $this->db->getTable($sql);
        // Log::write(print_r($sql,true));
        
        return $rows;
    }
}
?>