<?php
session_start();
$yr=$_SESSION['yea'];
$pid=$_SESSION['cid'];
$tr=$_SESSION['tam'];
include('../include/config.php');

    $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';
    $y=$yr;
    if($y=='' || $y==null)
    {$yr=$date->format('Y');}
    else
    {
     $yr=$y;
    }
    
    if($tr=='' || $tr==null)
    {
        $tr='Term One'; 
    }else
    {
    }
    
    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"];
    $dev=$row["Dev"];
    $tab=$row["Tablet"];
    $autono=$row["count"];
    $auna=$row["AutoNa"];
    }
     //////////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($tab==''){ $tab=0;}
    if($dev==''){ $dev=0;}
    
    $tot=$tut+$adm+$dia+$bof+$tran+$uni+$mot+$pef+$txt+$dev+$tab;
    
    $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"];
    $tabb=$row["Tablet"];
    $codD=$row["Cod"];
    $devv=$row["Dev"];

    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($tabb==''){ $tabb=0;}
    if($devv==''){ $devv=0;}
    
    $tolast=$tuU+$adD+$diI+$boO+$trR+$unN+$moO+$peE+$txX+$tabb+$devv;
    
    $query = "select * from feestatement where Cod='$codD' AND pid='$pid'";
    $result = $con->query($query);
    if ($result->num_rows > 0) 
    {
    $resul = mysqli_query($con,"select SUM(PaidAmount) AS lastpaid from feestatement where Cod='$codD' AND pid='$pid'");
    while($row = $resul->fetch_assoc()) 
    {
    $lpd=$row["lastpaid"];
    $feebala=$tolast-$lpd;
    }
    }else
    {
    $feebala=$tolast;
    }
    }
    }else
    {
     $feebala='0';
    }
  
    //Check paid fee for the term
    $sqll = "select * from feestatement where Year='$yr' AND Term='$tr' AND pid='$pid'";
    $resulttt = $con->query($sqll);
    if ($resulttt->num_rows > 0) 
    {
     $resultt = mysqli_query($con,"SELECT sum(PaidAmount) as paid from feestatement where Year='$yr' AND Term='$tr' AND pid='$pid'");
    while($row = $resultt->fetch_assoc()) 
    {
    $feepaid=$row['paid'];
    }   
        
    }else
    {
    $feepaid='0';
    }
     
   $totpayable=($tot+$feebala)-$feepaid;  
   $totfee=$tot+$feebala;
   require_once 'dompdf/vendor/autoload.php';
 use Dompdf\Dompdf;
$html='
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Pupils Proforma</title>
  <meta content="" name="description">
  <meta content="" name="keywords">
  
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Vendor CSS Files -->
 
   <link rel="stylesheet" href="data/css/bootstrap.min.css">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link rel="shortcut icon" href="../images/logo.ico">
</head>
<body style="border:1px solid #004d1a;border-radius:5px;padding:5px;border-style: double;outline-offset: -5px;">
    <div>
     <center> <h6 style="margin-top:5px;color:#0099cc" class="text-red" >CATHOLIC DIOCESE OF MURANGA </h6> </center>
   <center> <h3 style="margin-top:-20px" class="text-red" >BISHOP KAIRO EDUCATION CENTRE </h3> </center>
   <center> <h4 style="margin-top:-20px" class="text-red" >EXCELLENCE IS IN GODLINESS </h4> </center>
  
<img style="position:absolute;margin-top:-80px;margin-bottom:10px;max-height:90px" src="https://bishopkairoeducationcentre.sc.ke/QB-Beta/data/logo.png" alt="" class="" />
<br />
   <label style="margin-top:-20px" class="text-red" ><strong>Telephone:</strong> 0728 966 221, 0714 647 708 </label>
   <br />
  <label style="margin-top:-20px" class="text-red" ><strong>Email:</strong> cdmbishopkairo@gmail.com </label>
  <hr />

