<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Movement extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('hr/model_sfmove','staff');
		$this->load->model('CRM/model_crm','crm');
		$this->load->model('Admin/model_company','company');
		$this->load->model('Admin/model_user','user');
		$this->load->helper('date');
		$this->load->model('analysis/model_analysis','model_analysis'); //Fakru Add for analysis purpose
		$this->logged_user = $this->session->userdata('user');

		if($this->logged_user)
		{
			$this->login->checkModuleAccess(4);
			$this->logged_module = $this->session->userdata('module_data');
		}
		else
		{
			redirect(base_url());
		}	
	}

	function index($param=0)
	{
		return $param;
	}

	function groups()
	{

		$this->session->unset_userdata('groupsList');
		$this->session->unset_userdata('filter_start_date');
		$this->session->unset_userdata('filter_end_date');
		$this->session->unset_userdata('filter_type');
		$this->session->unset_userdata('filter_client');
		$this->session->unset_userdata('filter_department');

		if ($this->isModerator()) 
		{
			redirect(base_url('hr/movement/groups_admin'));
		}

		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', 'Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			// 'movements'		=> $this->staff->getMovement(),
			'movementTypes'	=> $this->staff->getMoveType(),
		);

		// $this->session->set_userdata('groupsList',$data['movements']);
		$this->session->set_userdata('filter_start_date','ALL');
		$this->session->set_userdata('filter_end_date','');
		$this->template->view('hr/staff-movement/group_movement.php',$data); 
		

	}

	function groups_admin()
	{
		$this->session->unset_userdata('groupsList');
		$this->session->unset_userdata('filter_start_date');
		$this->session->unset_userdata('filter_end_date');
		$this->session->unset_userdata('filter_type');
		$this->session->unset_userdata('filter_client');
		
		// open before upload to dev and production
		if (!$this->isModerator()) 
		{
			redirect(base_url('hr/movement/groups'));
		}
		
		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', 'Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			'movements'		=> $this->staff->getMovement(),
			'movementTypes'	=> $this->staff->getMoveType()
		);

		$this->session->set_userdata('groupsList',$data['movements']);
		$this->session->set_userdata('filter_start_date','ALL');
		$this->session->set_userdata('filter_end_date','');
		$this->template->view('hr/staff-movement/group_movement__admin.php',$data);
	}

	function create()
	{
		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', 'Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			'users'			=> $this->user->getUsersOrderByName(),
			'movementTypes'	=> $this->staff->getMoveType(),
		);

		$this->template->view('hr/staff-movement/add_steps.php',$data); 
	}

	function edit($id)
	{
		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', 'Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			'users'			=> $this->user->getUsers(),
			'movementTypes'	=> $this->staff->getMoveType(),
		);
		$this->template->view('hr/staff-movement/edit_movement.php',$data);
	}
	
	function movement_list($groupid = null)
	{
		$this->session->unset_userdata('movementLists');
		$this->session->unset_userdata('filter_start_date');
		$this->session->unset_userdata('filter_end_date');
		$this->session->unset_userdata('filter_dept');
		$this->session->unset_userdata('filter_staff');
		$this->session->unset_userdata('filter_type');
		$this->session->unset_userdata('filter_client');

		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', 'Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			'movementTypes'	=> $this->staff->getMoveType(),
			'users'			=> $this->user->getUsers(),
			'movementLists'	=> $this->staff->getMovementStaff(null,null, null, null, null, null, null, null,null,$groupid),
		);
		$this->session->set_userdata('movementLists',$data['movementLists']);
		$this->session->set_userdata('filter_start_date','ALL');
		$this->session->set_userdata('filter_end_date','');
		$this->template->view('hr/staff-movement/staff_movementList.php',$data);
	}

	function calendar()
	{
		// echo "sini";
		// exit();
		$date 		= ($this->input->post('date') && $this->input->post('date') != '')? date('Y-m-d',strtotime($this->input->post('date'))) : date('Y-m-d');
		$dept_id 	= ($this->input->post('dept_id') && $this->input->post('dept_id') != '-1')?$this->input->post('dept_id'):null;
		$type_id	= ($this->input->post('type_id') && $this->input->post('type_id') != '-1')?$this->input->post('type_id'):null;
		$client_id	= ($this->input->post('client_id') && $this->input->post('client_id') != '-1')?$this->input->post('client_id'):null;

		$movements = $this->staff->getMovementStaff(null,$client_id,$date,null,null,null,$type_id,null,$dept_id,null);
		
		$fc_res 	= array();
		$fc_event 	= array();
		$resource 	= array();

		if($movements != 'no result')
		{
			foreach ($movements as $key => $move) 
			{
				
				$tmpRes 		= new stdClass(); $tmpEvent = new stdClass();
				$tmpRes->id  	= $move->staffid;
				$tmpRes->title 	= $move->staffName;
			
				$tmpEvent->id 			= $key;
				$tmpEvent->resourceId 	= $move->staffid;
				$tmpEvent->start 		= $move->date.' '.$move->timeOut;
				$tmpEvent->end 			= $move->date.' '.$move->timeIn;
				$tmpEvent->title 		= $move->customerName;
				$tmpEvent->color 		= $move->color_code;

				array_push($fc_res, $tmpRes);
				array_push($fc_event, $tmpEvent);
				array_push($resource, $move);
			}
		}

		$data = array
		(
			'date'			=> $date,
			'page_setup' 	=> $this->template->pageSetup('HR', 'Staff Movement', ' Staff Movement'),
			'departments'	=> $this->company->getDepartments(),
			'customers'		=> $this->crm->getCustomer(),
			'movementTypes'	=> $this->staff->getMoveType(),
			'fc_res'		=> json_encode($fc_res),
			'fc_event'		=> json_encode($fc_event),
			'resource'		=> json_encode($resource),
		);

		$this->model_analysis->add_access($this->logged_user['id'],2); //Fakru Add for analysis purpose

		$this->template->view('hr/staff-movement/calendar-test.php',$data);
	}


	function main_report() 
	{
		$header = array();
		$header['title'] = 'Staff Movement';
		$this->load->view('hr/report/header.php', $header);
		$this->load->view('hr/report/staff-movement/movements.php');
		$this->load->view('hr/report/footer.php');
	}

	function list_report() 
	{
		$header = array();
		$header['title'] = 'Staff Movement List';
		$this->load->view('hr/report/header.php', $header);
		$this->load->view('hr/report/staff-movement/individual.php');
		$this->load->view('hr/report/footer.php');
	}
 	
 	function group_report() 
	{
		$header = array();
		$header['title'] = 'Staff Movement List';
		$this->load->view('hr/report/header.php', $header);
		$this->load->view('hr/report/staff-movement/groups.php');
		$this->load->view('hr/report/footer.php');	
	}

	private function isManager()
	{
		if($_SESSION['user']['position_id'] != 12){
			return false;
		}
		return true;
	}

	private function isModerator()
	{
		if ($_SESSION['module_data']['role'] != 'moderator') {
			return false;
		}
		return true;
	}
}?>