<?php require_once("includes/initialize.php");
require_once("phpqrcode/qrlib.php");
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
$user_data = Userdata::find_by_id($session->user_id);


if ((substr($user_data->formno,2,2)!=18) and ((substr($user_data->formno,2,2)!=19))){
      
       Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
      }
      
       $biodata = Biodata2::find_by_id($table, $user_data->formno, $user_data->email);
       
      
        $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
          if (!isset($new_dp->department)){
           $biodata->courses ='Nil';   
          }
          
          else if ($new_dp->department=='Microbiology (Option)'){
      //$biodata->courses= $dept='(Microbiology (Option)'; 
      
     // Schoolfees::updateDepartment($biodata->formno, $dept);
       
    }else if ($new_dp->department=='Electronic/Telecommunication (Option)'){
       $biodata->courses= $dept = 'Electronics/Telecommunication (Option)';
       
        Schoolfees::updateDepartment($biodata->formno, $dept);
       
    }else{
        $biodata->courses = $new_dp->department;
          }

       $biodata->programme = $biodata->level;
  
}else if ((substr($user_data->formno,2,2)==18) OR (substr($user_data->formno,2,2)==19)){


$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
}
 $id=$session->user_id;
 $payId = $_GET['id'];
  
  $year1 = Schoolfees::confirmschoolfees($user_data->formno, $user_data->email);
  
  $year2 = Schoolfees::confirmschoolfees2($user_data->formno, $user_data->email);
  
  
//$transact_feedback = Transaction::authenticate($biodata->formno, $biodata->email);
//$session->user_id;
//$transact_feedbackchk = Transaction::findacceptance($user_data->formno, $user_data->email);
//$transact_feedback = Schoolfees::findschoolfees($user_data->formno, $user_data->email);

$transact_details = Schoolfees::find_id($payId);
//if (!$transact_feedbackchk){
 // redirect_to ("http://eportal.oscotechesaoke.edu.ng/student_portal/dashboard.php");
  
