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

class Report extends Admin_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model("mod_report");	
		$this->load->library('user_agent');	
	}
	

	function index()
	{
		$tab['activeTab'] = "report";
		$data["report"] = $this->mod_report->getReport();
		$data["report"] = $data["report"]->result_array(); 
		
		$this->load->view('header',$tab);
		$this->load->view('report/view',$data);
		$this->load->view('footer');
	}
	
	
	function add()
	{
		$tab['activeTab'] = "report";
		$this->form_validation->set_rules('date', 'Date Service', 'trim|required');
		$this->form_validation->set_rules('company', 'Company Name', 'trim|required');
		$this->form_validation->set_rules('guset', 'Guset Name', 'trim|required');
		$this->form_validation->set_rules('driver', 'Driver Name', 'trim|required');
		$this->form_validation->set_rules('cartype', 'Car Type', 'trim|required');
		$this->form_validation->set_rules('servicedetail', 'Service Detail', 'trim|required');
		if ($this->form_validation->run() == TRUE)
		{
			//Altering Database
			$data = array(
				'Date'			=>	date("Y-m-d", strtotime($this->input->post('date',TRUE))),
				'Company'		=>	$this->input->post('company',TRUE),
				'GuestName'		=>	$this->input->post('guset',TRUE),
				'DriverName'	=>	$this->input->post('driver',TRUE),
				'Seller'		=>	$this->input->post('sellerName',TRUE),
				'CarType'		=>	$this->input->post('cartype',TRUE),
				'ServiceDetail'	=>	$this->input->post('servicedetail',TRUE),
				'VocherNumber'	=>	$this->input->post('vochernumber',TRUE),
				'InvoiceNumber'	=>	$this->input->post('invoicenumber',TRUE),
				'Remark'		=>	$this->input->post('remark',TRUE),
				'Status'		=>	1,
				'CreatedDate'	=>	date('Y-m-d H:i:s'),
				'CreatedBy'		=>	$this->session->userdata('userId'),
				'ModifiedDate'	=>	date('Y-m-d H:i:s'),
				'ModifiedBy'	=>	$this->session->userdata('userId'),
			);
			$msg = $this->mod_report->add($data);	
			//End Altering Database
			if($msg == 1 || $msg == 0){
				$this->session->set_flashdata('message',"New Report is Successfully Added");
				redirect('admin/report/add');
				exit();	
			}else{
				$this->session->set_flashdata('message',"New Report is Failure Added!");
				redirect('admin/report/add');
				exit();				
			}
		}
			
		$this->load->view("header",$tab);
		$this->load->view("report/add");
		$this->load->view("footer");	
	}
	
	
	function edit()
	{	
		$tab['activeTab'] = "report";
		if(empty($this->session->userdata('refered_from'))){
			$this->session->set_userdata('refered_from', $this->agent->referrer());
		}
		$id = $this->uri->segment(4);
		$userId = $this->session->userdata('userId');
		$result = $this->mod_report->getById($id);
		if(!$result)
		{
			redirect("admin/report");
			exit();	
		}
			
		if(@$_POST['update_report'])
		{
			$this->form_validation->set_rules('date', 'Date Service', 'trim|required');
			$this->form_validation->set_rules('company', 'Company Name', 'trim|required');
			$this->form_validation->set_rules('guset', 'Guset Name', 'trim|required');
			$this->form_validation->set_rules('cartype', 'Car Type', 'trim|required');
			$this->form_validation->set_rules('servicedetail', 'Service Detail', 'trim|required');
			
			if($userId == '2'||$userId == '3'||$userId == '5')
			{
				
			}else{
			   $this->form_validation->set_rules('remark', 'Remark', 'trim|required');
			}

			if ($this->form_validation->run() == TRUE)
			{
				if($userId == '2'||$userId =='3'||$userId =='5'){
					$vocher = $this->input->post('vochernumber',TRUE);
					$invoice = $this->input->post('invoicenumber',TRUE);	
				}else{
					$vocher = $result->VocherNumber;
					$invoice = $result->InvoiceNumber;	
				}

				$data = array(
						'Date'			=>	date("Y-m-d", strtotime($this->input->post('date',TRUE))),
						'Company'		=>	$this->input->post('company',TRUE),
						'GuestName'		=>	$this->input->post('guset',TRUE),
						'DriverName'	=>	$this->input->post('driver',TRUE),
						'Seller'		=>	$this->input->post('sellerName',TRUE),
						'CarType'		=>	$this->input->post('cartype',TRUE),
						'ServiceDetail'	=>	$this->input->post('servicedetail',TRUE),
						'VocherNumber'	=>	$vocher,
						'InvoiceNumber'	=>	$invoice,
						'Remark'		=>	$this->input->post('remark'),
						'Status'		=>	1,
						'ModifiedDate'	=>	date('Y-m-d H:i:s'),
						'ModifiedBy'	=>	$this->session->userdata('userId'),
				);
					
				$msg = $this->mod_report->update($data,$id);
				if($msg == 1 || $msg == 0)
				{
					$this->session->set_flashdata('message',"Report is Successfully Update");
					redirect($this->session->userdata('refered_from'));
					$this->session->unset_userdata('refered_from');
					exit();	
				}else{
					$this->session->set_flashdata('message',"Report is Failure Update!");	
					redirect($this->session->userdata('refered_from'));
					$this->session->unset_userdata('refered_from');
					exit();	
				}	
			}else{
				
				$this->session->set_flashdata('errors', validation_errors());
				redirect("site/report");	
				exit();		
			}	
		}
			
		$data['report'] = $result;
		$data['userId'] = $userId;
		$this->load->view("header",$tab);
		$this->load->view("report/edit",$data);
		$this->load->view("footer");		
	}
	
	function detail()
	{	
		$tab['activeTab'] = "report";
		if(empty($this->session->userdata('refered_from'))){
			$this->session->set_userdata('refered_from', $this->agent->referrer());
		}
		$id = $this->uri->segment(4);
		$userId = $this->session->userdata('userId');
		$result = $this->mod_report->getById($id);
		if(!$result)
		{
			redirect("admin/report");
			exit();	
		}
		
		$data['report'] = $result;
		$data['userId'] = $userId;
		$this->load->view("header",$tab);
		$this->load->view("report/detail",$data);
		$this->load->view("footer");	
	}
	
		
	function delete()
	{
		$id = $this->uri->segment(4);
		$msg = $this->mod_report->delete($id);
		if($msg == 1 || $msg == 0){
			$this->session->set_flashdata('message',"Report deleted successfully");
			redirect("admin/report");
			exit();		
		}else{
			$this->session->set_flashdata('message',"Report deleted Failure!");
			redirect("admin/report");
			exit();		
		}
	}
	
	
	function filterDate()
	{
		$data = array(
			'From'  => $this->input->get('date1'),
			'To' 	=> $this->input->get('date2'),
		);

		$this->form_validation->set_data($data);
		$this->form_validation->set_rules('From', 'From Date', 'trim|required');
		$this->form_validation->set_rules('To', 'To Date', 'trim|required');
		if($this->form_validation->run() == TRUE)
		{
			$fromDate = date("Y-m-d", strtotime($this->input->get('date1')));
			$toDate = date("Y-m-d", strtotime($this->input->get('date2')));
			$name = trim($this->input->get('name'));

			$newdata = array(
					'fromDate'  	=> $this->input->get('date1'),
					'toDate'    	=> $this->input->get('date2'),
					'name'  	=> $name,
			);
			
			if($this->input->get('date1')!= null and $this->input->get('date2')!= null){
				$data = array(
					'fromDate'  	=> '',
					'toDate'    	=> '',
					'name'  		=> '',
				);
			}
			$this->session->set_userdata($newdata);

			if($smts = $this->mod_report->report_countAll($fromDate,$toDate,$name) AND count($smts) > 0){
				$data["report"] = $this->mod_report->getSort($fromDate,$toDate,$name);
				$data["report"] = $data["report"]->result_array();
				$this->load->view('header');
				$this->load->view('report/view',$data);
				$this->load->view('footer');
			}
			else
			{	
		        $this->session->set_flashdata('message',"Sorry No Report Display!");
	 	        redirect("admin/report");
				exit();
			}
		}
	}	
  	
}




