<?php require_once("includes/initialize.php");

global $database;
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
  $id = $session->user_id;
$user_data = Userdata::find_by_id($session->user_id);

echo $mode= $user_data->formno;
echo $user_data->email;

if ((substr($user_data->formno,2,2)!=18) and ((substr($user_data->formno,2,2)!=19))){
      
      //echo "All new table: ".substr($user_data->formno,2,2);
      
      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);
}else{
    
        $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
}
//echo "Form: ".$biodata->courses."<br>";





if (isset($biodata->courses) or (isset($biodata->level))){
    
    $dept='';
   $dept = $biodata->courses;
   if ($dept==''){
    echo "Form222: ".  $dept = $biodata->department;
   }
    echo "Level: ". $level=$biodata->level;
     
     
}else {
     $dept = $biodata->courses=$biodata->department;
}

 





$dp = DEPARTMENTCLASS::department_authenticate($dept);
//$display_course = COURSECLASS::fetch_all_course($biodata->courses, $clevel, $semester_fin);

$faculty= $dp->faculty;




if ($dept=='Microbiology (Option)'){
    //   $biodata->courses= $dept='(Microbiology (Option)'; 
      
    //   Schoolfees::updateDepartment($biodata->formno, $dept);
       
    }else if ($dept=='Electronic/Telecommunication (Option)'){
       $biodata->courses= $dept = 'Electronics/Telecommunication (Option)';
       
        Schoolfees::updateDepartment($biodata->formno, $dept);
       
    }
    
    
   






$transact_feedbackchk = Transaction::findacceptance($user_data->formno, $user_data->email);
if (!$transact_feedbackchk){
    
    redirect_to ("http://eportal.oscotechesaoke.edu.ng/student_portal/dashboard.php");
 }



//$mode= $user_data->mode;
//$user_data->std_process_id;
//$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
$start_session = $user_data->start_session;
$current_session = SESSIONCLASS::retrieve_session_payment();

//echo "<br>CURRENT SESSION: ".$current_session->session;

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

if ((substr($user_data->formno,2,2)!=18)){




 $new_dp = DEPARTMENTCLASS::new_admin_dept($user_data->formno);
  $biodata->courses = $new_dp->department;
 
 
 
  $level = $biodata->level; 
  
  $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $pix = "../application_account/passport/".$pixf;
    
    $alldp = DEPARTMENTCLASS::find_all($dp->faculty);

//$current_session = SESSIONCLASS::session_authenticate();

//print_r($alldp);

$total_sc_fees = 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';
}

   
}else {

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


$total_sc_fees = 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 (($current_session != $start_session) && ($user_data->std_process_id==3) && ($user_data->status=='open'))
{
    $current_session->session=$start_session;
    $level = $biodata->programme;
}

}


  $dept = $biodata->courses;
  
  if ($dept=='Microbiology (Option)'){
    //   $biodata->courses= $dept='(Microbiology (Option)'; 
       
    }else if ($dept=='Electronic/Telecommunication (Option)'){
       $biodata->courses= $dept = 'Electronics/Telecommunication (Option)';
    }


 //$semester = $session_class->semester;

if ($level=='Adv. Cert.'){
    
}else if (($level=='ND I') && ($user_data->std_process_id ==6)){
  $level = 'ND II';
  
}else if (($level=='HND I') && ($user_data->std_process_id ==6)){
  $level = 'HND II';
}else if (($level=='HND I') || ($level=='ND I') && ($user_id->std_process_id ==3)){
    $level = $biodata->programme;
} 
// 
/*The beginning of if statement for late payment of ND 2 and HND 2*/
 else if (($level=='ND I') OR ($level=='HND I') && (($user_data->std_process_id ==3) && ($user_data->session=='2018/2019'))){
     $level = $level;

    
    
 }
 /* Please the platform for late payment of ND 2 and HND 2 ends here*/

else {
    $level = $level . " " . "I";
}





// To find regular/daily PT
 $adm_type = (substr($user_data->formno, 0, 2));
if (($adm_type == 'PU') or ($adm_type == 'FT')) 
{
    $adm_type = 'regular';
    
    } elseif (($adm_type == 'PT') OR ($adm_type == 'DP')) {
    
    $adm_type = 'dailypt';
    }
        
      // echo  $level." ". $pay_cat."". $adm_type;
     
        if ((substr($user_data->formno,2,2)>=22) AND (($level=='ND I') OR ($level=='HND I'))){
                  $status=0;
                    $total = Pay_settings::find_sch_fees(3, $level, $pay_cat, $adm_type);
        }else if (($level=='ND I') OR ($level=='HND I')) {
                   $status=1;
                    $total = Pay_settings::find_sch_fees(3, $level, $pay_cat, $adm_type);
        }else{
             $status=0;
                    $total = Pay_settings::find_sch_fees(3, $level, $pay_cat, $adm_type);
        }
     
     
     
     
     
     
     
     
        
//$total = Pay_settings::find_sch_fees(3, $level, $pay_cat, $adm_type);
foreach ($total as $samount) {
    $total_sc_fees += $samount->amount;
}
//echo "too ".$total_sc_fees;
//$current_session = SESSIONCLASS::session_authenticate();
$a = (substr(rand(1, 10000000000), 0, 10));
$txt_ref = $a . (substr($biodata->surname, 0, 2));

