


<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    include('../../../config/config.php');
       $UserID = $_POST['UserID'];

      $UserType = $_POST['UserType'];
      $studentID = $_POST['studentID'];

      



    $select_studnent = mysqli_query($link,"SELECT * FROM `student` INNER JOIN classordepartmentstudentallocation ON `student`.`StudentID` = `classordepartmentstudentallocation`.StudentID
     WHERE `student`.`StudentID`='$studentID'");

    $select_studnent_cnt  = mysqli_fetch_assoc($select_studnent);
    $select_studnent_cnt_row = mysqli_num_rows($select_studnent);

    $amountoptional_charge = 0;
    $amountcomp_charge = 0;

    $amounttransaction_amount = 0;


    if($select_studnent_cnt_row  > 0)
    {

                do{


                        $StudentID =  $select_studnent_cnt['StudentID'];
                        $StudentFirstName =  $select_studnent_cnt['StudentFirstName'];
                        $StudentLastName =  $select_studnent_cnt['StudentLastName'];

                        $Session =  $select_studnent_cnt['Session'];
                        $ClassOrDepartmentID=  $select_studnent_cnt['ClassOrDepartmentID'];
                        $CampusID =  $select_studnent_cnt['CampusID'];


                        $selectcompfee = mysqli_query($link,"SELECT SUM(Amount) AS Amount FROM
                         `chargestructure` WHERE ClassOrDepartmentID='$ClassOrDepartmentID' AND `Session`='$Session'  AND CampusID='$CampusID' AND Status='1'");

                       
                        
                          $selectcompfee_cnt_row = mysqli_fetch_assoc($selectcompfee);
                          $selectcompfee_cnt = mysqli_num_rows($selectcompfee);



                            $compamount = $selectcompfee_cnt_row['Amount'];
                          

                           
                            $amountcomp_charge += $compamount;

                          $selectoptfee = mysqli_query($link,"SELECT SUM(Amount) AS Amountoptional FROM `chargestructure`
                           INNER JOIN `assignoptionalfees` ON `chargestructure`.`SubcategoryID`
                            = `assignoptionalfees`.`SubcategoryID` WHERE 
                            `chargestructure`.`Status`='0' AND `assignoptionalfees`.`StudentID`='$StudentID'
                           AND `assignoptionalfees`.`Status`='0' AND `chargestructure`.`ClassOrDepartmentID`='$ClassOrDepartmentID' 
                           AND `chargestructure`.`Session`='$Session' AND `chargestructure`.`CampusID`='$CampusID'");



                              $selectoptfee_cnt_row = mysqli_fetch_assoc($selectoptfee);
                              $selectoptfee_cnt = mysqli_num_rows($selectoptfee);



                            $compamount_optional =     $selectoptfee_cnt_row ['Amountoptional'];
                            $amountoptional_charge +=$compamount_optional;


                            $prosp_loadtransaction = mysqli_query($link,"SELECT SUM(TransactionIn) AS TransactionInamount FROM `transactions` WHERE CampusID='$CampusID' AND  StudentID='$StudentID' AND `Session`='$Session' AND ClassOrDepartmentID='$ClassOrDepartmentID'");
                            $prosp_loadtransaction_cnt = mysqli_num_rows($prosp_loadtransaction);
                            $prosp_loadtransaction_row = mysqli_fetch_assoc($prosp_loadtransaction);


                            $TransactionInamount =   $prosp_loadtransaction_row ['TransactionInamount'];

                            $amounttransaction_amount +=$TransactionInamount;
                      

                }while($select_studnent_cnt  = mysqli_fetch_assoc($select_studnent));

                   


    }else
    {

    }



  

   $prostotalcharge_optional =  intval($amountoptional_charge);
   $prostotalcharge_comp =  intval($amountcomp_charge);
   $prostransactionamount =   intval($amounttransaction_amount);

   if($prostransactionamount >= $prostotalcharge_optional+ $prostotalcharge_optional)
   {
        $balance_amount =  0;
   }else
   {


      $balance_amount =  intval($prostotalcharge_optional+ $prostotalcharge_optional -  $prostransactionamount);
   }

  



    echo '<input type="hidden" id="prosloaamountforeach_stud_balance" value="'.$balance_amount.'">
    <input type="hidden" id="prosloaamountforeach_stud_paid" value="'.$prostransactionamount.'">';

  



?>