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/taxicamera/applicationold/controllers/admin/DriverSettlement_bk.php
<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class DriverSettlement extends MY_Controller {
	public function __construct() {
		parent::__construct();
		//$this->redirect_guest();
		$this->admin=$this->session->userdata('admin');
		$this->load->helper('download');	
		$this->load->model('admin/mdriver');
		$this->load->model('admin/Mdriversettlement');
	}
	public function  index() { 
		//echo $this->session->userdata('email');die;
		$this->_load_list_view_new_driver();		
	}	
	private function _load_list_view_new_driver() {
	 // FUNCTION WRIITTEN IN COMMON HELPER
		$data					=  array();
		$data['content'] 		= 'admin/driver_settlement/driver_settlement';
		$data['driver_list'] 	= $this->mdriver->get_driver_list('1');	
		//pr($data['driver_list']);
		$this->load->view('admin/layouts/index', $data);
		
	}
	public function ajaxDriverShiftDetails(){
		$shift_details		= array();
		$data				= array();
		$shift_data			= array();
		$drv_shift_data		= array();
		$week_array			= array();
		$shift_roster 		= array();
		$shift_roster_data	= array();
		$order_type			= '';
	 	$order				= '';
		$driver_id			= $this->input->post('driver_id');
		$current_dt 		= date('Y-m-d');
		$current_strtime 	= strtotime(date('Y-m-d'));
		$day_of_week 		= date('N', strtotime($current_dt));
		$given_date 		= strtotime($current_dt);
		$first_of_week 		=  date('Y-m-d', strtotime("- {$day_of_week} day", $given_date));
		$first_of_week 		= strtotime($first_of_week);
		for($i=1 ;$i<=7; $i++) {
		    $week_dt = date('Y-m-d', strtotime("+ {$i} day", $first_of_week));
		    $tmp_week_strtime	= strtotime("+ {$i} day", $first_of_week);
		    if($current_strtime < $tmp_week_strtime){

		    	$shift_roster_data['day']				=	strtoupper(date('D', $tmp_week_strtime));
		    	$shift_roster_data['shift_start_time']	=	' ';
		    	$shift_roster_data['shift_name']		=	' ';
		    }
		    else{
		    	
		    	$roster_joindata   = array('select'    		=>'roster.day_date,RSM.shift_name,DSD.shift_start_time',
					                        'first_table'   =>'roster_driver_maping RDM',
					                        'second_table'  =>'roster',
					                        'dependency1'   =>'RDM.roster_id = roster.roster_id',
					                        'join_type1'    =>'LEFT',
					                        /**********************************/                
				                            'third_table'   =>'roster_shift_maping RSM',
				                            'dependency2'   =>'RSM.roster_id = RDM.roster_id',
				                            'join_type2'    =>'LEFT',
				                            /********************************/
				                            'forth_table'   =>'driver_shift_details DSD',
				                            'dependency3'   =>'DSD.roster_driver_id = RDM.roster_driver_id',
				                            'join_type3'    =>'LEFT'
                            
	                      				);
				$shift_roster_cond		= array('RDM.driver_id' => $driver_id,'roster.day_date' =>$week_dt);
      			$shift_roster_list 		= $this->mcommon->joinQuery($roster_joindata,$shift_roster_cond,'result',$order,$order_type);
      			//pr($shift_roster_list,0);
      			if(!empty($shift_roster_list)){
      				foreach($shift_roster_list as $val_sft){      					
      					$shift_roster_data['day']				=	strtoupper(date('D', $tmp_week_strtime));		    			
		    			if(!empty($val_sft['shift_start_time'])){
		    				$shift_roster_data['shift_start_time']	=	'OK';
		    			}
		    			else{
		    				$shift_roster_data['shift_start_time']	=	'NO';
		    			}
		    			$shift_roster_data['shift_name']		=	$val_sft['shift_name'];
      				}
      				
      			}
      			else{
      					$shift_roster_data['day']					=	strtoupper(date('D', $tmp_week_strtime));
		    			$shift_roster_data['shift_start_time']		=	' ';
		    			$shift_roster_data['shift_name']			=	' ';
      			}
		    }
		    $shift_roster[]				= $shift_roster_data;
		}
		
		$data['shift_roster']  = $shift_roster;
		$joindata   = array('select'    	=>'master_user.dc_no,user_profile.dr_licence_expiry,user_profile.dr_dc_expiry,RDM.roster_shift_id,roster.*,master_car.registration_no as car_no',
	                        'first_table'   =>'roster_driver_maping RDM',
	                        'second_table'  =>'roster',
	                        'dependency1'   =>'RDM.roster_id = roster.roster_id',
	                        'join_type1'    =>'INNER',
	                        /**********************************/           
                            'third_table'   =>'master_car',
                            'dependency2'   =>'master_car.car_id = roster.car_id',
                            'join_type2'    =>'INNER',
                            /********************************/
                            'forth_table'   =>'master_user',
                            'dependency3'   =>'master_user.user_id = RDM.driver_id',
                            'join_type3'    =>'INNER',
                            /********************************/
                            'fifth_table'   =>'user_profile',
                            'dependency4'   =>'user_profile.user_id = master_user.user_id',
                            'join_type4'    =>'INNER'
	                      );
	 	
	 	$cond			= array('RDM.driver_id' => $driver_id);
      	$shift_lists 	= $this->mcommon->joinQuery($joindata,$cond,'result',$order,$order_type);
      	//pr($shift_lists);
      	if(!empty($shift_lists)){
      		foreach($shift_lists as $val){
      			$shift_data['dc']					= $val['dc_no'];
      			$shift_data['dr_dc_expiry']			= date('d/m/Y',STRTOTIME($val['dr_dc_expiry']));
      			$shift_data['dr_licence_expiry']	= date('d/m/Y',STRTOTIME($val['dr_licence_expiry']));
      			$shift_data['shift_id'] 			= $val['roster_shift_id'];
      			$shift_data['car_no'] 				= $val['car_no'];
      			$shift_data['shift_date'] 			= date('d/m/Y',STRTOTIME($val['day_date']));

      			$cond_shift		= array('shift_id' => $val['roster_shift_id'],'driver_id' =>$driver_id);
      			$payin_lists 	= $this->mcommon->getROW('payin',$cond_shift);
      			if(!empty($payin_lists)){
      				$shift_data['payin_payout_amt']	= $payin_lists['total_payin_payout'];
      			}
      			else{
      				$shift_data['payin_payout_amt']	= '';
      			}
      			$drv_shift_data[]  	= $shift_data;
      		}
      		$data['drv_shift_data']	= $drv_shift_data;
      		$shift_details['driver_shift_data']  = '1';
      	}
      	else{
      		$data['drv_shift_data']	= '';
      		$shift_details['driver_shift_data']  = '0';
      	}
      	//pr($drv_shift_data);
      	//echo $this->load->view('admin/driver_settlement/ajax_driver_shift_details',$data,true);exit;
      	$shift_details['html']  = $this->load->view('admin/driver_settlement/ajax_driver_shift_details',$data,true);

      	echo json_encode($shift_details);exit;
	}
	
	public function ajaxDriverSettlement(){		
		$data 				= array();
		$driver_settlement	= array();
		$drv_shift_data		= array();
		$week_array			= array();
		$driver_expense 	= array();
		$driver_dockets 	= array();
		$settlement_details = array();
		$payment_options 	= array();
		$shift_id			= $this->input->post('shift_id');
		$driver_id			= $this->input->post('driver_id');
		$order_type			= '';
	 	$order				= '';
	 	$payment_options  	= $this->mcommon->getFullDetails('master_payment_option');
	 	if(!empty($payment_options)){
	 		$data['payment_options']	= $payment_options;
	 	}
		$cond 				= array('driver_id' => $driver_id,'shift_id' =>$shift_id);
		$driver_settlement  = $this->mcommon->getRow('payin',$cond);
		if(!empty($driver_settlement)){

			/*************** Driver Dockets ***************/
			//echo $driver_settlement['payin_id'];exit;
			$joindata   = array('select'    	=>'payin_dockets.*,master_docket.docket_name',
		                        'first_table'   =>'payin_dockets',
		                        'second_table'  =>'master_docket',
		                        'dependency1'   =>'payin_dockets.docket_id = master_docket.docket_id',
		                        'join_type1'    =>'left'
	                        );

			$cond_dockets 		= array('payin_id' => $driver_settlement['payin_id']);
			$driver_dockets  	= $this->mcommon->joinQuery($joindata,$cond_dockets,'result',$order,$order_type);

			/*************** Driver Expense ***************/
			//echo $driver_settlement['payin_id'];exit;
			$expense_joindata   = array('select'    	=>'payin_expenses.*,master_expense.expense_name',
				                        'first_table'   =>'payin_expenses',
				                        'second_table'  =>'master_expense',
				                        'dependency1'   =>'payin_expenses.expense_id = master_expense.expense_id',
				                        'join_type1'    =>'left'
			                        );

			$cond_expense 		= array('payin_id' => $driver_settlement['payin_id']);
			$driver_expense  	= $this->mcommon->joinQuery($expense_joindata,$cond_expense,'result',$order,$order_type);
		}

		$total_settlement   	= $this->settlementCalculation($driver_settlement['payin_id'],$driver_id,$shift_id);

		if(!empty($total_settlement)){
			$data['total_settlement']	= $total_settlement;
		}
		//pr($data['total_settlement']);
		$data['driver_id']			= $driver_id;
		$data['shift_id']			= $shift_id;
		$data['payin_id']			= $driver_settlement['payin_id'];
		$data['driver_expenses']	= $driver_expense;
		$data['driver_docket']		= $driver_dockets;
		$data['driver_settlement']	= $driver_settlement;
		$settlement_details['html']	= $this->load->view('admin/driver_settlement/ajax_driver_settlement',$data,true);
		echo json_encode($settlement_details);exit;
	}

	public function settlementCalculation($payin,$driver_id,$shift_id,$discount = 0,$other = 0){
		
		$total_dockets			= 0.00;
		$total_expense			= 0.00;
		$accident_installment	= 0.00;
		$bond_total				= 0.00;
		$dr_commission			= 0.00;
		$total_extra_amount		= 0.00;
		$levy_total				= 0.00;
		$bond_total				= 0.00;
		$no_of_whl				= 0.00; 
		$amount_due				= 0.00;
		$total 					= 0.00;
		$settlement_calculation	= array();
		$cond_payin 			= array('payin_id' => $payin);

		/****************** total Dockets ***************/
			$total_dockets  = $this->Mdriversettlement->totalDockets('payin_dockets',$cond_payin);
			$settlement_calculation['total_dockets']	= number_format($total_dockets,2);

		/****************** total Expense ***************/
			$total_expense = $this->Mdriversettlement->totalExpense('payin_expenses',$cond_payin);
			$settlement_calculation['total_expense']	= number_format($total_expense,2);

		/******************* total Accident Installment ***************/		

			$cond_accident_installment 	= array('master_accident.driver_id' => $driver_id,'master_accident.shift_id' => $shift_id);
			$accident_installment  		= $this->Mdriversettlement->AccidentInstallment($cond_accident_installment);
			$settlement_calculation['accident_installment']	= number_format($accident_installment,2);

		/******************* Driver comission ***************************/
			$commission_cond 	= array('user_id' => '31');
			$dr_commission		= $this->Mdriversettlement->getCommission($commission_cond);


		/******************* other total***************************/
			$cond 	= array('driver_id' => $driver_id,'shift_id' => $shift_id);		
			$driver_settlement  = $this->mcommon->getRow('payin',$cond);
			if(!empty($driver_settlement)){
				if(!empty($driver_settlement['total_extra_end']) && !empty($driver_settlement['total_extra_start'])){
					$total_extra_amount = ($driver_settlement['total_extra_end'] - $driver_settlement['total_extra_start'])*$dr_commission;
				}
				if(!empty($driver_settlement['levy'])){
					$levy_total		= $driver_settlement['levy'];
				}
				if(!empty($driver_settlement['bond_charges'])){
					$bond_total			= $driver_settlement['bond_charges'];
				}
				if(!empty($driver_settlement['no_of_whl'])){
					$no_of_whl			= $driver_settlement['no_of_whl'];
				}
				$settlement_calculation['total_extra_amount']	= number_format($total_extra_amount,2);
				$settlement_calculation['levy_total']			= number_format($levy_total,2);
				$settlement_calculation['bond_total']			= number_format($bond_total,2);
				$settlement_calculation['no_of_whl']			= number_format($no_of_whl,2);
				$settlement_calculation['other']				= number_format($other,2);
				$amount_due			= ($total_extra_amount + $levy_total) - ($total_dockets + $total_expense + $accident_installment + $bond_total + $no_of_whl + $other);
				$total 				= $amount_due - $discount;
				$settlement_calculation['amount_due']			= number_format($amount_due,2);
				$settlement_calculation['discount']				= number_format($discount,2);
				$settlement_calculation['total']				= number_format($total,2);
			}	

		return 	$settlement_calculation;
	}
	public function saveDriverSettlement(){
		$respons_arr		= array();
		$payin_id		= $this->input->post('payin_id');
		$start_value	= $this->input->post('start_value');
		$end_value		= $this->input->post('end_value');
		$start_colmn	= $this->input->post('start_colmn');
		$end_colmn		= $this->input->post('end_colmn');
		$condition		= array('payin_id' => $payin_id);
		$updateArr		= array($start_colmn => $start_value,
								$end_colmn	 => $end_value,
								'updated_ts' => date('Y-m-d h:i:s'),
								'updated_by' =>	$this->session->userdata('user_data')
							);
		$update_Settlement	= $this->mcommon->update('payin',$condition,$updateArr);


		if($update_Settlement) {
			
			$respons_arr['process'] = 'success';
		}
		else{
			$respons_arr['process'] = "failed";
		}
		$driver_id				= $this->input->post('driver_id');
		$shift_id				= $this->input->post('shift_id');
		$payin_id				= $this->input->post('payin_id');
		$respons_arr['total_settlement']   	= $this->settlementCalculation($payin_id,$driver_id,$shift_id);
		echo json_encode($respons_arr);exit;
	}
	public function saveDriverDocket(){
		$respons_arr		= array();
		$payin_dockets_id	= $this->input->post('payin_dockets_id');
		$driver_docket_pr	= $this->input->post('driver_docket_pr');
		$condition			= array('payin_dockets_id' => $payin_dockets_id);
		$updateArr			= array('value' => $driver_docket_pr
							);
		$update_Settlement	= $this->mcommon->update('payin_dockets',$condition,$updateArr);
		if($update_Settlement) {
			$total_settlement   	= $this->settlementCalculation($payin_id,$driver_id,$shift_id,$discount);			

			$respons_arr['process'] = 'success';
			
		}
		else{
			$respons_arr['process'] = "failed";
		}
		$driver_id				= $this->input->post('driver_id');
		$shift_id				= $this->input->post('shift_id');
		$payin_id				= $this->input->post('payin_id');
		$respons_arr['total_settlement']   	= $this->settlementCalculation($payin_id,$driver_id,$shift_id,$discount);
		pr($respons_arr);
		//echo json_encode($respons_arr);exit;
	}
	
	public function saveDriverExpense(){
		$payin_expense_id	= $this->input->post('payin_expense_id');
		$driver_expense_pr	= $this->input->post('driver_expense_pr');
		$condition			= array('payin_expense_id' => $payin_expense_id);
		$updateArr			= array('value' => $driver_expense_pr
							);
		$update_Settlement	= $this->mcommon->update('payin_expenses',$condition,$updateArr);
		if($update_Settlement) {
			$respons_arr['process'] = 'success';
		}
		else{
			$respons_arr['process'] = "failed";
		}
		$driver_id				= $this->input->post('driver_id');
		$shift_id				= $this->input->post('shift_id');
		$payin_id				= $this->input->post('payin_id');
		$respons_arr['total_settlement']   	= $this->settlementCalculation($payin_id,$driver_id,$shift_id,$discount);
		echo json_encode($respons_arr);exit;
	}
	public function calculateDiscount(){
		$total_settlement	= array();
		$driver_id	= $this->input->post('driver_id');
		$shift_id	= $this->input->post('shift_id');
		$payin_id	= $this->input->post('payin_id');
		$discount 	= $this->input->post('discount');
		$other 		= $this->input->post('other');
		$total_settlement   	= $this->settlementCalculation($payin_id,$driver_id,$shift_id,$discount,$other);

		echo json_encode($total_settlement);exit;
	}
}