<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
require_once ("person_controller.php");
class Expences extends Person_controller
{
	function __construct()
	{
		parent::__construct('expences');
		$this->load->model('Expence');
	}
	
	function index($offset=0)
	{
            
		$params = $this->session->userdata('expence_search_data') ? $this->session->userdata('expence_search_data') : array('offset' => 0, 'order_col' => 'expence_time', 'order_dir' => 'asc', 'search' => FALSE);
		if ($offset!=$params['offset'])
		{
		   redirect('expences/index/'.$params['offset']);
		}
		$this->check_action_permission('search');
		$config['base_url'] = site_url('expences/sorting');
		$config['per_page'] = $this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20; 
		$data['controller_name']=strtolower(get_class());
		$data['per_page'] = $config['per_page'];
		$data['search'] = $params['search'] ? $params['search'] : "";
		if ($data['search'])
		{
			$config['total_rows'] = $this->Expence->search_count_all($data['search']);
			$table_data = $this->Expence->search($data['search'],$data['per_page'],$params['offset'],$params['order_col'],$params['order_dir']);
		}
		else
		{
			$config['total_rows'] = $this->Expence->count_all();
			$table_data = $this->Expence->get_all($data['per_page'],$params['offset'],$params['order_col'],$params['order_dir']);
		}
		
		$data['total_rows'] = $config['total_rows'];
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['order_col'] = $params['order_col'];
		$data['order_dir'] = $params['order_dir'];
		
		$data['manage_table']=get_expence_manage_table($table_data,$this);
		$this->load->view('expences/manage',$data);
	}
	
	
	function receipt($expence_id)
	{
		$expence_info = $this->Expence->get_info($expence_id);
		//print_r($expence_info);
		$data['total']=$expence_info['amount'];
		$data['expence_type']=$expence_info['expence_type'];
		$data['receipt_title']=ucfirst('Expense Receipt');
		$data['expence_time']= date(get_date_format().' '.get_time_format(), strtotime($expence_info['expence_time']));
		$emp_info=$this->Employee->get_info($expence_info['employee_id']);
		$data['title']=$expence_info['title'];
		$data['employee']=$emp_info->first_name.' '.$emp_info->last_name;
		$location_info=$this->Location->get_info($expence_info['location_id']);
		$data['location']=$location_info->name;
		$data['comments']=$expence_info['comments'];
		$data['paid_to']=$expence_info['paid_to'];
		$data['ref_no']=$expence_info['ref_no'];

		$data['expence_id']='Expense '.$expence_id;
		$this->load->view("expences/receipt",$data);
		//$this->removeDuplicateSessionCookieHeaders();

	}
	
	function sorting()
	{
		$this->check_action_permission('search');
		$search=$this->input->post('search') ? $this->input->post('search') : "";
		$per_page=$this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20;
		$offset = $this->input->post('offset') ? $this->input->post('offset') : 0;
		$order_col = $this->input->post('order_col') ? $this->input->post('order_col') : 'last_name';
		$order_dir = $this->input->post('order_dir') ? $this->input->post('order_dir'): 'asc';

		$expence_search_data = array('offset' => $offset, 'order_col' => $order_col, 'order_dir' => $order_dir, 'search' => $search);
		$this->session->set_userdata("expence_search_data",$expence_search_data);
		if ($search)
		{
			$config['total_rows'] = $this->Expence->search_count_all($search);
			$table_data = $this->Expence->search($search,$per_page,$this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'last_name' ,$this->input->post('order_dir') ? $this->input->post('order_dir'): 'asc');
		}
		else
		{
			$config['total_rows'] = $this->Expence->count_all();
			$table_data = $this->Expence->get_all($per_page,$this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'last_name' ,$this->input->post('order_dir') ? $this->input->post('order_dir'): 'asc');
		}
		$config['base_url'] = site_url('employees/sorting');
		$config['per_page'] = $per_page; 
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['manage_table']=get_expence_manage_table_data_rows($table_data,$this);
		echo json_encode(array('manage_table' => $data['manage_table'], 'pagination' => $data['pagination']));
	}
	
	function clear_state()
	{
		$this->session->unset_userdata('expence_search_data');
		redirect('expences');
	}

