<?php
require_once (PATH_TABLES . "activity/ActivityTimingDetailTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class ActivityTimingDetailClass extends MasterBase {
  
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= ActivityTimingDetailTable::C_TABLE ;
		$this->fldid 	= ActivityTimingDetailTable::C_ID ;
		$this->flddesc 	= ActivityTimingDetailTable::C_DESC ;
	}
	function __destruct() {
	}

function getTimingListItem($id,$line='',$api=false, $order="") {  
        
        $sql = "SELECT * FROM " .ActivityTimingDetailTable::C_TABLE . 
                " WHERE " . ActivityTimingDetailTable::C_ID . " = " . $id;

        if($line){
            $sql .= " AND ".ActivityTimingDetailTable::C_LINE. " = ".$line;
        }
        
        if($order !=""){
            $sql .= " ORDER BY ".ActivityTimingDetailTable::C_DATE." ";
        }

        $rows=$this->db->getTable($sql);
        
        $list = array() ;
        if(!is_null($rows) && count($rows) > 0){
            foreach($rows as $row){

             $data = array() ;
             $data[ActivityTimingDetailTable::C_ID]    		= $row[ActivityTimingDetailTable::C_ID];
             $data[ActivityTimingDetailTable::C_LINE] 		= $row[ActivityTimingDetailTable::C_LINE];
             $data[ActivityTimingDetailTable::C_START] 		= $row[ActivityTimingDetailTable::C_START];
             $data[ActivityTimingDetailTable::C_END] 		= $row[ActivityTimingDetailTable::C_END];
             $data[ActivityTimingDetailTable::C_TOTAL_TIME] = $row[ActivityTimingDetailTable::C_TOTAL_TIME];
             $data[ActivityTimingDetailTable::C_ASSIGN_ID]  = $row[ActivityTimingDetailTable::C_ASSIGN_ID];
             $data[ActivityTimingDetailTable::C_DESC]       = $row[ActivityTimingDetailTable::C_DESC];
             
             if($api){
                $data[ActivityTimingDetailTable::C_DATE]       = date_format(date_create($row[ActivityTimingDetailTable::C_DATE]), 'd/m/Y');
                $data[ActivityTimingDetailTable::C_END_DATE]   = date_format(date_create($row[ActivityTimingDetailTable::C_END_DATE]), 'd/m/Y');
                $data[ActivityTimingDetailTable::C_BREAK]      = number_format($row[ActivityTimingDetailTable::C_BREAK],2);
             }else{
                $data[ActivityTimingDetailTable::C_DATE]       = $row[ActivityTimingDetailTable::C_DATE];
                 $data[ActivityTimingDetailTable::C_END_DATE]   = $row[ActivityTimingDetailTable::C_END_DATE];
                 $data[ActivityTimingDetailTable::C_BREAK]      = $row[ActivityTimingDetailTable::C_BREAK];
             }
                 
             $data[ActivityTimingDetailTable::C_PHASE]      = $row[ActivityTimingDetailTable::C_PHASE];
             $data[ActivityTimingDetailTable::C_TOTAL_AFT_BREAK] 		= $row[ActivityTimingDetailTable::C_TOTAL_AFT_BREAK];
             $data[ActivityTimingDetailTable::C_TOTAL_OVERRIDE] 		= $row[ActivityTimingDetailTable::C_TOTAL_OVERRIDE];

             $list[] = $data;
         	unset($data);

        	}
    	}
    	return $list;

    }
    
    function getServiceReportCode($job_assignment_detail_id){
        
        $sql  = "SELECT distinct AH.".ActivityHeaderTable::C_CODE." FROM " .ActivityTimingDetailTable::C_TABLE ." TT ";
        $sql .= " LEFT OUTER JOIN ".ActivityHeaderTable::C_TABLE." AH ";
        $sql .= " ON (TT.".ActivityTimingDetailTable::C_ID." = AH.".ActivityHeaderTable::C_ID.") ";
        $sql .= " WHERE TT." . ActivityTimingDetailTable::C_ASSIGN_ID . " = " . $job_assignment_detail_id. " ";

        $rows   = $this->db->getTable($sql);
        $return = "";
        $sep    = "";
        $first  = "No.: ";
        if(!is_null($rows) && count($rows)>0){
            foreach ($rows as $row) {
                if($row[ActivityHeaderTable::C_CODE] == ''){
                    $row[ActivityHeaderTable::C_CODE] = "";
                }

                $return .= $first. $sep. $row[ActivityHeaderTable::C_CODE];
                $sep    = ", ";
                $first  = "";
            }

            return $return;

        }else{

            return "false";
        }
    }

    function getTimeTracker($id){
        $sql = "SELECT * FROM " ;
        $sql .= ActivityTimingDetailTable::C_TABLE . " WHERE " . ActivityTimingDetailTable::C_ID . " = " . $id;
        $sql .= "ORDER BY " . ActivityTimingDetailTable::C_DATE ." ASC";
        
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }

}
?>