MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/ntc_cafsinfotech_in_bk/application/controllers/Removing_and_fitting.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Removing_and_fitting extends Base_controller{
	
	public function __construct(){
		parent::__construct('removing_and_fitting');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		$this->collect_base_info();
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		// LIST VEHICLE NUMBER
		$this->db->select('prime_vehicle_master_id,register_no');
		$this->db->from('cw_vehicle_master');
		$this->db->where('cw_vehicle_master.trans_status', 1);
		$vehicle_num_rslt = $this->db->get()->result();
		foreach($vehicle_num_rslt as $rslt){
			$prime_vehicle_master_id  = $rslt->prime_vehicle_master_id;
			$register_no              = $rslt->register_no;
			$vehicle_num_list[$prime_vehicle_master_id] = $register_no;
		}
		$data['vehicle_num_list'] = $vehicle_num_list;
		$data['table_headers']= $this->xss_clean(get_removing_and_fitting_headers());
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//LOAD MODEL PAGE VIEW WITH DATA
	public function view($prime_removing_fitting_id = -1){
		$this->db->from('cw_removing_fitting');
		$this->db->join('cw_vehicle_master', 'cw_vehicle_master.prime_vehicle_master_id = cw_removing_fitting.vehicle_no','INNER');
		$this->db->where('cw_removing_fitting.prime_removing_fitting_id', $prime_removing_fitting_id);
		$rslt = $this->db->get()->result();
		$vehicle_no = $rslt[0]->vehicle_no;
		$data['removing_and_fitting'] = $rslt;
		
		$this->db->select('cw_vehicle_master_tyre_position.prime_vehicle_master_id as vehicle_id,cw_vehicle_master_tyre_position.total_no_of_tyre,cw_vehicle_master_tyre_position.spare_count');
		$this->db->from('cw_vehicle_master_tyre_position');
		$this->db->where('cw_vehicle_master_tyre_position.prime_vehicle_master_id', (int)$vehicle_no);
		$this->db->where('cw_vehicle_master_tyre_position.trans_status', 1);
		$survey_list_rslt  = $this->db->get()->result();
		$total_no_of_tyre  = (int)$survey_list_rslt[0]->total_no_of_tyre;
		$spare_count       = (int)$survey_list_rslt[0]->spare_count;
		$positions         = $total_no_of_tyre - $spare_count;
		$tyre_position[""] = "--- Select ---";
		for($i=1; $i<=$positions; $i++){
			$tyre_position[$i] = "Position - ".$i;
		}
		$data['tyre_position'] = $tyre_position;
		
		// LIST VEHICLE NUMBER
		$this->db->select('prime_vehicle_master_id,register_no');
		$this->db->from('cw_vehicle_master');
		$this->db->where('cw_vehicle_master.trans_status', 1);
		$vehicle_type_rslt = $this->db->get()->result();
		$vehicle_type_list[""] = "--- Select ---";
		foreach($vehicle_type_rslt as $cat_rlst){
			$prime_vehicle_master_id  = $cat_rlst->prime_vehicle_master_id;
			$register_no              = $cat_rlst->register_no;
			$vehicle_type_list[$prime_vehicle_master_id] = $register_no;
		}
		$data['vehicle_type_list'] = $vehicle_type_list;
		
		// LIST BRAND
		$this->db->select('prime_inventory_brand_id,brand_name');
		$this->db->from('cw_inventory_brand');
		$this->db->where('cw_inventory_brand.trans_status', 1);
		$brand_rslt = $this->db->get()->result();
		$brand_list[""] = "--- Select ---";
		foreach($brand_rslt as $rlst){
			$prime_inventory_brand_id  = $rlst->prime_inventory_brand_id;
			$brand_name                = $rlst->brand_name;
			$brand_list[$prime_inventory_brand_id] = $brand_name;
		}
		$data['brand_list'] = $brand_list;
		
		// LIST PATTERN
		$this->db->select('prime_tyre_pattern_id,tyre_pattern');
		$this->db->from('cw_tyre_pattern');
		$this->db->where('cw_tyre_pattern.trans_status', 1);
		$pattern_rslt = $this->db->get()->result();
		$pattern_list[""] = "--- Select ---";
		foreach($pattern_rslt as $rlst){
			$prime_tyre_pattern_id   = $rlst->prime_tyre_pattern_id;
			$tyre_pattern            = $rlst->tyre_pattern;
			$pattern_list[$prime_tyre_pattern_id] = $tyre_pattern;
		}
		$data['pattern_list'] = $pattern_list;
		
		// LIST TYRE SIZE 
		$this->db->select('prime_tyre_size_id,tyre_size');
		$this->db->from('cw_tyre_size');
		$this->db->where('cw_tyre_size.trans_status', 1);
		$tyre_size_rslt     = $this->db->get()->result();
		$tyre_size_list[""] = "--- Select ---";
		foreach($tyre_size_rslt as $rlst){
			$prime_tyre_size_id                  = $rlst->prime_tyre_size_id;
			$tyre_size                           = $rlst->tyre_size;
			$tyre_size_list[$prime_tyre_size_id] = $tyre_size;
		}
		$data['tyre_size_list'] = $tyre_size_list;
		
		// LIST CASTING LIST
		$this->db->select('prime_tyre_casting_id,casting_name');
		$this->db->from('cw_tyre_casting');
		$this->db->where('cw_tyre_casting.trans_status', 1);
		$casting_rslt     = $this->db->get()->result();
		$casting_list[""] = "--- Select ---";
		foreach($casting_rslt as $rlst){
			$prime_tyre_casting_id                = $rlst->prime_tyre_casting_id;
			$casting_name                         = $rlst->casting_name;
			$casting_list[$prime_tyre_casting_id] = $casting_name;
		}
		$data['casting_list'] = $casting_list;
		
		// LIST REPAIR TYPES
		$this->db->select('prime_tyre_repair_type_id,repair_type');
		$this->db->from('cw_tyre_repair_type');
		$this->db->where('cw_tyre_repair_type.trans_status', 1);
		$repair_rslt = $this->db->get()->result();
		foreach($repair_rslt as $rlst){
			$prime_tyre_repair_type_id   = $rlst->prime_tyre_repair_type_id;
			$repair_type                 = $rlst->repair_type;
			$repair_list[$prime_tyre_repair_type_id] = $repair_type;
		}
		$data['repair_list'] = $repair_list;
		$this->load->view("$this->control_name/form",$data);
	}
	
	
	//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH FILTERS
	public function search(){
		$search            = $this->input->get('search');
		$limit             = $this->input->get('limit');
		$offset            = $this->input->get('offset');
		$sort              = $this->input->get('sort');
		$order             = $this->input->get('order');
		$filters           = $this->input->get('filters');		
		$vehicle_num_fil   = $this->input->get('vehicle_num_fil');		
		$start_date        = str_replace("/","-",$this->input->get('start_date'));
		$end_date          = str_replace("/","-",$this->input->get('end_date'));
		$from_date         = date('Y-m-d',strtotime($start_date));
		$to_date           = date('Y-m-d',strtotime($end_date));
		$this->db->select('prime_removing_fitting_id,prime_tyre_survey_id,tyre_position,serial_number,date,fitting_tyre_status,survey_status,status,cw_vehicle_master.register_no as vehicle_no');	
		$this->db->from('cw_removing_fitting');
		$this->db->join('cw_vehicle_master', 'cw_vehicle_master.prime_vehicle_master_id = cw_removing_fitting.vehicle_no','INNER');
		$this->db->where('cw_removing_fitting.trans_status', 1);	
		$this->db->where('cw_vehicle_master.trans_status', 1);	
		if($search){
			$this->db->group_start();
				$this->db->like('vehicle_no',$search);
				$this->db->or_like('survey_status',$search);
				$this->db->or_like('total_positions',$search);
			$this->db->group_end();
		}
		//FILTERS
		$this->db->group_start();
			$filters_sts = array();
		if($filters[0] > 0){
			$this->db->where_in('cw_removing_fitting.tyre_status', $filters);
		}
		$filters_sts = array();
		foreach($vehicle_num_fil as $key=>$value){
			if($value){ $filters_sts[] = $value; }
		}
		if(count($filters_sts) > 0){
			$this->db->where_in('cw_removing_fitting.vehicle_no', $filters_sts);
		}
		$this->db->where('DATE_FORMAT(date, "%Y-%m-%d") BETWEEN '. $this->db->escape($from_date).' AND '.$this->db->escape($to_date));
		$this->db->group_end();
		
		$this->db->order_by($sort,$order);
		$removing_fitting      = $this->db->get();
		$removing_fitting_rlst = $removing_fitting->result();
		$num_rows              = $removing_fitting->num_rows();
		$datarows              = array();
		foreach($removing_fitting_rlst as $result){
			$datarows[] = get_removing_and_fitting_datarows($result,$this);
		}
		echo json_encode(array('total'=>$num_rows,'rows'=>$datarows));
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		$prime_removing_fitting_id     = (int)$this->input->post('prime_removing_fitting_id');
		$repair_type                   = implode(",",$this->input->post('repair_type'));
		$status                        = $this->input->post('status');
		$vehicle_no                    = $this->input->post('vehicle_no');
		$tyre_position                 = $this->input->post('tyre_position');
		$serial_no_change              = $this->input->post('serial_no_change');
		$tyre_status                   = $this->input->post('tyre_status');
		$survey_status                 = $this->input->post('survey_status');
		$removing_and_fitting_data     = array(
				'date'                 => date('Y-m-d',strtotime($this->input->post('date'))),
				'vehicle_no'           => $vehicle_no,
				'tyre_position'        => $tyre_position,
				'serial_number'        => $this->input->post('serial_number'),
				'brand'                => $this->input->post('brand'),
				'casting_type'         => $this->input->post('casting_type'),
				'mm'                   => $this->input->post('mm'),
				'tyre_size'            => $this->input->post('tyre_size'),
				'thread_type'          => $this->input->post('thread_type'),
				'valve_caps'           => $this->input->post('valve_caps'),
				'valve_extension'      => $this->input->post('valve_extension'),
				'repair_type'          => $repair_type,
				'survey_status'        => $survey_status,
				'fitting_tyre_status'  => $tyre_status,
				'fitting_serial_no'    => $serial_no_change,
				'fitting_brand'        => $this->input->post('fitting_brand'),
				'fitting_thread_type'  => $this->input->post('fitting_thread_type'),
				'fitting_tyre_size'    => $this->input->post('fitting_tyre_size'),
				'fitting_casting_type' => $this->input->post('fitting_casting_type'),
				'fitting_mm'           => $this->input->post('fitting_mm'),
				'status'               => $status,
				'rm_fit_remarks'       => $this->input->post('rm_fit_remarks')
			);
		if((int)$prime_removing_fitting_id === 0){
			if($this->update_info($removing_and_fitting_data,$survey_status,$status)){
				$removing_and_fitting_data['trans_created_by']   = $this->session->userdata('logged_id');
				$removing_and_fitting_data['trans_created_date'] = date("Y-m-d H:i:s");
				if($this->db->insert('cw_removing_fitting',$removing_and_fitting_data)){
					$prime_removing_fitting_id = $this->db->insert_id();
					$this->update_tyre_postion_vehicle($removing_and_fitting_data,$status);
					echo json_encode(array('success' => TRUE, 'message' => "Successfully Inserted",'prime_removing_fitting_id' => $prime_removing_fitting_id));
				}else{
					echo json_encode(array('success' => FALSE, 'message' => "Insertion failed, Please Try Again"));
				}
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Unable to Process Your request"));
			}
		}else
		if((int)$prime_removing_fitting_id > 0){
			if($this->update_info($removing_and_fitting_data,$survey_status,$status)){
				$removing_and_fitting_data['trans_updated_by']   = $this->session->userdata('logged_id');
				$removing_and_fitting_data['trans_updated_date'] = date("Y-m-d H:i:s");
				$this->db->where('prime_removing_fitting_id',$prime_removing_fitting_id);
				if($this->db->update('cw_removing_fitting',$removing_and_fitting_data)){
					$this->update_tyre_postion_vehicle($removing_and_fitting_data,$status);
					echo json_encode(array('success' => TRUE, 'message' => "Successfully Updated",'prime_removing_fitting_id'=> $prime_removing_fitting_id));
				}else{
					echo json_encode(array('success' => FALSE, 'message' => "Update failed, Please Try Again",'prime_removing_fitting_id'=> $prime_removing_fitting_id));
				}
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Update Not Allowed For This tyre position"));
			}
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "please Contact Admin..!"));
		}
	}
	
	//UPDATE TYRE IN VEHICLE MASTER
	public function update_tyre_postion_vehicle($update_data,$status){
		if((int)$status === 2){
			$update_data_new                          = array();
			$update_data_new['serial_no']             = $update_data['fitting_serial_no'];
			$update_data_new['tyre_status']           = $update_data['fitting_tyre_status'];
			$update_data_new['brand']                 = $update_data['fitting_brand'];
			$update_data_new['tread_pattern_type']    = $update_data['fitting_thread_type'];
			$update_data_new['tyre_size']             = $update_data['fitting_tyre_size'];
			$update_data_new['casting_type']          = $update_data['fitting_casting_type'];
			$update_data_new['mm']                    = $update_data['fitting_mm'];
			$update_data_new['trans_updated_by']      = $this->session->userdata('logged_id');
			$update_data_new['trans_updated_date']    = date("Y-m-d H:i:s");
			$this->db->where('position',(int)$update_data['tyre_position']);
			$this->db->where('prime_vehicle_master_id',(int)$update_data['vehicle_no']);
			$this->db->update('cw_tyres_position',$update_data_new);
		}
	}
	
	//CHECK SERIAL NUMBER
	public function check_serial_number(){
		$check_val               = $this->input->post('check_val');
		$tyre_position           = (int)$this->input->post('tyre_position');
		$this->db->from('cw_tyres_position');
		$this->db->where('serial_no',$check_val);
		$this->db->where('position !=',$tyre_position);
		$this->db->where('trans_status',1);
		$count = $this->db->get()->num_rows();
		if((int)$count > 0){
			echo json_encode(array('success' => FALSE,'message' => "Serial Number is already exists in different vehicle "));
		}else{
			echo json_encode(array('success' => True,'message' => "Can Process"));
		}
	}
	
	//UPDATE INFO IN RECAP AND SCRAP
	public function update_info($removing_and_fitting_data,$survey_status,$status){
		if((int)$survey_status === 1 && (int)$status === 2){
			if($this->update_scrap_table($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'])){
				$update_data = array(
								  'vehicle_no'           => $removing_and_fitting_data['vehicle_no'],
								  'tyre_position'        => $removing_and_fitting_data['tyre_position'],
								  'brand'                => $removing_and_fitting_data['brand'],
								  'thread_type'          => $removing_and_fitting_data['thread_type'],
								  'mm'                   => $removing_and_fitting_data['mm'],
								  'tyre_size'            => $removing_and_fitting_data['tyre_size'],
								  'casting_type'         => $removing_and_fitting_data['casting_type'],
								  'serial_no'            => $removing_and_fitting_data['serial_number'],
								  'status'               => 1
								  );
				if(!$this->check_vehicle_already_exists($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'],'cw_tyre_recap')){
					$update_data['slip_number']         = $this->unique_code('SN');
					$update_data['trans_created_by']    = $this->session->userdata('logged_id');
					$update_data['trans_created_date']  = date("Y-m-d H:i:s");
					$this->db->insert('cw_tyre_recap',$update_data);
					return TRUE;
				}else{
					if(!$this->check_status($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'],'cw_tyre_recap')){
						$update_data['trans_updated_by']       = $this->session->userdata('logged_id');
						$update_data['trans_updated_date']     = date("Y-m-d H:i:s");
						$this->db->where('vehicle_no',$removing_and_fitting_data['vehicle_no']);
						$this->db->where('tyre_position',$removing_and_fitting_data['tyre_position']);
						$this->db->update('cw_tyre_recap',$update_data);
						return TRUE;
					}else{
						return FALSE;
					}
				}
			}else{
				return FALSE;
			}
		}else
		if((int)$survey_status === 4 && (int)$status === 2){
			if($this->update_recap_table($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'])){
				$update_data = array(
								  'vehicle_no'           => $removing_and_fitting_data['vehicle_no'],
								  'tyre_position'        => $removing_and_fitting_data['tyre_position'],
								  'brand'                => $removing_and_fitting_data['brand'],
								  'mm'                   => $removing_and_fitting_data['mm'],
								  'thread_type'          => $removing_and_fitting_data['thread_type'],
								  'tyre_size'            => $removing_and_fitting_data['tyre_size'],
								  'casting_type'         => $removing_and_fitting_data['casting_type'],
								  'serial_number'        => $removing_and_fitting_data['serial_number'],
								  'status'               => 1
								  );
				if(!$this->check_vehicle_already_exists($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'],'cw_tyre_scrap')){
					$update_data['trans_created_by']    = $this->session->userdata('logged_id');
					$update_data['trans_created_date']  = date("Y-m-d H:i:s");
					$this->db->insert('cw_tyre_scrap',$update_data);
					return TRUE;
				}else{
					if(!$this->check_status($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'],'cw_tyre_scrap')){
						$update_data['trans_updated_by']       = $this->session->userdata('logged_id');
						$update_data['trans_updated_date']     = date("Y-m-d H:i:s");
						$this->db->where('vehicle_no',$removing_and_fitting_data['vehicle_no']);
						$this->db->where('tyre_position',$removing_and_fitting_data['tyre_position']);
						$this->db->update('cw_tyre_scrap',$update_data);
						return TRUE;
					}else{
						return FALSE;
					}
				}
			}else{
				return false;
			}
		}else{
			if($this->update_scrap_table($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position']) && $this->update_recap_table($removing_and_fitting_data['vehicle_no'],$removing_and_fitting_data['tyre_position'])){
				return TRUE;
			}else{
				return false;
			}
		}
	}
	
	//CHECKING VEHICLE ALREADY EXISTS IN TYRE RECAP OR TYRE SCRAP
	public function check_vehicle_already_exists($prime_vehicle_id,$tyre_position,$table_name){
		$this->db->from("$table_name");
		$this->db->where('vehicle_no',(int)$prime_vehicle_id);
		$this->db->where('tyre_position',(int)$tyre_position);
		$this->db->where('trans_status', 1);
		if((int)($this->db->get()->num_rows()) > 0){
			return TRUE;
		}else{ 
			return FALSE;
		}
	}
	
	//CHECK STATUS IN TYRE RECAP OR TYRE SCRAP
	public function check_status($prime_vehicle_id,$tyre_position,$table_name){
		$this->db->from("$table_name");
		$this->db->where('vehicle_no',(int)$prime_vehicle_id);
		$this->db->where('tyre_position',(int)$tyre_position);
		$this->db->where('status', 2);
		$this->db->where('trans_status', 1);
		if((int)($this->db->get()->num_rows()) > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	//UPDATE SCRAP TABLE
	public function update_scrap_table($prime_vehicle_id,$tyre_position){
		$this->db->from("cw_tyre_scrap");
		$this->db->where('vehicle_no',(int)$prime_vehicle_id);
		$this->db->where('tyre_position',(int)$tyre_position);
		$this->db->where('trans_status', 1);
		if((int)($this->db->get()->num_rows()) > 0){
			$this->db->from("cw_tyre_scrap");
			$this->db->where('vehicle_no',(int)$prime_vehicle_id);
			$this->db->where('tyre_position',(int)$tyre_position);
			$this->db->where('status', 1);
			$this->db->where('trans_status', 1);
			if((int)($this->db->get()->num_rows()) === 1){
				$update_data                           = array();
				$update_data['trans_status']           = 0;
				$update_data['trans_deleted_by']       = $this->session->userdata('logged_id');
				$update_data['trans_deleted_date']     = date("Y-m-d H:i:s");
				$this->db->where('vehicle_no',$prime_vehicle_id);
				$this->db->where('tyre_position',$tyre_position);
				$this->db->update('cw_tyre_scrap',$update_data);
				return true;
			}else{
				return False;
			}
		}
		return true;
	}
	
	//UPDATE RECAP TABLE
	public function update_recap_table($prime_vehicle_id,$tyre_position){
		$this->db->from("cw_tyre_recap");
		$this->db->where('vehicle_no',(int)$prime_vehicle_id);
		$this->db->where('tyre_position',(int)$tyre_position);
		$this->db->where('trans_status', 1);
		if((int)($this->db->get()->num_rows()) > 0){
			$this->db->from("cw_tyre_recap");
			$this->db->where('vehicle_no',(int)$prime_vehicle_id);
			$this->db->where('tyre_position',(int)$tyre_position);
			$this->db->where('status', 1);
			$this->db->where('trans_status', 1);
			if((int)($this->db->get()->num_rows()) === 1){
				$update_data                          = array();
				$update_data['trans_status']          = 0;
				$update_data['trans_deleted_by']       = $this->session->userdata('logged_id');
				$update_data['trans_deleted_date']     = date("Y-m-d H:i:s");
				$this->db->where('vehicle_no',$prime_vehicle_id);
				$this->db->where('tyre_position',$tyre_position);
				$this->db->update('cw_tyre_recap',$update_data);
				return true;
			}else{
				return False;
			}
		}
		return true;
	}
	
	public function get_vehicle_info(){
		$vehicle_no = (int)$this->input->post('vehicle_no');
		if($vehicle_no){
			$this->db->select('vehicle_category');
			$this->db->from("cw_vehicle_master");
			$this->db->where('prime_vehicle_master_id',(int)$vehicle_no);
			$this->db->where('trans_status', 1);
			$rlst             = $this->db->get()->row();
			$vehicle_category = (int)$rlst->vehicle_category;
			if($vehicle_category === 1){
				echo json_encode(array('success'=>true));
			}else{
				echo json_encode(array('success'=>false));
			}
		}
	}
}
?>