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

class CustomerContactClass extends MasterBase {
    private $dbsun;
    function __construct($db) {
        $this->db       = $db ;
        $this->tbl      = CustomerContactTable::C_TABLE ;
        $this->fldid2    = CustomerContactTable::C_ID ;
        $this->fldid    = CustomerContactTable::C_CONTACT_ID ;
        $this->flddesc  = CustomerContactTable::C_NAME ;
    }
    function __destruct() {
    }
    function getCustomerContact($id, $orderby='', $api=false){       
        if ($api) {
             $sql = "SELECT ".$this->fldid2.",".$this->flddesc." FROM " ;
        } else {
            $sql = "SELECT * FROM " ;
        }
        
        $sql .= CustomerContactTable::C_TABLE . " WHERE " . CustomerContactTable::C_ID . " = " . $id;
        if($orderby != "")
            $sql .= " ORDER BY " . $orderby;
        
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }    
    function getCustomerContactByContactId($id, $contactId){
        $sql = "SELECT * FROM " . CustomerContactTable::C_TABLE . " WHERE " . CustomerContactTable::C_ID . " = " . $id . 
                " AND " . CustomerContactTable::C_CONTACT_ID . " = " . $contactId;
        
        $rows = $this->db->getTable($sql);
        
        if(!is_null($rows) && count($rows) > 0){
            return $rows[0];
        }
        
        return null;
    }
    function deleteCustomerContactByContactId($id, $contactId){
            $sql = "DELETE FROM " . CustomerContactTable::C_TABLE . 
                    " WHERE " . CustomerContactTable::C_ID . " = " . $id . 
                    " AND " . CustomerContactTable::C_CONTACT_ID . " = " . $contactId;
        
        return $this->db->executeSql($sql);
    }

    function getCustomerByName($custId, $name){
        $sql = "SELECT * FROM " . CustomerContactTable::C_TABLE . 
                " WHERE " . CustomerContactTable::C_ID . " = " . $custId .
                " AND " . CustomerContactTable::C_NAME . " LIKE '%" . $name . "%'";

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

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

        return null;
    }
    
    function deleteCustomerContact($id){
            $sql = "DELETE FROM " . CustomerContactTable::C_TABLE . 
                    " WHERE " . CustomerContactTable::C_ID . " = " . $id;
        
        return $this->db->executeSql($sql);
    }

    function getCustomerContactbyDesignation($custId) {
        $sql = "SELECT * FROM " ;
        $sql .= CustomerContactTable::C_TABLE . " WHERE " . CustomerContactTable::C_ID . " = " . $custId;
        $sql .= " AND (" . CustomerContactTable::C_DESIGNATION . " LIKE '%Project Sponsor%'";
        $sql .= " OR " . CustomerContactTable::C_DESIGNATION . " LIKE '%Project Advisor%'";
        $sql .= " OR " . CustomerContactTable::C_DESIGNATION . " LIKE '%Project Manager%'";
        $sql .= " OR " . CustomerContactTable::C_DESIGNATION . " LIKE '%Finance%' )";

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

        if(!is_null($rows) && count($rows) > 0){
            $datas = [];
            $datas['project_sponsor'] = " ";
            $datas['project_advisor'] = " ";
            $datas['project_manager'] = " ";
            $datas['finance'] = " ";

            foreach ($rows as $row) {
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'Project Sponsor') !== false) {
                    $datas['project_sponsor'] = $row[CustomerContactTable::C_NAME];
                }
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'Project Advisor') !== false) {
                    $datas['project_advisor'] = $row[CustomerContactTable::C_NAME];
                }
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'Project Manager') !== false) {
                    $datas['project_manager'] = $row[CustomerContactTable::C_NAME];
                }
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'Finance') !== false) {
                    $datas['finance'] = $row[CustomerContactTable::C_NAME];
                }
            }
            return $datas;
        }
        
        return null;
    }

    function getCustomerFinanceAndIT($custId) {
        $sql = "SELECT TOP 4 * FROM " ;
        $sql .= CustomerContactTable::C_TABLE . " WHERE " . CustomerContactTable::C_ID . " = " . $custId;
        $sql .= " AND (" . CustomerContactTable::C_DESIGNATION . " LIKE '%Finance%'";
        $sql .= " OR " . CustomerContactTable::C_DESIGNATION . " LIKE '%IT%' )";
        $sql .= " ORDER BY " . CustomerContactTable::C_CONTACT_ID . " ASC";

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

        if(!is_null($rows) && count($rows) > 0){
            $datas = [];
            $datas['it'] = "";
            $datas['finance'] = "";
            foreach ($rows as $row) {
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'Finance') !== false) {
                    if($datas['finance']=="") {
                        $datas['finance'] = $row[CustomerContactTable::C_NAME];
                    } else {
                        $datas['finance2'] = $row[CustomerContactTable::C_NAME];
                    }
                }
                if(strpos($row[CustomerContactTable::C_DESIGNATION], 'IT') !== false) {
                    if($datas['it']=="") {
                        $datas['it'] = $row[CustomerContactTable::C_NAME];
                    } else {
                        $datas['it2'] = $row[CustomerContactTable::C_NAME];
                    }
                }
            }
            Log::write(print_r($datas,true));
            return $datas;
        }
        
        return null;
    }
}
?>