<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "admin/CompanyClass.php") ;
require_once (PATH_MODELS . "admin/CompanyLocationClass.php") ;
// require_once (PATH_MODELS . "request/WorkFlowClass.php") ;
require_once (PATH_MODELS . "admin/BankClass.php") ;
require_once (PATH_MODELS . "admin/CompanyOptions.php") ;
//require_once (PATH_MODELS . "hr/EmployeeClass.php") ;
require_once (PATH_MODELS . "activity/invoice/InvoiceTitleClass.php") ;

class Company extends ActivityBase {
	private $type = "" ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
		$this->orgid = $_SESSION[SE_ORGID] ;
		$this->fldorg = CompanyTable::C_ORG_ID ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = REQ_VIEW ;
		
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_ADD:
					$this->addRecord($params) ;
					break ;
				case REQ_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_DELETE:
					$this->deleteRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_LIST:
					$this->getList($params) ;
					break ;
				case REQ_REPORT:
					$this->getReport($params) ;
					break ;
				case REQ_VIEW:
					$this->getView() ;
					break ;
				case REQ_DELETE_LOGO:
					$this->deleteCompanyLogo($params) ;
					break ;
				case "DELETE_PAYNOW_QR_CODE":
					$this->deleteCompanyPayNowQRCode($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 addRecord($params) {
		$cls 		= new CompanyClass($this->db) ;
		$opts 		= new CompanyOptions() ;
		$datas 		= array() ;
		$orgid 		= $_SESSION[SE_ORGID] ;
		$modifyby 	= $_SESSION[SE_USERID] ;
		$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
		$ws 		= $_SESSION[SE_REMOTE_IP];
		$op 		= array() ;
		$op[CompanyOptions::C_CONTACT]  = $this->getContactOpt($params) ;
		$op[CompanyOptions::C_SETTING]  = $this->getSettingOpt($params) ;
		$op[CompanyOptions::C_IRAS] 	= $this->getIrasOpt($params) ;
 		$op[CompanyOptions::C_WORKFLOW] = $this->getWorkflowOpt($params) ;
 		$op[CompanyOptions::C_EMAIL_SETTING] = $this->getEmailSettingOpt($params) ;
 		$op[CompanyOptions::C_BANK_DETAIL] = $this->getBankDetailOpt($params) ;
 		
		$opts->setOption($op) ;
		$comapany_location	= $this->getParam($params,'comapany_location',"");
		$datas[] = $this->db->fieldValue(CompanyTable::C_DESC,$this->getParam($params,'name',"")) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_OPTIONS,$opts->getXml());
		$datas[] = $this->db->fieldValue(CompanyTable::C_WS_ID,$ws) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_MODIFY_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_CREATE_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_MODIFY_DATE,$modifydate) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_CREATE_DATE,$modifydate) ;
		$datas[] = $this->db->fieldValue(CompanyTable::C_ORG_ID,$orgid) ;
		
		try {
			$id = $cls->addRecord($datas) ;
			$this->setCompanyLocation($id, $comapany_location);
			$this->setInvoiceTitle($id, $this->getParam($params,'invoice_title',""));

			$this->sendJsonResponse(Status::Ok,"Company successfully added to the system.",$id,$this->type);
		} catch (Exception $e) {
			Log::write('[Company]' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Exception, there is a error in database operation.","",$this->type) ;
		}
		unset($cls);
		unset($opts) ;
		unset($datas) ;
	}
	private function updateRecord($params) {
		if (isset($params['id'])) {
			$id 	= $params['id'] ;
			$cls 	= new CompanyClass($this->db) ;
			$opts 	= new CompanyOptions() ;
			try {
				$datas 		= array() ;
				$modifyby 	= $_SESSION[SE_USERID] ;
				$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
				$ws 		= $_SESSION[SE_REMOTE_IP] ;

				$op 		= array() ;
				$op[CompanyOptions::C_CONTACT] 	= $this->getContactOpt($params) ;
				$op[CompanyOptions::C_SETTING] 	= $this->getSettingOpt($params) ;
				$op[CompanyOptions::C_IRAS] 	= $this->getIrasOpt($params) ;
 				$op[CompanyOptions::C_WORKFLOW] = $this->getWorkflowOpt($params) ;
 				$op[CompanyOptions::C_EMAIL_SETTING] = $this->getEmailSettingOpt($params) ;
				$op[CompanyOptions::C_BANK_DETAIL] = $this->getBankDetailOpt($params) ;				
				$opts->setOption($op) ;

				$comapany_location	= $this->getParam($params,'comapany_location',"");
				$datas[] = $this->db->fieldValue(CompanyTable::C_DESC,$this->getParam($params,'name',"")) ;
				$datas[] = $this->db->fieldValue(CompanyTable::C_OPTIONS,$opts->getXml());
				$datas[] = $this->db->fieldValue(CompanyTable::C_WS_ID,$ws) ;
				$datas[] = $this->db->fieldValue(CompanyTable::C_MODIFY_BY,$modifyby) ;
				$datas[] = $this->db->fieldValue(CompanyTable::C_MODIFY_DATE,$modifydate) ;

				$cls->updateRecord($id,$datas) ;
				$this->setCompanyLocation($id, $comapany_location);
				$this->setInvoiceTitle($id, $this->getParam($params,'invoice_title',""));

				$this->sendJsonResponse(Status::Ok,"Company detail successfully updated to the system.",$id,$this->type) ;
			} catch (Exception $e) {
				Log::write('[Company]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a error in updating company detail to the system.","",$this->type) ;
			}
			unset($cls) ;
			unset($datas) ;
			unset($opts) ;
		}else {
			$this->sendJsonResponse(Status::Error,"You must supply the company id you wish to update. Please try again.","",$this->type);
		}
	}
	private function deleteRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new CompanyClass($this->db) ;
			try {
				$cls->deleteRecord($id) ; 
				$this->sendJsonResponse(Status::Ok,"Company successfully deleted from the system.","",$this->type);
			} catch (Exception $e) {
				Log::write('[Company]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a problem in deleting company record from the system.","",$this->type) ;
			}
			unset($cls) ;
		} else {
			$this->sendJsonResponse(Status::Error,"You must supply the company id you wish to delete. Please try again.","",$this->type);
		}
	}
	private function getList($conditions=null) {
		// $cls = new CompanyClass($this->db) ;
		// $filter = $this->db->fieldParam(CompanyTable::C_ORG_ID) ;
		// $params = array() ;
		// $params[] = $this->db->valueParam(CompanyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		// $rows = $cls->getTable($filter,CompanyTable::C_DESC,$params) ;
		// $list = "" ;
		// foreach ($rows as $row) {
		// 	$id = $row[CompanyTable::C_COY_ID] ;
		// 	$list .= "<tr>" ;
		// 	$list .= "<td>" . $id . "</td>" ;
		// 	$list .= "<td>" . $row[CompanyTable::C_DESC] . "</td>" ;
		// 	$list .= "<td style='text-align:center'><a href='javascript:' onclick='editCompany(" . $id . ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" ;
		// 	$list .= "<td style='text-align:center'><a href='javascript:' onclick='deleteCompany(" . $id . ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" ;
		// 	$list .= "</tr>" ;
		// }
		// unset($rows) ;
		// unset($cls) ;
		// return $list ;

		//===============================================================

		$cls = new CompanyClass($this->db) ;

		$filter = $this->db->fieldParam(CompanyTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(CompanyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,CompanyTable::C_DESC,$params) ;
		$arrList = [];
		foreach ($rows as $i => $row) {
			$arrList[$i]["id"] = $row[CompanyTable::C_ID];
			$arrList[$i]["desc"] = $row[CompanyTable::C_DESC];	
			$arrList[$i]["actions"] = '<a onclick="javascript:editCompany('. $row[CompanyTable::C_ID] .',this);" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit details">
									  	<i class="fa fa-edit"></i>
									  </a>
									  <a onclick="javascript:deleteCompany('. $row[CompanyTable::C_ID] .',this);" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Delete">
									  	<i class="fa fa-trash"></i>
									  </a>';	
		}

		$this->sendJsonResponse(Status::Ok,"",$arrList,$this->type);
		
		unset($rows) ;
		unset($cls) ;
	}
	private function getRecord($params=null) {
		if (!isset($params['id'])) 
				$this->sendJsonResponse(Status::Error,"Missing company id. Please try again.",0,$this->type);
			
		$id = $params['id'] ;
		$datas = $this->getCompanyRecordById($id);
		
		if(count($datas) == 0)
			$this->sendJsonResponse(Status::Error,"Invalid company id. Please try again.",$id,$this->type);
		else
			$this->sendJsonResponse(Status::Ok,"",$datas,$this->type) ;
	}
	
	public function putActiveStepToArray($stepCheck, $arrayToAdd) {
		if($stepCheck != "")
			$arrayToAdd[] = $stepCheck;
		return $arrayToAdd;
	}
	
	public function getCompanyRecordById($id) {
		$cls   = new CompanyClass($this->db) ;
		$row   = $cls->getRecord($id) ;
		
		$datas = array() ;
		if (is_null($row)) {
			return array();
		} else {
			$datas['id'] = $id ;
			$datas['name'] = $row[CompanyTable::C_DESC] ;
			
			if (!is_null($row[CompanyTable::C_OPTIONS]) && $row[CompanyTable::C_OPTIONS] != "") {
				$opt = new CompanyOptions() ;
				$opt->loadXml($row[CompanyTable::C_OPTIONS]) ;
				$op = $opt->getOption() ;
				$datas['name2'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_NAME2] ;
				$datas['addr1'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_ADDR1] ;
				$datas['addr2'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_ADDR2] ;
				$datas['addr3'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_ADDR3] ;
				$datas['addr4'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_ADDR4] ;
				$datas['addr5'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_ADDR5] ;
				$datas['tel'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_TEL] ;
				$datas['fax'] = $op[CompanyOptions::C_CONTACT][CompanyOptions::C_CONT_FAX] ;
				
				$datas['cpfno'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_CPF_NO] ;
				$datas['cpfref'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_CPF_REF] ;
				$datas['refno'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_REF_NO] ;
				$datas['bank'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_BANK] ;
				$datas['acctno'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_BANK_ACCT] ;
				$datas['financialmonth'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_FINANCIAL_MONTH] ;
				$datas['financialyear'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_FINANCIAL_YEAR] ;
				$datas['code'] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_CODE] ;
				$datas[CompanyOptions::C_BASE_CURRENCY] = $op[CompanyOptions::C_SETTING][CompanyOptions::C_BASE_CURRENCY] ;
				
				$datas['regno'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_TAX_ID] ;
				$datas['regtype'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_ID_TYPE] ;
				$datas['authname'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_NAME] ;
				$datas['authtitle'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_DEST] ;
				$datas['authtelno'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_CONTACT] ;
				$datas['egmdate'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_EGM] ;
				$datas['bonusdate'] = $op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_BONUS] ;
				
				$datas['location'] = $this->getCompanyLocation($id);

				//workflow
				$datas[CompanyOptions::C_STEP]	= $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_STEP] ;
				$datas[CompanyOptions::C_CLAIM_REJECT_REASON] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_CLAIM_REJECT_REASON] ;
				$datas[CompanyOptions::C_PRORATION_CLAIM] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_PRORATION_CLAIM] ;
				$datas[CompanyOptions::C_PRORATION_MONTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_PRORATION_MONTH] ;
				$datas[CompanyOptions::C_CLEARING_ACCOUNT] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_CLEARING_ACCOUNT] ;
				$datas[CompanyOptions::C_ACTIVITY_CODE_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_ACTIVITY_CODE_PREFIX] ;
				$datas[CompanyOptions::C_ACTIVITY_CODE_LENGTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_ACTIVITY_CODE_LENGTH] ;
				$datas[CompanyOptions::C_ACTIVITY_CODE_RN] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_ACTIVITY_CODE_RN] ;
				$datas[CompanyOptions::C_GR_CODE_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_GR_CODE_PREFIX] ;
				$datas[CompanyOptions::C_GR_CODE_LENGTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_GR_CODE_LENGTH] ;
				$datas[CompanyOptions::C_GR_CODE_RN] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_GR_CODE_RN] ;
				$datas[CompanyOptions::C_QUOTATION_CODE_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_QUOTATION_CODE_PREFIX] ;
				$datas[CompanyOptions::C_QUOTATION_CODE_LENGTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_QUOTATION_CODE_LENGTH] ;
				$datas[CompanyOptions::C_QUOTATION_CODE_RN] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_QUOTATION_CODE_RN] ;
				$datas[CompanyOptions::C_SALES_QUOTATION_CODE_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_SALES_QUOTATION_CODE_PREFIX] ;
				$datas[CompanyOptions::C_SALES_QUOTATION_CODE_LENGTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_SALES_QUOTATION_CODE_LENGTH] ;
				$datas[CompanyOptions::C_SALES_QUOTATION_CODE_RN] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_SALES_QUOTATION_CODE_RN] ;
				$datas[CompanyOptions::C_SALES_QUOTATION_INTERNAL_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_SALES_QUOTATION_INTERNAL_PREFIX] ;
				$datas[CompanyOptions::C_PROFORMA_INVOICE_CODE_PREFIX] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_PROFORMA_INVOICE_CODE_PREFIX] ;
				$datas[CompanyOptions::C_PROFORMA_INVOICE_CODE_LENGTH] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_PROFORMA_INVOICE_CODE_LENGTH] ;
				$datas[CompanyOptions::C_PROFORMA_INVOICE_CODE_RN] = $op[CompanyOptions::C_WORKFLOW][CompanyOptions::C_PROFORMA_INVOICE_CODE_RN] ;
                
				//email setting
				$datas[CompanyOptions::C_EMAIL_SETTING_SERVER] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_SERVER] ;
				$datas[CompanyOptions::C_EMAIL_SETTING_PORT] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_PORT] ;
				$datas[CompanyOptions::C_EMAIL_SETTING_SENDER] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_SENDER] ;
				$datas[CompanyOptions::C_EMAIL_SETTING_PASSWORD] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_PASSWORD] ;
				$datas[CompanyOptions::C_EMAIL_SETTING_AUTH] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_AUTH] ;
				$datas[CompanyOptions::C_EMAIL_SETTING_SSL] = $op[CompanyOptions::C_EMAIL_SETTING][CompanyOptions::C_EMAIL_SETTING_SSL] ;
				
				//bank detail
				$datas[CompanyOptions::C_BANK_DETAIL_CODE] 			= $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_CODE] ;
				$datas[CompanyOptions::C_BANK_DETAIL_NAME] 			= $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_NAME] ;
				$datas[CompanyOptions::C_BANK_DETAIL_ACCOUNT_NAME] 	= $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_ACCOUNT_NAME] ;
				$datas[CompanyOptions::C_BANK_DETAIL_ACCOUNT_NUMBER] = $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_ACCOUNT_NUMBER] ;
				$datas[CompanyOptions::C_BANK_DETAIL_BRANCH_CODE] 	= $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_BRANCH_CODE] ;
				$datas[CompanyOptions::C_BANK_DETAIL_SWIFT_CODE] 	= $op[CompanyOptions::C_BANK_DETAIL][CompanyOptions::C_BANK_DETAIL_SWIFT_CODE] ;

				$paynowqrpath = Util::getCompanyPayNowQRFile(PATH_PICTURE, $id);
                if ($paynowqrpath != ""){
                    $paynowqrpath = "../web/picture/company-" . $id . "/" . $paynowqrpath;
                }
                $datas['paynowqrpath'] = $paynowqrpath;
				
				$datas['invoice_title'] = $this->getInvoiceTitle($id);
                
                $logopath = Util::getCompanyLogoFile(PATH_PICTURE, $id);
                if ($logopath != ""){
                    $logopath = "../web/picture/company-" . $id . "/" . $logopath;
                }
                $datas['logopath'] = $logopath;
				unset($opt) ;
			} 
		}
		unset($cls) ;
		return $datas;		
	}
	
	private function getView() {
		ob_start() ;
		include (PATH_VIEWS . "admin/CompanyView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	
	private function getRegType() {
		$arr = array() ;
		$arr[] = array ('code'=>'','desc'=>'--- Select Registeration No Type ---') ;
		$arr[] = array ('code'=>'7','desc'=>'Business Registeration number' );
		$arr[] = array ('code'=>'8','desc'=>'Local Company Registeration number' ) ;
		$arr[] = array ('code'=>'A','desc'=>'IRAS Tax Reference number' ) ;
		$arr[] = array ('code'=>'I','desc'=>'IRAS Income Tax Reference number') ;
		$arr[] = array ('code'=>'G','desc'=>'IRAS GST number') ;
		$arr[] = array ('code'=>'U','desc'=>'Others Unique Entity Number') ;
		return Util::createOptionValue($arr) ;
	}
	
	private function getProration() {
		$arr = array() ;
		$arr[] = array ('code'=>'0','desc'=>'None') ;
		$arr[] = array ('code'=>'1','desc'=>'By Month' );
		$arr[] = array ('code'=>'2','desc'=>'By Date' );
		return Util::createOptionValue($arr) ;
	}
	
	private function getProrationMonth() {
		$arr = array() ;
		$arr[] = array ('code'=>'0','desc'=>'None') ;
		$arr[] = array ('code'=>'1','desc'=>'January' );
		$arr[] = array ('code'=>'2','desc'=>'February' );
		$arr[] = array ('code'=>'3','desc'=>'March' );
		$arr[] = array ('code'=>'4','desc'=>'April' );
		$arr[] = array ('code'=>'5','desc'=>'May' );
		$arr[] = array ('code'=>'6','desc'=>'June' );
		$arr[] = array ('code'=>'7','desc'=>'July' );
		$arr[] = array ('code'=>'8','desc'=>'August' );
		$arr[] = array ('code'=>'9','desc'=>'September' );
		$arr[] = array ('code'=>'10','desc'=>'October' );
		$arr[] = array ('code'=>'11','desc'=>'November' );
		$arr[] = array ('code'=>'12','desc'=>'December' );
		return Util::createOptionValue($arr) ;
	}
	
	private function getMonth() {
		$arr = array() ;
		$arr[] = array ('code'=>'1','desc'=>'January' );
		$arr[] = array ('code'=>'2','desc'=>'February' );
		$arr[] = array ('code'=>'3','desc'=>'March' );
		$arr[] = array ('code'=>'4','desc'=>'April' );
		$arr[] = array ('code'=>'5','desc'=>'May' );
		$arr[] = array ('code'=>'6','desc'=>'June' );
		$arr[] = array ('code'=>'7','desc'=>'July' );
		$arr[] = array ('code'=>'8','desc'=>'August' );
		$arr[] = array ('code'=>'9','desc'=>'September' );
		$arr[] = array ('code'=>'10','desc'=>'October' );
		$arr[] = array ('code'=>'11','desc'=>'November' );
		$arr[] = array ('code'=>'12','desc'=>'December' );
		return Util::createOptionValue($arr) ;
	}
    
    private function getFinancialYear(){
		$arr = array() ;
		$arr[] = array ('code'=>'0','desc'=>'Previous year' );
		$arr[] = array ('code'=>'1','desc'=>'Current year' );
		return Util::createOptionValue($arr) ;
        
    }
	private function getContactOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_CONT_NAME2] = $this->getParam($params,'name2',"") ;
		$op[CompanyOptions::C_CONT_ADDR1] = $this->getParam($params,'addr1',"") ;
		$op[CompanyOptions::C_CONT_ADDR2] = $this->getParam($params,'addr2',"") ;
		$op[CompanyOptions::C_CONT_ADDR3] = $this->getParam($params,'addr3',"") ;
		$op[CompanyOptions::C_CONT_ADDR4] = $this->getParam($params,'addr4',"") ;
		$op[CompanyOptions::C_CONT_ADDR5] = $this->getParam($params,'addr5',"") ;
		$op[CompanyOptions::C_CONT_TEL] = $this->getParam($params,'tel',"") ;
		$op[CompanyOptions::C_CONT_FAX] = $this->getParam($params,'fax',"") ;
		$op[CompanyOptions::C_CONT_EMAIL] = $this->getParam($params,'email',"");
		$op[CompanyOptions::C_CONT_WEB] = $this->getParam($params,'website',"");
		
		return $op ;
	}
	private function getSettingOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_SET_REF_NO] = $this->getParam($params,'refno',"");
		$op[CompanyOptions::C_SET_CPF_NO] = $this->getParam($params,'cpfno',"") ;
		$op[CompanyOptions::C_SET_CPF_REF] = $this->getParam($params,'cpfref',"");
		$op[CompanyOptions::C_SET_BANK] = $this->getParam($params,'bank',"") ;
		$op[CompanyOptions::C_SET_BANK_ACCT] = $this->getParam($params,'acctno',"");
		$op[CompanyOptions::C_SET_FINANCIAL_MONTH] = $this->getParam($params,'financialmonth',"1");
		$op[CompanyOptions::C_SET_FINANCIAL_YEAR] = $this->getParam($params,'financialyear',"0");
		$op[CompanyOptions::C_SET_CODE] = $this->getParam($params,'code',"0");
		$op[CompanyOptions::C_BASE_CURRENCY] = $this->getParamInt($params,'basecurrency',0);
		return $op ;
	}
	private function getIrasOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_IRAS_NAME] 	= $this->getParam($params,'authname',"");
		$op[CompanyOptions::C_IRAS_DEST] 	= $this->getParam($params,'authtitle',"") ;
		$op[CompanyOptions::C_IRAS_CONTACT] = $this->getParam($params,'authtelno',"") ;
		$op[CompanyOptions::C_IRAS_EGM] 	= $this->getParam($params,'egmdate',"") ;
		$op[CompanyOptions::C_IRAS_BONUS] 	= $this->getParam($params,'bonusdate',"") ;
		$op[CompanyOptions::C_IRAS_TAX_ID] 	= $this->getParam($params,'regno',"") ;
		$op[CompanyOptions::C_IRAS_ID_TYPE] = $this->getParam($params,'regtype',"") ;
		return $op ;
	}
	private function getWorkflowOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_STEP] = "";
		$workFlowStr   = "";
		$workflowSteps = $this->getParam($params,'workFlowSteps',"");
		/*if($workflowSteps=="")
			return $op;
		*/
		$workFlowItems = explode("|", $workflowSteps);
		/*
		if(count($workFlowItems) == 0 )
			return array();
		*/
		for($i = 0; $i < count($workFlowItems); $i++ ) {
			$parts 		  	= explode("-", $workFlowItems[$i]);
			if(count($parts) > 1){
				$workflowStep 	= $parts[0];
				$workFlowId	  	= $parts[1];
				
				
				if($workflowStep == ($i + 1)) {
					if($workflowStep == 1) 
						$workFlowStr	.= $workFlowId;
					else 
						$workFlowStr	.= ",".$workFlowId;
				}
			}
		}
		$op[CompanyOptions::C_STEP] = $workFlowStr;
		$op[CompanyOptions::C_CLAIM_REJECT_REASON] = $this->getParamInt($params,'claim_reject_reason',"");
		$op[CompanyOptions::C_PRORATION_CLAIM] = $this->getParamInt($params,'proration_claim',"");
		$op[CompanyOptions::C_PRORATION_MONTH] = $this->getParamInt($params,'proration_month',"");
		$op[CompanyOptions::C_CLEARING_ACCOUNT] = $this->getParam($params,'clearingaccount',"");
		$op[CompanyOptions::C_ACTIVITY_CODE_PREFIX] = $this->getParam($params,'activitycodeprefix',"");
		$op[CompanyOptions::C_ACTIVITY_CODE_LENGTH] = $this->getParam($params,'activitycodelength',"");
		$op[CompanyOptions::C_ACTIVITY_CODE_RN] 	= $this->getParam($params,'activitycodern',"");
		$op[CompanyOptions::C_GR_CODE_PREFIX] = $this->getParam($params,'grcodeprefix',"");
		$op[CompanyOptions::C_GR_CODE_LENGTH] = $this->getParam($params,'grcodelength',"");
		$op[CompanyOptions::C_GR_CODE_RN] 	  = $this->getParam($params,'grcodern',"");
		$op[CompanyOptions::C_QUOTATION_CODE_PREFIX] = $this->getParam($params,'quotationcodeprefix',"");
		$op[CompanyOptions::C_QUOTATION_CODE_LENGTH] = $this->getParam($params,'quotationcodelength',"");
		$op[CompanyOptions::C_QUOTATION_CODE_RN] 	  = $this->getParam($params,'quotationcodern',"");
		$op[CompanyOptions::C_SALES_QUOTATION_CODE_PREFIX] = $this->getParam($params,'salesquotationcodeprefix',"");
		$op[CompanyOptions::C_SALES_QUOTATION_CODE_LENGTH] = $this->getParam($params,'salesquotationcodelength',"");
		$op[CompanyOptions::C_SALES_QUOTATION_CODE_RN] 	  = $this->getParam($params,'salesquotationcodern',"");
		$op[CompanyOptions::C_SALES_QUOTATION_INTERNAL_PREFIX] = $this->getParam($params,'salesquotationinternalprefix',"");
		$op[CompanyOptions::C_PROFORMA_INVOICE_CODE_PREFIX] = $this->getParam($params,'proformainvoicecodeprefix',"");
		$op[CompanyOptions::C_PROFORMA_INVOICE_CODE_LENGTH] = $this->getParam($params,'proformainvoicecodelength',"");
		$op[CompanyOptions::C_PROFORMA_INVOICE_CODE_RN] 	  = $this->getParam($params,'proformainvoicecodern',"");
		return $op ;
	}
	private function getEmailSettingOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_EMAIL_SETTING_SERVER] 	= $this->getParam($params,'emailsettingserver',"");
		$op[CompanyOptions::C_EMAIL_SETTING_PORT]       = $this->getParam($params,'emailsettingport',"") ;
		$op[CompanyOptions::C_EMAIL_SETTING_SENDER]     = $this->getParam($params,'emailsettingsender',"") ;
		$op[CompanyOptions::C_EMAIL_SETTING_PASSWORD] 	= $this->getParam($params,'emailsettingpassword',"") ;
		$op[CompanyOptions::C_EMAIL_SETTING_AUTH]       = $this->getParam($params,'emailsettingauth',"") ;
		$op[CompanyOptions::C_EMAIL_SETTING_SSL]        = $this->getParam($params,'emailsettingssl',"") ;
		return $op ;
	}
	private function getBankDetailOpt($params) {
		$op = array() ;
		$op[CompanyOptions::C_BANK_DETAIL_CODE] 			= $this->getParam($params,'bankdetailcode',"");
		$op[CompanyOptions::C_BANK_DETAIL_NAME]       		= $this->getParam($params,'bankdetailname',"") ;
		$op[CompanyOptions::C_BANK_DETAIL_ACCOUNT_NAME]     = $this->getParam($params,'bankdetailaccountname',"") ;
		$op[CompanyOptions::C_BANK_DETAIL_ACCOUNT_NUMBER] 	= $this->getParam($params,'bankdetailaccountnumber',"") ;
		$op[CompanyOptions::C_BANK_DETAIL_BRANCH_CODE]      = $this->getParam($params,'bankdetailbranchcode',"") ;
		$op[CompanyOptions::C_BANK_DETAIL_SWIFT_CODE]       = $this->getParam($params,'bankdetailswiftcode',"") ;
		return $op ;
	}
