<?php
include_once 'includes/functions.php';

$response_code ="01";


//	$sql = "SELECT std.id, std.number, sch_sp.invoice_id  FROM `sch_fees_pay_sport` sch_sp, students_nce std
//		WHERE std.application_number = sch_sp.application_number  AND sch_sp.batch > 0	AND sch_sp.checked = 0 ORDER BY std.id ASC";
       
     
    $sql = "SELECT std.id, std.number, sch_sp.invoice_id  FROM `sch_fees_pay_sport` sch_sp, students_nce std
		WHERE std.application_number = sch_sp.application_number AND sch_sp.batch = 1	AND sch_sp.checked = 0 ORDER BY std.id ASC";
       
     
     $r = mysqli_query($dbc,$sql);
    
   if(mysqli_num_rows($r) > 0){
    	while($row = mysqli_fetch_array($r) )
    	{
    		
    		 $iid = $row['invoice_id'];
    	
    		$orderID = 'COENCE1111111111';
    
    		 //invoice to
    		 $invoice_type  = get_invoice_type($iid);//get invoice type\
    		    //for applicants_nce table
    		  //echo $user_id = get_invoice_to($iid);
    		  //for students_nce table
    		  $user_id = $row['id'];
    		  //exit();
    		  //$user_id.'<br>';
    		 $pay_to = get_invoice_pay_to($iid); //get who to pay the invoce to
    		 $invoice_for = get_invoice_for($iid); //get what the invoice is made for 
    		  $invoice_title = get_invoice_title($iid); // get the invocie title
    		$invoice_desc = get_invoice_desc($iid); //get the invoice desc
    					 //	 $amount = get_invoice_amount($iid); //get the invocie amount
    	 $invoice_amount = get_invoice_total_amount($iid); //get invoice total amount
    			
    //	 $amount = get_invoice_amount($iid); //get the invocie amount
    //	 $total_amount = get_invoice_total_amount($iid); //get invoice total amount
    //	 $credit = get_invoiced_credit($iid); //get invoice credit
    	 
    //	 $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
    
         
    	 //pass invoice to user id
    
    	
    	//$user = get_user_id();//get user
    	//$level = '100'; //get user level
    	//$level = get_user_level($user_id); //get user level
    	$programme_type = get_user_programme_type_id($user_id); //programme type
    	$department = get_user_department_id($user_id); //department
    	$programme  = get_user_programme_id($user_id);//programme
    	$school = get_user_school_id($user_id); //school
    	$spt = get_user_sponsorship_type_1($user_id);	
    	
     
     //get current session 
     $session = get_current_session($id = 1); //get current session
     $session_title = get_session_title($session); //get session title
     
     //get current semester
     $semester = get_current_semester($id = 1); //get current semester
     $semester_title = get_semester_title($semester); //get semester title
     
     
       $remita_share_fix = '300';
       $vat_fix = '15';
       
       $extra_tot_m = $remita_share_fix + $vat_fix;
     
     
     
         $full_name = get_user_fullname($user_id);
    	 $email = get_user_email($user_id);
    	 $phone_no = get_user_phone_no($user_id);
     
     
      
    
     
     
    
    
    	   //now if response code 
    	   //check if the payment is succesful
    	   
    	   if($response_code == '00' || $response_code == '01')
    	   {
    		
    
    						  
    						  //if successful transaction enter the students and generate matric number
    						  
    	
    
    					    
    								 ///check if he / she has paid fee
    								if(has_paid_school_fee($user_id,$session))
    								{
    									
    											echo $msg ="&nbsp;&nbsp;&nbsp;&nbsp;School fee payment for this session has been made already. Please click on payment tab to print receipt";
    											
    									
    								}
    								else
    								{
    									$payment_year = date('Y');//payment year
    									
    						
    									$qqq1 = "INSERT INTO `school_fee_payments_nce`(`id`, `programme_type_id`, `department_id`, `programme_id`, `school_id`,`session`, `semester`, `student_id`,`sponsorship_type`,
    									 `payment_status`,`payment_method`,`payment_mode`, `invoice_id`,
    								   `title`, `description`, `amount`, `date_added`, added_by) 
    									VALUES (NULL,'$programme_type','$department','$programme','$school','$session','$semester','$user_id','$spt','1','3', '2','$iid','$invoice_title','$invoice_desc','$invoice_amount',NOW(),'ghost')";	
                                    /*    print_r($qqq1);
    						     
    	                             exit();*/
    								  $rrr1 = mysqli_query($dbc,$qqq1);
    								 			  	  
    		 		   
    								  if($rrr1)//mysqli_affected_rows($dbc)
    								  {
    									//if done finally collect all info for receipt
    						   
    								  
    										  $q441 = "UPDATE invoices SET payment_status = '1' WHERE invoice_id = '$iid'"; //update invoice to paid where payment status is 1
    										  $r441 = mysqli_query($dbc, $q441);
    										  
    										  if($r441)
    										  {//end of if update is successful
    											
    											//here the display is handled when its 00
    												//here the display is handled when its 00
    												mysqli_query($dbc,"UPDATE sch_fees_pay_sport  SET checked = '1' WHERE invoice_id = '$iid'");
    												
    														//echo '&nbsp;&nbsp;&nbsp;<a class="btn btn-success"  href="create_sf_payment_slip.php?me='.$user_id.'&s='.$session.'&myid='.md5('fuckusha1sha1').'."   data-toggle="tooltip" data-placement="top" title="Download / Print  ">Click here to  DOWNLOAD / PRINT </a>';
    												echo 'Successful <br>';
    
    											 
    											 
    											 //clear some sessions
    											 
    											 
    											 $school_fee_payments_entered = true;
    								
    										  }
    										  else
    										  {
    											 echo $msg =   '&nbsp;&nbsp;SERVER ERROR: Please try again.';
    										//	header("Location: dashboard.php?act=view_invoices4&error=".urlencode($msg)."&qlk=".md5(2)."&auth=".sha1(md5(2))."&iid=".$iid);
    								
    										  }//end of if invoice is updated
    									
    									}
    									else
    									{
    										echo $msg =   '&nbsp;&nbsp;SERVER ERROR: Please try again .(2)';
    											  
    										//header("Location: dashboard.php?act=view_invoices4&error=".urlencode($msg)."&qlk=".md5(2)."&auth=".sha1(md5(2))."&iid=".$iid);
    								
    									}//end of if inserted
    										
    										
    								
    								
    								
    								}//end of if he / she has paid school
    								  
    								  
    								  
    								  
    									  
    								      
    	   ///end of if application is successful and insert into students table  
    	  }//en dof json of response code 
     
     
    			  
    			  
    			 
     
    
    
     }
    }else{
        echo 'Sorry, No more records';
    }
 ?>
 

 
