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/qcr24/app/application/controllers/admin/Booking.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Booking extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin/mreservation');
		$this->load->model('admin/msportsfacilitiesrate');
		$this->load->model('admin/mbooking');
		$this->load->model('admin/mproperty'); 
		$this->load->model('frontend/query');
		$this->load->model('mcommon');
		$this->load->helper('sms');
		$this->load->helper('email');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$where = array();
		$data['start_date']= $this->input->post('start_date');
		$data['end_date']= $this->input->post('end_date');
		if($this->input->post()){
			if($this->input->post('start_date')){
				$where['bh.created_ts >='] = date('Y-m-d 0:0:1', strtotime($this->input->post('start_date')));
			}
			if($this->input->post('end_date')){
				$where['bh.created_ts <='] = date('Y-m-d 23:59:50', strtotime($this->input->post('end_date')));
			}
		}
		$order_by = " bh.booking_id DESC";
		$data['reservations'] = $this->mreservation->get($where,$order_by);
		// echo $this->db->last_query(); 
		// echo '<pre>';
		// print_r($data['reservations']);die;
		$data['content'] = 'admin/reservation/list';
		$this->load->view('admin/layouts/index', $data);  
	}

	public function add_booking()
	{
		$data['properties'] = $this->admin_session_data['role_id'] == ROLE_SUPERADMIN ? $this->mproperty->get_property(array('property_master.is_active'=>1)) : $this->mproperty->get_user_property_details($this->admin_session_data['user_id']);
		//echo '<pre>';print_r($data['properties']);die;
		$data['customer_list'] = $this->mbooking->get_customer_list(); 
		$data['states'] = $this->mbooking->get_property_state(array('state_master.country_id' => 101, 'is_active' => 1));
		$data['countries'] = $this->mbooking->get_property_country(array('country_id' => 101));
		
		$data['content'] = 'admin/booking/add_room_booking';
		$this->load->view('admin/layouts/index', $data); 
	}

	public function add_booking_1()
	{
		$data['properties'] = $this->admin_session_data['role_id'] == ROLE_SUPERADMIN ? $this->mproperty->get_property(array('property_master.is_active'=>1)) : $this->mproperty->get_user_property_details($this->admin_session_data['user_id']);
		//echo '<pre>';print_r($data['properties']);die;
		$data['customer_list'] = $this->mbooking->get_customer_list(); 
		$data['states'] = $this->mbooking->get_property_state(array('state_master.country_id' => 101, 'is_active' => 1));
		$data['countries'] = $this->mbooking->get_property_country(array('country_id' => 101));
		
		$data['content'] = 'admin/booking/add_room_booking_1';
		$this->load->view('admin/layouts/index', $data); 
	}


	public function booking_details($booking_id){
		$data['content'] = 'admin/booking/booking_details';
		$data['booking_details'] = $this->mbooking->get_booking_details($booking_id); 
		$data['booking_payment_details'] = $this->mbooking->get_booking_payment_details($booking_id); 
		//echo '<pre>';print_r($data['booking_details']);die;
		$this->load->view('admin/layouts/index', $data); 
	}

	public function getaccommodation()
	{
		$data = array();
		$property_id=$this->input->post('property_id');
		$data = $this->mbooking->get_getaccommodation($property_id);
		echo json_encode($data); 
	}





	public function register()
	{
		$data = array();
		$reservations = array();
		$data['content'] = 'admin/reservation/register'; 
		$request_data = array();

		if(!empty($this->input->post())){
			
			$request_data['fieldunit_id'] = $this->input->post('fieldunit_id');
			$request_data['location_id'] = $this->input->post('location_id');
			$request_data['sports_facilities_id'] = $this->input->post('sports_facilities_id');
			
		} 
		
		// $reservation_details = $this->mreservation->get_reservation_booking_details($request_data); 
		// //print_r($reservation_details);die;
		// foreach ($reservation_details as $key => $reservation) { 
			
        //     $description = '';
		// 	$reservations[$key]['title'] = $reservation["sports_facilities_name"];
        //     $reservations[$key]['start'] = date('Y-m-d',strtotime($reservation["start_date"]));
        //     $reservations[$key]['end'] = NULL;
            
            

		// 	$reservations[$key]['backgroundColor'] = ($reservation['status'] == 1) ? 'blue' : (($reservation['status'] == 2)?'red':(($reservation['status'] == 3)?'green':'orange')); 
		// 	$reservations[$key]['borderColor'] = ($reservation['status'] == 1) ? 'blue' : (($reservation['status'] == 2)?'red':(($reservation['status'] == 3)?'green':'orange')); 
			
        //     $reservations[$key]['textColor'] = "white";
        //     //$reservations[$key]['display'] = "background";
		// 	$reservations[$key]['eventColor'] = ($reservation['status'] == 1) ? 'blue' : (($reservation['status'] == 2)?'red':(($reservation['status'] == 3)?'green':'orange')); 
			

		// 	$description .= 'Booking ID : Re-'.str_pad($reservation['booking_id'],6,"0",STR_PAD_LEFT);
		// 	$description .= '<br> Fieldunit : '.$reservation["fieldunit_name"];
		// 	$description .= '<br> Location : '.$reservation["location_name"];
		// 	$description .= '<br> Organization Type : '.$reservation["category_name"];
		// 	$description .= '<br> Organization Name : '.$reservation["organization_name"];
		// 	$description .= '<br> Contact No : '.$reservation["contact_no"];
		// 	$description .= '<br> Total Amount : '.$reservation["total_rate"]; 
		// 	$description .= '<br> Status : '.(($reservation['status'] == 1) ? 'Approved' : (($reservation['status'] == 2)?'Rejected':(($reservation['status'] == 3)?'Confirmed':'Pending')));

		// 	$reservations[$key]['description'] = $description;  
		// 	$reservations[$key]['status'] = $reservation['status']; 

		 
            
		
		// }

		
        // $data['reservations'] = $reservations;
		// $data['request_data'] = $request_data;

		// //print_r($data['reservations']);die;

		// $data['fieldunits'] = $this->msportsfacilitiesrate->get_fieldunit();

		$this->load->view('admin/layouts/index', $data);
	}
	public function view_details($booking_id)
	{
		$data = array();
		$data['reservation'] = $this->mreservation->get_reservation_details($booking_id);
		$data['reservation_details'] = $this->mreservation->get_sports_facilities_booking_details($booking_id);
		$data['content'] = 'admin/reservation/view_details';
		$this->load->view('admin/layouts/index', $data);
	}

	public function payment($booking_id)
	{
		$data = array();
		$data['reservation'] = $this->mreservation->get_reservation_details($booking_id);
		$data['reservation_details'] = $this->mreservation->get_sports_facilities_booking_details($booking_id);
		$data['content'] = 'admin/reservation/payment';
		$this->load->view('admin/layouts/index', $data);
	}

	
	public function book_room_submit()
	{
		//echo '<pre>';print_r($this->input->post());die; 

		$book_room_qty = $this->input->post('book_room_qty');
		$customer_id = 0;
		$customer_data=array();

		$this->db->trans_start(); # Starting Transaction

        if(empty($this->input->post('customer_id'))){
            $check_customer = $this->db->from('customer_master')->where('mobile',$this->input->post('mobile'))->get()->row_array();
            
            
            
            if(!empty($check_customer)) {
                $return_data = array('status'=>false,'msg'=> 'Customer mobile number already exist');
				echo json_encode($return_data);die;
            } 
                
			
                
			    $customer_data['customer_title'] = $this->input->post('customer_title');
                $customer_data['first_name'] = $this->input->post('first_name');
                $customer_data['last_name'] = $this->input->post('last_name');
                $customer_data['email'] = $this->input->post('email');
                $customer_data['mobile'] = $this->input->post('mobile');
                $customer_data['customer_type'] = $this->input->post('customer_type');
                $customer_data['company_name'] = $this->input->post('company_name');
                $customer_data['company_email'] = $this->input->post('company_email');
                $customer_data['company_phone'] = $this->input->post('company_phone');
                $customer_data['gst_number'] = $this->input->post('gst_number');
                $customer_data['company_state_id'] = $this->input->post('company_state_id');
                $customer_data['company_country_id'] = $this->input->post('company_country_id');
                $customer_data['company_address'] = $this->input->post('company_address');
                $customer_data['is_active'] = 1;
                $customer_data['created_by'] = $this->admin_session_data['user_id'];
                $customer_data['created_ts'] = date('Y-m-d H:i:s');

				$this->db->insert('customer_master',$customer_data);
				$customer_id = $this->db->insert_id();
		} else {
            $customer_id = $this->input->post('customer_id');
			$customer_data = $this->db->from('customer_master')->where('customer_id',$customer_id)->get()->row_array();
        }
		
		$booking_header_data = array(
			'booking_no' => date('Ymd').mt_rand(100000,999999),
			'property_id' => $this->input->post('property_id'),
			'room_count' => array_sum($book_room_qty),
			'customer_id' => $customer_id,
			'invoice_generated' => '0',
			'check_in' => $this->input->post('check_in_date'),
			'check_out' => $this->input->post('check_out_date'),
			'booking_for' => $customer_data['customer_type'],
			'customer_title' => $customer_data['customer_title'],
			'first_name' => $customer_data['first_name'], 
			'last_name' => $customer_data['last_name'],
			'email' => $customer_data['email'],
			'mobile' => $customer_data['mobile'],
			'personal_address' => '',
			'company_name' => $customer_data['company_name'],
			'company_email' => $customer_data['company_email'],
			'company_phone' => $customer_data['company_phone'],
			'gst_number' => $customer_data['gst_number'],
			'company_address' => $customer_data['company_address'],
			'company_state_id' => $customer_data['company_state_id'],
			'company_country_id' => $customer_data['company_country_id'],
			'room_base_price' => $this->input->post('room_base_price'),
			'room_total_discount' => $this->input->post('discount_amount'),
			'room_total_discount_perc' => $this->input->post('discount_perc'),
			'room_price_before_tax' => $this->input->post('room_price_before_tax'),
			'room_total_cgst'=> $this->input->post('room_total_cgst'),
			'room_total_sgst'=> $this->input->post('room_total_sgst'),
			'room_total_igst'=> $this->input->post('room_total_igst'),
			'room_payable_amount' => $this->input->post('total_amount'),
			'net_payable_amount' => $this->input->post('net_amount'),
			'booking_status' => 'A',
			'created_by' => $this->admin_session_data['user_id'],
			'created_user_type' => 'U',
			'created_ts' => date('Y-m-d H:i:s'),
			'booking_source' => 'B'
		);

		$this->db->insert('booking_header',$booking_header_data);
		$booking_id  =  $this->db->insert_id();

		
		if(!empty($book_room_qty)){

			foreach($book_room_qty as $room_key => $room_qty){
				
				for($i = 1; $i<= $room_qty; $i++){

					$booking_detail_data [] = array(
						'booking_id'=>$booking_id, 
						'accommodation_id'=>$this->input->post('book_room_accommodation_id')[$room_key], 
						'in_date'=>$this->input->post('check_in_date'), 
						'out_date'=>$this->input->post('check_out_date'), 
						'adults'=>$this->input->post('book_room_adult')[$room_key], 
						'children'=>$this->input->post('book_room_child')[$room_key], 
						'allotment_status'=>'B', 
						'rate_category_id' => 1,
						'room_rate'=>$this->input->post('book_room_base_price')[$room_key], 
						'room_charge'=>$this->input->post('book_room_base_price')[$room_key],
						'room_discount_percent'=>$this->input->post('room_discount_percent')[$room_key], 
						'room_discount_amount'=>$this->input->post('room_discount_amount')[$room_key], 
						'room_taxable_amount'=>$this->input->post('room_taxable_amount')[$room_key], 
						'room_cgst'=>$this->input->post('room_cgst')[$room_key], 
						'room_sgst'=>$this->input->post('room_sgst')[$room_key], 
						'room_igst'=>$this->input->post('room_igst')[$room_key], 
						'room_cgst_percent'=>$this->input->post('room_cgst_percent')[$room_key], 
						'room_sgst_percent'=>$this->input->post('room_sgst_percent')[$room_key], 
						'room_igst_percent'=>$this->input->post('room_igst_percent')[$room_key], 
						'room_net_amount'=>$this->input->post('book_room_net_amount')[$room_key] 
					);
				}
			}

		}

		if(!empty($booking_detail_data)){
			$this->db->insert_batch('booking_detail',$booking_detail_data);
		}

		/* Online Payment & Booking Confirmation Email Sending */

		$config = email_config();
		$email_from = $config['email_from'];
		unset($config['email_from']);
	  
		
		$subject = 'Booking ID  '.$booking_header_data['booking_no'].' is Confirmed';

$message = 'Dear Sir / Madam,

Your Booking (ID   '.$booking_header_data['booking_no'].') is Confirmed.Please Login to www.prdtourism.in to download the Booking Slip or you may ask for the same to the person on duty at the venue.For more details please login to www.prdtourism.in

Wish you a happy stay.

Panchayat Tourism
Department of Panchayat & Rural Development
Government of West Bengal';
		
		

		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from($email_from); // change it to yours
		$this->email->to($booking_header_data['email']);// change it to yours 
		$this->email->subject($subject); 
		$this->email->message($message);
		$this->email->send();
			
		//echo '<pre>'; print_r($this->email->print_debugger());die;

		$this->db->trans_complete(); # Completing transaction

		if ($this->db->trans_status() === FALSE) {
			# Something went wrong.
			$this->db->trans_rollback();
			$return_data = array('status'=>false,'msg'=> 'Oops!Something went wrong...');

		} 
		else { 
			# Everything is Perfect. 
			# Committing data to the database.
			$this->db->trans_commit();
			
			offline_payment_confirmed($booking_header_data['mobile'], $booking_header_data['booking_no']);
			
			$return_data = array('status'=>true,'msg'=> 'Congratulations!Booking done successfully.');

		}

		echo json_encode($return_data);die;
		
	}

	public function test_mail(){
		
		$config = email_config();
		$email_from = $config['email_from'];
		unset($config['email_from']);
	  
		
		$subject = 'Booking ID  XXXXX is Confirmed';

		$message = 'Dear Sir / Madam,

		Thank you for your payment of Rs. 500.00 and your Booking (ID XXXXX) is Confirmed.
		
		Please Login to www.prdtourism.in to download the Booking Slip.
		
		You will be allowed to enter the check-in only upon production of the Booking Slip to the person on duty at the venue.
		
		For more details please login to www.prdtourism.in
		
		Wish you a happy stay.
		
		Panchayat Tourism
		Department of Panchayat & Rural Development
		Government of West Bengal';
		
		

		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from($email_from); // change it to yours
		$this->email->to('arindamkbiswas@gmail.com');// change it to yours 
		$this->email->subject($subject); 
		$this->email->message($message);
		$this->email->send();
			
		//echo '<pre>'; print_r($this->email->print_debugger());die;
				
		echo '<pre>'; print_r($this->email->print_debugger());die;
	
	}


	public function submit_payment()
	{
		//echo '<pre>';print_r($this->input->post());die;
		$booking_id = $this->input->post('booking_id');
		if(!$booking_id){

			$this->session->set_flashdata('error_msg', 'Booking ID is required');
			redirect("admin/booking");
		}

		$booking_header_data = $this->db->from('booking_header')->where('booking_id',$booking_id)->get()->row_array();
		
		
			$data = array(
				'booking_id'=>$booking_id,
				'customer_id'=>$booking_header_data['customer_id'],
				'payment_date' => $this->input->post('payment_date'),
				'payment_mode' => $this->input->post('payment_mode'),
				'amount' => $booking_header_data['net_payable_amount'],
				// 'check_draft_no' => $this->input->post('check_draft_no'),
				// 'branch_name' => $this->input->post('branch_name'),
				// 'bank_name' => $this->input->post('bank_name'),
				// 'check_draft_date' => date('Y-m-d',strtotime($this->input->post('check_draft_date'))),
				'status'=>'success',
				'remarks' => $this->input->post('remarks'),
				'created_by' => $this->admin_session_data['user_id'],
				'created_ts' => date('Y-m-d H:i:s')
			);

			$booking_payment = $this->db->insert('booking_payment',$data);

			
			
			if ($booking_payment) {
				$this->session->set_flashdata('success_msg', 'Payment Collected Successfully');
				redirect("admin/booking/booking_details/".$booking_id);
			}
	}

	public function check_not_responded_booking()
	{
		
		$not_responded_booking = $this->mreservation->check_not_responded_booking();
		if(!empty($not_responded_booking)){
			
			foreach($not_responded_booking as $not_responded){
				$updateArray[] = array(
					'status'=>'5',
					'not_responded_ts' => date('Y-m-d H:i:s'),
					'booking_id' => $not_responded['booking_id']
				);
			}

			$this->db->update_batch('sports_facilities_booking',$updateArray, 'booking_id'); 
		}

		echo 'Executed Successfully';die;
		
	}


	public function search_room(){

		$request_data = $this->input->post();
		//echo '<pre>'; print_r($this->input->post()); die;

		$check_in_date=date_create($request_data['check_in_date']);
		$check_out_date=date_create($request_data['check_out_date']);
		$diff_check_in_out=date_diff($check_in_date,$check_out_date);
		$diff_check_in_out_nights = $diff_check_in_out->format("%a Nights");
		$request_data['discount_perc'] = isset($request_data['discount_perc']) && !empty($request_data['discount_perc'])?$request_data['discount_perc']:0;

		$search_room_data = $this->mbooking->search_room($request_data);
		
		
		$search_room_data_key = array();

		
		
		if(!empty($request_data['adult'])){

			foreach($request_data['adult'] as $adult_key => $adult){

				$array_search_params = array(
					'adult' => $adult,
					'child' => isset($request_data['child'][$adult_key])?$request_data['child'][$adult_key]:'',
					'accommodation_id' => isset($request_data['accommodation_id'][$adult_key])?$request_data['accommodation_id'][$adult_key]:'',
				);
				
				$array_search_function = $this->array_search_function($search_room_data,$array_search_params);
				//print_r($array_search_function);die;
				if($array_search_function){

					$search_room_data_key = array_merge($search_room_data_key,$array_search_function);
					
				}

			}

		}
		
		// echo '<pre>'; print_r($search_room_data);
		//echo '<pre>'; print_r(array_unique($search_room_data_key)); die;
		$search_room_data = array_values(array_intersect_key($search_room_data,array_flip(array_unique($search_room_data_key))));

		



		$return_data = array('status'=>true,'search_room_data'=>$search_room_data,'diff_check_in_out_nights'=>$diff_check_in_out_nights);
		echo json_encode($return_data); 
	}