/*
	private function getWorkFlow() {
		$filter = array();
		$filter[] = array('field'=>$this->fldorg,'value'=>$this->orgid) ;
		$vls = $this->getValueList(WorkFlowTable::C_TABLE, WorkFlowTable::C_ID, WorkFlowTable::C_DESC,null,$filter) ;
		return Util::createOptionValue($vls) ;
	}
	
	public function getWorkFlowOptionByCompId($companyId, $default="") {
		$workflowCls	= new WorkFlowClass($this->db);
		$companyInfo	= $this->getCompanyRecordById($companyId);
		
		$wfSteps		= $companyInfo[CompanyOptions::C_STEP];
		$wfSteps		= explode(",", $wfSteps);
		$vls 			= array() ;
		$vls[] 			= array ('code'=>"",'desc'=>"----- Select a Workflow -----") ;
		
		if(is_null($wfSteps) || count($wfSteps) == 0)
			return null;
		
		foreach ($wfSteps as $wfStep) {
			$workflow	= $workflowCls->getRecord($wfStep);
			$vls[] 		= array ('code'=>$workflow[WorkFlowTable::C_ID],'desc'=>$workflow[WorkFlowTable::C_DESC]) ;
		}
		return Util::createOptionValue($vls, $default) ;
	}
	*/
