<?php  

   session_start();
   error_reporting(0);
   include("db.php");
   include("all_query.php");
   
   
 $json = '{"callID":"TE_Manual_25841_206","resource_url":"http://192.168.1.112/PREDIALERREC/TE_Manual_25841_206.wav","missedFrom":"Agent","campaignID":"Callextremein","destination":"09911225841","end_time":"2022-02-14 21:12:21","callType":"Outbound","call_duration":"0","dispnumber":"NA","start_time":"2022-02-14 21:12:21","caller_id":"agent1","npfID":"null","status":"Failed"}';


	$data  = array();
    $jsonArray = json_decode(file_get_contents('php://input'),true);
	
	
	
	//$jsonArray = json_decode($json,true);
	
	 
	 $DATA = array();
	 
	  
	 if(isset($jsonArray['npfID']))
	 {
      $DATA['uniqueid'] = $jsonArray['npfID'];
	 }
	 
	 
	 if(isset($jsonArray['callID']))
	 {

	  
      $DATA['callID'] = $jsonArray['callID'];	 
      $DATA['resource_url'] = $jsonArray['resource_url'];	 
      $DATA['missedFrom'] = $jsonArray['missedFrom'];	 
      $DATA['campaignID'] = $jsonArray['campaignID'];	 
      $DATA['destination'] = $jsonArray['destination'];	 
      $DATA['end_time'] = $jsonArray['end_time'];	 
      $DATA['callType'] = $jsonArray['callType'];	 
      $DATA['call_duration'] = $jsonArray['call_duration'];	 
      $DATA['dispnumber'] = $jsonArray['dispnumber'];	 
      $DATA['start_time'] = $jsonArray['start_time'];	 
      $DATA['caller_id'] = $jsonArray['caller_id'];	 
      $DATA['npfID'] = $jsonArray['npfID'];	 
      $DATA['call_status'] = $jsonArray['status'];	 
	 
	 
	 
	 
	 // get data from call init
	 
	 $uniqueid =  $DATA['uniqueid'];
	 
	 $initQ = mysqli_query($conn,"select *from `call_init` where uniqueid = '$uniqueid' ");
	 $initData = mysqli_fetch_assoc($initQ);
	 
	 $DATA['customer_name'] = $initData['customer_name'];
	 $DATA['customer_mobile'] = $initData['customer_mobile'];
	 $DATA['account_holder_id'] = $initData['account_holder_id'];
	 
	 $account_holder_id =  $initData['account_holder_id'];
	
     $DATA['is_read'] = '0';
	 
	  insertTable($conn,'call_reponse',$DATA,'add_submit');
	  
	  
	  
	 
	  
	  
	  
	   
	   //  call api here for zestmoney
	   
	   
	   /***********************************************************************************************/
	   
	   if(strtoupper($DATA['call_status']) != 'ANSWER')
	   {
		   
		   
	   $customerQ = mysqli_query($conn,"select *from `customer` where account_holder_id = '$account_holder_id' ");
	   $customerData = mysqli_fetch_assoc($customerQ);
	   
	   $customer_id = $customerData['id'];
	   $mobile = $initData['customer_mobile'];
	   $attempt_type = 'Tellecalling';
	   $attempt_status = 'No Contact';
	   $disposition = 'NC';
	   $sub_disposition = 'Switch off/Not reachable/DNE';
	   $reason_for_default = 'No contact';
	   $remark = 'Call Failed';
	   $update_by = $initData['advisor_name'];
	   $advisor_mobile =  $initData['advisor_mobile'];
  	   
	   
	   
	   if(strtoupper($DATA['call_status']) == 'BUSY' ||  strtoupper($DATA['call_status']) == 'CANCEL' || strtoupper($DATA['call_status']) == 'NOANSWER'  ||  strtoupper($DATA['call_status']) == 'NOT ANSWER' )
	   {
		 $disposition = 'RNR';
	      $sub_disposition = 'Ringing No Response/Busy/Disconnecting the calls';
		   
	   }
	   
	   
	    if(strtoupper($DATA['call_status']) == 'CHANUNAVAIL' ||  strtoupper($DATA['call_status']) == 'CONGESTION' || strtoupper($DATA['call_status']) == 'FAILED' )
	   {
		 $disposition = 'NC';
	      $sub_disposition = 'Switch off/Not Reachable/DNE';
		   
	   }
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	    
					$curl = curl_init();

					curl_setopt_array($curl, array(
					  CURLOPT_URL => 'http://finmanservices.in/bhawani/crm/save_update_ajax.php',
					  CURLOPT_RETURNTRANSFER => true,
					  CURLOPT_ENCODING => '',
					  CURLOPT_MAXREDIRS => 10,
					  CURLOPT_TIMEOUT => 0,
					  CURLOPT_FOLLOWLOCATION => true,
					  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					  CURLOPT_CUSTOMREQUEST => 'POST',
					  CURLOPT_POSTFIELDS => array('customer_id' => $customer_id,'account_holder_id' => $account_holder_id,'mobile' => $mobile,'attempt_type' => $attempt_type,'attempt_status' => $attempt_status,'disposition' => $disposition,'sub_disposition' => $sub_disposition,'reason_for_default' => $reason_for_default,'remark' => $remark,'update_by' => $update_by,'advisor_mobile' => $advisor_mobile,'uniqueid' => $uniqueid ),
					  CURLOPT_HTTPHEADER => array(
						'Cookie: PHPSESSID=064fdc6edddd9a77bc1073ef7dd10b42'
					  ),
					));

					$response = curl_exec($curl);

					curl_close($curl);
					 
	   
	   
	   
	   }
	   
	   
	   
	   
	   /*******************************************************************************************************/
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
			
			
	  $result = array();
	  
	  $result['status'] = 'success'; 
	  $result['message'] = 'Response added successfully'; 
	  
	  
	  
	 }
	 else
		 
		 {
			 
			 		
			
	  $result = array();
	  
	  $result['status'] = 'falied'; 
	  $result['message'] = 'invalid Data'; 
	  
		 }
			
			
			
	     echo json_encode($result);
			
   


?>