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/allyindian_com/sbltt/application/models/Enquiry_model.php
<?php
class Enquiry_model extends CI_Model
{
	public function get_customer_suggestions($search){		
		$this->db->from('customers');	
		$this->db->where('deleted', 0);
		$this->db->group_start();
			$this->db->like('customer_name', $search);
			$this->db->or_like('phone_number',$search);
			$this->db->or_like('cust_email',$search);
		$this->db->group_end();
		$this->db->order_by('customer_name', 'asc');
		
		$suggestions = array();
		foreach($this->db->get()->result() as $row){
			$suggestions[] = array('cust_id' => $row->cust_id,'customer_name' => $row->customer_name,'phone_number' => $row->phone_number,'cust_email' => $row->cust_email,'cust_address' => $row->cust_address);
		}
		return $suggestions;
	}
	public function get_customer_info($search){	
		$this->db->from('customers');	
		$this->db->where('cust_id', $search);		
		$suggestions = array();
		foreach($this->db->get()->result() as $row){
			
			$suggestions[] = array('cust_id' => $row->cust_id,'customer_name' => $row->customer_name,'phone_number' => $row->phone_number,'alt_number' => $row->alt_number,'land_line' => $row->land_line,'cust_email' => $row->cust_email,'cust_address' => $row->cust_address,'city' => $row->city,'state' => $row->state,'dob' => date('d-m-Y',strtotime($row->dob)),'referral_type' => $row->referral_type,'allow_contract' => $row->allow_contract,'cust_sts' => $row->cust_sts,'includ_gst' => $row->includ_gst,'cust_zone' => $row->cust_zone,'csut_gst' => $row->csut_gst,'csut_pan' => $row->csut_pan);
		}
		return $suggestions;
	}
	public function get_tariff($trip_type,$vehicle_type) {
	   $this->db->from('rate');
	   $this->db->join('veh_type', 'veh_type.veh_type_id = rate.rate_vehicle_type','left');
	   $this->db->join('vendor', 'vendor.vendor_id = rate.rate_vendor','left');
	   $this->db->where('rate_trip_type', $trip_type);
	   $this->db->where('rate_vehicle_type', $vehicle_type);
	   $this->db->where('rate_vendor',1);
	   $this->db->order_by('vendor_name', 'ACS');
	   $suggestions = array();
		foreach($this->db->get()->result() as $row){
			$suggestions[] = array('vendor_name' => $row->vendor_name,'veh_type' => $row->veh_type,'upto_hrs' => $row->upto_hrs,'upto_km' => $row->upto_km,'upto_amt' => $row->upto_amt,'extra_km_amt' => $row->extra_km_amt,'extra_amt' => $row->extra_amt);
		}
		return $suggestions;
	}
	public function get_average_km($orgin,$destination){
		$query =  $this->db->query("SELECT * FROM `sblt_average_km` where (frm_city = '$orgin' and to_city = '$destination') or (frm_city = '$destination' and to_city = '$orgin') limit 1");
		return $query->row();
	}
	public function get_tariff_amt($trip_type,$vehicle_type,$avg_km) {
		$query =  $this->db->query("select * from sblt_rate where  rate_vendor = '1' and rate_trip_type = '$trip_type' and rate_vehicle_type = '$vehicle_type' order by abs(upto_km - $avg_km) limit 1");
		return $query->row();
	}
	public function get_hsn() {
		$query =  $this->db->query("select * from sblt_hsn limit 1");
		return $query->row();
	}
	
	public function get_state() {
	   $this->db->from('state');
	   $this->db->order_by('state_name', 'asc');
	   return $this->db->get();
	}
	public function get_city() {
	   $this->db->from('city');
	   $this->db->where('status ',1);
	   $this->db->order_by('city_name', 'asc');
	   return $this->db->get();
	}
	
	public function get_vehicle_type() {
		$this->db->select('veh_type_id,veh_type,veh_category.veh_category');
	   $this->db->from('veh_type');
	   $this->db->join('veh_category', 'veh_category.veh_cat_id = veh_type.veh_category','left');
	   $this->db->where('veh_type.status', 1);
	   $this->db->order_by('veh_type', 'asc');
	   return $this->db->get();
	}
	
	public function get_booking_type() {
	   $this->db->from('booking_type');
	   $this->db->order_by('booking_type_name', 'asc');
	   return $this->db->get();
	}
	public function get_trip_type() {
	   $this->db->from('trip_type');
	   $this->db->where('status', 1);
	   $this->db->order_by('trip_type_name', 'asc');
	   return $this->db->get();
	}
	public function get_enquiry_log($enquiry_id){
		$this->db->select('*,enquiry_log.created_date as log_created_date,created.username,created_ppl.first_name as updated_user');
		$this->db->from('enquiry_log');
		$this->db->join('trip_type', 'trip_type.trip_type_id = enquiry_log.trip_type','left');
		$this->db->join('veh_type', 'veh_type.veh_type_id = enquiry_log.vehicle_type','left');
		$this->db->join('employees created', 'created.id = enquiry_log.created_by','left');
		$this->db->join('people created_ppl', 'created_ppl.person_id = created.person_id','left');
		$this->db->where('enquiry_id',$enquiry_id);
		return $this->db->get()->result();
	}
	