private function array_search_function($products, $array_search_params)
{	$return_key_array = array();
	
   foreach($products as $key => $product)
   {$search_params_result = 1;
      
		foreach($array_search_params as $array_search_params_key => $array_search_params_value){
			
			if (!empty($array_search_params_value)) {
				
				if(in_array($array_search_params_key,array("adult","child")) && ($product[$array_search_params_key] < $array_search_params_value)){
					
					$search_params_result = 0;
				} 
				if(in_array($array_search_params_key,array("accommodation_id")) && ($product[$array_search_params_key] != $array_search_params_value)){
					
					$search_params_result = 0;
				}  

			} 

		}	
		
		if($search_params_result == 1){
			$return_key_array[] = $key;
		}
		
		
   }
   return $return_key_array;
}

public function downloadInvoice($booking_id)
	{
		// echo "We are working on it. When it's completed then you can use this functionality. Thanks !";exit();
		$this->load->library('pdf');
		$data = array();
		$data['customer_details'] = $this->query->getBookingDetailsOfCustomer($this->session->userdata('customer_id'));
		$data['countries'] = $this->mcommon->getDetails('country_master', array());
		$data['booking_details'] = $this->mcommon->getRow('booking_listing_view', array('booking_id' => $booking_id));
		$data['guest_details'] = $this->query->getguestDetails($booking_id);
		$data['booking_payment_listing'] = $this->mcommon->getRow('booking_payment_listing_view', array('booking_id' => $booking_id));
		$data['content'] = 'frontend/downloadInvoice';
		$filename = 'invoice-'.time();
		$html=$this->load->view('frontend/downloadInvoice', $data,true);
		// $this->pdf->create($html, $filename);
		// echo $html;die;

		$this->pdf->loadHtml($html);
		$this->pdf->set_paper("a4", "landscape" );
		$this->pdf->render();

		$this->pdf->stream("".$filename.".pdf", array("Attachment"=>0));
		
	}

	/**
	 * @request input booking_id, reason
	 * response json
	*/
	public function cancel_booking()
	{
		if(empty($this->input->post('booking_id'))){
			$response = array(
				'success' => FALSE,
				'message' => 'Booking ID is required.',
			);
			echo json_encode($response); exit;
		}

		if(empty($this->input->post('reason'))){
			$response = array(
				'success' => FALSE,
				'message' => 'Booking Cancellation reason is required.',
			);
			echo json_encode($response); exit;
		}

		$update_array = array(
			'booking_status' => 'C',
			'cancellation_remarks'=> $this->input->post('reason'),
			'updated_by'=> $this->admin_session_data['user_id'],
			'updated_ts'=> date('Y-m-d H:i:s')
		);
		$result = $this->mbooking->update_booking_details($this->input->post('booking_id'), $update_array);
		
		$booking_det = $this->mbooking->get_booking_details($this->input->post('booking_id'));
		if($result){

		/* Booking Cancellation Email Sending */

		$config = email_config();
		$email_from = $config['email_from'];
		unset($config['email_from']);

		$subject = 'Booking ID  '.$booking_det['booking_no'].' has been cancelled';
	  
		$message = 'Dear Sir / Madam,

		You Booking (ID '.$booking_det['booking_no'].') has been cancelled.
		
		Refund (if any) will be initiated shortly
		
		For more details please login to www.prdtourism.in
		
		Panchayat Tourism
		Department of Panchayat & Rural Development
		Government of West Bengal';
		
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from($email_from); // change it to yours
		$this->email->to($booking_det['email']);// change it to yours 
		$this->email->subject($subject); 
		$this->email->message($message);
		$this->email->send();

		
			
		
				
			//echo '<pre>'; print_r($this->email->print_debugger());die;
			
			payment_cancelled($booking_det['mobile'], $booking_det['booking_no']);
			
			$response = array(
				'success' => TRUE,
				'message' => 'Booking Cancellation done successfully.',
			);
		}else{
			$response = array(
				'success' => FALSE,
				'message' => 'Unable to Cancel booking.',
			);
		}

		echo json_encode($response); exit;
	}

}