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

class JobQuotationTaskClass extends MasterBase {
    function __construct($db) {
        $this->db       = $db ;
        $this->tbl      = JobQuotationTaskTable::C_TABLE ;
        $this->fldid    = JobQuotationTaskTable::C_ID ;
        $this->fldid2   = JobQuotationTaskTable::C_DETAIL_ID ;
    }

    function __destruct() {
    }
    function deleteTask($id){
        $sql = "delete from " . JobQuotationTaskTable::C_TABLE 
        . " where " . $this->db->fieldParam(JobQuotationTaskTable::C_ID) ;

        $params = array() ;
        $params[] = $this->db->valueParam(JobQuotationTaskTable::C_ID,$id) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }

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

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

    function getAssignmentDetailByUserId($id,$userId,$assignId,$assignDate,$startTime,$totalHours){
        $sql = "SELECT * FROM " ;
        $sql .= JobQuotationTaskTable::C_TABLE . " D " ;
        $sql .= "LEFT JOIN " . JobAssignmentTable::C_TABLE . " H ON (H." . JobAssignmentTable::C_ID . " = D." . JobQuotationTaskTable::C_ID .") ";
        $sql .= " WHERE H." . JobAssignmentTable::C_JOB_CENTRE_ID . " = " . $id;
        $sql .= " AND D." . JobQuotationTaskTable::C_AGENT . " = " . $userId;
        $sql .= " AND H." . JobQuotationTaskTable::C_ID . " = " . $assignId;
        $sql .= " AND D." . JobQuotationTaskTable::C_DATE . " = '" . $assignDate . "'";
        $sql .= " AND D." . JobQuotationTaskTable::C_TIME . " = '" . $startTime . "'";
        $sql .= " AND D." . JobQuotationTaskTable::C_HOURS . " = '" . $totalHours . "'";
        
        $rows = $this->db->getTable($sql);

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

    function showJobCenterTask($id){
        $sql = "SELECT D.* FROM " ;
        $sql .= JobQuotationTaskTable::C_TABLE . " D " ;
        $sql .= "LEFT JOIN " . JobAssignmentTable::C_TABLE . " H ON (H." . JobAssignmentTable::C_ID . " = D." . JobQuotationTaskTable::C_ID .") ";
        $sql .= "LEFT JOIN " . JobCentreTable::C_TABLE . " C ON (H." . JobAssignmentTable::C_JOB_CENTRE_ID . " = C." . JobCentreTable::C_ID .") ";
        $sql .= "WHERE H." . JobAssignmentTable::C_JOB_CENTRE_ID . " = " . $id;

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

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

    function getAgentSchedule($month,$year,$agent){
        $sql = "SELECT a.*,u.".UserTable::C_FULL_NAME.",u.".UserTable::C_NAME." FROM " ;
        $sql .= JobQuotationTaskTable::C_TABLE ." a ";
        $sql .= " left outer join ".UserTable::C_TABLE ." u ";
        $sql .= " on ( u.".UserTable::C_ID ." = a.".JobQuotationTaskTable::C_AGENT ." ) ";
        $sql .= " WHERE MONTH(a." . JobQuotationTaskTable::C_DATE . ") = " . $month ;
        $sql .= " AND YEAR(a." . JobQuotationTaskTable::C_DATE . ") = " . $year ;

        if($agent!=0)
            $sql .= " AND a." . JobQuotationTaskTable::C_AGENT . " = " . $agent;
        
        $sql .= " ORDER BY a.".    JobQuotationTaskTable::C_DATE." ASC";
        $rows = $this->db->getTable($sql);

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

    function getHeaderbyDetailId($detalId){
        $sql = " SELECT " . JobQuotationTaskTable::C_ID . "  FROM " . JobQuotationTaskTable::C_TABLE . 
                " WHERE " . JobQuotationTaskTable::C_DETAIL_ID . " = " . $detalId;
        
        $rows = $this->db->getTable($sql);

        return $rows[0][JobQuotationTaskTable::C_ID];
    }



    function deleteDetail(){
        $sql = " DELETE FROM " . JobQuotationTaskTable::C_TABLE . " " .
                " WHERE " . JobQuotationTaskTable::C_ID . " = 0";
        
        $this->db->executeSql($sql);
    }


    function updateRecordByDetailId($update='',$detailId){
        $sql = " UPDATE " . JobQuotationTaskTable::C_TABLE . " " .
                " SET " . $update .
                " WHERE " . JobQuotationTaskTable::C_DETAIL_ID . " = " . $detailId;
        
        $this->db->executeSql($sql);
    }

    function getJobAssignmentListMobile($filter='',$orderby=''){
        $sql = "SELECT C." . JobCentreTable::C_JOB_NUMBER . ", C." . JobCentreTable::C_JOB_NAME . ", C." . JobCentreTable::C_JOB_TYPE . ", C." . JobCentreTable::C_PRODUCT_TYPE . ", C." . JobCentreTable::C_COY_ID . " AS COYID, C." . JobCentreTable::C_CUST_ID . ", D.* ";
        $sql .= "FROM " . JobQuotationTaskTable::C_TABLE . " D ";
        $sql .= "LEFT JOIN " . JobAssignmentTable::C_TABLE . " H ON (H." . JobAssignmentTable::C_ID . " = D." . JobQuotationTaskTable::C_ID .") ";
        $sql .= "LEFT JOIN " . JobCentreTable::C_TABLE . " C ON (H." . JobAssignmentTable::C_JOB_CENTRE_ID . " = C." . JobCentreTable::C_ID .") ";

        if($filter != ''){
            $sql .= "WHERE ".$filter;
        }

        if($orderby != ''){
            $sql .= " ORDER BY ".$orderby;
        }
		
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }

    function getRecordByDetailId($detalId){
        $sql = "SELECT * FROM " .$this->tbl ;
        $sql .= " WHERE " . JobQuotationTaskTable::C_DETAIL_ID . " = " . $detalId;
        
        $rows = $this->db->getTable($sql);

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

    function getTotalSummary($job_center_id,$status ='',$agent_status = ''){
        $sql = "SELECT ".JobQuotationTaskTable::C_DETAIL_ID." FROM " .$this->tbl ;
        $sql .= " WHERE " . JobQuotationTaskTable::C_ID . " = " . $job_center_id;

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

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

    function getJobCentreDetail($filter='',$orderby=''){
        $sql = "SELECT C.*, D.* ";
        $sql .= "FROM " . JobQuotationTaskTable::C_TABLE . " D ";
        $sql .= "LEFT JOIN " . JobAssignmentTable::C_TABLE . " H ON (H." . JobAssignmentTable::C_ID . " = D." . JobQuotationTaskTable::C_ID .") ";
        $sql .= "LEFT JOIN " . JobCentreTable::C_TABLE . " C ON (H." . JobAssignmentTable::C_JOB_CENTRE_ID . " = C." . JobCentreTable::C_ID .") ";

        if($filter != ''){
            $sql .= "WHERE ".$filter;
        }

        if($orderby != ''){
            $sql .= " ORDER BY ".$orderby;
        }
        
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }
}
?>