<?php
require_once (PATH_TABLES . "suspect/SuspectTable.php") ;
require_once (PATH_TABLES . "suspect/SuspectContactTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class SuspectClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= SuspectTable::C_TABLE ;
		$this->fldid 	= SuspectTable::C_ID ;
		$this->flddesc 	= SuspectTable::C_NAME ;
		// $this->fldcode 	= SuspectTable::C_CODE ;
		$this->fldorg 	= SuspectTable::C_ORG_ID ;
	}
	function __destruct() {
	}

    function getSuspectList($params){
        $industry           = (isset($params['industry']))? $params['industry'] : '';
        $business_sector    = (isset($params['business_sector']))? $params['business_sector'] : '';
        $total_employee     = (isset($params['total_employee']))? $params['total_employee'] : '';
        $company_name       = (isset($params['company_name']))? $params['company_name'] : '';
        $telephone          = (isset($params['telephone']))? $params['telephone'] : '';

        $sql    = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
        $filter = "";
        
        if($industry != ""){
            $filter = ($filter != "") ? " AND ". SuspectTable::C_INDUSTRY . " like '%" . $industry. "%' " :
                                        " WHERE " . SuspectTable::C_INDUSTRY . " like '%" . $industry. "%' ";
        }
        if($business_sector != ""){
            $filter = ($filter != "") ? " AND ". SuspectTable::C_BUSINESS_SECTOR . " like '%" . $business_sector. "%' " :
                                        " WHERE " . SuspectTable::C_BUSINESS_SECTOR . " like '%" . $business_sector. "%' ";
        }
        if($total_employee != ""){
            $filter = ($filter != "") ? " AND ". SuspectTable::C_TOTAL_EMPLOYEE . " like '%" . $total_employee. "%' " :
                                        " WHERE " . SuspectTable::C_TOTAL_EMPLOYEE . " like '%" . $total_employee. "%' ";
        }
        if($company_name != ""){
            $filter = ($filter != "") ? " AND ". SuspectTable::C_NAME . " like '%" . $company_name. "%' " :
                                        " WHERE " . SuspectTable::C_NAME . " like '%" . $company_name. "%' ";
        }
        if($telephone != ""){
            $filter = ($filter != "") ? " AND ". SuspectTable::C_TELEPHONE . " like '%" . $telephone. "%' " :
                                        " WHERE " . SuspectTable::C_TELEPHONE . " like '%" . $telephone. "%' ";
        }

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

        if (is_null($rows) || count($rows) == 0)
            return null ;
        else
            return $rows;
    }
    
    function getSuspectActionList($params){
        $industry           = (isset($params['industry']))? $params['industry'] : '';
        $business_sector    = (isset($params['business_sector']))? $params['business_sector'] : '';
        $total_employee     = (isset($params['total_employee']))? $params['total_employee'] : '';
        $company_name       = (isset($params['company_name']))? $params['company_name'] : '';
        $telephone          = (isset($params['telephone']))? $params['telephone'] : '';

        $sql    = "SELECT * FROM " . SuspectTable::C_TABLE . " as sus " .
                  " LEFT JOIN " . SuspectContactTable::C_TABLE . " as sus_cont ";
        $filter = " WHERE sus." . SuspectTable::C_ACTIVE_STATUS . " = " . SuspectStatus::Assigned . 
                  " AND sus_cont." . SuspectContactTable::C_ASSIGN_TO . " = " .$_SESSION[SE_USERID]. " AND " . SuspectContactTable::C_ACTIVE_STATUS . " = 0";
        
        if($industry != ""){
            $filter = " AND ". SuspectTable::C_INDUSTRY . " like '%" . $industry. "%' ";
        }
        if($business_sector != ""){
            $filter = " AND ". SuspectTable::C_BUSINESS_SECTOR . " like '%" . $business_sector. "%' ";
        }
        if($total_employee != ""){
            $filter = " AND ". SuspectTable::C_TOTAL_EMPLOYEE . " like '%" . $total_employee. "%' ";
        }
        if($company_name != ""){
            $filter = " AND ". SuspectTable::C_NAME . " like '%" . $company_name. "%' ";
        }
        if($telephone != ""){
            $filter = " AND ". SuspectTable::C_TELEPHONE . " like '%" . $telephone. "%' ";
        }

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

        if (is_null($rows) || count($rows) == 0)
            return null ;
        else
            return $rows;
    }

    // public function getExistingRecords($cust_code) {
    //     $sql  = "SELECT * FROM " . SuspectTable::C_TABLE .
    //         " WHERE " . SuspectTable::C_TELEPHONE . " = '" . $cust_code . "' ";

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

    //     if (is_null($rows) || count($rows) == 0) {
    //         return null;
    //     } else {
    //         return $rows;
    //     }
    // }
    // function getActiveAndConvertedSuspect(){
    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
    //     $sql .= "WHERE " . SuspectTable::C_STATUS . " IN ('" . SuspectStatus::Active . "', '" . SuspectStatus::Converted . "') ";

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

    //     if (is_null($rows) || count($rows) == 0)
    //         return null ;
    //     else
    //         return $rows;
    // }
    // function getActiveSuspect(){
    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
    //     $sql .= "WHERE " . SuspectTable::C_STATUS . " = '" . SuspectStatus::Active . "' ";

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

    //     if (is_null($rows) || count($rows) == 0)
    //         return null ;
    //     else
    //         return $rows;
    // }

    // function getSuspectByCode($custCode){
    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
    //     $sql .= "WHERE " . SuspectTable::C_CODE . " = '" . $custCode . "' ";

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

    //     if (is_null($rows) || count($rows) == 0)
    //         return null ;
    //     else
    //         return $rows[0] ;
    // }
    // function getSuspectList($namefilter = '', $statusfilter = 'all', $orderby = ''){
    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
    //     $sql .= "WHERE " . SuspectTable::C_NAME . " LIKE '%" . $namefilter . "%' ";

    //     if($statusfilter == 'active'){
    //         $sql .= "AND " . SuspectTable::C_STATUS . " = 1 ";
    //     } else if($statusfilter == 'non-active'){
    //         $sql .= "AND " . SuspectTable::C_STATUS . " = 0 ";
    //     } else if($statusfilter == 'converted'){
    //         $sql .= "AND " . SuspectTable::C_STATUS . " = 2 ";
    //     }

    //     if($orderby != "")
    //         $sql .= " ORDER BY " . $orderby;

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

    //     return $rows;
    // }
    // function getSuspectListSuggest() {
    //     $sql = "select distinct ".SuspectTable::C_NAME." from " . SuspectTable::C_TABLE ." ";

    //     $rows = $this->db->getTable($sql) ;
    //     // Log::write(print_r($rows,true));
    //     if (is_null($rows) || count($rows) == 0)
    //         return null ;
    //     else
    //         return $rows ;
    // }

    // function getContactList($id)
    // {
    //     $datas = array() ;

    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE;
    //     $sql .= " WHERE " . SuspectTable::C_ID . " = " . $id;
    //     $rows = $this->db->getTable($sql);

    //     $vls   = array() ;
    //     $vls[] = array ('code'=>"0",'desc'=>"-- Select a Contact --") ;

    //     if(!is_null($rows) && count($rows) > 0) {
    //         foreach ($rows as $row) {
    //             $vls[] = array ('code'=>SuspectType::License,'desc'=>$row[SuspectTable::C_LIC_NAME]) ;
    //             $vls[] = array ('code'=>SuspectType::Finance,'desc'=>$row[SuspectTable::C_FIN_NAME]) ;
    //             $vls[] = array ('code'=>SuspectType::IT,'desc'=>$row[SuspectTable::C_IT_NAME]) ;
    //             $vls[] = array ('code'=>SuspectType::Manager,'desc'=>$row[SuspectTable::C_PROJ_NAME]) ;
    //         }
    //     }

    //     $datas = Util::createOptionValue($vls) ;

    //     return $datas;
    // }

    // function getSuspectByAccountId($accId) {
    //     $sql = "SELECT * FROM " . SuspectTable::C_TABLE . " ";
    //     $sql .= "WHERE " . SuspectTable::C_ACCOUNT . " = " . $accId . " ";

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

    //     if (is_null($rows) || count($rows) == 0)
    //         return 0;
    //     else
    //         return $rows[0][SuspectTable::C_ID];
    // }

    // function getSuspectSuggest() {
    //     $sql = "select distinct (" . SuspectTable::C_CODE . "), " . SuspectTable::C_NAME . " from " . SuspectTable::C_TABLE . " ";

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

    //     if (is_null($rows) || count($rows) == 0)
    //         return null;
    //     else
    //         return $rows;
    // }

    // public function getExistingRecords($cust_code) {
    //     $sql  = "SELECT * FROM " . SuspectTable::C_TABLE .
    //         " WHERE " . SuspectTable::C_CODE . " = '" . $cust_code . "' ";

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

    //     if (is_null($rows) || count($rows) == 0) {
    //         return null;
    //     } else {
    //         return $rows;
    //     }
    // }

    // public function updateStatusConvertToCustomer($id) {
    //     $sql  = "UPDATE " . SuspectTable::C_TABLE . " SET " . SuspectTable::C_STATUS . " = 2 ".
    //             " WHERE " . SuspectTable::C_ID . " = " . $id;

    //     $this->db->executeSql($sql);
    // }
}
