 <?php header('Refresh: 20'); ?>

 <?php 
 
 
				$smsdb  =  mysql_connect("localhost", "root", "robisearchltd@2023") or die(mysql_error());
				$date= date("Y-m-d h:i:s");

								mysql_query("USE  robipos",$smsdb);
								$user_query=mysql_query("select   * from  sms_in where used='0' or  amount <1 ORDER BY id DESC limit 20 ")or die(mysql_error());
								$num_row = mysql_num_rows($user_query);
									
									if( $num_row > 0 ) {
										$exactime="";
											  function  convertString ($date)
    {
        // convert date and time to seconds
        $sec = strtotime($date);
 
        // convert seconds into a specific format
        $date = date("Y-m-d H:i", $sec);
 
        // append seconds to the date and time
        $exactime = $date . ":00";
 
        // print final date and time
       return $exactime;
    }
									while($row=mysql_fetch_array($user_query)){
									
   
     
    // Driver code
	
	
	




								
						   $smsid=$row['id'];
								
			 $message=$row['sms_text'];
				$senttime=$row['sent_dt'];
		
		 $mpesadata = explode(" ",$message);
			$mpescode = explode("Ref. ",$message);
			$realcode=trim($mpescode[1]);
			$getcode=explode(" ",$realcode);
	
			
	 $mpesamount = explode("KES.",$message);
	 	$regetamount=trim($mpesamount[1]);
	 	$getamount=explode(" ",$regetamount);
		
		 $amount = $getamount[0];
	
	 $mpesphone = explode("Phone No",$message);
	 	$realphone=$mpesphone[1];
	 	$getphone=explode(" ",$realphone);
	
	
				$phone = $getphone[1];
				
				
			
				
				

				
					 $transactioncode = $getcode[0]; 
						  
					
						 // 	var_dump($transactioncode);
			
						  
						   
					

	 
					
							
				
					 $amount=str_replace(",", "", $amount);
					//echo $amount=formatMoney($finalmpesaamount[0], true); echo"<br>";

					 $mpesanames = explode("from ",$message);
	 	$realname=$mpesanames[1];
	 	$getnames=explode(" ",$realname);
	
					
  $firstname = $getnames[1];
 $lastname = ""; 
 
  if(stripos($getnames[1],'.')){
	  $mylastname = $getnames[1];
$names = explode(".",$mylastname);
 $lastname = $getnames[2];
	 
 // echo "hiko";
  }else{ 
    $lastname = $getnames[2];
  

  } 

		
					
							
								
									 
							  if($amount > 0){
										if(mysql_query("UPDATE sms_in SET used='2',	sender_number='$phone',exactime='$senttime',code='$transactioncode',amount='$amount',firstname='$firstname',lastname='$lastname' WHERE id='$smsid' and  sms_text='$message'")){
											
												//echo "all data saved";
										}else{
												echo "No  data saved";
										}
							  }
								
								
								}
								
								
								
								
								
								
								
								
									}else{
										
																	echo "No  new message available";

										
									}

							?>

 
				
				