<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_LIB . 'LicenseFile.php') ;
require_once (PATH_MODELS . "admin/OrganizationClass.php") ;
require_once (PATH_MODELS . "admin/OrganizationOptions.php") ;

class Setting extends ControllerBase {
	private $type = "" ;
	private $datas = array() ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = "v" ;
		
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_DELETE:
					$this->removeLogo($params) ;
					break ;
				case REQ_VIEW:
					$this->getView() ;
					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 updateRecord($params) {
		$cls = new OrganizationClass($this->db) ;
		$opts = new OrganizationOptions() ;
		
		try {
			$datas = array() ;
			$modifyby = $_SESSION[SE_USERID] ;
			$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
			$ws = $_SERVER['REMOTE_ADDR']; 
			$op = array() ;
			$op[OrganizationOptions::C_CONTACT] = $this->getContactOpt($params) ;
			$op[OrganizationOptions::C_SETTING] = $this->getSettingOpt($params) ;
			$opts->setOption($op) ;
			$datas[] = $this->db->fieldValue(OrganizationTable::C_CODE, $this->getParam($params,'code',"")) ;
			$datas[] = $this->db->fieldValue(OrganizationTable::C_OPTIONS,$opts->getXml());
			$datas[] = $this->db->fieldValue(OrganizationTable::C_WS_ID,$ws) ;
			$datas[] = $this->db->fieldValue(OrganizationTable::C_MODIFY_BY,$modifyby) ;
			$datas[] = $this->db->fieldValue(OrganizationTable::C_MODIFY_DATE,$modifydate) ;
			$cls->updateRecord($_SESSION[SE_ORGID],$datas) ;
			$this->sendJsonResponse(Status::Ok,"System Setting successfully updated to the system.","",$this->type) ;
		} catch (Exception $e) {
			$this->sendJsonResponse(Status::Error,"Sorry, there is a error in updating system setting to the system.","",$this->type) ;
		}
		unset($coy) ;
	}
	private function getContactOpt($params) {
		$op = array() ;
		$op[OrganizationOptions::C_CONT_NAME1] = $this->getParam($params,'name1',"") ;
		$op[OrganizationOptions::C_CONT_NAME2] = $this->getParam($params,'name2',"") ;
		$op[OrganizationOptions::C_CONT_ADDR1] = $this->getParam($params,'addr1',"") ;
		$op[OrganizationOptions::C_CONT_ADDR2] = $this->getParam($params,'addr2',"") ;
		$op[OrganizationOptions::C_CONT_ADDR3] = $this->getParam($params,'addr3',"") ;
		$op[OrganizationOptions::C_CONT_ADDR4] = $this->getParam($params,'addr4',"") ;
		$op[OrganizationOptions::C_CONT_ADDR5] = $this->getParam($params,'addr5',"") ;
		$op[OrganizationOptions::C_CONT_TEL]   = $this->getParam($params,'telno',"") ;
		$op[OrganizationOptions::C_CONT_FAX]   = $this->getParam($params,'faxno',"") ;
		$op[OrganizationOptions::C_CONT_EMAIL] = $this->getParam($params,'email',"");
		$op[OrganizationOptions::C_CONT_WEB]   = $this->getParam($params,'website',"");
		
		return $op ;
	}
	private function getSettingOpt($params) {
// 		var_dump($params);
// 		die;
		
		$op = array() ;
		$op[OrganizationOptions::C_SET_FAIL_COUNT]                      = $this->getParamInt($params,'failcount',3) ;
		$op[OrganizationOptions::C_SET_REF_NO]                          = $this->getParam($params,'refno',"");
		$op[OrganizationOptions::C_SET_LIC]                             = "" ;
		$op[OrganizationOptions::C_EMAIL_SERVER]                        = $this->getParam($params,'mailServer',"") ;
		$op[OrganizationOptions::C_EMAIL_PORT]                          = $this->getParam($params,'mailPort',"") ;
		$op[OrganizationOptions::C_EMAIL_USER]                          = $this->getParam($params,'mailUser',"");
		$op[OrganizationOptions::C_EMAIL_PWD]                           = $this->getParam($params,'mailPwd',"");
		$op[OrganizationOptions::C_EMAIL_ENABLE_AUTHENTICATION] 	 	= $this->getParam($params,'ea',"");
		$op[OrganizationOptions::C_EMAIL_SSL]                           = $this->getParam($params,'ssl',"");
		$op[OrganizationOptions::C_UPLOAD_SIZE_LIMT]                    = $this->getParam($params,'upload_size_limit',"");
		$op[OrganizationOptions::C_BASE_CURRENCY]                       = $this->getParam($params,'base_currency',"");
		$op[OrganizationOptions::C_CLAIM_DAY_LAST]                      = $this->getParam($params,'claim_day_last',"");
		$op[OrganizationOptions::C_ALLOW_DELETE_MASTER]                 = $this->getParam($params,'allow_delete_master',"");
		$op[OrganizationOptions::C_USE_PAYMENT_REFERENCE]               = $this->getParam($params,'use_payment_reference',"");
		$op[OrganizationOptions::C_EMP_CODE_TO_EXPORT]                  = $this->getParam($params,'emp_code_to_export',"");
		$op[OrganizationOptions::C_USE_BU]                  			= $this->getParam($params,'use_bu',"");
		$op[OrganizationOptions::C_START_TIME]                  		= $this->getParam($params,'start_time',"");
		$op[OrganizationOptions::C_END_TIME]                  			= $this->getParam($params,'end_time',"");

		$op[OrganizationOptions::C_USE_REMINDER]                  	= $this->getParam($params,'use_reminder',"");
		$op[OrganizationOptions::C_REMINDER_MESSAGE1]                  	= $this->getParam($params,'reminder_message1',"");
		$op[OrganizationOptions::C_REMINDER_MESSAGE2]                  	= $this->getParam($params,'reminder_message2',"");
		$op[OrganizationOptions::C_REMINDER_MESSAGE3]                  	= $this->getParam($params,'reminder_message3',"");
		$op[OrganizationOptions::C_REMINDER_MESSAGE4]                  	= $this->getParam($params,'reminder_message4',"");
		$op[OrganizationOptions::C_REMINDER_INTERVAL]                  	= $this->getParam($params,'reminder_interval',"");
		$op[OrganizationOptions::C_REMINDER_INTERVAL_UNIT]                  	= $this->getParam($params,'reminder_interval_unit',"");

		$op[OrganizationOptions::C_REMINDER_INTERVAL_APPROVAL]                  	= $this->getParam($params,'reminder_interval_approval',"");
		$op[OrganizationOptions::C_REMINDER_INTERVAL_APPROVAL_UNIT]                  	= $this->getParam($params,'reminder_interval_approval_unit',"");
		return $op ;
	}
	private function getRecord() {
		$cls = new OrganizationClass($this->db) ;
		$row = $cls->getRecord($_SESSION[SE_ORGID]) ;
		$this->datas['name1'] = "";
		$this->datas['name2'] = "" ;
		$this->datas['addr1'] = "" ;
		$this->datas['addr2'] = "";
		$this->datas['addr3'] = "" ;
		$this->datas['addr4'] = "";
		$this->datas['addr5'] = "" ;
		$this->datas['failcount'] = "3";
		$this->datas['code']  = "";
		$this->datas['refno'] = "" ;
		$this->datas['telno'] = "" ;
		$this->datas['faxno'] = "" ;
		$this->datas['mailserver'] 	= "" ;
		$this->datas['mailport'] 	= "" ;
		$this->datas['mailuser'] 	= "" ;
		$this->datas['mailpwd'] 	= "" ;
		$this->datas['ea'] 	= "1" ;
		$this->datas['ssl'] 	= "1" ;
		$this->datas['claim_day_last'] 	= "30" ;
		$this->datas['allow_delete_master'] 	= "" ;
		$this->datas['emp_code_to_export'] 	= "0" ;
		$this->datas['use_bu'] 	= "0" ;
		$this->datas['start_time'] 	= "0" ;
		$this->datas['end_time'] 	= "1" ;

		$this->datas['use_reminder'] 	= "1" ;
		$this->datas['reminder_message1'] 	= "" ;
		$this->datas['reminder_message2'] 	= "" ;
		$this->datas['reminder_message3'] 	= "" ;
		$this->datas['reminder_message4']   = "" ;
 		$this->datas['reminder_interval'] 	= "1" ;
		$this->datas['reminder_interval_unit'] 	= "1" ;

		$this->datas['reminder_interval_approval'] 	= "1" ;
		$this->datas['reminder_interval_approval_unit'] 	= "1" ;

		if (!is_null($row)) {
			if (!is_null($row[OrganizationTable::C_OPTIONS]) && $row[OrganizationTable::C_OPTIONS] != "") {
				$this->datas['code'] = $row[OrganizationTable::C_CODE] ;
				$opt = new OrganizationOptions() ;
				$opt->loadXml($row[OrganizationTable::C_OPTIONS]) ;
				$op = $opt->getOption() ;
				$this->datas['name1']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_NAME1] ;
				$this->datas['name2']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_NAME2] ;
				$this->datas['addr1']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_ADDR1] ;
				$this->datas['addr2']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_ADDR2] ;
				$this->datas['addr3']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_ADDR3] ;
				$this->datas['addr4']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_ADDR4] ;
				$this->datas['addr5']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_ADDR5] ;
				$this->datas['telno']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_TEL] ;
				$this->datas['faxno']                   = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_FAX] ;
				$this->datas['failcount']               = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_SET_FAIL_COUNT] ;
				$this->datas['refno']                   = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_SET_REF_NO] ;
				$this->datas['mailserver']              = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_SERVER] ;
				$this->datas['mailport']                = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_PORT] ;
				$this->datas['mailuser']                = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_USER] ;
				$this->datas['mailpwd']                 = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_PWD] ;
				$this->datas['ea']                      = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_ENABLE_AUTHENTICATION] ;
				$this->datas['ssl']                     = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMAIL_SSL] ;
				$this->datas['upload_size_limit']       = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_UPLOAD_SIZE_LIMT] ;
				$this->datas['base_currency']           = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_BASE_CURRENCY] ;
				$this->datas['claim_day_last']          = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_CLAIM_DAY_LAST] ;
				$this->datas['allow_delete_master'] 	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_ALLOW_DELETE_MASTER] ;
				$this->datas['use_payment_reference'] 	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_USE_PAYMENT_REFERENCE] ;
				$this->datas['emp_code_to_export']      = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_EMP_CODE_TO_EXPORT] ;
				$this->datas['use_bu']      			= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_USE_BU] ;
				$this->datas['start_time']      		= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_START_TIME] ;
				$this->datas['end_time']      			= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_END_TIME] ;

				$this->datas['use_reminder']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_USE_REMINDER] ;
				$this->datas['reminder_message1']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_MESSAGE1] ;
				$this->datas['reminder_message2']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_MESSAGE2] ;
				$this->datas['reminder_message3']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_MESSAGE3] ;
				$this->datas['reminder_message4']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_MESSAGE4] ;
				$this->datas['reminder_interval']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_INTERVAL] ;
				$this->datas['reminder_interval_unit']     = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_INTERVAL_UNIT] ;

				$this->datas['reminder_interval_approval']      	= $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_INTERVAL_APPROVAL] ;
				$this->datas['reminder_interval_approval_unit']     = $op[OrganizationOptions::C_SETTING][OrganizationOptions::C_REMINDER_INTERVAL_APPROVAL_UNIT] ;
				
				unset($opt) ;
			}
		}
		unset($cls) ;
		unset($row) ;
	}

	private function getUseReminder() {
		return ($this->datas['use_reminder'] == '1') ? 'checked' : "";
	
	}

	private function getReminderMessage1() {
		return $this->datas['reminder_message1'] ;
	}
	private function getReminderMessage2() {
		return $this->datas['reminder_message2'] ;
	}
	private function getReminderMessage3() {
		return $this->datas['reminder_message3'] ;
	}
	private function getReminderMessage4() {
		return $this->datas['reminder_message4'] ;
	}
	private function getReminderInterval() {
		return $this->datas['reminder_interval'] ;
	}
	private function getReminderIntervalUnit() {
		return $this->datas['reminder_interval_unit'] ;
	}

	private function getReminderIntervalApproval() {
		return $this->datas['reminder_interval_approval'] ;
	}
	private function getReminderIntervalApprovalUnit() {
		return $this->datas['reminder_interval_approval_unit'] ;
	}

	private function getStart() {
		return $this->datas['start_time'] ;
	}
	private function getEnd() {
		return $this->datas['end_time'] ;
	}
	private function getName1() {
		return $this->datas['name1'] ;
	}
	private function getName2() {
		return $this->datas['name2'] ;
	}
	private function getAddr1() {
		return $this->datas['addr1'] ;
	}
	private function getAddr2() {
		return $this->datas['addr2'] ;
	}
	private function getAddr3() {
		return $this->datas['addr3'] ;
	}
	private function getAddr4() {
		return $this->datas['addr4'] ;
	}
	private function getAddr5() {
		return $this->datas['addr5'] ;
	}
	private function getFailCount() {
		return $this->datas['failcount'] ;
	}
	private function getCode() {
		return $this->datas['code'] ;
	}
	private function getRefNo() {
		return $this->datas['refno'] ;
	}
	private function getTelNo() {
		return $this->datas['telno'] ;
	}
	private function getFaxNo() {
		return $this->datas['faxno'] ;
	}
	private function getEmailServer() {
		return $this->datas['mailserver'] ;
	}
	private function getEmailPort() {
		return $this->datas['mailport'] ;
	}
	private function getEmailUser() {
		return $this->datas['mailuser'] ;
	}
	private function getEmailPwd() {
		return $this->datas['mailpwd'] ;
	}
	private function getEnableAuthentication() {
		return ($this->datas['ea'] == '1') ? 'checked' : "";
	}
	private function getEmailSSL() {
		return ($this->datas['ssl'] == '1') ? 'checked' : "";
	}
	private function getAllowDeleteMaster() {
		return ($this->datas['allow_delete_master'] == '1') ? 'checked' : "";
	}
	private function getClaimUsePaymentReference() {
		return ($this->datas['use_payment_reference'] == '1') ? 'checked' : "";
	}
	private function getUploadSizeLimit() {
		return floatval($this->datas['upload_size_limit']) == 0 ? 5000 : floatval($this->datas['upload_size_limit']);
	}
	private function getBaseCurrency() {
		return $this->datas['base_currency'] ;
	}
	private function getClaimDayLast() {
		return intval($this->datas['claim_day_last']) == 0 ? 30 : intval($this->datas['claim_day_last']);
	}
	private function getUseBu() {
		return ($this->datas['use_bu'] == '1') ? 'checked' : "";
	}
	private function getLogo() {
		$fn = Util::getLogoFile(PATH_PICTURE, $_SESSION[SE_ORGID]) ;
		if ($fn == "")
			return "" ;
		else 
			return "picture/" . $_SESSION[SE_ORGID] . "/" . $fn ;
	}
	private function removeLogo($params=null) {
		Util::removeLogoFile(PATH_PICTURE, $_SESSION[SE_ORGID]) ;
		$this->sendJsonResponse(Status::Ok,"Logo successfully removed from the system.","",$this->type) ;
	}
	private function getView() {
		$this->getRecord();
		ob_start() ;
		include (PATH_VIEWS . "admin/SettingView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
    
    private function getEmpCodeToExportSelection(){
		$arr = array() ;
		$arr[] = array ('code'=>'0','desc'=>'Use Employee Code' );
		$arr[] = array ('code'=>'1','desc'=>'Use Employee FIN' ) ;
		return Util::createOptionValue($arr) ;
    }
    
    private function getEmpCodeToExportSelected(){
		return $this->datas['emp_code_to_export'];
    }
}
?>