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

class CustomerLocationClass extends MasterBase {
    private $dbsun;
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= CustomerLocationTable::C_TABLE ;
        $this->fldid    = CustomerLocationTable::C_ID ;
		$this->flddesc 	= CustomerLocationTable::C_NAME ;
	}
	function __destruct() {
    }
    
    function getNameByAssignId($assignId) {
        $sql  = "SELECT CL." . CustomerLocationTable::C_CONTACT_NAME . " ";
        $sql .= "FROM " . CustomerLocationTable::C_TABLE . " CL ";
        $sql .= "LEFT JOIN " . JobCentreTable::C_TABLE . " J ON (J.". JobCentreTable::C_CUST_LOC_ID . " = CL." . CustomerLocationTable::C_ID .") ";
        $sql .= "LEFT JOIN " . JobAssignmentTable::C_TABLE . " JA ON (JA.". JobAssignmentTable::C_JOB_CENTRE_ID . " = J." . JobCentreTable::C_ID .") ";
        $sql .= "WHERE JA." . JobAssignmentTable::C_ID . " = " . $assignId;

        $rows = $this->db->getTable($sql);
        
		if(!is_null($rows) && count($rows) > 0)
			return $rows[0][CustomerLocationTable::C_CONTACT_NAME];
		else
			return "";
    }

    function getCustomerLocationList($customerfilter = ''){
        $sql  = "SELECT CL.*, C." . CustomerTable::C_NAME. " ";
        $sql .= "FROM " . CustomerLocationTable::C_TABLE . " CL ";
        $sql .= "LEFT JOIN " . CustomerTable::C_TABLE . " C ON (C.". CustomerTable::C_ID . " = CL." . CustomerLocationTable::C_CUST_ID .") ";

        if($customerfilter!='') {
            $sql .= "WHERE C." . CustomerTable::C_NAME . " LIKE '%" . $customerfilter . "%' ";
        }

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

    function getLocByCustId($custId){
        $sql  = "SELECT * ";
        $sql .= "FROM " . CustomerLocationTable::C_TABLE . " ";
        $sql .= "WHERE " . CustomerLocationTable::C_CUST_ID . " = " . $custId;

        $rows = $this->db->getTable($sql);
        return $rows[0];
    }
}
?>