//}else
if (!$transact_details){
redirect_to ("http://eportal.oscotechesaoke.edu.ng/student_portal/dashboard.php");
}else { 
    //$transact_feedback1 = Schoolfees::authenticate($user_data->formno, $user_data->email);
    
    // $transact_feedback1 = Schoolfees::find_id($payId);






if ((substr($user_data->formno,2,2)=='19') OR (substr($user_data->formno,2,2)=='20') ){

 $new_dp = DEPARTMENTCLASS::new_admin_dept($user_data->formno);
  $biodata->courses = $new_dp->department;
  $biodata->programme = $biodata->level; 
  
  $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $pix = "../application_account/passport/".$pixf;
   
}else {



$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
//$display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);
$alldp = DEPARTMENTCLASS::find_all($dp->faculty);
$pix = "../Oscotech-Wallet/passport/".$pixf;

}
  $dept = $transact_details->department;
  $level = $transact_details->level;
 // $semester = $session_class->semester;

















//$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);

$alldp = DEPARTMENTCLASS::find_all($dp->faculty);


$totamount = 0;
foreach ($alldp as $aldp) {
    $faculty = $aldp->faculty;
}
$pay_cat = '';
if ($faculty == 'Engineering' or $faculty=='Pure and applied Sciences' or $faculty == 'Applied Sciences' or $faculty == 'Environmental Studies'or $faculty=='Information Communication and Technology') {
    $pay_cat = 'science';
} elseif ($faculty == 'Business and Management Sciences') {
    $pay_cat = 'nonscience';
}
// $level = $biodata->programme;
// if ($transact_feedback1->programme=='ND' or $transact_feedback1->programme=='HND'){
//     $level = $transact_feedback1->programme;
// }else if ($transact_feedback1->programme=='ND I' or $transact_feedback1->programme=='HND I'){
//     $level =$transact_feedback1->programme;
// }
//echo $level;
  $level =$transact_details->level;
  $adm_type = (substr($user_data->formno, 0, 2));
if (($adm_type == 'PU') or ($adm_type == 'FT')) {
            $adm_type = 'regular';

        } elseif ($adm_type == 'DP') {

            $adm_type = 'dailypt';
        }
$count = 0;




  if (($level=='ND') or ($level=='HND')){
       $level=$level." ".'I';
  }
  
 // elseif (($level=='ND I') or ($level=='HND I')){
   
//   $level=$level."I";
//   }
// //echo "LEVEL: ".$level;
 $payLevel = $level;
//echo $payLevel. $pay_cat. $adm_type;
$total = Pay_settings::find_sch_fees(3, $payLevel, $pay_cat, $adm_type);


//echo $pay_settings->amount;


//print_r($total);
foreach ($total as $fees) {
    $totamount = $totamount + $fees->amount;
}
date_default_timezone_set('Africa/Lagos');
// echo $transact_feedback1->amount."khkhk";
// echo $totamount.'00';
// echo $transact_feedback1->amount;
// echo $totamount.'.00';

//echo $user_data->formno ." ". $user_data->email."".$transact_feedback1->responsecode;
//if ($totamount.'.00' == $transact_feedback1->amount && $transact_feedback1->responsecode=='00'){
    //echo $transact_feedback->txt_ref;
    
    if ($transact_details->responsecode=='00') {

$output = $transact_details->txt_ref.'- '.$transact_details->formno.'  '.$transact_details->surname .' '.$transact_details->othernames.' '.$transact_details->level.' '.$transact_details->amount.' School Fees: PAID';

// QRcode::png($output,$transact_feedback1->formno.'.png');
$handle = fopen('qrFolder/'.$transact_details->formno.".png", "w");

QRcode::png($output,$handle);


     

//A4 width: 219mm
//default margin : 10mm each side
//writable horizontal : 219 - (10*2)=189mm
if ($user_data->std_process_id == 3 && $year1->responsecode=='00'){
 
 		 $user_level = $user_data->std_process_id + 1; 
 	 
 	  //  if ($user_level > 4){
 	  //      redirect_to ("http://eportal.oscotechesaoke.edu.ng/student_portal/dashboard.php"); // This is to be removed later when second semester registration is given access
 	  //  }else {
 	  
 	  
		 Userdata::update($id, $user_level);
		 
		 
 	  //  }
}else if ($user_data->std_process_id == 6 && $year2->responsecode=='00'){
    
     $user_level = $user_data->std_process_id + 1; 
 	 
 	  //  if ($user_level > 4){
 	  //      redirect_to ("http://eportal.oscotechesaoke.edu.ng/student_portal/dashboard.php"); // This is to be removed later when second semester registration is given access
 	  //  }else {
 	  
 	  
		 Userdata::update($id, $user_level);
}
//echo $id." ". $user_level;
class PDF extends FPDF
{

    function Header()
    {
    global $user_data;
    global $transact_details;
        $this->SetFont('Arial', 'B', 16);

        $this->Cell(12);
        $this->Image('images/icon.png', 10, 13, 22, 22);
        $this->Cell(189, 5, 'OSUN STATE COLLEGE OF TECHNOLOGY, ESA OKE.', 0, 1, 'C');
        $this->Ln(1);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(189, 5, 'P.M.B 1011, ESA-OKE, OSUN STATE.', 0, 1, 'C');
        $this->Ln(19);
//$this->Cell(189,1,'',0,1);
//$this->Cell(189,5,'',0,1);

        $this->SetTextColor(255, 255, 255);
        $this->SetFillColor(0, 0, 0);
        $this->SetFont('Courier', 'B', 14);

        $this->Cell(189, 5, 'School Fee Payment Receipt', 1, 1, 'C', true);


        $this->Cell(189, 5, "", 0, 1);

    }

    function Footer()
    {
        $this->Cell(189, 30, '', 0, 1);//end of line
        $this->Cell(189, 5, '', 0, 1);//end of line
        $this->Cell(189, 5, '', 0, 1);//end of line
        $this->Cell(189, 30, '', 0, 1);//end of line
        $this->SetTextColor(255, 255, 255);
        $this->SetFillColor(0, 0, 0);
        $this->SetFont('Courier', 'B', 7);
        $this->Cell(189, 5, 'Directorate of ICLSTS, ESA OKE. ' . date('Y', time()) . ' All Rights Reserved.', 1, 1, 'C', true);

    }

    function Footer2()
    {
        $this->SetFont('Arial', '', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 1, 'C');
    }

}


$pdf = new PDF('p', 'mm', 'A4');

$pdf->AddPage();
$pdf->SetTitle('School Fee Receipt','UTF-8');

$pdf->Image('images/img2.png', 60, 55, 100, 100);
$pdf->Cell(189, 5, '', 0, 1);

$pdf->SetFont('Arial', '', 12);

if (isset($user_data->matricno) && ($user_data->matricno!=0)){
    $pdf->Cell(30, 5, 'Matric No: ', 0, 0);
$pdf->Cell(40, 5, strtoupper($user_data->matricno), 0, 0);

}else {
 
 $pdf->Cell(30, 5, 'Form no: ', 0, 0);
$pdf->Cell(40, 5, strtoupper($transact_details->formno), 0, 0);
   
}

$pdf->Cell(50, 5, '', 0, 0);//end of line

//$pdf->Cell(80 ,5,'',0,0);
$pdf->Cell(20, 5, 'Date: ', 0);
//date("jS \of F, Y", strtotime($ftranzact->verified_date));
$pdf->Cell(49, 5, date('d/m/Y h:i a',strtotime($transact_details->datetime)), 0, 1);//end of line


$pdf->Cell(30, 5, 'Department: ', 0, 0);
$pdf->Cell(159, 5, strtoupper($transact_details->department), 0, 1);//end of line

//$pdf->Cell(50, 5, '', 0, 0);//end of line


//$pdf->Cell(120, 5,'',0,0);
//$pdf->Cell(20, 5, 'Level: ', 0);
//$pdf->Cell(49, 5, strtoupper($level), 0, 1);//end of line

$pdf->Cell(30, 5, 'Phone: ', 0, 0);
$pdf->Cell(90, 5, $transact_details->phone, 0, 0);
$pdf->Cell(20, 5, 'Level: ', 0);
$pdf->Cell(49, 5, strtoupper($payLevel), 0, 1);//end


//make a dummy empty cell as a vertical spacer
$pdf->Cell(189, 5, '', 0, 1);//end of line

$pdf->SetFont('Arial', 'B', 12);

$pdf->Cell(189, 5, 'FULLNAME: ' . strtoupper($transact_details->surname . ' ' . $transact_details->othernames), 0, 1, 'C');
$pdf->Cell(189, 3, '', 0, 1);


$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 5, '', 0, 0);

$pdf->Cell(10, 5, 'S/N', 1, 0, 'C');
$pdf->Cell(90, 5, 'Description', 1, 0, 'C');
$pdf->Cell(25, 5, '', 1, 0);
$pdf->Cell(34, 5, 'Amount', 1, 0, 'C');//end of line
$pdf->Cell(20, 5, '', 0, 1);

$pdf->SetFont('Arial', '', 12);

//Numbers are right-aligned so we give 'R' after new line parameter
$count = 0;

foreach ($total as $fees) {
    //$totamount=$totamount + $fees->amount;
    //}
    $pdf->Cell(10, 5, '', 0, 0);

    $pdf->Cell(10, 5, $count += 1, 1, 0);

    $pdf->Cell(90, 5, $fees->fees_description, 1, 0);
    $pdf->Cell(25, 5, '', 1, 0);
    $pdf->Cell(34, 5, $fees->amount, 1, 0, 'R');
    $pdf->Cell(20, 5, '', 0, 1);
}
//end of line

/*$pdf->Cell(20 ,5,'',0,0);
$pdf->Cell(90 ,5,'Additional charges',1,0);
$pdf->Cell(25  ,5,'',1,0);
$pdf->Cell(34 ,5,'0.00',1,1,'R');
$pdf->Cell(20 ,5,'',0,0);*/

//end of line
/*$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi*/

$pdf->Cell(120, 5, '', 0, 0);
$pdf->Cell(25, 5, 'Total Due', 1, 0);
$pdf->Cell(4, 5, 'N', 1, 0);
$pdf->Cell(20, 5, $transact_details->amount, 1, 1, 'R');//end of line
//$pdf->Cell(20, 5, $transact_feedback1->amount $totamount, 1, 1, 'R');//end of line

$pdf->ln(10);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(20,10,'* Note: ',0,0,'L');
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(169,10,'This E-Receipt MUST be changed to the official Green Receipt at the Bursary within', 0,1,'L');
$pdf->Cell(169,10,'two (2) working days from the day of payment.', 0,1,'L');
$pdf->Ln(6);
/*$pay_settings->amount+$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi*/

// $pdf->image($transact_feedback1->formno.'.png',185,15,25,25,'png');


$pdf->image('qrFolder/'.$user_data->formno.'.png',87,200,50,50,'png');
// $pdf->image($biodata->formno.'.png',95,155,30,30,'png');


$pdf->Output('School fee.pdf','I');
}
} 
?>