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

class CustomerContactClass extends MasterBase {
    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 getActiveCustomerContact($id){
        $sql = "Select * FROM " . CustomerTable::C_TABLE . " cus " .
                    " LEFT JOIN " . CustomerContactTable::C_TABLE . " con ON cus." . CustomerTable::C_ID . " = con." . CustomerContactTable::C_ID . 
                " WHERE con." . CustomerTable::C_ID . " = " . $id . " AND " . CustomerContactTable::C_STATUS . " = 1";

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

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

        return null;
    }
    function getCustomerContact($id){
        $sql  = "Select * FROM " . CustomerContactTable::C_TABLE . " cus " .
                " LEFT JOIN " . DepartmentGroupTable::C_TABLE . " dept ON cus." . CustomerContactTable::C_DEPT_GROUP . " = dept." . DepartmentGroupTable::C_ID .
                " WHERE " . CustomerContactTable::C_ID . " = " . $id;
        $sql .= " ORDER BY " . CustomerContactTable::C_NAME;

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

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

        return null;
    }
    function getCustomerContactList($id, $namefilter = '', $orderby = ''){
        $sql = "SELECT * FROM " . $this->tbl . " ";
        $sql .= "WHERE " . $this->flddesc . " LIKE '%" . $namefilter . "%' ";
        $sql .= "AND " . $this->fldid2 . " = " . $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,$active=""){
        $sql = "SELECT * FROM " . CustomerContactTable::C_TABLE . " WHERE " . CustomerContactTable::C_ID . " = " . $id .
                " AND " . CustomerContactTable::C_CONTACT_ID . " = " . $contactId;

        if($active==""){
            $sql .= " AND " .CustomerContactTable::C_STATUS. "= 1 ";
        }
        $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 deleteCustomerContact($id){
        $sql = "DELETE FROM " . CustomerContactTable::C_TABLE .
                    " WHERE " . CustomerContactTable::C_ID . " = " . $id;

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

    function getIdByContactName($cust_id, $desc="") {
        // $sql = "select " . $this->fldid . " from " . $this->tbl
        // . " where " . CustomerContactTable::C_PREFER_NAME
        // . " and " . $this->db->fieldParam($this->fldid2);

        // $params = array();
        // $params[] = $this->db->valueParam(CustomerContactTable::C_PREFER_NAME, strtolower($desc));
        // $params[] = $this->db->valueParam($this->fldid2, $cust_id);

        // Log::write(print_r($sql, true));
        // Log::write(print_r($params, true));
        
        // $rows = $this->db->getRow($sql, $params);

        $sql = "SELECT " . $this->fldid . " FROM " . $this->tbl . 
                " WHERE " . $this->fldid2 . " = " . $cust_id;

        if($desc != "") {
            $sql .= " AND " . CustomerContactTable::C_PREFER_NAME . " = '" . strtolower($desc) . "' ";
        }
                
        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0)
            return "";
        else
            return $rows[0][$this->fldid];
    }

    public function getExistingRecords($cust_id, $contact_name)
    {
        $sql  = "SELECT * FROM " . CustomerContactTable::C_TABLE .
            " WHERE " . CustomerContactTable::C_ID . " = '" . $cust_id . "' " .
            " AND " . CustomerContactTable::C_NAME . " = '" . $contact_name . "' ";

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

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

    public function updateStatusSendEmail($id){
		$sql = "UPDATE " . CustomerContactTable::C_TABLE . 
                " SET " . CustomerContactTable::C_STATUS_SEND_EMAIL . " = 1" .
                " WHERE " . CustomerContactTable::C_CONTACT_ID . " = " . $id ;
        $this->db->executeSql($sql);
	}

    function updateStatusSendWhatsapp($id){
		$sql = "UPDATE " . CustomerContactTable::C_TABLE . 
                " SET " . CustomerContactTable::C_STATUS_SEND_WA . " = 1" . 
                " WHERE " . CustomerContactTable::C_NAME . " = " . $id ;

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

    public function updateReferenceID($id, $ref_id){
		$sql = "UPDATE " . CustomerContactTable::C_TABLE . 
                " SET " . CustomerContactTable::C_REF_ID . " = '" . $ref_id . "'" .
                " WHERE " . CustomerContactTable::C_CONTACT_ID . " = " . $id ;
        $this->db->executeSql($sql);
	}
}
