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/RentIn.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');

class RentIn extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin/mrentin');
		$this->load->library('advantagepay');

	}
	/**
	 * 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/driver_guide/general/urls.html
	 */
	public function index() 
	{
		$data['rentinDetails'] = $this->mrentin->getRentinDetails();
		//print_r($data);die;
		$data['content'] = 'admin/rent_in/list';
		$this->load->view('admin/layouts/index', $data);
	}

	public function add()
	{
		$data['drivers'] = $this->mrentin->get_rentout_driver();
		$data['content'] = 'admin/rent_in/add';
		$this->load->view('admin/layouts/index', $data);
	}

	public function getRentOutDetails()
	{
		$driver_rent_out_id=$this->input->post('driver_id');
		$driver_rent_out_array=explode('-',$driver_rent_out_id);
		$driver_id=$driver_rent_out_array[0];
		$rent_out_id=$driver_rent_out_array[1];
		$rent_out_data= $this->mrentin->getRentOutDetails($driver_id,$rent_out_id);
		$actual_bond_amount= $this->mrentin->getdriverbondamount($driver_id);
		
		echo json_encode(array('status'=>true,'rent_out_data'=>$rent_out_data,'actual_bond_amount'=>$actual_bond_amount));
	}

	public function viewrentin($rent_in_id)
	{
		$data = array();
		$data['rentinDetails'] = $this->mrentin->getRentinDetails($rent_in_id);
		$data['rentOutDetails'] = $this->mrentin->getRentOutDetails($data['rentinDetails']['driver_id'],$data['rentinDetails']['rent_out_id']);
		$data['content'] = 'admin/rent_in/view';
		$this->load->view('admin/layouts/index', $data);
	}

	public function editrentin($rent_in_id)
	{
		// $data['drivers'] = $this->mrentin->get_rentout_driver();
		$data['rentinDetails'] = $this->mrentin->getRentinDetails($rent_in_id);
		$data['content'] = 'admin/rent_in/edit';
		$this->load->view('admin/layouts/index', $data);
	}

	public function submitrentin()
	{//echo '<pre>'; print_r($this->input->post());die; 
		$driver_rent_out_id=$this->input->post('driver_id');
		$driver_rent_out_array=explode('-',$driver_rent_out_id);
		$driver_id=$driver_rent_out_array[0];
		$rent_out_id=$driver_rent_out_array[1];

		if(empty($driver_id)){
			$return_data = array('status'=>false,'msg'=>'Please fill up the details');
			echo json_encode($return_data);die;
		}
			$car_id=$this->input->post('car_id');
			$odometer_reading=$this->input->post('odometer_reading');
			$car_details = $this->mrentin->edit_car($car_id);

			if($car_details['total_odometer_reading']>=$odometer_reading){
	
				$return_data = array('status'=>false,'msg'=>'Rent In <b>Odometer Reading</b> must be grater than this car previous <b>Odometer Reading</b>.');
				echo json_encode($return_data);die;
			}
			
			$data = array(
				'driver_id' => $driver_id,
				'rent_out_id' => $rent_out_id,
				'odometer_reading' =>$odometer_reading,
				'damage' =>$this->input->post('damage'),
				'damage_amount' => $this->input->post('damage_amount'),
				'fuel' => $this->input->post('fuel'),
				// 'bond_refund_amount' => $this->input->post('bond_refund_amount'),
				'bond_refund_request' => $this->input->post('bond_refund_request'),
				// 'bond_refund_date'=> date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('bond_refund_date')))),
				'notes'=> $this->input->post('notes'),
				'rent_in_no'=> 'RI'.time().'/CAR'.$this->input->post('car_no'),
				'created_by' => $this->admin_session_data['user_id'],
				'created_ts' => date('Y-m-d H:i:s')
			);												

			if(!empty($_FILES['damage_img']['name'])){

				$path1='damage_img';
				$damage_img=$this->image_upload($path1);
				$data['damage_img'] = $damage_img;
			}
			if(!empty($_FILES['front_img']['name'])){

				$path2='front_img';
				$front_img=$this->image_upload($path2);
				$data['front_img'] = $front_img;
			}
			if(!empty($_FILES['rear_img']['name'])){

				$path3='rear_img';
				$rear_img=$this->image_upload($path3);
				$data['rear_img'] = $rear_img;
			}
			if(!empty($_FILES['driver_side_img']['name'])){

				$path4='driver_side_img';
				$driver_side_img=$this->image_upload($path4);
				$data['driver_side_img'] = $driver_side_img;
			}
			if(!empty($_FILES['passenger_side_img']['name'])){

				$path5='passenger_side_img';
				$passenger_side_img=$this->image_upload($path5);
				$data['passenger_side_img'] = $passenger_side_img;
			}
			if(!empty($_FILES['odometer_img']['name'])){

				$path6='odometer_img';
				$odometer_img=$this->image_upload($path6);
				$data['odometer_img'] = $odometer_img;
			}
			if(!empty($_FILES['fuel_guage_img']['name'])){

				$path7='fuel_guage_img';
				$fuel_guage_img=$this->image_upload($path7);
				$data['fuel_guage_img'] = $fuel_guage_img;
			}


			
			try {

				$rent_out_details = $this->mrentin->get_rent_out_details($rent_out_id);
				if($rent_out_details['direct_debit_status'] == 'Draft'){

					$advantage_pay_token = $this->advantagepay->getToken($rent_out_details['advantage_pay_credentials']);
					$advantage_pay_token = json_decode($advantage_pay_token,true);
					
					if(!$advantage_pay_token){
						$return_data = array('status' => false, 'msg' => 'Token not generated');
						echo json_encode($return_data);die;
					}

					//print_r($rent_out_details);die; 
					$direct_debit_response = $this->advantagepay->deleteDirectDebits($rent_out_details['direct_debit_id'], $advantage_pay_token['access_token']);
					$direct_debit_response_array = json_decode($direct_debit_response,true);
					//echo '<pre>';print_r($direct_debit_response_array);die;
					if(!$direct_debit_response_array){
						$return_data = array('status' => false, 'msg' => 'Direct debit not deleted');
						echo json_encode($return_data);die;
					}
					if(isset($direct_debit_response_array['ErrorCode']) && $direct_debit_response_array['ErrorCode'] !='not_found'){
						$return_data = array('status' => false, 'msg' => $direct_debit_response_array['Messages'][0]);
						echo json_encode($return_data);die;
					}
					// if(empty($direct_debit_response_array['http_code']) || $direct_debit_response_array['http_code'] != 200){
					// 	$return_data = array('status' => false, 'msg' => 'Direct debit not deleted');
					// 	echo json_encode($return_data);die;
					// } 

					
					$data['is_deleted_direct_debit'] = ($direct_debit_response_array['http_code'] == 200)?1:0;
					
				}


			$this->db->trans_start(); # Starting Transaction
			$result = $this->mrentin->submit_rentin($data);
			
			$condition = array('car_id' => $car_id);
			$car_data= array(
				'total_odometer_reading' =>$odometer_reading,
				'user_type' =>'USER',
				'updated_by' => $this->admin_session_data['user_id'],
				'updated_ts' => date('Y-m-d H:i:s')
			);
		    $this->mrentin->update_car($condition, $car_data);

			  if($this->input->post('bond_refund_request') == 'Yes'){

					$bond_data[] = array(
						'driver_id' => $driver_id,
						'bond_amount' => $this->input->post('amount_want_to_refund'),
						'transaction_type' => 'DEBIT',
						'bond_date' => date('Y-m-d'),
						'bond_payment_method' => $this->input->post('bond_payment_method'),
						'bond_reference_no' => $this->input->post('bond_reference_no'),
						'bond_reference_type' => 'RENTIN',
						'bond_reference_id' => $result,
						'created_by' => $this->admin_session_data['user_id'],
						'created_ts' => date('Y-m-d H:i:s')
					);

					if($this->input->post('damage_amount') > 0){

					$bond_data[] = array(
						'driver_id' => $driver_id,
						'bond_amount' => $this->input->post('damage_amount'),
						'transaction_type' => 'DEBIT',
						'bond_date' => date('Y-m-d'),
						'bond_payment_method' => $this->input->post('bond_payment_method'),
						'bond_reference_no' => $this->input->post('bond_reference_no'),
						'bond_reference_type' => 'RENTIN',
						'bond_reference_id' => $result,
						'created_by' => $this->admin_session_data['user_id'],
						'created_ts' => date('Y-m-d H:i:s')
					);
				}
				if($this->input->post('fuel') > 0){

					$bond_data[] = array(
						'driver_id' => $driver_id,
						'bond_amount' => $this->input->post('fuel'),
						'transaction_type' => 'DEBIT',
						'bond_date' => date('Y-m-d'),
						'bond_payment_method' => $this->input->post('bond_payment_method'),
						'bond_reference_no' => $this->input->post('bond_reference_no'),
						'bond_reference_type' => 'RENTIN',
						'bond_reference_id' => $result,
						'created_by' => $this->admin_session_data['user_id'],
						'created_ts' => date('Y-m-d H:i:s')
					);
					

				}

				$this->db->insert_batch('bond_wallet', $bond_data);

					$bond_refund_data = array(
						'driver_id' => $driver_id,
						'total_bond_amount' => $this->input->post('bond_refund_amount'),
						'refund_type' =>$this->input->post('refund_type'),
						'amount_want_to_refund' =>$this->input->post('amount_want_to_refund'),
						'notice_date' => date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('notice_date')))),
						'bond_refund_due_date' => date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('bond_refund_due_date')))),
						'action_type' => 'RENTIN',
						'remarks' => $this->input->post('remarks'),
						'reference_id' => $result,
						'created_by' => $this->admin_session_data['user_id'],
						'created_ts' => date('Y-m-d H:i:s')
					);
				  	$this->db->insert('bond_refund', $bond_refund_data);
			  }

			  $condition = array('rent_out_id' => $rent_out_id);
			  $this->mrentin->update_rent_out($condition, array('is_rent_in'=>'1'));

			  //-----------------For Inactive Driver-------------------------//

			  $this->db->update('master_driver',array('status'=>'2'),array('driver_id'=>$driver_id));

			  //-----------------For Inactive Driver-------------------------//

			  $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 Part is for Activity Log*******************************//

				$activityLogData = array(
					'activity_type' => '<b>Add Rent In</b>',
					'description' => 'New Rent In - '.$data['rent_in_no'].' Added',
					'link' => 'admin/rentIn',
					'icon' => '<i class="fa fa-history" aria-hidden="true"></i>',
				);

				$this->activity_log($activityLogData);

				//************************This Part is for Activity Log*******************************//
				//************************************************************************************//
				
				$this->db->trans_commit();
				$return_data = array('status'=>true,'msg'=>'Rent In Added Successfully');
				
			}

			echo json_encode($return_data);die;

		} catch (Exception $e) {
			// this will not catch DB related errors. But it will include them, because this is more general. 
			$return_data = array('status' => false, 'msg' => $e->getMessage());
			echo json_encode($return_data);die;
		}

		
			
	}

	public function updaterentin()
	{
		$car_id=$this->input->post('car_id');
		$odometer_reading=$this->input->post('odometer_reading');
		$car_details = $this->mrentin->edit_car($car_id);
		$rentinDetails = $this->mrentin->getRentinDetails($this->input->post('hid_rent_in_id'));

		if($car_details['total_odometer_reading']>=$odometer_reading){

			$return_data = array('status'=>false,'msg'=>'Rent In <b>Odometer Reading</b> must be grater than this car previous <b>Odometer Reading</b>.');
			echo json_encode($return_data);die;
		}
		
			$data = array(
				// 'odometer_reading' =>$this->input->post('odometer_reading'),
				// 'damage' =>$this->input->post('damage'),
				// 'damage_amount' => $damageAmount,
				// 'fuel' => $this->input->post('fuel'),
				// 'bond_refund_amount' => $bond_refund_amount,
				// 'bond_refund_request' => $this->input->post('bond_refund_request'),
				// 'bond_refund_date'=> $bond_refund_date,
				'notes'=> $this->input->post('notes'),
				'updated_by' => $this->admin_session_data['user_id'],
				'updated_ts' => date('Y-m-d H:i:s')
			);	

			if(!empty($_FILES['damage_img']['name'])){

				$path1='damage_img';
				$damage_img=$this->image_upload($path1);
				@unlink('./public/admin_images/rent_in/damage_imgs/' . $this->input->post('damage_img_old'));
				$data['damage_img'] = $damage_img;
			}
			if(!empty($_FILES['front_img']['name'])){

				$path2='front_img';
				$front_img=$this->image_upload($path2);
				@unlink('./public/admin_images/rent_in/front_imgs/' . $this->input->post('front_img_old'));
				$data['front_img'] = $front_img;
			}
			if(!empty($_FILES['rear_img']['name'])){
	
				$path3='rear_img';
				$rear_img=$this->image_upload($path3);
				@unlink('./public/admin_images/rent_in/rear_imgs/' . $this->input->post('rear_img_old'));
				$data['rear_img'] = $rear_img;
			}
			if(!empty($_FILES['driver_side_img']['name'])){
	
				$path4='driver_side_img';
				$driver_side_img=$this->image_upload($path4);
				@unlink('./public/admin_images/rent_in/driver_side_imgs/' . $this->input->post('driver_side_img_old'));
				$data['driver_side_img'] = $driver_side_img;
			}
			if(!empty($_FILES['passenger_side_img']['name'])){
	
				$path5='passenger_side_img';
				$passenger_side_img=$this->image_upload($path5);
				@unlink('./public/admin_images/rent_in/passenger_side_imgs/' . $this->input->post('passenger_side_img_old'));
				$data['passenger_side_img'] = $passenger_side_img;
			}
			if(!empty($_FILES['odometer_img']['name'])){
	
				$path6='odometer_img';
				$odometer_img=$this->image_upload($path6);
				@unlink('./public/admin_images/rent_in/odometer_imgs/' . $this->input->post('odometer_img_old'));
				$data['odometer_img'] = $odometer_img;
			}
			if(!empty($_FILES['fuel_guage_img']['name'])){
	
				$path8='fuel_guage_img';
				$fuel_guage_img=$this->image_upload($path8);
				@unlink('./public/admin_images/rent_in/fuel_guage_imgs/' . $this->input->post('fuel_guage_img_old'));
				$data['fuel_guage_img'] = $fuel_guage_img;
			}

			$condition1 = array('rent_in_id' => $this->input->post('hid_rent_in_id'));
			$result = $this->mrentin->update_rentin($condition1, $data);

			$condition2 = array('car_id' => $car_id);
			$car_data= array(
				'total_odometer_reading' =>$odometer_reading,
				'user_type' =>'USER',
				'updated_by' => $this->admin_session_data['user_id'],
				'updated_ts' => date('Y-m-d H:i:s')
			);
		    $this->mrentin->update_car($condition2, $car_data);


			if($this->input->post('bond_refund_request') == 'Yes'){
				
			$bond_data = array(
				// 'bond_amount' => $this->input->post('bond_refund_amount'),
				'transaction_type' => 'DEBIT',
				'bond_date' => date('Y-m-d'),
				// 'bond_payment_method' => $this->input->post('bond_payment_method'),
				'bond_reference_no' => $this->input->post('bond_reference_no'),
				'bond_reference_type' => 'RENTIN',
				'created_by' => $this->admin_session_data['user_id'],
				'created_ts' => date('Y-m-d H:i:s')
			  );
			  $condition3 = array('bond_reference_id' => $this->input->post('hid_rent_in_id'));
			  $this->mrentin->update_bond_wallet($condition3, $bond_data);

				$bond_refund_data = array(
					'total_bond_amount' => $this->input->post('bond_refund_amount'),
					// 'refund_type' =>$this->input->post('refund_type'),
					// 'amount_want_to_refund' =>$this->input->post('amount_want_to_refund'),
					'notice_date' => date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('notice_date')))),
					'bond_refund_due_date' => date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('bond_refund_due_date')))),
					'action_type' => 'RENTIN',
					'remarks' => $this->input->post('remarks'),
					'created_by' => $this->admin_session_data['user_id'],
					'created_ts' => date('Y-m-d H:i:s')
				);
			  $condition4 = array('reference_id' => $this->input->post('hid_rent_in_id'));
			  $this->mrentin->update_bond_refund($condition4, $bond_refund_data);
		  }

			if ($result) {

				//************************************************************************************//	
				//************************This Part is for Activity Log*******************************//

				$activityLogData = array(
					'activity_type' => '<b>Update Rent out</b>',
					'description' => 'Rent out - '.$rentinDetails['rent_in_no'].' updated',
					'link' => 'admin/rentIn/editrentin/'.$this->input->post('hid_rent_in_id'),
					'icon' => '<i class="fa fa-history" aria-hidden="true"></i>',
				);

				$this->activity_log($activityLogData);

				//************************This Part is for Activity Log*******************************//
				//************************************************************************************//

				$return_data = array('status'=>true,'msg'=>'Rent In Added Successfully');
				echo json_encode($return_data);die;
			}
		
	}

	public function image_upload($path){
		// Upload folder location***
		$config = array();
		$config['upload_path'] = './public/admin_images/rent_in/'.$path.'s';
		// Allowed file type***
		$config['allowed_types'] = '*';
		$config['encrypt_name'] = TRUE;
		// load upload library***            
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ($this->upload->do_upload($path)) {
			return $this->upload->data()['file_name'];
		}else { 
            echo 'File Name'.$path.' Errors'.$this->upload->display_errors();die;
             
        }
	}
	
	// public function generatePdf($rent_in_id)
    // {
    //     $this->load->library('pdf');

	// 	$data['rentinDetails'] = $this->mrentin->getRentinDetails($rent_in_id);

    //     $filename = 'rental-' . time(); 
	// 	$html = $this->load->view('admin/rent_in/rentalPdf', $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));
    // }
}