	public function get_referral() {
		$this->db->from('employees');
		$this->db->join('people', 'people.person_id = employees.person_id');
		$this->db->join('role', 'employees.role = role.role_id');
	   return $this->db->get();
	}
	public function search($search,$filters,$rows = 0,$limit_from= 0,$sort='',$order='asc'){
		$start_date = $filters['start_date'];
		$end_date   = $filters['end_date'];
		$emp_id     = $this->session->userdata('emp_id');
		$category   = $this->session->userdata('emp_category');
		$role_id    = $this->session->userdata('emp_role');
		
		if(!$sort){
			$sort = "ncd"; 
		}
		$this->db->select('enquiry_id,customer_name,booking_type_name,frm.city_name as frm_city_name, to.city_name as to_city_name,from_date,to_date,trip_type_name,veh_type,enquiry_sts,prospect,trip_details,ncd,bus_count,phone_number');
		$this->db->from('enquiry');
		$this->db->join('customers', 'customers.cust_id = enquiry.cust_id','left');
		$this->db->join('trip_type', 'trip_type.trip_type_id = enquiry.trip_type','left');
		$this->db->join('veh_type', 'veh_type.veh_type_id = enquiry.vehicle_type','left');
		$this->db->join('booking_type', 'booking_type.booking_type_id = enquiry.cust_type','left');
		$this->db->join('city frm', 'frm.city_id = enquiry.orgin','left');
		$this->db->join('city to', 'to.city_id = enquiry.destination','left');
		
		if($search){
			$this->db->group_start();
				$this->db->like('enquiry_id',$search);
				$this->db->or_like('customer_name',$search);
				$this->db->or_like('booking_type_name',$search);
				$this->db->or_like('trip_type_name',$search);
				$this->db->or_like('frm.city_name',$search);
				$this->db->or_like('to.city_name',$search);
				$this->db->or_like('veh_type',$search);
				if(strpos($search, '-') !== false){
				  $this->db->or_like('from_date', date('Y-m-d',strtotime($search))); 
				  $this->db->or_like('to_date', date('Y-m-d',strtotime($search)));
				  $this->db->or_like('ncd', date('Y-m-d',strtotime($search)));
				}
			$this->db->group_end();	
		}			
		$this->db->where('DATE_FORMAT(from_date, "%Y-%m-%d") BETWEEN '. $this->db->escape($start_date).' AND '.$this->db->escape($end_date));
		/* UDY - GS asked to remove on 12-JULY-2018
		$this->db->group_start();			
			$this->db->where('DATE_FORMAT(from_date, "%Y-%m-%d") BETWEEN '. $this->db->escape($start_date).' AND '.$this->db->escape($end_date))->or_where('DATE_FORMAT(ncd, "%Y-%m-%d") BETWEEN '. $this->db->escape($start_date).' AND '.$this->db->escape($end_date));
		$this->db->group_end();	
		*/
		$sts = array();
		if($filters['Follow_Up'] != FALSE){ $sts[] = 1; }
		if($filters['Confirm']   != FALSE){ $sts[] = 2; }
		if($filters['Cancelled']   != FALSE){ $sts[] = 3; }
		if(!empty($sts)) {
			$this->db->where_in('enquiry_sts', $sts);
		}else{
			$this->db->where('enquiry_sts', '1');
		}
		
		$prospect = array();
		if($filters['Hot']     != FALSE){ $prospect[] = 1; }
		if($filters['Warm']    != FALSE){ $prospect[] = 2; }
		if($filters['Dropped'] != FALSE){ $prospect[] = 3; }
		if(!empty($prospect)) {
			$this->db->where_in('prospect', $prospect);
		}
		/*
		if(($role_id === "3") || ($role_id === "4") || ($role_id === "5") || ($role_id === "6") || ($role_id === "7") || ($role_id === "8") || ($role_id === "9") || ($role_id === "10")){
			$this->db->where("enquiry.created_by",$emp_id);
		}
		*/
		$this->db->where('enquiry.status',1);
		$this->db->order_by($sort,$order);
		if($rows>0){
			$this->db->limit($rows, $limit_from);
		}
		return $this->db->get();
		//echo $this->db->last_query();
	}
	
