<?php 
/**
 * Employee_details Page Controller
 * @category  Controller
 */
class Employee_detailsController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "employee_details";
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function index($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("employee_details.id", 
			"portfolio.name AS portfolio_name", 
			"employee_details.employee_name", 
			"employee_details.basic_pay", 
			"employee_details.bank_acc_no", 
			"employee_details.bank_name", 
			"employee_details.email", 
			"employee_details.dob", 
			"employee_details.date_of_join", 
			"employee_details.date_of_retirement", 
			"employee_details.department", 
			"employee_details.IFSC", 
			"employee_details.designation", 
			"employee_details.caste", 
			"employee_details.relegion", 
			"employee_details.nominee", 
			"employee_details.payscale", 
			"employee_details.gender", 
			"employee_details.identification_mark", 
			"employee_details.place_of_birth", 
			"employee_details.balanceleave", 
			"employee_details.mobile", 
			"employee_details.photo", 
			"employee_details.handicapped", 
			"employee_details.blind", 
			"employee_details.employee_type", 
			"employee_details.service_book", 
			"employee_details.service_book_remark");
		$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				employee_details.id LIKE ? OR 
				portfolio.name LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				employee_details.portfolio_id LIKE ? OR 
				employee_details.basic_pay LIKE ? OR 
				employee_details.bank_acc_no LIKE ? OR 
				employee_details.bank_name LIKE ? OR 
				employee_details.email LIKE ? OR 
				employee_details.dob LIKE ? OR 
				employee_details.date_of_join LIKE ? OR 
				employee_details.date_of_retirement LIKE ? OR 
				employee_details.department LIKE ? OR 
				employee_details.IFSC LIKE ? OR 
				employee_details.designation LIKE ? OR 
				employee_details.caste LIKE ? OR 
				employee_details.relegion LIKE ? OR 
				employee_details.nominee LIKE ? OR 
				employee_details.payscale LIKE ? OR 
				employee_details.gender LIKE ? OR 
				employee_details.identification_mark LIKE ? OR 
				employee_details.place_of_birth LIKE ? OR 
				employee_details.balanceleave LIKE ? OR 
				employee_details.mobile LIKE ? OR 
				employee_details.photo LIKE ? OR 
				employee_details.handicapped LIKE ? OR 
				employee_details.blind LIKE ? OR 
				employee_details.employee_type LIKE ? OR 
				employee_details.service_book LIKE ? OR 
				employee_details.service_book_remark LIKE ? OR 
				portfolio.pid LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "employee_details/search.php";
		}
		$db->join("portfolio", "employee_details.portfolio_id = portfolio.id", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("employee_details.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		$tc = $db->withTotalCount();
		$records = $db->get($tablename, $pagination, $fields);
		$records_count = count($records);
		$total_records = intval($tc->totalCount);
		$page_limit = $pagination[1];
		$total_pages = ceil($total_records / $page_limit);
		$data = new stdClass;
		$data->records = $records;
		$data->record_count = $records_count;
		$data->total_records = $total_records;
		$data->total_page = $total_pages;
		if($db->getLastError()){
			$this->set_page_error();
		}
		$page_title = $this->view->page_title = "Employee Details";
		$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
		$this->view->report_title = $page_title;
		$this->view->report_layout = "report_layout.php";
		$this->view->report_paper_size = "A4";
		$this->view->report_orientation = "portrait";
		$this->render_view("employee_details/list.php", $data); //render the full page
	}
	/**
     * View record detail 
	 * @param $rec_id (select record by table primary key) 
     * @param $value value (select record by value of field name(rec_id))
     * @return BaseView
     */
	function view($rec_id = null, $value = null){
		$request = $this->request;
		$db = $this->GetModel();
		$rec_id = $this->rec_id = urldecode($rec_id);
		$tablename = $this->tablename;
		$fields = array("employee_details.id", 
			"employee_details.email", 
			"portfolio.name AS portfolio_name", 
			"portfolio.pid AS portfolio_pid");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("employee_details.id", $rec_id);; //select record based on primary key
		}
		$db->join("portfolio", "employee_details.portfolio_id = portfolio.id", "INNER ");  
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Employee Details";
		$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
		$this->view->report_title = $page_title;
		$this->view->report_layout = "report_layout.php";
		$this->view->report_paper_size = "A4";
		$this->view->report_orientation = "portrait";
		}
		else{
			if($db->getLastError()){
				$this->set_page_error();
			}
			else{
				$this->set_page_error("No record found");
			}
		}
		return $this->render_view("employee_details/view.php", $record);
	}
	/**
     * Insert new record to the database table
	 * @param $formdata array() from $_POST
     * @return BaseView
     */
	function add($formdata = null){
		if($formdata){
			$db = $this->GetModel();
			$tablename = $this->tablename;
			$request = $this->request;
			//fillable fields
			$fields = $this->fields = array("employee_name","portfolio_id","basic_pay","bank_name","bank_acc_no","email","dob","date_of_join","date_of_retirement","department","IFSC","designation","caste","relegion","nominee","payscale","gender","identification_mark","place_of_birth","balanceleave","mobile","photo","handicapped","blind","employee_type","service_book","service_book_remark");
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'employee_name' => 'required',
				'portfolio_id' => 'required',
				'basic_pay' => 'required|numeric',
				'bank_name' => 'required',
				'bank_acc_no' => 'required',
				'email' => 'required|valid_email',
				'dob' => 'required',
				'date_of_join' => 'required',
				'date_of_retirement' => 'required',
				'department' => 'required',
				'IFSC' => 'required',
				'designation' => 'required',
				'balanceleave' => 'numeric',
				'photo' => 'required',
				'handicapped' => 'required',
				'blind' => 'required',
				'employee_type' => 'required',
			);
			$this->sanitize_array = array(
				'employee_name' => 'sanitize_string',
				'portfolio_id' => 'sanitize_string',
				'basic_pay' => 'sanitize_string',
				'bank_name' => 'sanitize_string',
				'bank_acc_no' => 'sanitize_string',
				'email' => 'sanitize_string',
				'dob' => 'sanitize_string',
				'date_of_join' => 'sanitize_string',
				'date_of_retirement' => 'sanitize_string',
				'department' => 'sanitize_string',
				'IFSC' => 'sanitize_string',
				'designation' => 'sanitize_string',
				'caste' => 'sanitize_string',
				'relegion' => 'sanitize_string',
				'nominee' => 'sanitize_string',
				'payscale' => 'sanitize_string',
				'gender' => 'sanitize_string',
				'identification_mark' => 'sanitize_string',
				'place_of_birth' => 'sanitize_string',
				'balanceleave' => 'sanitize_string',
				'mobile' => 'sanitize_string',
				'photo' => 'sanitize_string',
				'handicapped' => 'sanitize_string',
				'blind' => 'sanitize_string',
				'employee_type' => 'sanitize_string',
				'service_book' => 'sanitize_string',
				'service_book_remark' => 'sanitize_string',
			);
			$this->filter_vals = true; //set whether to remove empty fields
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			//Check if Duplicate Record Already Exit In The Database
			$db->where("employee_name", $modeldata['employee_name']);
			if($db->has($tablename)){
				$this->view->page_error[] = $modeldata['employee_name']." Already exist!";
			}
			//Check if Duplicate Record Already Exit In The Database
			$db->where("email", $modeldata['email']);
			if($db->has($tablename)){
				$this->view->page_error[] = $modeldata['email']." Already exist!";
			} 
			if($this->validated()){
				$rec_id = $this->rec_id = $db->insert($tablename, $modeldata);
				if($rec_id){
					$this->set_flash_msg("Record added successfully", "success");
					return	$this->redirect("employee_details");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Employee Details";
		$this->render_view("employee_details/add.php");
	}
	/**
     * Update table record with formdata
	 * @param $rec_id (select record by table primary key)
	 * @param $formdata array() from $_POST
     * @return array
     */
	function edit($rec_id = null, $formdata = null){
		$request = $this->request;
		$db = $this->GetModel();
		$this->rec_id = $rec_id;
		$tablename = $this->tablename;
		 //editable fields
		$fields = $this->fields = array("id","employee_name","portfolio_id","basic_pay","bank_name","bank_acc_no","email","dob","date_of_join","date_of_retirement","department","IFSC","designation","caste","relegion","nominee","payscale","gender","identification_mark","place_of_birth","balanceleave","mobile","photo","handicapped","blind","employee_type","service_book","service_book_remark");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'employee_name' => 'required',
				'portfolio_id' => 'required',
				'basic_pay' => 'required|numeric',
				'bank_name' => 'required',
				'bank_acc_no' => 'required',
				'email' => 'required|valid_email',
				'dob' => 'required',
				'date_of_join' => 'required',
				'date_of_retirement' => 'required',
				'department' => 'required',
				'IFSC' => 'required',
				'designation' => 'required',
				'balanceleave' => 'numeric',
				'photo' => 'required',
				'handicapped' => 'required',
				'blind' => 'required',
				'employee_type' => 'required',
			);
			$this->sanitize_array = array(
				'employee_name' => 'sanitize_string',
				'portfolio_id' => 'sanitize_string',
				'basic_pay' => 'sanitize_string',
				'bank_name' => 'sanitize_string',
				'bank_acc_no' => 'sanitize_string',
				'email' => 'sanitize_string',
				'dob' => 'sanitize_string',
				'date_of_join' => 'sanitize_string',
				'date_of_retirement' => 'sanitize_string',
				'department' => 'sanitize_string',
				'IFSC' => 'sanitize_string',
				'designation' => 'sanitize_string',
				'caste' => 'sanitize_string',
				'relegion' => 'sanitize_string',
				'nominee' => 'sanitize_string',
				'payscale' => 'sanitize_string',
				'gender' => 'sanitize_string',
				'identification_mark' => 'sanitize_string',
				'place_of_birth' => 'sanitize_string',
				'balanceleave' => 'sanitize_string',
				'mobile' => 'sanitize_string',
				'photo' => 'sanitize_string',
				'handicapped' => 'sanitize_string',
				'blind' => 'sanitize_string',
				'employee_type' => 'sanitize_string',
				'service_book' => 'sanitize_string',
				'service_book_remark' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			//Check if Duplicate Record Already Exit In The Database
			if(isset($modeldata['employee_name'])){
				$db->where("employee_name", $modeldata['employee_name'])->where("id", $rec_id, "!=");
				if($db->has($tablename)){
					$this->view->page_error[] = $modeldata['employee_name']." Already exist!";
				}
			}
			//Check if Duplicate Record Already Exit In The Database
			if(isset($modeldata['email'])){
				$db->where("email", $modeldata['email'])->where("id", $rec_id, "!=");
				if($db->has($tablename)){
					$this->view->page_error[] = $modeldata['email']." Already exist!";
				}
			} 
			if($this->validated()){
				$db->where("employee_details.id", $rec_id);;
				$bool = $db->update($tablename, $modeldata);
				$numRows = $db->getRowCount(); //number of affected rows. 0 = no record field updated
				if($bool && $numRows){
					$this->set_flash_msg("Record updated successfully", "success");
					return $this->redirect("employee_details");
				}
				else{
					if($db->getLastError()){
						$this->set_page_error();
					}
					elseif(!$numRows){
						//not an error, but no record was updated
						$page_error = "No record updated";
						$this->set_page_error($page_error);
						$this->set_flash_msg($page_error, "warning");
						return	$this->redirect("employee_details");
					}
				}
			}
		}
		$db->where("employee_details.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Employee Details";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("employee_details/edit.php", $data);
	}
	/**
     * Update single field
	 * @param $rec_id (select record by table primary key)
	 * @param $formdata array() from $_POST
     * @return array
     */
	function editfield($rec_id = null, $formdata = null){
		$db = $this->GetModel();
		$this->rec_id = $rec_id;
		$tablename = $this->tablename;
		//editable fields
		$fields = $this->fields = array("id","employee_name","portfolio_id","basic_pay","bank_name","bank_acc_no","email","dob","date_of_join","date_of_retirement","department","IFSC","designation","caste","relegion","nominee","payscale","gender","identification_mark","place_of_birth","balanceleave","mobile","photo","handicapped","blind","employee_type","service_book","service_book_remark");
		$page_error = null;
		if($formdata){
			$postdata = array();
			$fieldname = $formdata['name'];
			$fieldvalue = $formdata['value'];
			$postdata[$fieldname] = $fieldvalue;
			$postdata = $this->format_request_data($postdata);
			$this->rules_array = array(
				'employee_name' => 'required',
				'portfolio_id' => 'required',
				'basic_pay' => 'required|numeric',
				'bank_name' => 'required',
				'bank_acc_no' => 'required',
				'email' => 'required|valid_email',
				'dob' => 'required',
				'date_of_join' => 'required',
				'date_of_retirement' => 'required',
				'department' => 'required',
				'IFSC' => 'required',
				'designation' => 'required',
				'balanceleave' => 'numeric',
				'photo' => 'required',
				'handicapped' => 'required',
				'blind' => 'required',
				'employee_type' => 'required',
			);
			$this->sanitize_array = array(
				'employee_name' => 'sanitize_string',
				'portfolio_id' => 'sanitize_string',
				'basic_pay' => 'sanitize_string',
				'bank_name' => 'sanitize_string',
				'bank_acc_no' => 'sanitize_string',
				'email' => 'sanitize_string',
				'dob' => 'sanitize_string',
				'date_of_join' => 'sanitize_string',
				'date_of_retirement' => 'sanitize_string',
				'department' => 'sanitize_string',
				'IFSC' => 'sanitize_string',
				'designation' => 'sanitize_string',
				'caste' => 'sanitize_string',
				'relegion' => 'sanitize_string',
				'nominee' => 'sanitize_string',
				'payscale' => 'sanitize_string',
				'gender' => 'sanitize_string',
				'identification_mark' => 'sanitize_string',
				'place_of_birth' => 'sanitize_string',
				'balanceleave' => 'sanitize_string',
				'mobile' => 'sanitize_string',
				'photo' => 'sanitize_string',
				'handicapped' => 'sanitize_string',
				'blind' => 'sanitize_string',
				'employee_type' => 'sanitize_string',
				'service_book' => 'sanitize_string',
				'service_book_remark' => 'sanitize_string',
			);
			$this->filter_rules = true; //filter validation rules by excluding fields not in the formdata
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			//Check if Duplicate Record Already Exit In The Database
			if(isset($modeldata['employee_name'])){
				$db->where("employee_name", $modeldata['employee_name'])->where("id", $rec_id, "!=");
				if($db->has($tablename)){
					$this->view->page_error[] = $modeldata['employee_name']." Already exist!";
				}
			}
			//Check if Duplicate Record Already Exit In The Database
			if(isset($modeldata['email'])){
				$db->where("email", $modeldata['email'])->where("id", $rec_id, "!=");
				if($db->has($tablename)){
					$this->view->page_error[] = $modeldata['email']." Already exist!";
				}
			} 
			if($this->validated()){
				$db->where("employee_details.id", $rec_id);;
				$bool = $db->update($tablename, $modeldata);
				$numRows = $db->getRowCount();
				if($bool && $numRows){
					return render_json(
						array(
							'num_rows' =>$numRows,
							'rec_id' =>$rec_id,
						)
					);
				}
				else{
					if($db->getLastError()){
						$page_error = $db->getLastError();
					}
					elseif(!$numRows){
						$page_error = "No record updated";
					}
					render_error($page_error);
				}
			}
			else{
				render_error($this->view->page_error);
			}
		}
		return null;
	}
	/**
     * Delete record from the database
	 * Support multi delete by separating record id by comma.
     * @return BaseView
     */
	function delete($rec_id = null){
		Csrf::cross_check();
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$this->rec_id = $rec_id;
		//form multiple delete, split record id separated by comma into array
		$arr_rec_id = array_map('trim', explode(",", $rec_id));
		$db->where("employee_details.id", $arr_rec_id, "in");
		$bool = $db->delete($tablename);
		if($bool){
			$this->set_flash_msg("Record deleted successfully", "success");
		}
		elseif($db->getLastError()){
			$page_error = $db->getLastError();
			$this->set_flash_msg($page_error, "danger");
		}
		return	$this->redirect("employee_details");
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function empdetails($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("id", 
			"employee_name", 
			"designation", 
			"handicapped", 
			"blind", 
			"employee_type", 
			"service_book", 
			"service_book_remark");
		$pagination = $this->get_pagination(2000); // get current pagination e.g array(page_number, page_limit)
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				employee_details.id LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				employee_details.portfolio_id LIKE ? OR 
				employee_details.basic_pay LIKE ? OR 
				employee_details.bank_acc_no LIKE ? OR 
				employee_details.bank_name LIKE ? OR 
				employee_details.email LIKE ? OR 
				employee_details.dob LIKE ? OR 
				employee_details.date_of_join LIKE ? OR 
				employee_details.date_of_retirement LIKE ? OR 
				employee_details.department LIKE ? OR 
				employee_details.IFSC LIKE ? OR 
				employee_details.designation LIKE ? OR 
				employee_details.caste LIKE ? OR 
				employee_details.relegion LIKE ? OR 
				employee_details.nominee LIKE ? OR 
				employee_details.payscale LIKE ? OR 
				employee_details.gender LIKE ? OR 
				employee_details.identification_mark LIKE ? OR 
				employee_details.place_of_birth LIKE ? OR 
				employee_details.balanceleave LIKE ? OR 
				employee_details.mobile LIKE ? OR 
				employee_details.photo LIKE ? OR 
				employee_details.handicapped LIKE ? OR 
				employee_details.blind LIKE ? OR 
				employee_details.employee_type LIKE ? OR 
				employee_details.service_book LIKE ? OR 
				employee_details.service_book_remark LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "employee_details/search.php";
		}
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("employee_details.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		$tc = $db->withTotalCount();
		$records = $db->get($tablename, $pagination, $fields);
		$records_count = count($records);
		$total_records = intval($tc->totalCount);
		$page_limit = $pagination[1];
		$total_pages = ceil($total_records / $page_limit);
		$data = new stdClass;
		$data->records = $records;
		$data->record_count = $records_count;
		$data->total_records = $total_records;
		$data->total_page = $total_pages;
		if($db->getLastError()){
			$this->set_page_error();
		}
		$page_title = $this->view->page_title = "Employee Details";
		$this->render_view("employee_details/empdetails.php", $data); //render the full page
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function retirementreport($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("id", 
			"employee_name", 
			"department", 
			"designation", 
			"DATEDIFF(  date_of_retirement,CURRENT_TIMESTAMP())   AS days_to_retire");
		$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				employee_details.id LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				employee_details.portfolio_id LIKE ? OR 
				employee_details.basic_pay LIKE ? OR 
				employee_details.bank_acc_no LIKE ? OR 
				employee_details.bank_name LIKE ? OR 
				employee_details.email LIKE ? OR 
				employee_details.dob LIKE ? OR 
				employee_details.date_of_join LIKE ? OR 
				employee_details.date_of_retirement LIKE ? OR 
				employee_details.department LIKE ? OR 
				employee_details.IFSC LIKE ? OR 
				employee_details.designation LIKE ? OR 
				employee_details.caste LIKE ? OR 
				employee_details.relegion LIKE ? OR 
				employee_details.nominee LIKE ? OR 
				employee_details.payscale LIKE ? OR 
				employee_details.gender LIKE ? OR 
				employee_details.identification_mark LIKE ? OR 
				employee_details.place_of_birth LIKE ? OR 
				employee_details.balanceleave LIKE ? OR 
				employee_details.mobile LIKE ? OR 
				employee_details.photo LIKE ? OR 
				employee_details.handicapped LIKE ? OR 
				employee_details.blind LIKE ? OR 
				employee_details.employee_type LIKE ? OR 
				employee_details.service_book LIKE ? OR 
				employee_details.service_book_remark LIKE ? OR 
				DATEDIFF(  date_of_retirement,CURRENT_TIMESTAMP())   LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "employee_details/search.php";
		}
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("days_to_retire", "ASC");
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		$tc = $db->withTotalCount();
		$records = $db->get($tablename, $pagination, $fields);
		$records_count = count($records);
		$total_records = intval($tc->totalCount);
		$page_limit = $pagination[1];
		$total_pages = ceil($total_records / $page_limit);
		$data = new stdClass;
		$data->records = $records;
		$data->record_count = $records_count;
		$data->total_records = $total_records;
		$data->total_page = $total_pages;
		if($db->getLastError()){
			$this->set_page_error();
		}
		$page_title = $this->view->page_title = "Employee Details";
		$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
		$this->view->report_title = $page_title;
		$this->view->report_layout = "report_layout.php";
		$this->view->report_paper_size = "A4";
		$this->view->report_orientation = "portrait";
		$this->render_view("employee_details/retirementreport.php", $data); //render the full page
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function employee_slip($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("employee_details.id", 
			"employee_details.employee_name", 
			"portfolio.name AS portfolio_name", 
			"employee_details.department", 
			"employee_details.designation");
		$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				employee_details.id LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				portfolio.name LIKE ? OR 
				employee_details.portfolio_id LIKE ? OR 
				employee_details.basic_pay LIKE ? OR 
				employee_details.bank_acc_no LIKE ? OR 
				employee_details.bank_name LIKE ? OR 
				employee_details.email LIKE ? OR 
				employee_details.dob LIKE ? OR 
				employee_details.date_of_join LIKE ? OR 
				employee_details.date_of_retirement LIKE ? OR 
				employee_details.department LIKE ? OR 
				employee_details.IFSC LIKE ? OR 
				employee_details.designation LIKE ? OR 
				employee_details.caste LIKE ? OR 
				employee_details.relegion LIKE ? OR 
				employee_details.nominee LIKE ? OR 
				employee_details.payscale LIKE ? OR 
				employee_details.gender LIKE ? OR 
				employee_details.identification_mark LIKE ? OR 
				employee_details.place_of_birth LIKE ? OR 
				employee_details.balanceleave LIKE ? OR 
				employee_details.mobile LIKE ? OR 
				employee_details.photo LIKE ? OR 
				employee_details.handicapped LIKE ? OR 
				employee_details.blind LIKE ? OR 
				employee_details.employee_type LIKE ? OR 
				employee_details.service_book LIKE ? OR 
				employee_details.service_book_remark LIKE ? OR 
				portfolio.pid LIKE ?
			)";
			$search_params = array(
				"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
			);
			//setting search conditions
			$db->where($search_condition, $search_params);
			 //template to use when ajax search
			$this->view->search_template = "employee_details/search.php";
		}
		$db->join("portfolio", "employee_details.portfolio_id = portfolio.id", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("employee_details.id", ORDER_TYPE);
		}
		if($fieldname){
			$db->where($fieldname , $fieldvalue); //filter by a single field name
		}
		if(!empty($request->portfolio_name)){
			$val = $request->portfolio_name;
			$db->where("portfolio.name", $val , "=");
		}
		if(!empty($request->employee_details_department)){
			$val = $request->employee_details_department;
			$db->where("employee_details.department", $val , "=");
		}
		$tc = $db->withTotalCount();
		$records = $db->get($tablename, $pagination, $fields);
		$records_count = count($records);
		$total_records = intval($tc->totalCount);
		$page_limit = $pagination[1];
		$total_pages = ceil($total_records / $page_limit);
		$data = new stdClass;
		$data->records = $records;
		$data->record_count = $records_count;
		$data->total_records = $total_records;
		$data->total_page = $total_pages;
		if($db->getLastError()){
			$this->set_page_error();
		}
		$page_title = $this->view->page_title = "Employee Details";
		$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
		$this->view->report_title = $page_title;
		$this->view->report_layout = "report_layout.php";
		$this->view->report_paper_size = "A4";
		$this->view->report_orientation = "portrait";
		$this->render_view("employee_details/employee_slip.php", $data); //render the full page
	}
}
