<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "hr/EmployeeClass.php") ;
require_once (PATH_MODELS . "hr/EmployeePasswordClass.php") ;
require_once (PATH_MODELS . "general/ContactInfoClass.php") ;
require_once (PATH_MODELS . "hr/DepartmentClass.php") ;
require_once (PATH_TABLES . "hr/EmployeeTypeTable.php") ;
require_once (PATH_TABLES . "hr/NationalityTable.php") ;
require_once (PATH_TABLES . "hr/RaceTable.php") ;
require_once (PATH_TABLES . "hr/WorkPermitTable.php") ;
require_once (PATH_TABLES . "hr/JobTitleTable.php") ;
require_once (PATH_MODELS . "admin/CompanyClass.php") ;

class EmployeePassword extends HrmsBase {
	private $type = "" ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
		$this->orgid = $_SESSION[SE_ORGID] ;
		$this->fldorg = EmployeeTable::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_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_LIST:
					echo $this->getList($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 getRecord($params=null) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new EmployeeClass($this->db) ;
			$pwdCls = new EmployeePasswordClass($this->db);
			$row = $cls->getRecord($id) ;
			$row_pwd = $pwdCls->getRecord($id);
			if (is_null($row)) {
				$this->sendJsonResponse(Status::Error,"Invalid employee id. Please try again.",$id,$this->type);
			} else {
				$datas = array() ;
				$datas['id'] = $id ;
				$datas['name'] = $row[EmployeeTable::C_NAME];
				$datas['code'] = $row[EmployeeTable::C_CODE] ;
				$dte = date_create($row_pwd[EmployeePasswordTable::C_LAST_LOGIN]) ;
				if ($dte == date_create(NULL_DATE))
					$datas['login'] = "" ;
				else
					$datas['login'] = date_format($dte,'d-M-Y H:i:s') ;
				$this->sendJsonResponse(Status::Ok,"",$datas,$this->type) ;
			}
			unset($cls) ;
		} else {
			$this->sendJsonResponse(Status::Error,"Missing employee id. Please try again.","",$this->type);
		}
	}
	private function updateRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls_p = new EmployeePasswordClass($this->db) ;
			try {
				$this->db->beginTran() ;
				$cls_p->resetPassword($id) ; 
				$this->db->commitTran() ;
				$this->sendJsonResponse(Status::Ok,"Employee password is successfully reset from the system.","",$this->type);
			} catch (Exception $e) {
				$this->db->rollbackTran() ;
				Log::write('[Employee Password]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a problem in resetting employee password from the system.","",$this->type) ;
			}
			unset($cls) ;
			unset($cls_p) ;
			
		} else {
			$this->sendJsonResponse(Status::Error,"You must supply the employee id whose password you wish to reset. Please try again.","",$this->type);
		}
	}
	private function getList($conditions=null) {
		$cls 	= new EmployeeClass($this->db) ;
		$coy 	= new CompanyClass($this->db) ;
		$dept 	= new DepartmentClass($this->db) ;
		$params = array() ;
		
		$filter 	= $this->db->fieldParam(EmployeeTable::C_ORG_ID) ;
		$params[] 	= $this->db->valueParam(EmployeeTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		
		if(isset($conditions['company'])  && $conditions['company'] != "") {
			$filter 	.= ' AND ' . $this->db->fieldParam(EmployeeTable::C_COY_ID) ;
			$params[] 	= $this->db->valueParam(EmployeeTable::C_COY_ID,$conditions['company']) ;
		}
		
		if(isset($conditions['department']) && $conditions['department'] != "") {
			$filter 	.= ' AND ' . $this->db->fieldParam(EmployeeTable::C_DEPT) ;
			$params[] 	= $this->db->valueParam(EmployeeTable::C_DEPT,$conditions['department']) ;
		}
		
		$rows = $cls->getTable($filter,EmployeeTable::C_NAME,$params) ;
		
		$list = "" ;
		foreach ($rows as $row) {
			$id = $row[EmployeeTable::C_ID] ;
			$list .= "<tr>" ;
			$list .= "<td>" . $id . "</td>" ;
			$list .= "<td>" . $row[EmployeeTable::C_CODE] . "</td>" ;
			$list .= "<td>" . $row[EmployeeTable::C_NAME] . "</td>" ;
			$list .= "<td>" . $coy->getDescription($row[EmployeeTable::C_COY_ID]) . "</td>" ;
			$list .= "<td>" . $dept->getDescription($row[EmployeeTable::C_DEPT]) . "</td>" ;
			$list .= "<td style='text-align:center'><a href='javascript:' onclick='editResetEmpPwd(" . $id . ",this)'><img src='image/changepwd_20.png' title='Edit'></img></a></td>" ;
			$list .= "</tr>" ;
		}
		unset($rows) ;
		unset($cls) ;
		unset($dept) ;
		unset($coy) ;
		
		return $list ;
	}
	private function getView() {
		ob_start() ;
		include (PATH_VIEWS . "hr/EmployeePasswordView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
}
?>