<?php 
/* **************************************************************************
** This page   collects user id and pass it to invoice generation methods
**
*****************************************************************************/

//here hf is hostel fee
//sf is school fee
//rf is result fee
//echo '<h3 class="text-info">Under Maintenance, be right back soon!</h3>';
//exit();
//collect the  invoice to for school fee
if(isset($_GET['invoice_to']))
{
	
	
	//collect what the payment is for
	$payment_for = $_GET['for'];
	
	//collect pay to
	$invoice_to = $_GET['invoice_to']; 
	
	//get payment mode /// ayment mode is instalment or fullpayment, 0 - default  1- instalment 2 - fullpayment
	$payment_mode = $_GET['pm']; 
	
	//collect what invoice programme type i.e either nce or de or epre nce chek db for more information
	$pt = 2; //for nce

	  
	  
	$user = get_user_id();//get user

	

	//$level = '100'; //get user level
	$programme_type = get_user_programme_type_applied_for($user); //programme type
	$department = get_user_department_applied_for($user); //department
	$school =  get_user_school_applied_for($user);
	$programme  = get_user_programme_applied_for($user);//programme
	
	//get current semester 
	$current_semester = get_current_semester($id = 1);
	
	
	//get current session
	$current_session = get_current_session($id= 1);
	
	
	$spt = get_user_sponsorship_type($user);
	
	//get user mode_of_entry 
	$entry_mode = get_user_mode_of_entry($user);
	

	//now check if the payment is app fee let this action happens
	if($payment_for == 'af')
	{//let tghis happen
	
		//here accept the mode of payment regualr or partime
		$mode = 1;

		//invoice type 
	    $invoice_type = 0;  //1- application fee
		

	
	
	  
	  
	  //collect other params
	  $title = get_application_fee_title($mode, $entry_mode);
	  $description = get_application_fee_desc($mode, $entry_mode);
	  $total_amount = get_application_fee_total_amount($mode, $entry_mode);
	  $amount =  get_application_fee_amount($mode, $entry_mode);
	  $credit = get_invoice_credit($total_amount, $amount); //pass total anmount , amount paid
	  $pay_to = 'NIGER STATE COLLEGE OF EDUCATION. MINNA. <br> ';//MICRO FINANCE BANK ACCT:  
	  
	  
	 
	 

	  
	  //set the invoice for to be zero since we are paying for application fee
	  $invoice_for  = '0';
	  
	  
	  
		//flip the gentleman back if he has not finish application process
		if($total_amount < 2500 || $amount < 2500)
		{
			$msg = "Please make sure you complete your application process before making payment.";
			  //head to same page saying that not successful
			header("Location: dashboard.php?act=application_fee_payments&error=".urlencode($msg)."&qlk=".md5(8)."");

			exit();
		}
	  
	  
	  //insert into the invoice 
	  $q = "INSERT INTO `invoices`(`id`, `invoice_type`,`invoice_to`, `pay_to`,`invoice_for`,`session`,`programme_type`,`title`, `description`, `amount`,`total_amount`, `credit`, `date_added`, `due_date`) 
		   VALUES (NULL, '$invoice_type','$user','$pay_to','$invoice_for','$current_session',$programme_type,'$title','$description','$amount','$total_amount','$credit',NOW(),NOW())";
	  
	  $r = mysqli_query($dbc,$q);
	  
	  
	  if(mysqli_affected_rows($dbc))
	  {
		  
		  //get id of last inserted value
		  $id = mysqli_insert_id($dbc);
		  
		  
		  
		  //generate invoice id and show the invoice 
		  $generated = generate_invoice_id($id, $user, $invoice_type);
		  
		  if($generated =='')
		  {
			  $msg = "Invoice not generated due to system error, we are sorry for the inconvenience.";
			  //head to same page saying that not successful
			  header("Location: dashboard.php?act=application_fee_payments&error=".urlencode($msg)."&qlk=".md5(8)."");

			 
		  }
		  else
		  {
			  $msg = "Invoice generated successfully. Thank you!";
			 
			  //head to invoice page
			  header("Location: dashboard.php?act=view_invoices&is=".urlencode($msg)."&qlk=".md5(8)."&iid=".$generated);

		  }
		  
	  }
	  
	}//end of if for af
	
	
	//now check if the payment is screen fee let this action happens
	if($payment_for == 'scrf')
	{//let tghis happen
	
		//here accept the mode of payment regualr or partime
		$mode = 1;

		//invoice type 
	    $invoice_type = 0;  //1- application fee
		
		//set the invoice for to be 1 since we are paying for screening fee
		$invoice_for  = 1;

	
	  
	  //collect other params
	  $title = get_screening_fee_title($pt, $mode);
	  $description = get_screening_fee_desc($pt, $mode);
	  $total_amount = get_screening_fee_total_amount($pt, $mode);
	  $amount =  get_screening_fee_amount($pt, $mode);
	  $credit = get_invoice_credit($total_amount, $amount); //pass total anmount , amount paid
	  $pay_to = 'NIGER STATE COLLEGE OF EDUCATION. MINNA. <br> ';//MICRO FINANCE BANK ACCT:  
	  
	 

	  
		//flip the gentleman back if he has not finish application process
		if($total_amount < 2500 || $amount < 2500)
		{
			$msg = "Please make sure you complete your application process before making payment.";
			  //head to same page saying that not successful
			header("Location: dashboard.php?act=screening_fee_payments&error=".urlencode($msg)."&qlk=".md5(8)."");

			exit();
		}
	  
	  
	  //insert into the invoice 
	  $q = "INSERT INTO `invoices`(`id`, `invoice_type`,`invoice_to`, `pay_to`,`invoice_for`,`session`,`programme_type`,`title`, `description`, `amount`,`total_amount`, `credit`, `date_added`, `due_date`) 
		   VALUES (NULL, '$invoice_type','$user','$pay_to','$invoice_for','$current_session',$programme_type,'$title','$description','$amount','$total_amount','$credit',NOW(),NOW())";
	  
	  $r = mysqli_query($dbc,$q);
	  
	  
	  if(mysqli_affected_rows($dbc))
	  {
		  
		  //get id of last inserted value
		  $id = mysqli_insert_id($dbc);
		  
		  
		  
		  //generate invoice id and show the invoice 
		  $generated = generate_invoice_id($id, $user, $invoice_type);
		  
		  if($generated =='')
		  {
			  $msg = "Invoice not generated due to system error, we are sorry for the inconvenience.";
			  //head to same page saying that not successful
			  header("Location: dashboard.php?act=screening_fee_payments&error=".urlencode($msg)."&qlk=".md5(8)."");

			 
		  }
		  else
		  {
			  $msg = "Invoice generated successfully. Thank you!";
			 
			  //head to invoice page
			  header("Location: dashboard.php?act=view_invoices2&is=".urlencode($msg)."&qlk=".md5(8)."&iid=".$generated);

		  }
		  
	  }
	  
	}//end of if for af
	
	





	
	//now check if the payment is school fee let this action happens
	if($payment_for == 'sf')
	{//let tghis happen
	
	// ijust implemented current session hoping to follow with other ones
	
	   //here level is 100
	   $level = 'I';
	
	
	  //invoice type 
	  $invoice_type = 1;  // 1 fresh student fee
	  
	  
	  //flip the gentleman back if he has not finish application process
		if($programme_type == '0' || $school == '0' || $programme == '0')
		{
			$msg = "Please make sure you complete your application process before making payment.";
			  //head to same page saying that not successful
			header("Location: dashboard.php?act=school_fee_payments_r&error=".urlencode($msg)."&qlk=".md5(8)."");

			exit();
		}
	  
	  
	  
	  //collect infor for instalment payment
	  $programme_type = get_user_programme_type_applied_for($user); //programme type
	  $school = get_school_id($user); //school
	  $department = get_department_id($user); //department
	  $programme  = get_programme_id($user);//programme
	  
	  
	  //collect if the payment is instalment or full payment 1 - installment, 2 - fullpayment
	  if($payment_mode == 1)
	  {
		  
		  
		  
		  
		 $total_amount = $partly_amount = get_school_fee_partly_amount($level,$programme_type = 2, $school, $programme,$current_session);
		  $amount = get_school_fee_amount($level, $programme_type = 2, $school, $programme,$current_session);
		  $credit = get_invoice_credit($amount,$total_amount); //pass total anmount , amount paid
	  
		  
	  }
	  else
	  {
		  $total_amount = get_school_fee_total_amount($level,$programme_type = 2, $school, $programme,$current_session);
		  $amount = get_school_fee_amount($level, $programme_type = 2, $school, $programme,$current_session);
		  $credit = get_invoice_credit($amount, $total_amount); //pass total anmount , amount paid
	  }

	  //flip the gentleman back if he has not finish application process
		if($total_amount < 27000 || $amount < 27000)
		{
			$msg = "Please Contact the ICT Unit.";
			  //head to same page saying that not successful
			header("Location: dashboard.php?act=payments&error=".urlencode($msg)."&qlk=".md5(8)."");

			exit();
		}
	  //collect other params
	  $title = get_school_fee_title($level, $programme_type = 2, $school, $programme); //
	  $description = get_school_fee_desc($level, $programme_type = 2, $school, $programme);
	  $pay_to = 'NIGER STATE COLLEGE OF EDUCATION. MINNA <br> '; // 
	  
	  //set the invoice for to be 2 since we are paying for school fee
	  $invoice_for  = '2';
	  
	  
	  //total amount stands for amount to be paid
	  
	  //insert into the invoice 
	  $q = "INSERT INTO `invoices`(`id`, `invoice_type`,`invoice_to`, `pay_to`,`invoice_for`,`payment_mode`,`session`,`programme_type`,`title`, `description`, `amount`,`total_amount`, `credit`, `date_added`, `due_date`) 
		   VALUES (NULL, '$invoice_type','$user','$pay_to','$invoice_for','$payment_mode', '$current_session','$pt','$title','$description','$amount','$total_amount','$credit',NOW(),NOW())";
	  
	  $r = mysqli_query($dbc,$q);
	  
	  
	  if(mysqli_affected_rows($dbc))
	  {
		  
		  //get id of last inserted value
		  $id = mysqli_insert_id($dbc);
		  
		  
		  
		  //generate invoice id and show the invoice 
		  $generated = generate_invoice_id($id, $user, $invoice_type);
		  
		  if($generated =='')
		  {
			  $msg = "Invoice not generated due to system error, we are sorry for the inconvenience.";
			  //head to same page saying that not successful
			  //header("Location: dashboard.php?act=school_fee_payments&error=".urlencode($msg)."&qlk=".md5(8)."");
			  header("Location: dashboard.php?act=school_fee_payments_r&error=".urlencode($msg)."&qlk=".md5(8)."");


			 
		  }
		  else
		  {
			  $msg = "Invoice generated successfully. Thank you!";
			 
			  //head to invoice page
			  header("Location: dashboard.php?act=view_invoices3&is=".urlencode($msg)."&qlk=".md5(8)."&iid=".$generated);

		  }
		  
	  }
	  
	}//end of if for cf


}//end of if invoice_to has value



?>