<?php
error_reporting(0);
    $tolast='';
    $pai='';
    $date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
    $timenow=$date->format('Y-m-d H:i:s');
    $timenow2=$date->format('Y-m-d');
    $month=$date->format('M');
    $feepaid='00';
     
    $tuU='';
    $adD='';
    $diI='';
    $boO='';
    $trR='';
    $unN='';
    $moO='';
    $peE='';
    $txX='';

    $y=intval($_GET['Year']);
    if($y=='' || $y==null)
    {$yr=$date->format('Y');}
    else
    {
     $yr=$y;
    }
    
    $tr=$_GET['Term'];
    if($tr=='' || $tr==null)
    {
        $tr='Term One'; 
    }else
    {
    }
    
      if($tr=='Term One')
    {
        $trmm='Term Three';
        $yrr=(int)$yr;
        $yr2=$yrr-1;
        
    }else if($tr=='Term Two')
    {
        $trmm='Term One';
        $yr2=$yr;
    }else if($tr=='Term Three')
    {
        $trmm='Term Two';
        $yr2=$yr;
    }
    
    $pid=$_GET['JHGHJUXXXYYII*hssyjkoqwwhjkwjloonk'];
    if($pid=='' || $pid==null)
    {
      $pid='0';
    }else
    {
    }
    
    $result = mysqli_query($con,"SELECT * from pupils where count='$pid'");
    while($row = $result->fetch_assoc()) 
    {
    $nam=$row["Names"];
    $admno=$row["AdmNo"];
    $claz=$row["Claz"];
    }
    
    $resultt = mysqli_query($con,"SELECT * from Fees2 where pid='$pid' AND Year='$yr' AND Term='$tr'");
    while($row = $resultt->fetch_assoc()) 
    {
    $tut=$row["TutionF"];
    $adm=$row["AdmF"];
    $dia=$row["DiariesF"];
    $bof=$row["BoF"];
    $tran=$row["TranF"];
    $uni=$row["UniF"];
    $mot=$row["MotF"];
    $pef=$row["PEF"];
    $txt=$row["TxtF"];
    $autono=$row["count"];
    $auna=$row["AutoNa"];
    $dev=$row["Dev"];
    $tab=$row["Tablet"];
    $asbk=$row["Assbok"];
    }
    
    ///Last Term balances
    $resultt = mysqli_query($con,"SELECT * from Balances where Year='$yr2' AND Term='$trmm' AND SID='$pid'");
    while($row = $resultt->fetch_assoc()) 
    {
    $prebala=$row["Amount"];
    }
    
    //////////Calculating -1 Last Number

    $mwaka=substr($auna, 0,2);
    $tamu=substr($auna, 2,2);
    if($tamu=='01'){
      $mwaka=$mwaka-1;  
      $tam='Term Three';
    }else if($tamu=='02')
    {
       $tam='Term One'; 
    }else if($tamu=='03')
    {
        $tam='Term Two';
    }
    $mwaka='20'.$mwaka; 
    
    $feebala='';
    $autono=$autono-1;
    
    if($adm==''){ $adm=0;}
    if($dia==''){ $dia=0;}
    if($bof==''){ $bof=0;}
    if($tran==''){ $tran=0;}
    if($uni==''){ $uni=0;}
    if($mot==''){ $mot=0;}
    if($pef==''){ $pef=0;}
    if($txt==''){ $txt=0;}
    if($dev==''){ $dev=0;}
    if($tab==''){ $tab=0;}
    if($asbk==''){ $asbk=0;}
    
    $tot=$tut+$adm+$dia+$bof+$tran+$uni+$mot+$pef+$txt+$tab+$dev+$asbk;
    
    $sql = "select * from Fees2 where Year='$mwaka' AND Term='$tam' AND pid='$pid'";
    $result = $con->query($sql);
    if ($result->num_rows > 0) 
    {
    $resul = mysqli_query($con,"select * from Fees2 where Year='$mwaka' AND Term='$tam' AND pid='$pid'");
    while($row = $resul->fetch_assoc()) 
    {
    $tuU=$row["TutionF"];
    $adD=$row["AdmF"];
    $diI=$row["DiariesF"];
    $boO=$row["BoF"];
    $trR=$row["TranF"];
    $unN=$row["UniF"];
    $moO=$row["MotF"];
    $peE=$row["PEF"];
    $txX=$row["TxtF"];
    $devv=$row["Dev"];
    $tabb=$row["Tablet"];
    $asbkk=$row["Assbok"];
    
    $codD=$row["Cod"];

    if($adD==''){ $adD=0;}
    if($diI==''){ $diI=0;}
    if($boO==''){ $boO=0;}
    if($trR==''){ $trR=0;}
    if($unN==''){ $unN=0;}
    if($moO==''){ $moO=0;}
    if($peE==''){ $peE=0;}
    if($txX==''){ $txX=0;}
    if($devv==''){ $devv=0;}
    if($tabb==''){ $tabb=0;}
    if($asbkk==''){ $asbkk=0;}
    
    $lpd='0';
    $tolast=$tuU+$adD+$diI+$boO+$trR+$unN+$moO+$peE+$txX+$devv+$tabb+$asbkk;
    
    $query = "select * from feestatement where Year='$mwaka' AND Term='$tam' AND pid='$pid'";
    $result = $con->query($query);
    if ($result->num_rows > 0) 
    {
    $resul = mysqli_query($con,"select SUM(PaidAmount) AS lastpaid from feestatement where Year='$mwaka' AND Term='$tam' AND pid='$pid'");
    while($row = $resul->fetch_assoc()) 
    {
    $lpd=$row["lastpaid"];
    $feebala=$tolast-$lpd;
    }
    }else
    {
    $feebala=$tolast;
    }
    }
    }else
    {
     $feebala='0';
    }
    
    //Check previous balance from saved balance
    $sqll = "select * from Balances where Year='$yr2' AND Term='$trmm' AND SID='$pid'";
    $resulttt = $con->query($sqll);
    if ($resulttt->num_rows > 0) 
    {
     $resultt = mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND SID='$pid'");
    while($row = $resultt->fetch_assoc()) 
    {
    $feebala=$row['Amount'];
    }   
    }else
    {
    $feebala='0';
    }