	function check_duplicate()
	{
		echo json_encode(array('duplicate'=>$this->Employee->check_duplicate($this->input->post('term'))));

	}
	/* added for excel expert */
	function excel_export() {
		$data = $this->Employee->get_all()->result_object();
		$this->load->helper('report');
		$rows = array();
		$row = array(lang('employees_username'),lang('common_first_name'),lang('common_last_name'),lang('common_email'),lang('common_phone_number'),lang('common_address_1'),lang('common_address_2'),lang('common_city'),	lang('common_state'),lang('common_zip'),lang('common_country'),lang('common_comments'));
		$rows[] = $row;
		foreach ($data as $r) {
			$row = array(
				$r->username,
				$r->first_name,
				$r->last_name,
				$r->email,
				$r->phone_number,
				$r->address_1,
				$r->address_2,
				$r->city,
				$r->state,
				$r->zip,
				$r->country,
				$r->comments
			);
			$rows[] = $row;
		}
		
		$content = array_to_csv($rows);
		force_download('employees_export' . '.csv', $content);
		exit;
	}
	
	
	
	
	
	
	/*
	Returns employee table data rows. This will be called with AJAX.
	*/
	function search()
	{
		$this->check_action_permission('search');
		$search=$this->input->post('search');
		$offset = $this->input->post('offset') ? $this->input->post('offset') : 0;
		$order_col = $this->input->post('order_col') ? $this->input->post('order_col') : 'expence_time';
		$order_dir = $this->input->post('order_dir') ? $this->input->post('order_dir'): 'asc';

		$expence_search_data = array('offset' => $offset, 'order_col' => $order_col, 'order_dir' => $order_dir, 'search' => $search);
		$this->session->set_userdata("expence_search_data",$expence_search_data);
		$per_page=$this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20;
		$search_data=$this->Expence->search($search,$per_page,$this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'last_name' ,$this->input->post('order_dir') ? $this->input->post('order_dir'): 'asc');
		$config['base_url'] = site_url('expences/search');
		$config['total_rows'] = $this->Expence->search_count_all($search);
		$config['per_page'] = $per_page ;
		$this->pagination->initialize($config);				
		$data['pagination'] = $this->pagination->create_links();
		$data['manage_table']=get_expence_manage_table_data_rows($search_data,$this);
		echo json_encode(array('manage_table' => $data['manage_table'], 'pagination' => $data['pagination']));
		
	}
	
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Expence->get_search_suggestions($this->input->get('term'),100);
		echo json_encode($suggestions);
	}
	
	/*
	Loads the expence edit form
	*/
	function view($expence_id=-1,$redirect_code=0)
	{
		error_reporting(0);
		$expence_info = $this->Expence->get_info($expence_id);
		$data = array();
		$data['max_expence_id'] = $this->Expence->get_max_id();
		//$data['needs_auth'] = FALSE;
         
		
		$data['expence_info']=$expence_info;
		$data['redirect']=$redirect_code;
		$this->load->view("expences/form",$data);
	
	}
	
	
	function exmployee_exists()
	{
		if($this->Employee->employee_username_exists($this->input->post('username')))
		echo 'false';
		else
		echo 'true';
		
	}
	/*
	Inserts/updates an expence
	*/
	function save($expence_id=-1)
	{
		
            
		$expence_data = array(
		'title'=>$this->input->post('title'),
		'ref_no'=>$this->input->post('ref_no'),
		'paid_to'=>$this->input->post('paid_to'),
		'comments'=>$this->input->post('comments'),
		'expence_type'=>$this->input->post('expence_type'),
		'amount'=>$this->input->post('amount'),
		'expence_time'=> date('Y-m-d H:i:s',strtotime($this->input->post('expence_time').date('H:i:s'))),
		'location_id' => $this->Employee->get_current_location_id(),
		'employee_id' => $this->Employee->get_logged_in_employee_info()->person_id,
			
	);

		
		$redirect = $this->input->post('redirect');

		
		$expence_id = $this->Expence->save($expence_data,$expence_id);
                if(isset($expence_id))
		{
			//New item
			if($expence_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>'Expence successfully added! '.
				$expence_data['title'],'expence_id'=>$expence_data['expence_id']));
			}
			else //previous item
			{
				echo json_encode(array('success'=>true,'message'=>'Expence Succesfuly updated! '.
				$expence_data['title'],'expence_id'=>$expence_id,'redirect'=>$redirect));
			}
		}
		else//failure
		{
			echo json_encode(array('success'=>false,'message'=>'some error occue please try again'));
		}

	}
	
	/*
	This deletes employees from the employees table
	*/
	function delete()
	{
		$this->check_action_permission('delete');
		$employees_to_delete=$this->input->post('ids');
		
		if (in_array(1,$employees_to_delete))
		{
			//failure
			echo json_encode(array('success'=>false,'message'=>lang('employees_cannot_delete_default_user')));
		}
		elseif($this->Employee->delete_list($employees_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>lang('employees_successful_deleted').' '.
			count($employees_to_delete).' '.lang('employees_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>lang('employees_cannot_be_deleted')));
		}
	}
	
	function set_employee_current_location_id()
	{
            $this->Employee->set_employee_current_location_id($this->input->post('employee_current_location_id'));
	}
		
	function cleanup()
	{
		$this->Employee->cleanup();
		echo json_encode(array('success'=>true,'message'=>lang('employees_cleanup_sucessful')));
	}
}
?>