if ((($level=='ND II') or ($level=='HND II')) and ($user_data->std_process_id==6) and (substr($user_data->formno,2,2)==20)){
    
    $current_session->session = substr($user_data->formno,2,2). (substr($user_data->formno,2,2)+1) .'/'. substr($user_data->formno,2,2). (substr($user_data->formno,2,2)+2);
    
    
}else if ((($level=='ND II') or ($level=='HND II')) and ($user_data->std_process_id==6) and (substr($user_data->formno,2,2)!=20)){
    
    switch (substr($user_data->formno,2,2))
    {
       case 19: $valtoadd=1;
        break;
        
       case 18: $valtoadd=2;
       break;
    }
    
    
    $current_session->session = (substr($user_data->formno,2,2)+$valtoadd). (substr($user_data->formno,2,2)+1) .'/'. (substr($user_data->formno,2,2)+$valtoadd). (substr($user_data->formno,2,2)+2);

}

//if ($current_session->session==$start_session){
  $current_session->session=$user_data->start_session;
//}

// echo $start_session;
// echo $current_session->session;
// echo "<br><br><br>".$id."  ". $current_session->session."   ". $total_sc_fees."   ". $txt_ref."  ".$adm_type."  ".$level;



$transact = Schoolfees::t_processing($id, $current_session->session, $total_sc_fees, $txt_ref, $adm_type,$level);
//$transaction = Schoolfees::authenticate($biodata->formno, $biodata->email);
$transaction = Schoolfees::find_by_txnref($txt_ref);

$fno = $transaction->formno;
$txn_ref = $transaction->txt_ref;
$level = $transaction->level;
$full_name = $transaction->surname . ' ' . $transaction->othernames;
$surname = $transaction->surname;
$othername = $transaction->othernames;
$amount = $transaction->amount;
$courses = $transaction->department;
$email = $transaction->email;
$phone = $transaction->phone;

$level0 = $transaction->programme;
$session = $transaction->session;
$transfee = $transaction->txt_fees;
$overhead = $transaction->overhead;

$_SESSION['formno'] = $fno;
$_SESSION['txn_ref'] = $txn_ref;
$_SESSION['wayout'] = "School Fee";
$charges = $transfee + $overhead;
$amount_new = $amount - $charges;


$fname = $transaction->surname . ' ' . $transaction->othernames;

//@todo: fix //into webpaylog
//$sqlw = $database->query("INSERT INTO webpaylog (txnref,datetime,debittedamount,responsedesc,responsecode,custname,custid) 
//						VALUES('$txn_ref',NOW(),'$amount','null','null','$fname','$fno')") or die(mysql_error());


//header("location: dashboard.php?type=invoicedetails2");











//$transaction = Transaction::find_by_txnref($txt_ref);

/*if ($transaction->responsecode == 'Approved Successful') {

    if ($user_data->std_process_id < 4) {
        $level = $user_data->std_process_id + 1;
        $update_process = Userdata::update($id, $level);
    }
    echo $user_data->std_process_id;


    $session->message("{$transaction->responsecode}");


    redirect_to("http://localhost/student_portal/dashboard.php?type=schoolfees");

} else {
    //echo $transaction->responsecode." Testing page";
    $session->message("{$transaction->responsecode}");

    redirect_to("http://localhost/student_portal/dashboard.php?type=schoolfees");

}
*/
?>

// <?php //require_once("includes/initialize.php");
// //$par = array();
// echo $id = $session->user_id;
// $user_data = Userdata::find_by_id($session->user_id);


// if (substr($user_data->formno,2,2)==20){
      
//       $biodata = Biodata2::find_by_id($user_data->formno, $user_data->email);
// }else{

// $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
// }

// $current_session = SESSIONCLASS::session_authenticate();

// if ((!$current_session->session)&&(($biodata->level=='ND') OR ($biodata->level=='HND'))) {
//   $current_session= $user_data->start_session;   
// }
// $a = (substr(rand(1, 10000000000), 0, 10));
// $txt_ref = $a . (substr($biodata->surname, 0, 2));

// //@todo:fix //inserts into the acceptance_tranz table from here
// $transact = Transaction::t_processing($id, $current_session->session, $txt_ref);
// $transaction = Transaction::find_by_txnref($txt_ref);


// $fno = $transaction->formno;
// $txn_ref = $transaction->txt_ref;
// $level = $transaction->level;
// $full_name = $transaction->surname . ' ' . $transaction->othernames;
// $surname = $transaction->surname;
// $othername = $transaction->othernames;
// $amount = $transaction->amount;
// $courses = $transaction->department;
// $email = $transaction->email;
// $phone = $transaction->phone;

// $level0 = $transaction->programme;
// $session = $transaction->session;
// $transfee = $transaction->txt_fees;
// $overhead = $transaction->overhead;
// $_SESSION['txn_ref'] = $txn_ref;
// $_SESSION['wayout'] = "School Fee";
// $charges = $transfee + $overhead;
// $amount_new = $amount - $charges;


// $fname = $transaction->surname . ' ' . $transaction->othernames;

// //@todo: fix //into webpaylog
// $sqlw = mysql_query("INSERT INTO webpaylog (txnref,datetime,debittedamount,responsedesc,responsecode,custname,custid) 
// 						VALUES('$txn_ref',NOW(),'$amount','null','null','$fname','$fno')") or die(mysql_error());


header("location: dashboard.php?type=invoicedetails2");

?>

























	