?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h5 class="m-0">Pupil's Proforma </h5>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">Dashboard</li>
              <li class="breadcrumb-item ">Reports</li>
              <li class="breadcrumb-item active">Proforma</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-3 col-sm-3 col-12">
            <div class="info-box">
              <span class="info-box-icon bg-success"><i class="far fa-calendar"></i></span>
              
              <div class="info-box-content">
                <div class="form-group">
                <label for="inputStatus">Year <span class="requireds">*</span>
                </label>
                <select id="yea" class="form-control custom-select" required>
                <option selected value="<?php echo $yr; ?>"><?php echo $yr; ?>
                <option value="2022">2022</option></option>
                <option value="2023">2023</option>
                <option value="2024">2024</option>
                <option value="2025">2025</option>
                <option value="2026">2026</option>
                <option value="2027">2027</option>
                <option value="2028">2028</option>
                <option value="2029">2029</option>
                <option value="2030">2030</option>
                <option value="2031">2031</option>
                <option value="2032">2032</option>
                <option value="2033">2033</option>
                <option value="2034">2034</option>
                <option value="2035">2035</option>
                <option value="2036">2036</option>
                <option value="2037">2037</option>
                <option value="2038">2038</option>
                <option value="2039">2039</option>
                <option value="2040">2040</option>
                </select>
                </div>
                  
              </div>
              <!-- /.info-box-content -->
            </div>
          </div>
          
          <div class="col-md-3 col-sm-3 col-12">
            <div class="info-box">
              <span class="info-box-icon bg-success"><i class="far fa-calendar"></i>
              </span>
        
              <div class="info-box-content">
                <div class="form-group">
                <label for="inputStatus">Term <span class="requireds">*</span>
                </label>
                <select id="tam" class="form-control custom-select">
                 <option value="<?php echo $tr; ?>"><?php echo $tr; ?></option> <option value="">Select
                </option>
                <option value="Term One">Term One</option></option>
                <option value="Term Two">Term two</option>
                <option value="Term Three">Term Three</option>
                </select>
                </div>
              </div>
              <!-- /.info-box-content -->
            </div>
          </div>
          
          
           <div class="col-md-3 col-sm-3 col-12">
            <div class="info-box">
              <span class="info-box-icon bg-success"><i class="far fa-group"></i></span>
              <div class="info-box-content">
                <div class="form-group">
                <label for="inputStatus">Select Pupil <span class="requireds">*</span>
                </label>
                     <select id="pupilselect" class="form-control custom-select" required style="height:40px">
                    <option value="">Select</option></option>
                    <?php
                            //include('../include/config.php');
                    $sql = "select * from pupils where Status='Active' Order BY Names ASC";
                    $result = $con->query($sql);
                    if ($result->num_rows > 0) 
                    {
                    $cnt='1';
                    while($row = $result->fetch_assoc()) 
                    {
                    ?>
                    <option value="<?php echo $row["count"]; ?>"><?php echo $row["AdmNo"].' '.$row["Names"]; ?>
                    </option>
                    <?php
                    }
                    }
                    ?>
                    </select>
                </div>
                  
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </div>
          </div>
         </div>
    </section>
          
    
    <section class="content">
         <div class="row">
         <div class="col-md-12">   
         <div class="card">
             <div class="card-body">
                <div class="tab-content">
                 <div class="container">
                     <center><h3>SCHOOL FEES PROFORMA FOR: <strong class="text-warning"><?php echo $tr.' '.$yr.'.';  ?></strong></h3></center>
                     <h6><strong>Pupil Name:</strong> <?php echo $nam; ?>  |  &nbsp &nbsp <strong>Admission No:</strong> <?php echo  $admno; ?> |  &nbsp &nbsp <strong>Class/Grade:</strong> <?php echo $claz; ?></h6>
                     
                     <table class="table table-success">
                         <thead>
                             <tr><th>Particular</th>
                             <th>Amount</th>
                             </tr>
                         </thead>
                         <tbody>
                             <tr class="alert-warning">
                                 <td>Previous Balance</td> <td><strong class="text-red"><?php echo number_format($feebala); ?></strong> </td>
                             </tr>
                             <tr>
                                 <td>Tution Fees</td>  <td><strong><?php echo $tut; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Admission Fees</td>  <td><strong><?php echo $adm; ?></strong></td>
                             </tr>
                             <tr>
                                 <td>Boarding Fees</td>  <td><strong><?php echo $bof; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Motivation</td>  <td><strong><?php echo $mot; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Transport</td>  <td><strong><?php echo $tran; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Assesment Books</td>  <td><strong><?php echo $txt; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>School Diary</td>  <td><strong><?php echo $dia; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Uniforms Cost</td> <td><strong><?php echo $uni; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>P. Expenses</td>  <td><strong><?php echo $pef; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Tablet</td>  <td><strong><?php echo $tab; ?></strong></td>
                             </tr>
                             <tr>
                                 <td>Assignment Books</td>  <td><strong><?php echo $asbk; ?></strong></td>
                             </tr>
                              <tr>
                                 <td>Development Fee</td>  <td><strong><?php echo $dev; ?></strong></td>
                             </tr>
                             <tr class="bg-black">
                                 <td>TOTAL FEE</td>  <td><strong><?php echo number_format($tot+$feebala); ?></strong></td>
                             </tr>
                             <tr>
                                 <td colspan="2" class="text-danger
                                 "><strong>LESS</strong></td> 
                             </tr>
                             <tr>
                                 <td>School Fees pre-payments</td>  <td><strong class="text-success"><?php echo number_format($feepaid); ?> </strong></td>
                             </tr>
                             
                             <tr class="alert-danger">
                                 <td>TOTAL BALANCE (KES)</td>  <td><strong class="" style="font-weight:900"><?php echo number_format(($tot+$feebala)-$feepaid); ?> </strong></td>
                             </tr>
                             
                             <tr>
                                 <td colspan="2">
                                 <a href="profolink.php?pid=<?php echo $pid;?>&Year=<?php echo $yr; ?>&Term=<?php echo $tr; ?>" target="_blank" title="Link Not Ready" class="btn btn-info pull-right"><i class="fa fa-cloud-download"></i> Dowload PDF File</a>
                                 </td>
                             </tr>
                         </tbody>
                     </table>
                 </div>
          
              </div>
            </div>
          </div>
          </div>
          </div>
    </section>
</div>

