


<?php

            error_reporting(E_ALL);
            ini_set('display_errors', 1);

            include('../../../config/config.php');


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





            $currenttermquery = mysqli_query($link, "SELECT * FROM `termorsemester` WHERE `status`='1'");
            $currenttermquerycnt =  mysqli_num_rows($currenttermquery);
            $currenttermquerycnt_row =  mysqli_fetch_assoc($currenttermquery);

             $TermOrSemesterIDcurrent   = $currenttermquerycnt_row['TermOrSemesterID'];



            $currentsessionquery = mysqli_query($link, "SELECT * FROM `session` WHERE `sessionStatus`='1'");
            $currentsessionquerycnt =  mysqli_num_rows($currentsessionquery);
            $currentsessionquerycnt_row =  mysqli_fetch_assoc($currentsessionquery);

             $sessionName =  $currentsessionquerycnt_row['sessionName'];

            $prosgetstudent_content = mysqli_query($link,"SELECT * FROM `parent` INNER JOIN  `student` ON `parent`.`ParentID` = 
            `student`.`ParentID` WHERE  `parent`.`ParentID`='$UserID'");
            $prosgetstudent_contentcnt_cnt = mysqli_num_rows($prosgetstudent_content);
            $prosgetstudent_contentcnt_row = mysqli_fetch_assoc($prosgetstudent_content);

            $prosloadtotalfeeremaining = 0;

            if( $prosgetstudent_contentcnt_cnt  > 0){






                do{




                    $studentfname = $prosgetstudent_contentcnt_row['StudentFirstName'];
                    $StudentLastName = $prosgetstudent_contentcnt_row['StudentLastName'];
                    $StudentID = $prosgetstudent_contentcnt_row['StudentID'];
                    $CampusID = $prosgetstudent_contentcnt_row['CampusID'];



                        $prosp_loadtransaction_cuerrnet = mysqli_query($link,"SELECT SUM(TransactionIn) AS TransactionInamountcurrent FROM `transactions` WHERE CampusID='$CampusID' AND  StudentID='$StudentID' AND `Session`='$sessionName' AND TermOrSemesterID='$TermOrSemesterIDcurrent'");
                        $prosp_loadtransaction_cnt_cuerrnet = mysqli_num_rows($prosp_loadtransaction_cuerrnet);
                        $prosp_loadtransaction_row_cuerrnet = mysqli_fetch_assoc($prosp_loadtransaction_cuerrnet);

                        $proslpaidcuuerntamount = $prosp_loadtransaction_row_cuerrnet['TransactionInamountcurrent'];
                        

                        
                            $prospoverallpaid = number_format($proslpaidcuuerntamount);


                    $amountoptional_charge = 0;
                    $amountcomp_charge = 0;

                    $amounttransaction_amount = 0;

                    

                    $select_classallocation = mysqli_query($link,"SELECT * FROM `classordepartmentstudentallocation` WHERE 
                    CampusID='$CampusID ' AND StudentID='$StudentID'");
                    $select_classallocation_cnt = mysqli_num_rows($select_classallocation);
                    $prosgetstudent_contentcnt_row = mysqli_fetch_assoc($select_classallocation);


                    do{




                        $Session =  $prosgetstudent_contentcnt_row['Session'];
                        $ClassOrDepartmentID=  $prosgetstudent_contentcnt_row['ClassOrDepartmentID'];
                       


                    $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($prosgetstudent_contentcnt_row = mysqli_fetch_assoc($select_classallocation));



                   



                    $prostotalcharge = $amountoptional_charge  + $amountcomp_charge; 

                    if($amounttransaction_amount >= $prostotalcharge)
                    {

                        $prostotalchargeoverall = '0.00';

                    }else{


                        $prostotalchargeoverall = number_format($prostotalcharge -  $amounttransaction_amount);

                    }


                   
                   
                    $prosloadtotalfeeremaining+=$prostotalchargeoverall;



                }while($prosgetstudent_contentcnt_row = mysqli_fetch_assoc($prosgetstudent_content));
    
    
    


            }else{

            }
           



            $select_institution = mysqli_query($link, "SELECT * FROM `setcountdown` INNER JOIN parent  ON  `setcountdown`.InstitutionID = `parent`.InstitutionID  WHERE `parent`.ParentID='$UserID'");
            $select_institution_cnt = mysqli_num_rows($select_institution);
            $select_institution_cnt_row = mysqli_fetch_assoc($select_institution);

            if ($select_institution_cnt > 0) {

                $start = $select_institution_cnt_row['StartDate'];
                $EndDate = $select_institution_cnt_row['EndDate'];
                $Description = $select_institution_cnt_row['Description'];
                $name = $select_institution_cnt_row['ParentFirstName'].' '.$select_institution_cnt_row['ParentLastName'];

                $proscoundown = array(
                    'startdate' => $start,
                    'enddate' => $EndDate,
                    'description' => $Description,
                    'name' =>$name,
                    'balance' =>$prosloadtotalfeeremaining


                );
                
                $json_data = json_encode($proscoundown, JSON_PRETTY_PRINT);

                echo $json_data;
            } else {
                // Return an empty JSON object to ensure consistent format
                echo json_encode(new stdClass());
            }
?>