<?php 
session_start();
/** 

** author:  aslapai 
**
** date created: 26/06/2015
**
** this page serves ajax call to update password sent from function.js
*/


//include the headers resources
require_once('includes/functions.php');


//collect the user id
 $user_id = get_user_id();

//collec the password data
$pin = $_POST['pin'];
 $sn = $_POST['password'];
 
 
 
 $sid = get_current_session($sid = '1');

//check if he / she has paid fee
if(has_paid_school_fee($user_id, $pin, $sn, $sid))
{
	echo  '<div class="notification alert alert-danger in">
					   <button class="close" data-dismiss="alert" type="button">X</button>				
					   <i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;School fee payment for this session has been made already .
					</div>';
	
}
else
{



	
	$q1 = "SELECT * FROM pin  WHERE (pin = '$pin' AND serial_number = '$sn') AND card_type = '2'";
	$r1 = mysqli_query($dbc, $q1);
	
	if(mysqli_num_rows($r1)>0)
	{
	
	  $_SESSION = mysqli_fetch_array($r1, MYSQLI_ASSOC);
	  
	  $id = $_SESSION['id'];
	  
	  $pin = $_SESSION['pin'];
	  
	  $serial_no = $_SESSION['serial_number'];
	  
	  
	  
	  
		  //query
		  $q = "UPDATE pin SET status = 0, date_used = NOW() WHERE id = '$id'";
		  $r = mysqli_query($dbc, $q);
		  
		  if(mysqli_affected_rows($dbc))
		  {//end of if update is successful
		  
		  
						  $aa=date('Y');
	  
						  $ab=date('Y'+1);
	  
						  $cc=$aa.'/'.$ab;
						  
						  //payment status set 1 is paid
						  
						  
		  
						  $qry42="INSERT INTO `school_fee_payment` (`student_id`, `card_pin`, `card_serialno`, `payment_year`, `session`, `payment_status`, `date_entered`) 
								  VALUES ('$user_id', '$pin', '$sn', NOW(), '$sid', '1', NOW())";
						  $rst42 =  mysqli_query($dbc, $qry42);
						  
						  if(mysqli_affected_rows($dbc))
						  {//end of if update is successful
						  
							  echo  '<div class="notification alert alert-success in">
									   <button class="close" data-dismiss="alert" type="button">X</button>				
									  <i class="glyphicon glyphicon-ok"></i>&nbsp;&nbsp;Payment has been successfully made, please click on payment history to access receipt.
									</div>';
						  
						  
						  }
						  else
						  {
							  echo  '<div class="notification alert alert-danger in">
									   <button class="close" data-dismiss="alert" type="button">X</button>				
									  <i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;SERVER ERROR: Please try again 1.
									</div>';
						  }
						  
			  
			  
			  
			  
		  }
		  else
		  {
			  echo  '<div class="notification alert alert-danger in">
						 <button class="close" data-dismiss="alert" type="button">X</button>				
						 <i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;SERVER ERROR: Please try again 2.
					  </div>';
		  }
  

	}
	else
	{//else of if pin is not valid
		echo  '<div class="notification alert alert-danger in">
					   <button class="close" data-dismiss="alert" type="button">X</button>				
					   <i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;ERROR: Please try again with valid credentials .
					</div>';
	}


}
?>