/*	public function getWorkFlowByEmpId($empId, $default="") {
		$employeeCls	= new EmployeeClass($this->db);
		$companyId		= $employeeCls->getCompanyId($empId);
		$this->getWorkFlowOptionByCompId($companyId);
	}*/
	
	private function getReport($params=null) {
		require_once(PATH_LIB . 'ListPdf.php');
		
		$cls = new CompanyClass($this->db) ;
		$clsbank = new BankClass($this->db) ;
		$filter = $this->db->fieldParam(CompanyTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(CompanyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,CompanyTable::C_DESC,$params) ;
		$w = 'width';
		$h = 'height';
		$a = 'align';
		$t = 'text';
		$i = 'items';
		$nr = 'newrow';
		$datas = array() ;
		foreach ($rows as $row) {
			$items = array() ;
			$items[$i][] = $this->createPdfItem($row[CompanyTable::C_COY_ID],30) ;
			$items[$i][] = $this->createPdfItem($row[CompanyTable::C_DESC],200) ;
			if (!is_null($row[CompanyTable::C_OPTIONS]) && $row[CompanyTable::C_OPTIONS] != "") {
				$opt = new CompanyOptions() ;
				$opt->loadXml($row[CompanyTable::C_OPTIONS]) ;
				$op = $opt->getOption() ;
				// $b = $op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_BANK] ;
				$items[$i][] = $this->createPdfItem($op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_REF_NO],70) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_CPF_NO],100) ;
				// $items[$i][] = $this->createPdfItem($clsbank->getDescription($b),80) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_EGM],60) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_DEST],100) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_CONTACT],80) ;
				
				// $items[$i][] = $this->createPdfItem("",30,0,"L","0","1") ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_ID_TYPE],200) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_CPF_REF],70) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_TAX_ID],100) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_SETTING][CompanyOptions::C_SET_BANK_ACCT],80) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_BONUS],60) ;
				// $items[$i][] = $this->createPdfItem($op[CompanyOptions::C_IRAS][CompanyOptions::C_IRAS_NAME],100) ;
				$items[$i][] = $this->createPdfItem("",80) ;
				
			} else {
				$items[$i][] = $this->createPdfItem("",70) ;
				$items[$i][] = $this->createPdfItem("",100) ;
				$items[$i][] = $this->createPdfItem("",80) ;
				$items[$i][] = $this->createPdfItem("",60) ;
				$items[$i][] = $this->createPdfItem("",100) ;
				$items[$i][] = $this->createPdfItem("",80) ;
				
				$items[$i][] = $this->createPdfItem("",30,0,"L","0","1") ;
				$items[$i][] = $this->createPdfItem("",200) ;
				$items[$i][] = $this->createPdfItem("",70) ;
				$items[$i][] = $this->createPdfItem("",100) ;
				$items[$i][] = $this->createPdfItem("",80) ;
				$items[$i][] = $this->createPdfItem("",60) ;
				$items[$i][] = $this->createPdfItem("",100) ;
				$items[$i][] = $this->createPdfItem("",80) ;
			}
			$items[$nr] = "1" ;
			$datas[] = $items ;
		}
		$cols = array() ;
		$cols[] = $this->createPdfItem("ID",30,0,"L","B");
		$cols[] = $this->createPdfItem("Description",200,0,"L","B") ;
		$cols[] = $this->createPdfItem("Ref",70,0,"L","B") ;
		// $cols[] = $this->createPdfItem("CPF No",100) ;
		// $cols[] = $this->createPdfItem("Bank",80) ;
		// $cols[] = $this->createPdfItem("EGM Date",60) ;
		// $cols[] = $this->createPdfItem("Designation",100) ;
		// $cols[] = $this->createPdfItem("Contact No",80) ;
		
		// $cols[] = $this->createPdfItem("",30,0,"L","B","1");
		// $cols[] = $this->createPdfItem("Tax Ref No Type",200,0,"L","B") ;
		// $cols[] = $this->createPdfItem("CPF Ref",70,0,"L","B") ;
		// $cols[] = $this->createPdfItem("Tax Ref No",100,0,"L","B") ;
		// $cols[] = $this->createPdfItem("Acct. No",80,0,"L","B") ;
		// $cols[] = $this->createPdfItem("Bonus Date",60,0,"L","B") ;
		// $cols[] = $this->createPdfItem("Auth. Person",100,0,"L","B") ;
		// $cols[] = $this->createPdfItem("",80,0,"L","B") ;
		
		$pdf = new ListPdf('L');
		$pdf->setCompanyName($_SESSION[SE_ORGNAME]) ;
		$pdf->setReportTitle("Company Listing") ;
		$pdf->setColumnsHeader($cols) ;
		$pdf->setHeaderHeight(125) ;
		$pdf->render($datas) ;
		$pdf->Output('company.pdf', 'I');
		unset($rows) ;
		unset($cls) ;
		unset($datas) ;
		unset($params) ;
		unset($items) ;
		unset($cols) ;
	}
	private function deleteCompanyLogo($params=null) {
		Util::removeCompanyLogoFile(PATH_PICTURE, $params['id']) ;
		$this->sendJsonResponse(Status::Ok,"Logo successfully removed from the system.","",$this->type) ;
	}
	private function deleteCompanyPayNowQRCode($params=null) {
		Util::removeCompanyPayNowQRFile(PATH_PICTURE, $params['id']) ;
		$this->sendJsonResponse(Status::Ok,"PayNow QR Code successfully removed from the system.","",$this->type) ;
	}

	private function setCompanyLocation($id, $checklist) {
		$cls = new CompanyLocationClass($this->db) ;

		$cls->deleteCompanyLocation($id); 
	
		if ($checklist != "") {
			$lines 	= explode("|",$checklist) ;
			
			$l = array();
			foreach($lines as $line){
				$l = explode('~', $line);
				$datas = array() ;
				$datas[] = $this->db->fieldValue(CompanyLocationTable::C_DESC ,$l[0]) ;
				$datas[] = $this->db->fieldValue(CompanyLocationTable::C_COY_ID ,$id);
				$datas[] = $this->db->fieldValue(CompanyLocationTable::C_ADDRESS ,$l[1]);
				$cls->addRecord($datas) ;

			}
		
		}
        unset($cls);
	}

	private function getCompanyLocation($id){
		$cls = new CompanyLocationClass($this->db) ;
		$rows = $cls->getCompanyLocation($id) ;
		$lines = "" ;
		if (!is_null($rows) && count($rows) > 0) {
			foreach ($rows as $row) {
                if (strlen($lines) > 0){
					$lines .= "|" ;
				}
				$lines .= $row[CompanyLocationTable::C_DESC];
				$lines .= "~";
				$lines .= $row[CompanyLocationTable::C_ADDRESS];
                
                
			}							
        }
		unset($cls) ;
		return $lines ;
	}
	//invoice setting
	private function setInvoiceTitle($coy_id, $invoice_title) {
		try {
			$cls 		= new InvoiceTitleClass($this->db) ;
			$rows = json_decode($invoice_title, true);
			if(!is_null($rows) && count($rows)>0) {
				$cls->deleteAllByCoyId($coy_id);
				foreach($rows as $row) {
					$id = $this->getParamInt($row,'id',0);
					$datas 	 = array() ;
					$datas[] = $this->db->fieldValue(InvoiceTitleTable::C_COY_ID,$coy_id) ;
					$datas[] = $this->db->fieldValue(InvoiceTitleTable::C_DESC,$this->getParam($row,'title',"")) ;
					$datas[] = $this->db->fieldValue(InvoiceTitleTable::C_DEFAULT,$this->getParam($row,'default',0)) ;
					// if($cls->isFound($id)) {
					// 	$cls->updateRecord($id, $datas) ;
					// } else {						
						$cls->addRecord($datas) ;
					// }
				}
			}
			unset($cls) ;
		} catch (Exception $e) {
			Log::write('[InvoiceTitle] ' . $e->getMessage());
		}
	}

	private function getInvoiceTitle($id){
		$lines = [] ;
		try {
			$cls = new InvoiceTitleClass($this->db) ;
			$rows = $cls->getInvoiceTitleList($id) ;
			if (!is_null($rows) && count($rows) > 0) {
				foreach ($rows as $row) {
					$line = [];
					$line['id'] = $row[InvoiceTitleTable::C_ID];
					$line['title'] = $row[InvoiceTitleTable::C_DESC];
					$line['default'] = $row[InvoiceTitleTable::C_DEFAULT] == 0 ? false : true;
					array_push($lines, $line);
				}						
			}
			unset($cls) ;
		} catch (Exception $e) {
			Log::write('[InvoiceTitle] ' . $e->getMessage());
		}
		return $lines ;
	}
}
?>