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

class CustomerClass extends MasterBase {
    private $dbsun;
	function __construct($db, $dbsun = null) {
		$this->db 		= $db ;
		$this->tbl 		= CustomerTable::C_TABLE ;
		$this->fldid 	= CustomerTable::C_ID ;
		$this->flddesc 	= CustomerTable::C_NAME ;
		$this->fldorg 	= CustomerTable::C_ORG_ID ;
	}
	function __destruct() {
	}
    function getCustomerByCode($custCode){
        $sql = "SELECT * FROM " . CustomerTable::C_TABLE . " ";
        $sql .= "WHERE " . CustomerTable::C_CODE . " = '" . $custCode . "' ";

        $rows = $this->db->getTable($sql);
        
        if (is_null($rows) || count($rows) == 0)
            return null ;
        else 
            return $rows[0] ; 
    }
    function getCustomerList($namefilter = '', $statusfilter = 'all', $orderby = ''){
        $sql = "SELECT * FROM " . CustomerTable::C_TABLE . " ";
        $sql .= "WHERE " . CustomerTable::C_NAME . " LIKE '%" . $namefilter . "%' ";
        
        if($statusfilter == 'active'){
            $sql .= "AND " . CustomerTable::C_STATUS . " = 1 ";
        } else if($statusfilter == 'non-active'){
            $sql .= "AND " . CustomerTable::C_STATUS . " = 0 ";
        }

        if($orderby != "")
            $sql .= " ORDER BY " . $orderby;
            
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }
    function getCustomerListSuggest() {
        $sql = "select distinct ".CustomerTable::C_NAME." from " . CustomerTable::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 " . CustomerTable::C_TABLE;
        $sql .= " WHERE " . CustomerTable::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'=>CustomerType::License,'desc'=>$row[CustomerTable::C_LIC_NAME]) ;
                $vls[] = array ('code'=>CustomerType::Finance,'desc'=>$row[CustomerTable::C_FIN_NAME]) ;
                $vls[] = array ('code'=>CustomerType::IT,'desc'=>$row[CustomerTable::C_IT_NAME]) ;
                $vls[] = array ('code'=>CustomerType::Manager,'desc'=>$row[CustomerTable::C_PROJ_NAME]) ;
            }           
        }

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

        return $datas;
    }

}
?>