<div style="">
 NAME: <strong> <u>'.$nam.' </u></strong> CLASS:   <strong><u>'.$claz.'</u></strong> ADM NO:  <strong><u>'.$admno.'</u> </strong> 
       <center><h3><u>School Fees Proforma for '.$_SESSION["feeperiod"].' </u></h3></center>
    <table>
        <tbody>
        <tr style="background-color:#000;color:#FFF">
            <td> C/F. Balance  (Last Term Balance): </td>
            <td><strong> '.$feebala.' </strong></td>
         </tr>
         <tr>
            <td>1. School Fees: </td>
            <td><strong>'.$tut.'</strong></td>
        </tr> 
        <tr>
            <td> 2. Admission Fees: </td>
            <td> <strong>'.$adm.' </strong></td>
        </tr>
        <tr>
            <td> 3. Boarding Fees: </td>
            <td> <strong>'.$bof.' </strong> </td>
         </tr>
         <tr>
            <td> 4. Motivation: </td>
            <td> <strong>'.$mot.'  </strong></td>
         </tr>
         <tr>
            <td> 5. Transport: </td>
            <td> <strong>'.$tran.'  </strong></td>
         </tr>
         <tr>
            <td> 6. Assesment Books: </td>
            <td> <strong>'.$txt.'  </strong></td>
         </tr>
          <tr>
            <td> 7. School Diary: </td>
            <td> <strong>'.$dia.'  </strong></td>
         </tr>
          <tr>
            <td> 8. Uniforms Cost: </td>
            <td> <strong>'.$uni.'  </strong></td>
         </tr>
         <tr>
            <td> 9. P. Expenses: </td>
            <td><strong> '.$pef.' </strong></td>
         </tr>
             <tr>
            <td> 10. Tablet: </td>
            <td> <strong>'.$tab.'  </strong></td>
         </tr>
         <tr>
            <td> 11. Development Fee: </td>
            <td><strong> '.$dev.' </strong></td>
         </tr>
        </tbody>
         <hr />
        <tfoot>
        <tr>
          <td colspan="2">
          </td>
        </tr>
        <tr>
        <td colspan="2">
             <hr />
        </td>
        </tr>
          <tr>
            <td> <strong>Total:</strong> </td>
            <td> <strong>'.$totfee.'</strong> </td>
         </tr>
         
            <tr>
        <td colspan="2">
            <hr />
        </td>
        </tr>
         
          <tr>
            <td> <strong>LESS: </strong> </td>
         </tr>
        
         <tr>
            <td> <strong>School Fees pre-payments: </strong> </td>
            <td> <strong>'.$feepaid.'</strong> </td>
         </tr>
             <tr>
            <td colspan="2">
            <hr />
            </td>
            </tr>
         <tr>
            <td> <strong  style="color:red">TOTAL AMOUNT PAYABLE</strong> </td>
            <td> <strong>'.$totpayable.'</strong> </td>
         </tr>
        </tfoot>
     </table>
</div>
<strong>NOTE:</strong> Kindly pay your school fees through:
 <br />
    i. Business Name:  <strong>BISHOP KAIRO</strong>

            <blockquote>
                <table>
                <tbody>
                <tr>
                    <td> PAYBILL NO:</td>
                    <td> 222 111</td>
                </tr>
                
                <tr>
                    <td> ACCOUNT NUMBER: </td>
                    <td> 179744#ADM</td>
                </tr>
                </tbody>
                </table>
            <blockquote>
      </div>
    ii. Fees must be paid in full on or before the opening day. The bank deposit slip and this chool fees proforma are to be presented to the school. <br />
    iii. Fees MUST BE paid direct to the bank account NOT through the agents. <br />
    iv. Fees once paid is not refundable.

      </section>
     </div>

  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/quill/quill.min.js"></script>
  <script src="assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>
  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>
  <script src="assets/js/jQuery-2.1.4.min.js"></script>
</body>
</html>'; 
$name=$nam.'.pdf';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$options = $dompdf->getOptions(); 
$options->set(array('isRemoteEnabled' => true));
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($name,['Attachment' => 0]);