<?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
//hf is hostel fee

include_once '../db_connect/db.php';
//get current session

//collect the  invoice to for hostel fee
if(isset($_GET['invoice_to']))
{
	//collect what the payment is for
	$payment_for = $_GET['for'];

	//collect pay to
	$invoice_to = $_GET['invoice_to'];

	//invoice type
	$invoice_type = 5;  //2-returning student


	$user = get_user_id();//get user
	$level = get_user_level($user);

	//get current semester
	//$current_semester = get_current_semester($id = 1);

	$current_semester = '0';

	//get current session
	//$current_session = get_current_session($id = 1);
	$current_session = 6;

	//now check if the payment is school fee let this action happens
	if($payment_for == 'hfff')
	{//let tghis happen

	  //collect other params
	  $hostel_cat = get_hostel_category($conn_1, $user);
	  $room_id = get_hostel_room($conn_1, $user);
	  $site_id = get_hostel_site($conn_1, $user);


	 // $hostel_room_title = get_hostel_room_title($conn_1, $room_id);
	 // $hostel_cat_title = get_hostel_cat_title($conn_1, $hostel_cat);
	 // $hostel_site_title = get_hostel_site_title($conn_1, $site_id);
	  $total_amount = '1323';
	  $desc = "Hostel Application Fee";


	  $credit = get_invoice_credit($total_amount, $total_amount); //pass total anmount , amount paid
	  $pay_to = 'Niger State College of Education Minna <br> ';
	  $title = "Hostel Application Fee";

	  //set the invoice for to be 2 since we are paying for return school fee
	  $invoice_for  = '55';
	  $payment_mode = '1';
    	$invoice_type = '55';

	  //insert into the invoice
	  $q = "INSERT INTO `invoices`(`id`, `invoice_type`,`invoice_to`, `pay_to`,`invoice_for`,`session`,`title`, `description`, `amount`,`total_amount`, `credit`, `date_added`, `due_date`)
		   VALUES (NULL, '$invoice_type','$user','$pay_to','$invoice_for','$current_session','$title', '$desc','$total_amount','$total_amount','$credit',NOW(),NOW())";

	  $r = mysqli_query($conn_1,$q) or die(mysqli_error($conn_1));

	  if(mysqli_affected_rows($conn_1))
	  {
		  //get id of last inserted value
		  $id = mysqli_insert_id($conn_1);


		  //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?sms=hostel_app_payment&error=".urlencode($msg)."&qlk=".md5(2)."");


		  }
		  else
		  {
			  $msg = "Invoice generated successfully. Thank you!";

			  //head to invoice page
			  header("Location: dashboard.php?sms=view_invoice2&is=".urlencode($msg)."&qlk=".md5(2)."&iid=".$generated);

		  }

	  }

	} else {
		header("Location: dashboard?sms=hostel_app_payment");
	}


//hostel fee payment
	if($payment_for == 'hf')
	{//let tghis happen

	  //collect other params
	  $hostel_cat = get_hostel_category($conn_1, $user);
	  $room_id = get_hostel_room($conn_1, $user);
	  $site_id = get_hostel_site($conn_1, $user);


	  $hostel_room_title = get_hostel_room_title($conn_1, $room_id);
	  $hostel_cat_title = get_hostel_cat_title($conn_1, $hostel_cat);
	  $hostel_site_title = get_hostel_site_title($conn_1, $site_id);
	  $hostel_cat_amount = get_hostel_cat_amount($conn_1, $hostel_cat);
	  $desc = $hostel_site_title .' - '. $hostel_cat_title .' - '. $hostel_room_title;


	  $credit = get_invoice_credit($hostel_cat_amount, $hostel_cat_amount); //pass total anmount , amount paid
	  $pay_to = 'Niger State College of Education Minna <br> ';
	  $title = "Hostel Fee";

	  //set the invoice for to be 2 since we are paying for return school fee
	  $invoice_for  = '5';
	  $payment_mode = '1';

	  //insert into the invoice
	  $q = "INSERT INTO `invoices`(`id`, `invoice_type`,`invoice_to`, `pay_to`,`invoice_for`,`session`,`title`, `description`, `amount`,`total_amount`, `credit`, `date_added`, `due_date`)
		   VALUES (NULL, '$invoice_type','$user','$pay_to','$invoice_for','$current_session','$title', '$desc','$hostel_cat_amount','$hostel_cat_amount','$credit',NOW(),NOW())";

	  $r = mysqli_query($conn_1,$q) or die(mysqli_error($conn_1));

	  if(mysqli_affected_rows($conn_1))
	  {
		  //get id of last inserted value
		  $id = mysqli_insert_id($conn_1);


		  //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?sms=hostel_fee_payment&error=".urlencode($msg)."&qlk=".md5(2)."");


		  }
		  else
		  {
			  $msg = "Invoice generated successfully. Thank you!";

			  //head to invoice page
			  header("Location: dashboard.php?sms=view_invoice&is=".urlencode($msg)."&qlk=".md5(2)."&iid=".$generated);

		  }

	  }

	} else {
		header("Location: dashboard?sms=hostel_fee_payment");
	}






} else {
	header("Location: dashboard?sms=hostel_fee_payment");
}

?>
