HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /var/www/html/pmw24/app/application/controllers/admin/Job_12_08_2019.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Job extends MY_Controller {
	public function __construct() {
		parent::__construct();
		$this->redirect_guest();
		//$this->load->model('mcommon');
		$this->admin=$this->session->userdata('admin');
		$this->load->model('admin/mjob');
	}
		
	public function index() {	
		// $condition['type']=1;
		// $udata['is_read']=1;
		// $this->mcommon->update('notification',$condition,$udata);	
		$this->_load_list_view();		
	}
	
	private function _load_list_view() {
		$data['admin'] = $this->admin;
		$data['content'] = 'admin/job/list';
		$this->load->view('admin/layouts/index', $data);
	}
	
	
	
	public function all_content_list(){
		$list = $this->mjob->get_datatables();
		$data = array();
        $no = $_POST['start'];
		$i=1;
		foreach ($list as $person) {
			$row = array();	
			$row[]=$i;		
			
			//$condition = array("job_id"=>$person->job_id,"assign_status"=>1);
			$condition = array("job_id"=>$person->job_id);
			$assign_job = $this->mjob->getRow('assign_job',$condition);
			
			if(!empty($assign_job))
			{
				$mechanic_condition = array("admin_id"=>$assign_job['mechanic_id']);
				$mechanic = $this->mjob->getRow('admins',$mechanic_condition);
				
				if(!empty($mechanic)){
					$row[] = $person->car_no." (".$mechanic['first_name']." ".$mechanic['last_name'].")";
				}
			}else{
				$row[] = $person->car_no;
			}
			
			
			$new_service = array();
			$services = $this->mjob->getServices($person->job_id);
			foreach($services as $key=>$service)
			{
				$new_service[$key] = $service['service_name'];
			}
			
			$service = implode(',',$new_service);									
			$row[] = $service;
			
			$service_date_condition = array("job_id"=>$person->job_id);
			$job_service_date = $this->mjob->getRow("job_service_date",$service_date_condition);
			//$row[] = $job_service_date['service_date'];
			
			$row[] = date("d/m/Y", strtotime($job_service_date['service_date']));
			
			if($person->job_status == 0){
				$job_status = "Not Started";
			}else if($person->job_status == 1){
				$job_status = "In Progress";
			}else if($person->job_status == 2){
				$job_status = "Completed";

				if(!empty($job_service_date['service_end_date']))
				{
					$job_status=$job_status.' On <b>'.$job_service_date['service_end_date'].'</b>';
				}
			}
		
 			$row[] = $job_status;
			$condition = array("job_id"=>$person->job_id);
			$assign_job = $this->mjob->getRowA("assign_job",$condition);			
			
			//$row[] = $assign_job['assign_status'];
			
			if($assign_job['assign_status'] != ""){
				if($assign_job['assign_status'] == 1)
				{
					$row[] = '<div class="dropdown">
								<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Accepted
							</div>';
				}else if($assign_job['assign_status'] == 0){
					$row[] = '<div class="dropdown">
							  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Rejected
							  <span class="caret"></span></button>
							  <ul class="dropdown-menu">
								<li><a href="'.base_url().'admin/job/rejectlist/'.$person->job_id.'">RejectList</a></li>
								<li><a href="'.base_url().'admin/job/reassign/'.$person->job_id.'">Re Assign</a></li>							
							  </ul>
							</div>';
				}
				
               else if($assign_job['assign_status'] == 2){
					$row[] = '<div class="dropdown">
								<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Assigned
							</div>';
				}


			}else{
				$row[] = $assign_job['assign_status'];
			}
			
			// if($person->job_status == 2){
			// 	$row[] = '<a class="cstm_view" id="view" style="padding-left:5px" href="javascript:void(0)" title="'.$person->job_id.'"><i class="glyphicon glyphicon-eye-open"></i></a>';
			// }
			// else
			// {
			$row[] = '<a class="cstm_view" id="view" style="padding-left:5px" href="javascript:void(0)" title="'.$person->job_id.'"><i class="glyphicon glyphicon-eye-open"></i></a>&nbsp;&nbsp;<a href="'.base_url("admin/job/edit/".$person->job_id).'" class="cstm_view" id="edit" style="padding-left:5px" href="javascript:void(0)"><i class="glyphicon glyphicon-edit"></i></a>';
			//}
			$admin = $this->admin; 
			$CI =& get_instance();
			$CI->db->select('*');
			$CI->db->from('role_menu');	
			$CI->db->join('menu', 'menu.menu_id = role_menu.menu_id', 'left');
			$CI->db->where('role_id',$admin['role_id']);
			$CI->db->where('parent_id',36);					
			$query=$CI->db->get();
			$menus = $query->result_array();		
			
			$status_result = $this->searcharray('Status', 'menu_name', $menus);
			
			$edit_result = $this->searcharray('Edit', 'menu_name', $menus); 

			if(!empty($status_result)){
			
				$row[] = ($person->status==1?'<a class="cstm_view_status btn btn-success" id="active" href="javascript:void(0)" title="'.$person->job_id.'"><span class="glyphicon glyphicon-ok"></span></a>':'<a class="cstm_view_status btn btn-danger" id="inactive" href="javascript:void(0)" title="'.$person->job_id.'"><span class="glyphicon glyphicon-remove"></span></a>');
			}
			
			if(!empty($edit_result)){	
				$row[] = '<a href="'.base_url('admin/job/details/'.$person->job_id).'" title="Edit" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></span></a></a>';
			}			
			
			
			
            $data[] = $row;
			$i++;
        }	
		
		$output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->mjob->count_all(),
                        "recordsFiltered" => $this->mjob->count_filtered(),
                        "data" => $data,
                );
        echo json_encode($output);
	}
	
	public function all_content_list_reject()
	{
		$job_id = $_POST['job_id'];
		$list = $this->mjob->get_datatables_reject($job_id);
		
		$data = array();
        $no = $_POST['start'];
		$i=1;
		foreach ($list as $person) {
			$row = array();	
			$row[]=$i;		
			$row[] = $person->name;						
			$row[] = $person->remarks;	
			$row[] = '<a href="'.base_url('admin/job/reassign/'.$job_id).'" title="Reassign" class="btn btn-info">Reassign</a>';
            $data[] = $row;
			$i++;
        }	
		
		$output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->mjob->count_all_reject($job_id),
                        "recordsFiltered" => $this->mjob->count_filtered_reject($job_id),
                        "data" => $data,
                );
        echo json_encode($output);
		
	}
	
	private function searcharray($value, $key, $array) {
	   foreach ($array as $k => $val) {
		   if ($val[$key] == $value) {
			   return $val;
		   }
	   }
	   return null;
	}
	
	public function all_details(){
		$job_id = $this->input->post('job_id');		
		$result = $this->mjob->get_details($job_id);

		$new_service = array();
		$services = $this->mjob->getServices($job_id);
		foreach($services as $key=>$service)
		{
			$new_service[$key] = $service['service_name'];
		}
			
		$service = implode(',',$new_service);	
		$result['service'] = $service;
		
		$service_date_condition = array("job_id"=>$job_id);
		$job_service_date = $this->mjob->getRow("job_service_date",$service_date_condition);
		
		//print_r($job_service_date);exit;
		
		$result['service_date'] = date("d/m/Y", strtotime($job_service_date['service_date']));

		$result['next_service_date'] = date("d/m/Y", strtotime($job_service_date['next_service_date']));
		
		$result['assign_job'] = $this->mjob->getAsssignJob($job_id);	
		
		echo json_encode($result);
	}
	
	public function edit($job_id){
		$data['job_details']=$this->mjob->get_details($job_id);	
		if(empty($data['job_details'])){
			$this->_load_list_view();
		}else{			
			$this->_load_details_view($data);
		}
	}
	
	public function reassign($job_id){
		$mechanic_condition = array("role_id"=>14,"status"=>1);
		$mechanics = $this->mjob->getRows('admins',$mechanic_condition);
		
		$rejected_mechanic_condition = array("job_id"=>$job_id);
		$rejected_mechanics = $this->mjob->getRows('assign_job',$rejected_mechanic_condition);
		
		$new_rejected_mechanic = array();
		foreach($rejected_mechanics as $key=>$rejected_mechanic)
		{
			$new_rejected_mechanic[] = $rejected_mechanic['mechanic_id'];
		}
		
		$new_mechanic = array();		
		foreach($mechanics as $mechanic)
		{
			$new_mechanic[] = $mechanic['admin_id'];
		}
		
		$reassign_mechanic_list = array_diff($new_mechanic,$new_rejected_mechanic);
		
		$data['new_mechanic_list'] = array();
		
		foreach($reassign_mechanic_list	as $key_m=>$reassign_mechanic)
		{
			$reassign_mechanic_condition = array("admin_id"=>$reassign_mechanic);
			$reassign_mechanic = $this->mjob->getRow('admins',$reassign_mechanic_condition);
			$data['new_mechanic_list'][$key_m] = $reassign_mechanic;
		}
		
		$data['job_id'] = $job_id;
		$data['content']='admin/job/reassign';
		$this->load->view('admin/layouts/index',$data);
		
	}
	
	public function savereassign()
	{	
		$job_id = $this->input->post('job_id');
		$condition['job_id'] = $job_id;
		$data['mechanic_id'] = $this->input->post('mechanic');
		$data['assign_status'] = 2;
		$data['remarks'] = "";
		//$this->mjob->insert('assign_job',$data);
		$this->mcommon->update('assign_job',$condition,$data);

		/////////insert into user roaster////////////////////////////////////////////////
		$job_date_condition = array("job_id"=>$job_id);
		$job_service_date = $this->mjob->getRows('job_service_date',$job_date_condition);
		
		$roaster_data['mechanic_id']=$this->input->post('mechanic');
		$roaster_data['available_date']=$job_service_date[0]['service_date'];
		$roaster_data['date_of_creation']=date('Y-m-d');
		$this->mcommon->insert('user_roaster',$roaster_data);		
		
		$this->session->set_flashdata('success_msg','Job updated successfully');
		redirect('admin/job');
	}
	
	private function _load_details_view($parms){
		
		$data['assigned_mechanic'] = $this->mjob->getAssignedMechanic($parms['job_details']['job_id']);
		
		$service_condition = array("status"=>1);
		$data['services'] = $this->mjob->getRows('service',$service_condition);	

		$service_date_condition = array("job_id"=>$parms['job_details']['job_id']);
		$data['selected_services_date'] = $this->mjob->getRows('job_service_date',$service_date_condition);	
		
		$services_condition = array("job_id"=>$parms['job_details']['job_id']);
		$data['selected_services'] = $this->mjob->getRows('job_services',$services_condition);

		$selected_service_arr=array();
		foreach($data['selected_services'] as $selected_services)
		{
			$selected_service_arr[]=$selected_services['service_id'];
		}
		$data['selected_service_arr']=$selected_service_arr;

		$selected_car_condition = array("car_id"=>$parms['job_details']['car_id']);
		//$data['cars'] = $this->mjob->getRows('car',$car_condition);
		$data['selected_car'] = $this->mjob->get_cars('car',$selected_car_condition);

		$car_condition = array("status"=>1,"car_assign_status"=>0);
		//$data['cars'] = $this->mjob->getRows('car',$car_condition);
		$data['cars'] = $this->mjob->get_cars('car',$car_condition);
		//echo '<pre>'; print_r($data['cars']); die();
		$mechanic_condition = array("role_id"=>14,"status"=>1);
		$mechanics=$data['mechanics'] = $this->mjob->getRows('admins',$mechanic_condition);
		
		$newDate = $data['selected_services_date'][0]['service_date'];
		
		$condition = array('available_date'=>$newDate);
		
		$off_mechanics = $this->mjob->getRows('user_roaster',$condition);
		
		$new_off_mechanic = array();
		
		foreach($off_mechanics as $off_mechanic)
		{
			$new_off_mechanic[] =  $off_mechanic['mechanic_id'];
		}	
		
		
		$new_mechanic = array();		
		foreach($mechanics as $mechanic)
		{
			$new_mechanic[] = $mechanic['admin_id'];
		}
		
		$roasted_mechanic_list = array_diff($new_mechanic,$new_off_mechanic);
		
		$result['new_mechanic_list'] = array();
		
		foreach($roasted_mechanic_list	as $key_m=>$roasted_mechanic)
		{
			$reassign_mechanic_condition = array("admin_id"=>$roasted_mechanic);
			$reassign_mechanic = $this->mjob->getRow('admins',$reassign_mechanic_condition);
			$result['new_mechanic_list'][$key_m] = $reassign_mechanic;
		}
		
		
		$data['available_mechanic']=$result['new_mechanic_list'];

		$data['job_details']=$parms['job_details'];	
		
		$data['content'] = 'admin/job/detail';		
		$this->load->view('admin/layouts/index', $data);
	}
	
	public function update(){
		if($this->input->post()){
			$job_id = $this->input->post('job_id');
			$this->form_validation->set_rules('car','Car','required');
			$this->form_validation->set_rules('mechanic','Mechanic','required');			
			$this->form_validation->set_rules('service_date','Service Date','required');
			
			if($this->form_validation->run()==FALSE){
				$data['job_details']=$this->mjob->get_details($job_id);				
				$this->_load_details_view($data);
			}else{
				$condition=array('job_id'=>$job_id);
				$udata['car_id'] = $this->input->post('car');
				
				$this->mjob->update($condition,$udata);

				$s_date = $this->input->post('service_date');
					$s_date_array = explode('/',$s_date);
					$service_date = $s_date_array[2]."-".$s_date_array[1]."-".$s_date_array[0];
					
					// $job_service_data['service_date'] = $service_date;
					// //$service_date = $this->input->post('service_date');
					// $next_date = date('Y-m-d', strtotime($service_date. ' + 45 days'));	
					// $next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + 38 days'));
					// $next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + 42 days'));
					// $job_service_data['next_service_date'] = $next_date;
					// $job_service_data['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven;
					// $job_service_data['next_service_date_before_three'] = $next_service_date_before_three;
					// $job_service_data['job_id'] = $job_id;
					// $job_service_data['car_id'] = $this->input->post('car');
					// $cond_jobservice_date['job_id']=$job_id;
					// $this->mcommon->update('job_service_date',$cond_jobservice_date,$job_service_data);
					
					$cond_job_services['job_id']=$job_id;
					$this->mcommon->delete('job_services',$cond_job_services);
					$this->mcommon->delete('job_service_date_specific',$cond_job_services);
					$service_data = array();
					$services = $this->input->post('service');	
					foreach($services as $key=>$service)
					{
						$service_data[$key]['service_id'] = $service;
						$service_data[$key]['job_id'] = $job_id;
						if($service==7||$service==8||$service==20){
					

						/////////////////////////////////For specific job noti///////////////////////////////
						$service_noti_condition = array("service_id"=>$service);
						$service_noti_arr = $this->mjob->getRow("service",$service_noti_condition);	
						$next_days_noti=$service_noti_arr['notification_days'];
						$next_days_noti_before_seven=$next_days_noti-7;
						$next_days_noti_before_three=$next_days_noti-3;

							$job_service_data_specific['service_date'] = $service_date;
							//$service_date = $this->input->post('service_date');
							$next_date_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti.' days'));	
							$next_date_remider_before_seven_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_seven.' days'));
							$next_service_date_before_three_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_three.' days'));
							$job_service_data_specific['next_service_date'] = $next_date_specific;
							$job_service_data_specific['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven_specific;
							$job_service_data_specific['next_service_date_before_three'] = $next_service_date_before_three_specific;
							$job_service_data_specific['job_id'] = $job_id;
							$job_service_data_specific['car_id'] = $this->input->post('car');
							$job_service_data_specific['service_id'] = $service;
							
							$this->mjob->insert('job_service_date_specific',$job_service_data_specific);
							//echo $this->db->last_query(); die;
							}
					}
					

					$this->mjob->batch_insert('job_services',$service_data);

					$next_days_noti=60;
					if(in_array("7",$services))
					{
						$service_noti_condition = array("service_id"=>7);
						$service_noti_arr = $this->mjob->getRow("service",$service_noti_condition);	
						$next_days_noti=$service_noti_arr['notification_days'];
						
					}
					$next_days_noti_before_seven=$next_days_noti-7;
					$next_days_noti_before_three=$next_days_noti-3;
					
					$job_service_data['service_date'] = $service_date;
					//$service_date = $this->input->post('service_date');
					// $next_date = date('Y-m-d', strtotime($service_date. ' + 45 days'));	
					// $next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + 38 days'));
					// $next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + 42 days'));
					$next_date = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti.' days'));	
					$next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_seven.' days'));
					$next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_three.' days'));
					$job_service_data['next_service_date'] = $next_date;
					$job_service_data['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven;
					$job_service_data['next_service_date_before_three'] = $next_service_date_before_three;
					$job_service_data['job_id'] = $job_id;
					$job_service_data['car_id'] = $this->input->post('car');
					$cond_jobservice_date['job_id']=$job_id;
					$this->mcommon->update('job_service_date',$cond_jobservice_date,$job_service_data);

					$car_condition['car_id']=$this->input->post('car');
					$car_update_data['car_assign_status']=1;
					$this->mcommon->update("car",$car_condition,$car_update_data);

					$cond_user_roaster['mechanic_id']=$this->input->post('mechanic');
					$cond_user_roaster['available_date']=$service_date;
					$this->mcommon->delete('user_roaster',$cond_user_roaster);
					///////insert roster//////////////////////////////////////////////
					$roaster_data=array();
					$roaster_data['mechanic_id']=$this->input->post('mechanic');
					$roaster_data['available_date']=$service_date;
					$roaster_data['date_of_creation']=date('Y-m-d');
					$this->mcommon->insert("user_roaster",$roaster_data);

					$assign_condition['job_id']=$job_id;
					$assign_data['mechanic_id'] = $this->input->post('mechanic');
					$assign_data['assign_status'] = "2";
					$this->mcommon->update("assign_job",$assign_condition,$assign_data);
			
				$this->session->set_flashdata('success_msg','Job updated successfully');
				redirect('admin/job');
			}
		}else{
			$this->_load_list_view();
		}
	}
	
	public function add_content()
	{	
		if($this->input->post()){	
			$this->form_validation->set_rules('car','Car','required');
			$this->form_validation->set_rules('mechanic','Mechanic','required');			
			$this->form_validation->set_rules('service_date','Service Date','required');
			
			if($this->form_validation->run()==FALSE){			
				$this->_load_add_view();
			}else{
				//echo "<pre>";print_r($this->input->post('service'));die();
				$services = $this->input->post('service');				
				$udata['car_id'] = $this->input->post('car');				
				//$udata['service'] = implode(',',$service);				
				$udata['job_status'] = 0;
				$udata['created_by'] = $this->admin['admin_id'];
				
				
				$udata['date_of_creation']=date('Y-m-d H:i:s');	
				$job_id = $this->mjob->add($udata);
				
				if($job_id)
				{
				
					$s_date = $this->input->post('service_date');
					$s_date_array = explode('/',$s_date);
					$service_date = $s_date_array[2]."-".$s_date_array[1]."-".$s_date_array[0];

					$service_data = array();
					foreach($services as $key=>$service)
					{
						$service_data[$key]['service_id'] = $service;
						$service_data[$key]['job_id'] = $job_id;
						if($service==7||$service==8||$service==20){
					

						/////////////////////////////////For specific job noti///////////////////////////////
						$service_noti_condition = array("service_id"=>$service);
						$service_noti_arr = $this->mjob->getRow("service",$service_noti_condition);	
						$next_days_noti=$service_noti_arr['notification_days'];
						$next_days_noti_before_seven=$next_days_noti-7;
						$next_days_noti_before_three=$next_days_noti-3;

							$job_service_data_specific['service_date'] = $service_date;
							//$service_date = $this->input->post('service_date');
							$next_date_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti.' days'));	
							$next_date_remider_before_seven_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_seven.' days'));
							$next_service_date_before_three_specific = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_three.' days'));
							$job_service_data_specific['next_service_date'] = $next_date_specific;
							$job_service_data_specific['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven_specific;
							$job_service_data_specific['next_service_date_before_three'] = $next_service_date_before_three_specific;
							$job_service_data_specific['job_id'] = $job_id;
							$job_service_data_specific['car_id'] = $this->input->post('car');
							$job_service_data_specific['service_id'] = $service;
							
							$this->mjob->insert('job_service_date_specific',$job_service_data_specific);
							//echo $this->db->last_query(); die;
							}
					}
					
					$this->mjob->batch_insert('job_services',$service_data);
					$next_days_noti=60;
					if(in_array("7",$services))
					{
						$service_noti_condition = array("service_id"=>7);
						$service_noti_arr = $this->mjob->getRow("service",$service_noti_condition);	
						$next_days_noti=$service_noti_arr['notification_days'];
						
					}
					$next_days_noti_before_seven=$next_days_noti-7;
					$next_days_noti_before_three=$next_days_noti-3;
					
					$job_service_data['service_date'] = $service_date;
					//$service_date = $this->input->post('service_date');
					// $next_date = date('Y-m-d', strtotime($service_date. ' + 45 days'));	
					// $next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + 38 days'));
					// $next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + 42 days'));
					$next_date = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti.' days'));	
					$next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_seven.' days'));
					$next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + '.$next_days_noti_before_three.' days'));
					$job_service_data['next_service_date'] = $next_date;
					$job_service_data['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven;
					$job_service_data['next_service_date_before_three'] = $next_service_date_before_three;
					$job_service_data['job_id'] = $job_id;
					$job_service_data['car_id'] = $this->input->post('car');
					
					$this->mjob->insert('job_service_date',$job_service_data);
				
				
					$data['job_id'] = $job_id;
					$data['mechanic_id'] = $this->input->post('mechanic');
					$data['assign_status'] = "2";
					$data['remarks'] = "";
					$this->mjob->insert('assign_job',$data);
					
					
					
					$car_condition = array("car_id"=>$this->input->post('car'));
					$car = $this->mjob->getRow("car",$car_condition);

					$service_condition = array("job_id"=>$job_id);
					$job = $this->mjob->getRow("job",$service_condition);					
					
						
					$device_condition = array("user_id"=>$this->input->post('mechanic'));
					$device = $this->mjob->getRow("devices",$device_condition);
						
					$push['notification_type'] = "job";
					$push['car'] = $car['car_no'];
					$push['service'] = $job['service'];
					$push['service_date'] = $this->input->post('service_date');

					//echo $this->input->post('service_date');die();
					
					$bookingdate = $this->input->post('service_date');

					$this->send_android_notification($device['device_token'],$push);

					$condition = array("car_id"=>$this->input->post('car'));
					//echo "<pre>";print_r($condition);die();
					$car_user = $this->mjob->getRow('car',$condition);

					//echo "<pre>";print_r($car_user);die();

					/*$message = "";

					foreach($services as $key=>$service)
					{
						$condition = array("service_id"=>$service);
						$service_details = $this->mjob->getRow('service',$condition);
						$message .= ++$key.") ".$service_details['service_name']."<br>";
					}*/

					///////update car assign//////////////////////////////////////////////
					$car_update_data['car_assign_status']=1;
					$this->mcommon->update("car",$car_condition,$car_update_data);

					///////insert roster//////////////////////////////////////////////
					$roaster_data=array();
					$roaster_data['mechanic_id']=$this->input->post('mechanic');
					$roaster_data['available_date']=$service_date;
					$roaster_data['date_of_creation']=date('Y-m-d');
					$this->mcommon->insert("user_roaster",$roaster_data);
					
					$mail_temp = file_get_contents('./global/mail/job_create.html');
					$mail_temp = str_replace("{shop_name}","Punjab Motor Workshop",$mail_temp);
					$mail_temp = str_replace("{shop_logo}",LOGOURL,$mail_temp);
					$mail_temp = str_replace("{name}",$car_user['name'],$mail_temp);
					$mail_temp = str_replace("{carnumber}",$car_user['car_no'],$mail_temp);
					$mail_temp = str_replace("{bookingdate}",$bookingdate,$mail_temp);
					$regmail['name']='Punjab Motor';
					$regmail['to']=$car_user['email'];
					$regmail['subject']='Service List';
					$regmail['message']=$mail_temp;
					//echo $mail_temp;exit;
					registration_mail($regmail);
					
					$this->session->set_flashdata('success_msg','Job assign successfully');
					redirect('admin/job/content');
				}else{
					$this->session->set_flashdata('error_msg','Something Went Wrong');
					redirect('admin/job/content');
				}
			}
		}else{
			$this->_load_list_view();
		}
	}
	
	private function image_upload(){ 
		$img='imgInp';
		if (!is_dir('./public/admin_assets/images/profilepics/')) {
			mkdir('./public/admin_assets/images/profilepics/' ,0777, TRUE);			
		}		
		$config['upload_path'] = './public/admin_assets/images/profilepics/';
		$config['allowed_types'] = 'gif|jpg|png';
		//$config['min_width']  = '200';
		//$config['min_height']  = '200';
		//$config['max_size']	= '100';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		$config['encrypt_name']  = true;
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload($img)){
			$message = array('result' => $this->upload->display_errors(),'status'=>0);
		}else{ 
			$data = array('upload_data' => $this->upload->data());
			$message = array('result' => $data['upload_data']['file_name'],'status'=>1);
		}
		return $message;
	}
	
	public function add(){
		$this->_load_add_view();
	}
	
	private function _load_add_view(){
		$car_condition = array("status"=>1,"car_assign_status"=>0);
		//$data['cars'] = $this->mjob->getRows('car',$car_condition);
		$data['cars'] = $this->mjob->get_cars('car',$car_condition);
		//echo '<pre>'; print_r($data['cars']); die();
		$mechanic_condition = array("role_id"=>14,"status"=>1);
		$data['mechanics'] = $this->mjob->getRows('admins',$mechanic_condition);


		$service_condition = array("status"=>1);
		$data['services'] = $this->mjob->getRows('service',$service_condition);		
		$data['content']='admin/job/add';
		$this->load->view('admin/layouts/index',$data);
	}
	
	public function getmechanicList()
	{
		$mechanic_condition = array("role_id"=>14,"status"=>1);
		$mechanics = $this->mjob->getRows('admins',$mechanic_condition);
		//$date = '10/01/2019';
		$date = $this->input->post('date');
		
		$date_array = explode('/',$date);
		$newDate = $date_array[2]."-".$date_array[1]."-".$date_array[0];
		
		$condition = array('available_date'=>$newDate);
		
		$off_mechanics = $this->mjob->getRows('user_roaster',$condition);
		//echo $this->db->last_query(); die;
		//print_r($off_mechanics);die;
		$new_off_mechanic = array();
		
		foreach($off_mechanics as $off_mechanic)
		{
			$new_off_mechanic[] =  $off_mechanic['mechanic_id'];
		}	
		
		//print_r($new_off_mechanic);die;
		$new_mechanic = array();		
		foreach($mechanics as $mechanic)
		{
			$new_mechanic[] = $mechanic['admin_id'];
		}
		
		$roasted_mechanic_list = array_diff($new_mechanic,$new_off_mechanic);
		
		$result['new_mechanic_list'] = array();
		
		foreach($roasted_mechanic_list	as $key_m=>$roasted_mechanic)
		{
			$reassign_mechanic_condition = array("admin_id"=>$roasted_mechanic);
			$reassign_mechanic = $this->mjob->getRow('admins',$reassign_mechanic_condition);
			$result['new_mechanic_list'][$key_m] = $reassign_mechanic;
		}	
				
		echo json_encode($result);
	}
	
	public function delete_content(){
		$condition['job_id']=$this->input->post('job_id');
		
		$this->mjob->delete($condition);
		$response=array('status'=>1,'message'=>'Success');
		echo header('Content-Type: application/json');
		echo json_encode($response);
	}
	
	public function active()
	{
		$condition['user_id']=$this->input->post('user_id');
		$udata['status'] = 1;
		$this->musersetting->active($condition,$udata);
		$response=array('status'=>1,'message'=>'Success');		
		echo json_encode($response);
	}
	
	public function inactive()
	{
		$condition['user_id']=$this->input->post('user_id');
		$udata['status'] = 0;
		$this->musersetting->active($condition,$udata);
		$response=array('status'=>1,'message'=>'Success');		
		echo json_encode($response);
	}
	
	public function multiple_del()
	{
		$job_ids = explode(',',$this->input->post('job_ids'));
		foreach($job_ids as $job_id)
		{
			$condition['job_id'] = $job_id;					
			$this->mjob->delete($condition);			
			$response=array('status'=>1,'message'=>'Success');
		}		
		echo header('Content-Type: application/json');
		echo json_encode($response);
	}
	
	public function get_sub_category()
	{
		$category_id =  $this->input->post('category_id');
		$result['sub_category'] = $this->musersetting->get_sub_category($category_id);
		if(empty($result))
		{
			$result = array("category_name"=>"Select");
		}
		echo json_encode($result);
	}
	
	public function get_client_address()
	{
		$client_name = $this->input->post('client_name');
		$result['client_addresss'] = $this->mjob->get_client_address($client_name);
		echo json_encode($result);
	}
	
	public function client_job()
	{
		$client_name = $this->input->post('client_name');
		$client_address = $this->input->post('client_address');
		$result['job_list'] = $this->mjob->getJobInformationForclient($client_name,$client_address);
		echo json_encode($result);
	}
	
	public function rejectlist($job_id)
	{
		$data['job_id'] = $job_id;
		$data['content'] = 'admin/job/rejectlist';

		// $condition['type']=2;
		// $condition['details_id']=$job_id;
		// $udata['is_read']=1;
		// $this->mcommon->update('notification',$condition,$udata);	
		$this->load->view('admin/layouts/index', $data);
	}
	
	public function send_special_notification()
	{
		$job_id = $this->input->post('job_id');
		$comment = $this->input->post('comment');
		$condition = array('job_id'=>$job_id);
		$jobDetail = $this->mjob->getRow('job',$condition);
		$category_id = $jobDetail['cat_id'];
		$city_id = $jobDetail['city_id'];
		
		$deviceList=$this->mjob->deviceList($category_id,$city_id);
		
		
		$push['notification_type'] = "message";
		$push['message'] = $comment;
		
		
		if(!empty($deviceList)){
			foreach($deviceList as $device){
				if($device['device_type']==2 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
					$this->send_android_notification($device['device_token'],$push);
				}else if($device['device_type']==1 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
					$this->send_ios_notification($device['device_token'],$push);
				}
			}					
		}
		
		$result['notify_message'] = 'Notification Successfully Send';
		echo json_encode($result);
	}
	
	public function send_ios_notification($devicetoken,$data){
		$passphrase='123456';
        $deviceToken = $devicetoken;    
        $ctx = stream_context_create();
		// ck.pem is your certificate file ssl://gateway.sandbox.push.apple.com:2195
		stream_context_set_option($ctx, 'ssl', 'local_cert', './public/WeCareStaffPem.pem');
		stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
		// Open a connection to the APNS server
		$fp = stream_socket_client(
			'ssl://gateway.sandbox.push.apple.com:2195', $err,
			$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

		if (!$fp){ 
			//exit("Failed to connect: $err $errstr" . PHP_EOL);
			return 2;
		}else{
			// Create the payload body
			$body['aps'] = array(
					'alert' => array(
					    'title' => 'Wecare App',
		                'body' => 'Wecare App',
					 ),
					'sound' => 'default',
					'data'=>$data
			);

			// Encode the payload as JSON
			$payload = json_encode($body);
			// Build the binary notification
			$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;  //
			//$msg = chr(0) . pack('n', 32) . pack('H*',str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload;  // 
			//str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))
			//$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload
			// Send it to the server
			$result = fwrite($fp, $msg, strlen($msg));
			// Close the connection to the server
			fclose($fp);
			if (!$result){
				//return 'Message not delivered' . PHP_EOL;
				return 0; 
			}else{
				//return  'Message successfully delivered' . PHP_EOL;
				return 1;
			}
		} 
	}
	public function send_android_notification($registration_ids, $data) {
		$fields = array(
		'registration_ids' => array($registration_ids),
		'data'=> $data,
		);
		$headers = array(
		'Authorization: key=AAAAAjykQAw:APA91bF2xwn1yqpfBkoxg_K_fTp5bOaI4M3zXL_yG63hmQpqvVFZlMiilFdf65DWdqAO5Xy3eBxJjFybwKMwDMnr16ls_YnMDpMAfwTbJUcBnpHK4supv42pZIoDEBHrdUwOFI9wmQYs', // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
		'Content-Type: application/json'
		);
		// Open connection
		$ch = curl_init();
		 
		// Set the url, number of POST vars, POST data
		curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
		curl_setopt( $ch,CURLOPT_POST, true );
		curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
		curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
		 
		// Disabling SSL Certificate support temporarly
		curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
		 
		// Execute post
		$result = curl_exec($ch );
		if($result === false){
		//die('Curl failed:' .curl_errno($ch));
			return 0;
		}
		 
		// Close connection
		curl_close( $ch );
		return $result;
	}
	public function delete_job_details(){
		$job_id=$this->input->post('job_id');
		$this->mjob->delete_all_job_details($job_id);
		$response=array('error_code'=>'0','message'=>'Job canceled successfully');
		echo json_encode($response); 
	}

	///////////////////////update notification status ajax/////////////////////////
	public function update_status() {
		 $id=$this->input->post('id');	
		 $condition['id']=$id;
		 $udata['is_read']=1;
		 $this->mcommon->update('notification',$condition,$udata);	
		echo 1; die;	
	}
}