<?php 
/**
 * Absent_in_out Page Controller
 * @category  Controller
 */
class Absent_in_outController extends SecureController{
	function __construct(){
		parent::__construct();
		$this->tablename = "absent_in_out";
	}
	/**
     * 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("id", 
			"attendance_type", 
			"in_out", 
			"for_date", 
			"entrytime", 
			"time");
		$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 = "(
				absent_in_out.id LIKE ? OR 
				absent_in_out.empid LIKE ? OR 
				absent_in_out.userid LIKE ? OR 
				absent_in_out.attendance_type LIKE ? OR 
				absent_in_out.in_out LIKE ? OR 
				absent_in_out.for_date LIKE ? OR 
				absent_in_out.entrytime LIKE ? OR 
				absent_in_out.time LIKE ?
			)";
			$search_params = array(
				"%$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 = "absent_in_out/search.php";
		}
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("absent_in_out.id", ORDER_TYPE);
		}
		$db->where("userid='".USER_ID."'");
		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 = "Absent In Out";
		$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("absent_in_out/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("absent_in_out.id", 
			"absent_in_out.empid", 
			"absent_in_out.userid", 
			"absent_in_out.attendance_type", 
			"absent_in_out.in_out", 
			"absent_in_out.for_date", 
			"absent_in_out.entrytime", 
			"absent_in_out.time", 
			"user_info.id AS user_info_id", 
			"user_info.username AS user_info_username", 
			"user_info.password AS user_info_password", 
			"user_info.email AS user_info_email", 
			"user_info.user_full_name AS user_info_user_full_name", 
			"user_info.user_role_id AS user_info_user_role_id", 
			"user_info.portfolio_id AS user_info_portfolio_id", 
			"employee_details.id AS employee_details_id", 
			"employee_details.employee_name AS employee_details_employee_name", 
			"employee_details.portfolio_id AS employee_details_portfolio_id", 
			"employee_details.basic_pay AS employee_details_basic_pay", 
			"employee_details.bank_acc_no AS employee_details_bank_acc_no", 
			"employee_details.bank_name AS employee_details_bank_name", 
			"employee_details.email AS employee_details_email", 
			"employee_details.dob AS employee_details_dob", 
			"employee_details.date_of_join AS employee_details_date_of_join", 
			"employee_details.date_of_retirement AS employee_details_date_of_retirement", 
			"employee_details.department AS employee_details_department", 
			"employee_details.IFSC AS employee_details_IFSC", 
			"employee_details.designation AS employee_details_designation", 
			"employee_details.caste AS employee_details_caste", 
			"employee_details.relegion AS employee_details_relegion", 
			"employee_details.nominee AS employee_details_nominee", 
			"employee_details.payscale AS employee_details_payscale", 
			"employee_details.gender AS employee_details_gender", 
			"employee_details.identification_mark AS employee_details_identification_mark", 
			"employee_details.place_of_birth AS employee_details_place_of_birth", 
			"employee_details.balanceleave AS employee_details_balanceleave", 
			"employee_details.mobile AS employee_details_mobile", 
			"employee_details.photo AS employee_details_photo");
		if($value){
			$db->where($rec_id, urldecode($value)); //select record based on field name
		}
		else{
			$db->where("absent_in_out.id", $rec_id);; //select record based on primary key
		}
		$db->join("user_info", "absent_in_out.userid = user_info.username", "RIGHT ");
		$db->join("employee_details", "user_info.email = employee_details.email", "RIGHT ");  
		$record = $db->getOne($tablename, $fields );
		if($record){
			$page_title = $this->view->page_title = "View  Absent In Out";
		$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("absent_in_out/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("empid","userid","attendance_type","in_out","for_date","time");
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'attendance_type' => 'required',
				'in_out' => 'required',
				'for_date' => 'required',
				'time' => 'required',
			);
			$this->sanitize_array = array(
				'attendance_type' => 'sanitize_string',
				'in_out' => 'sanitize_string',
				'for_date' => 'sanitize_string',
				'time' => 'sanitize_string',
			);
			$this->filter_vals = true; //set whether to remove empty fields
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			$modeldata['empid'] = USER_ID;
$modeldata['userid'] = USER_ID;
			if($this->validated()){
				$rec_id = $this->rec_id = $db->insert($tablename, $modeldata);
				if($rec_id){
		# Statement to execute after adding record
//query the database and return a single field value
$db->where("email", USER_EMAIL);
$value = $db->getValue("employee_details", "id");
$table_data = array(
    "empid" => $value 
);
$db->where("id", $rec_id);
$bool = $db->update("absent_in_out", $table_data);
		# End of after add statement
					$this->set_flash_msg("Record added successfully", "success");
					return	$this->redirect("Home");
				}
				else{
					$this->set_page_error();
				}
			}
		}
		$page_title = $this->view->page_title = "Add New Absent In Out";
		$this->render_view("absent_in_out/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","empid","userid","attendance_type","in_out","for_date","time");
		if($formdata){
			$postdata = $this->format_request_data($formdata);
			$this->rules_array = array(
				'attendance_type' => 'required',
				'in_out' => 'required',
				'for_date' => 'required',
				'time' => 'required',
			);
			$this->sanitize_array = array(
				'attendance_type' => 'sanitize_string',
				'in_out' => 'sanitize_string',
				'for_date' => 'sanitize_string',
				'time' => 'sanitize_string',
			);
			$modeldata = $this->modeldata = $this->validate_form($postdata);
			$modeldata['empid'] = USER_ID;
$modeldata['userid'] = USER_ID;
			if($this->validated()){
				$db->where("absent_in_out.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("absent_in_out");
				}
				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("absent_in_out");
					}
				}
			}
		}
		$db->where("absent_in_out.id", $rec_id);;
		$data = $db->getOne($tablename, $fields);
		$page_title = $this->view->page_title = "Edit  Absent In Out";
		if(!$data){
			$this->set_page_error();
		}
		return $this->render_view("absent_in_out/edit.php", $data);
	}
	/**
     * 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("absent_in_out.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("absent_in_out");
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function presenttoday($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("absent_in_out.id", 
			"employee_details.employee_name AS employee_details_employee_name", 
			"absent_in_out.attendance_type", 
			"absent_in_out.in_out", 
			"employee_details.department AS employee_details_department", 
			"employee_details.designation AS 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 = "(
				absent_in_out.id LIKE ? OR 
				absent_in_out.empid LIKE ? OR 
				absent_in_out.userid LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				absent_in_out.attendance_type LIKE ? OR 
				absent_in_out.in_out LIKE ? OR 
				absent_in_out.for_date LIKE ? OR 
				absent_in_out.entrytime LIKE ? OR 
				absent_in_out.time LIKE ? OR 
				user_info.id LIKE ? OR 
				user_info.username LIKE ? OR 
				user_info.password LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				user_info.email LIKE ? OR 
				user_info.user_role_id LIKE ? OR 
				user_info.portfolio_id LIKE ? OR 
				employee_details.id 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 ?
			)";
			$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%","%$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 = "absent_in_out/search.php";
		}
		$db->join("user_info", "absent_in_out.userid = user_info.id", "INNER");
		$db->join("employee_details", "user_info.email = employee_details.email", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("absent_in_out.id", ORDER_TYPE);
		}
		$db->where("attendance_type='ATTENDANCE' AND in_out='IN' AND for_date=CURRENT_DATE()");
		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 = "Absent In Out";
		$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("absent_in_out/presenttoday.php", $data); //render the full page
	}
	/**
     * List page records
     * @param $fieldname (filter record by a field) 
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
	function absenttoday($fieldname = null , $fieldvalue = null){
		$request = $this->request;
		$db = $this->GetModel();
		$tablename = $this->tablename;
		$fields = array("absent_in_out.id", 
			"absent_in_out.empid", 
			"employee_details.employee_name AS employee_details_employee_name", 
			"absent_in_out.attendance_type", 
			"absent_in_out.in_out", 
			"employee_details.id AS employee_details_id", 
			"employee_details.department AS employee_details_department", 
			"employee_details.designation AS employee_details_designation");
		$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
	#Statement to execute before list record
	$empdata = $db->get("employee_details");
	# End of before list statement
		//search table record
		if(!empty($request->search)){
			$text = trim($request->search); 
			$search_condition = "(
				absent_in_out.id LIKE ? OR 
				absent_in_out.empid LIKE ? OR 
				absent_in_out.userid LIKE ? OR 
				employee_details.employee_name LIKE ? OR 
				absent_in_out.attendance_type LIKE ? OR 
				absent_in_out.in_out LIKE ? OR 
				absent_in_out.for_date LIKE ? OR 
				absent_in_out.entrytime LIKE ? OR 
				absent_in_out.time LIKE ? OR 
				user_info.id LIKE ? OR 
				user_info.username LIKE ? OR 
				user_info.password LIKE ? OR 
				user_info.user_full_name LIKE ? OR 
				user_info.email LIKE ? OR 
				user_info.user_role_id LIKE ? OR 
				user_info.portfolio_id LIKE ? OR 
				employee_details.id 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 ?
			)";
			$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%","%$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 = "absent_in_out/search.php";
		}
		$db->join("user_info", "absent_in_out.userid = user_info.id", "INNER");
		$db->join("employee_details", "user_info.email = employee_details.email", "INNER");
		if(!empty($request->orderby)){
			$orderby = $request->orderby;
			$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
			$db->orderBy($orderby, $ordertype);
		}
		else{
			$db->orderBy("absent_in_out.id", ORDER_TYPE);
		}
		$db->where("attendance_type='ATTENDANCE' AND in_out='IN' AND for_date=CURRENT_DATE()");
		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 = "Absent In Out";
		$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("absent_in_out/absenttoday.php", $data); //render the full page
	}
}
