<?php
include (PATH_CONTROLLERS . "Common.php") ;
include (PATH_MODELS . "admin/DepartmentGroupClass.php") ;
include (PATH_MODELS . "admin/SoftwareClass.php") ;
include (PATH_MODELS . "customer/CustomerClass.php") ;
require_once (PATH_MODELS . "customer/CustomerContactClass.php") ;
require_once (PATH_MODELS . "customer/CustomerBranchClass.php") ;
require_once (PATH_MODELS . "customer/CustomerPolicyClass.php") ;
require_once (PATH_MODELS . "customer/CustomerSoftwareClass.php");

class CustomerEntry extends GLBase {
	private $type = "" ;
	private $user ;

	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
	}
	function __destruct() {
		unset($this->user) ;
	}
	public function processRequest($params) {

		$this->type = REQ_VIEW ;
		try {
			$this->db->open() ;
			$this->loadSetting() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			if (!$this->checkSessionTimeout($this->type)) {
				$this->db->close();
				return false;
			}

			switch ($this->type) {
				case Command::CustomerEntryView:
					$this->getView($params) ;
					break ;
				case REQ_GET:
					$this->getCustomerEntryRecord($params);
					break;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}

	private function getView($params) {
		ob_start() ;
		include (PATH_VIEWS . "customer/CustomerEntryView.php") ;

		$action_menu = "";

		$var_screen = str_replace("[index]",$params['index'], Util::minifyHtml(ob_get_clean())) ;
        $dataReturn=[];
		$dataReturn['htmlContent'] = $var_screen;
		$dataReturn['action_menu'] = $action_menu;
        $dataReturn['headTitle']  = "Customer Entry" ;
		$this->sendJsonResponse(Status::Ok,"Customer Entry Info",$dataReturn,$this->type);
	}

	private function getCustomerEntryRecord($params) {
        ob_start();

        $id             = $params['id'];
        $clsCustomer    = new CustomerClass($this->db);
        $clsContact     = new CustomerContactClass($this->db);
        $clsSoftware    = new CustomerSoftwareClass($this->db);
        $clsBranch      = new CustomerBranchClass($this->db);
        $clsPolicy      = new CustomerPolicyClass($this->db);
        
        $row            = $clsCustomer->getRecord($id);
        $rows_contact   = $clsContact->getCustomerContact($id);
        $rows_software  = $clsSoftware->getCustomerSoftware($id);
        $rows_branch    = $clsBranch->getCustomerBranch($id);
        $rows_policy    = $clsPolicy->getCustomerPolicy($id);

        $datas = array();
        if (!is_null($row) && count($row) > 0) {
            //customer info

            $datas['id']                 = $row[CustomerTable::C_ID];
            $datas['code']               = $row[CustomerTable::C_CODE];
            $datas['status']             = $row[CustomerTable::C_STATUS];
            $datas['name']               = $row[CustomerTable::C_NAME];
            $datas['address']            = $row[CustomerTable::C_ADDRESS];
            $datas['company_reg_no']     = $row[CustomerTable::C_COMPANY_REG_NO];
            $datas['industry']           = $row[CustomerTable::C_INDUSTRY];
            $datas['business_sector']    = $row[CustomerTable::C_BUSINESS_SECTOR];
            $datas['no_people']          = $row[CustomerTable::C_NUM_PEOPLE];
            $datas['sales_turn_over']    = $row[CustomerTable::C_TURN_OVER];
            $datas['sunsystem_ver']      = $row[CustomerTable::C_SUN_VERSION];
            $datas['mssql_ver']          = $row[CustomerTable::C_MSSQL_VERSION];
            $datas['qa_ver']             = $row[CustomerTable::C_QA_VERSION];
            $datas['windows_server_ver'] = $row[CustomerTable::C_WIN_SERVER_VERSION];
            $datas['supporting_partner'] = $row[CustomerTable::C_SUPP_PARTNER];
            $datas['software_info']      = $row[CustomerTable::C_SOFTWARE_INFO];
            $datas['billing_cycle']      = $row[CustomerTable::C_BILLING_CYCLE];
            $datas['remarks']            = $row[CustomerTable::C_REMARKS];
            $datas['account']            = $row[CustomerTable::C_CUS_ACC];
            $datas['telephone']          = $row[CustomerTable::C_TELEPHONE];
            $datas['company_policy']     = $row[CustomerTable::C_COMP_POLICY];
            $datas['product_conver']     = $row[CustomerTable::C_PROD_CONVERSATION];
            $datas['pri_remote_access']  = $row[CustomerTable::C_PRIMARY_REMOTE];
            $datas['sec_remote_access']  = $row[CustomerTable::C_SECOND_REMOTE];
            $datas['pri_video_mesg']     = $row[CustomerTable::C_PRIMARY_MESG];
            $datas['sec_video_mesg']     = $row[CustomerTable::C_SECOND_MESG];

            $contact_list = array();
            if(!is_null($rows_contact) && count($rows_contact) > 0){
                $datas_contact = array();
                foreach($rows_contact as $row_contact){

                    $datas_contact['id']                    = $row_contact[CustomerContactTable::C_CONTACT_ID];
                    $datas_contact['prefer_name']           = $row_contact[CustomerContactTable::C_PREFER_NAME];
                    $datas_contact['dept']                  = $row_contact[CustomerContactTable::C_DEPT];
                    $datas_contact['dept_group']            = $row_contact[DepartmentGroupTable::C_DESC];
                    $datas_contact['dept_group_id']         = $row_contact[CustomerContactTable::C_DEPT_GROUP];
                    $datas_contact['contact_status']        = ($row_contact[CustomerContactTable::C_STATUS] == 0)? "Inactive":"Active";
                    $datas_contact['status']                = $row_contact[CustomerContactTable::C_STATUS];
                    $datas_contact['email']                 = $row_contact[CustomerContactTable::C_EMAIL];
                    $datas_contact['telephone']             = $row_contact[CustomerContactTable::C_TELEPHONE];
                    $datas_contact['mobile']                = $row_contact[CustomerContactTable::C_MOBILE];
                    $datas_contact['full_name']             = $row_contact[CustomerContactTable::C_NAME];
                    $datas_contact['designation']           = $row_contact[CustomerContactTable::C_DESIGNATION];
                    $datas_contact['salutation']            = $row_contact[CustomerContactTable::C_SALUTATION];
                    $datas_contact['job_desc']              = $row_contact[CustomerContactTable::C_JOB_DESC];
                    $datas_contact['report_to']             = $row_contact[CustomerContactTable::C_REPORT_TO];
                    $datas_contact['commun_prefer']         = $row_contact[CustomerContactTable::C_COMM_PREF];
                    $datas_contact['hobbies']               = $row_contact[CustomerContactTable::C_HOBBIES];
                    $datas_contact['character']             = $row_contact[CustomerContactTable::C_CHARACTER];
                    $datas_contact['priority']              = $row_contact[CustomerContactTable::C_PRIORITY];
                    $datas_contact['esupport_finance']      = $row_contact[CustomerContactTable::C_ESUPPORT_FINANCE];
                    $datas_contact['esupport_it']           = $row_contact[CustomerContactTable::C_ESUPPORT_IT];
                    $datas_contact['esupport_project']      = $row_contact[CustomerContactTable::C_ESUPPORT_MANAGEMENT];
                    $datas_contact['esupport_management']   = $row_contact[CustomerContactTable::C_ESUPPORT_PROJECT];

                    array_push($contact_list, $datas_contact);
                }
            }
            
            $software_list = array();
            if(!is_null($rows_software) && count($rows_software) > 0){
                $datas_software = array();
                foreach($rows_software as $row_software){
                    $datas_software['id']           = $row_software[CustomersoftwareTable::C_CUS_SOFTWARE_ID];  
                    $datas_software['code']         = $row_software[CustomersoftwareTable::C_SOFTWARE];  
                    $datas_software['type']         = $row_software[CustomersoftwareTable::C_TYPE];  
                    $datas_software['version']      = $row_software[CustomersoftwareTable::C_VERSION];  
                    $datas_software['user']         = $row_software[CustomersoftwareTable::C_USER];
                    $datas_software['live_date']    = ($row_software[CustomerSoftwareTable::C_LIVE_DATE] != NULL_DATE . ".000") ? date_create($row_software[CustomersoftwareTable::C_LIVE_DATE])->format('Y-m-d') : '';
                    $datas_software['renew_date']   = ($row_software[CustomerSoftwareTable::C_RENEW_DATE] != NULL_DATE . ".000") ? date_create($row_software[CustomersoftwareTable::C_RENEW_DATE])->format('Y-m-d') : '';

                    array_push($software_list, $datas_software);
                }
            }

            $branch_list = array();
            if(!is_null($rows_branch) && count($rows_branch) > 0){
                $datas_branch = array();
                foreach($rows_branch as $row_branch){
                    $array_branch                       = ['', 'HQ', 'Branch'];

                    $datas_branch['id']                 = $row_branch[CustomerBranchTable::C_BRANCH_ID];  
                    $datas_branch['code']               = $row_branch[CustomerBranchTable::C_CODE];  
                    $datas_branch['name']               = $row_branch[CustomerBranchTable::C_NAME];  
                    $datas_branch['branch_type_name']   = $array_branch[$row_branch[CustomerBranchTable::C_TYPE]];
                    $datas_branch['branch_type']        = $row_branch[CustomerBranchTable::C_TYPE];
                    $datas_branch['telephone']          = $row_branch[CustomerBranchTable::C_TELEPHONE];
                    $datas_branch['add']                = $row_branch[CustomerBranchTable::C_ADD];
                    $datas_branch['web']                = $row_branch[CustomerBranchTable::C_WEB];
                    $datas_branch['contact']            = $row_branch[CustomerBranchTable::C_CONTACT];
                    $datas_branch['fax']                = $row_branch[CustomerBranchTable::C_FAX];

                    array_push($branch_list, $datas_branch);
                }
            }
           
            $policy_list = array();
            if(!is_null($rows_policy) && count($rows_policy) > 0){
                $data_policy = array();
                foreach($rows_policy as $row_policy){
                    $data_policy['id']                  = $row_policy[CustomerPolicyTable::C_POLICY_ID] ;
                    $data_policy['desc']                = $row_policy[CustomerPolicyTable::C_DESC] ;
                    $data_policy['filename']            = $row_policy[CustomerPolicyTable::C_ATTACH];

                    array_push($policy_list, $data_policy);
                }
            }
        }
        include(PATH_VIEWS . "customer/CustomerEntryView.php");

        $var_screen = str_replace("[index]", $params['index'], Util::minifyHtml(ob_get_clean()));

        $dataReturn = array();
        $dataReturn['htmlContent']  = $var_screen;
        $dataReturn['headTitle']    = "Customer Entry";
        $dataReturn['contactData']  = $contact_list;
        $dataReturn['softwareData'] = $software_list;
        $dataReturn['branchData']   = $branch_list;
        $dataReturn['policyData']   = $policy_list;
        $dataReturn['headerData']   = $datas;

        $this->sendJsonResponse(Status::Ok, "Customer Entry", $dataReturn, $this->type);
        unset($clsCustomer);
        unset($clsContact);
        unset($clsBranch);
	}

    private function getDepartmentGroupOption(){
        $vls  = array();

        try {
            $cls  = new DepartmentGroupClass($this->db);
            $rows = $cls->getTable(); 
            $vls[] = array('code'=>0,'desc'=>"--- Select Department Group ---") ;
            if(!is_null($rows) && count($rows) > 0) {
                foreach ($rows as $row) {
                    $vls[] = array('code'=>$row[DepartmentGroupTable::C_ID],'desc'=>$row[DepartmentGroupTable::C_DESC]) ;
                }
            }
        } catch (Exception $e) {
			Log::write($e->getMessage());
			return $vls;
		}
		return Util::createOptionValue($vls);
    }

    private function getSoftwareOption(){
        $vls  = array();

        try {
            $cls  = new SoftwareClass($this->db);
            $rows = $cls->getTable(); 
            $vls[] = array('code'=>0,'desc'=>"--- Select Software ---") ;
            if(!is_null($rows) && count($rows) > 0) {
                foreach ($rows as $row) {
                    $vls[] = array('code'=>$row[SoftwareTable::C_ID],'desc'=>$row[SoftwareTable::C_DESC]) ;
                }
            }
        } catch (Exception $e) {
			Log::write($e->getMessage());
			return $vls;
		}
		return Util::createOptionValue($vls);
    }
}