	public function get_found_rows($search,$filters){
		return $this->search($search, $filters)->num_rows();
		/*
		$this->db->from('enquiry');
		$this->db->where('status',1);
		return $this->db->get()->num_rows();
		*/
	}
	public function get_load_data($enquiry_id){
		$this->db->select('*,frm.city_name as frm_city_name, to.city_name as to_city_name,enquiry.created_date as enquiry_date,created_ppl.first_name as attend,ref_ppl.first_name as reffered,customers.phone_number as cust_mobile,customers.alt_number as cust_alt_number,customers.land_line as cust_land_line,customers.city as cust_city,customers.state as cust_state');
		$this->db->from('enquiry');
		$this->db->join('customers', 'customers.cust_id = enquiry.cust_id','left');
		$this->db->join('trip_type', 'trip_type.trip_type_id = enquiry.trip_type','left');
		$this->db->join('veh_type', 'veh_type.veh_type_id = enquiry.vehicle_type','left');
		$this->db->join('booking_type', 'booking_type.booking_type_id = enquiry.cust_type','left');
		$this->db->join('city frm', 'frm.city_id = enquiry.orgin','left');
		$this->db->join('city to', 'to.city_id = enquiry.destination','left');
		$this->db->join('state', 'state_code = customers.state','left');
		$this->db->join('employees created', 'created.id = enquiry.created_by','left');
		$this->db->join('people created_ppl', 'created_ppl.person_id = created.person_id','left');
		$this->db->join('employees ref', 'ref.id = enquiry.referral','left');
		$this->db->join('people ref_ppl', 'ref_ppl.person_id = ref.person_id','left');
		$this->db->where('enquiry_id',$enquiry_id);
		return $this->db->get()->result();
	}
	public function get_info($enquiry_id)	
	{
		$this->db->from('enquiry');
		$this->db->where('enquiry_id',$enquiry_id);
		$a=$this->db->get();
		if($a->num_rows() === 1){
			return $a->row();		
		}else{
			foreach ($this->db->list_fields('enquiry') as $field){
				$PersonObj->field= '';
			}
			return $PersonObj;
		}
	}
	public function get_customer($cust_id)	{
		$this->db->from('customers');	
		$this->db->where('cust_id', $cust_id);
		$a=$this->db->get();
		if($a->num_rows() === 1){
			return $a->row();		
		}else{
			foreach ($this->db->list_fields('customers') as $field){
				$PersonObj->field= '';
			}
			return $PersonObj;
		}
	}

	public function exists($enquiry_id){
		$this->db->from('enquiry');
		$this->db->where('enquiry_id', $enquiry_id);
		return ($this->db->get()->num_rows() == 1);
	}

	public function booking_exists($enquiry_id){
		$this->db->from('booking');
		$this->db->where('enquiry_id', $enquiry_id);
		return $this->db->get()->num_rows();
	}

	/*
	Inserts or updates a enquiry
	*/
	public function save_enquiry(&$enquiry_data, $enquiry_log_data,$booking_data,$enquiry_id = FALSE){
		if(!$enquiry_id || !$this->exists($enquiry_id, TRUE)){
			if($this->db->insert('enquiry', $enquiry_data)){
				
				$enquiry_data['enquiry_id'] = $this->db->insert_id();
				$enquiry_id = $this->db->insert_id();
				
				$enquiry_log_data['enquiry_id'] = $enquiry_id;
				$this->db->insert('enquiry_log', $enquiry_log_data);
		
				$booking_count = $this->booking_exists($enquiry_id);
				if(((int)$booking_count === 0) && ($enquiry_data['enquiry_sts'] === "2")){
					$booking_data['enquiry_id'] = $enquiry_id;
					$this->db->insert('booking', $booking_data);
				}
				return TRUE;
			}
			return FALSE;
		}
		
		$booking_count = $this->booking_exists($enquiry_id);
		if(((int)$booking_count === 0) && ($enquiry_data['enquiry_sts'] === "2")){
			$this->db->insert('booking', $booking_data);
		}
		$enquiry_log_data['enquiry_id'] = $enquiry_id;
		$this->db->insert('enquiry_log', $enquiry_log_data);
		
		$this->db->where('enquiry_id', $enquiry_id);
		return $this->db->update('enquiry', $enquiry_data);

	}

	public function delete_list($enquiry_id){
		$this->db->where_in('enquiry_id',$enquiry_id);
		return $this->db->update('enquiry',array('status'=>0,'deleted_by'=>$this->session->userdata('emp_id'),'deleted_date'=>date('Y-m-d H:i:s')));
	}

	public function get_multiple_info($cat_ids){
		$this->db->from('enquiry');
		$this->db->where_in('enquiry_id', $cat_ids);
		return $this->db->get()->result_array();
	}
}
?>