<?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"];
    $asbk=$row["Assbok"];
    
    }
     //////////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;}
    if($asbk==''){ $asbk=0;}
    
    $tot=$tut+$adm+$dia+$bof+$tran+$uni+$mot+$pef+$txt+$dev+$tab+$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"];
    $tabb=$row["Tablet"];
    $codD=$row["Cod"];
    $devv=$row["Dev"];
    $asbkk=$row["Assbok"];
    
    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;}
    if($asbkk==''){ $asbkk=0;}
    
    $tolast=$tuU+$adD+$diI+$boO+$trR+$unN+$moO+$peE+$txX+$tabb+$devv+$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 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';
    }
    
    if( $claz=='PP2' || $claz=='PP1' || $claz=='BABY CLASS')
    {
        header("location:Nursery_Proforma.php");
    }
    
    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;
    }
        
    //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';
    }
    
   $totpayable=($tot+$feebala)-$feepaid;  
   $totfee=$tot+$feebala;
   
   $query=mysqli_query($con,"select * FROM companydata");
   while($row=mysqli_fetch_array($query))
   {
   $com_name=$row["School"];
   $tag=$row["Tag"];
   $em=$row["Email"];
   $conts=$row["Contact"];
   } 
   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" >. </h6> </center>
   <center> <h3 style="margin-top:-20px" class="text-red" >'.$com_name.' </h3> </center>
   <center> <h4 style="margin-top:-20px" class="text-red" >'.$tag.'</h4> </center>
  
<img style="position:absolute;margin-top:-80px;margin-bottom:10px;max-height:90px" src="https://sch.cwmk.co.ke/images/schicon.jpg" alt="" class="" />
<br />
   <label style="margin-top:-20px" class="text-red" ><strong>Telephone:</strong> '.$conts.' </label>
   <br />
  <label style="margin-top:-20px" class="text-red" ><strong>Email:</strong> '.$em.' </label>
  <hr />

<div style="font-size:13px">
 NAME: <strong> <u>'.$nam.' </u></strong> CLASS:   <strong><u>'.$claz.'</u></strong> ADM NO:  <strong><u>'.$admno.'</u> </strong> 
       <center><h4><u>School Fees Proforma for '.$_SESSION["feeperiod"].' </u></h4></center>
    <table>
        <tbody>
        <tr style="background-color:#000;color:#FFF">
            <td> Previous Balance: </td>
            <td><strong> '.number_format($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. Assessment Book: </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. Assignment Books: </td>
            <td><strong> '.$asbk.' </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>'.number_format($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>'.number_format($feepaid).'</strong> </td>
         </tr>
             <tr>
            <td colspan="2">
            <hr />
            </td>
            </tr>
         <tr>
            <td> <strong  style="color:red">TOTAL AMOUNT PAYABLE</strong> </td>
            <td> <strong>'.number_format($totpayable).'</strong> </td>
         </tr>
        </tfoot>
     </table>
</div>
<strong>NOTE:</strong>
 <br />
    i. Account Name: <strong>SHEPHERDS TECH SCHOOL
         <blockquote>
         <table style="font-size:11px">
        <tbody>
        <tr>
            <td> 
           1. <strong> Equity Bank:  
           </td>
           <td>
           A/C 0220 2000 86782</strong>
           </td>
           </tr>
           <tr>
            <td> 
           2. <strong> Co-operative Bank: 
           </td>
           <td>
           A/C 0110 0422 397100</strong>
           </td>
           </tr>
           <tr>
            <td> 
           3. <strong> Family Bank: </td>
           </td>
           <td>
           A/C 0060 0000 2173</strong>
           </td>
           </tr>
           </tbody>
           </table>
         </blockquote>
<p> You can also pay through PAYBILL NUMBER </p>
            <blockquote>
        <table>
        <tbody>
        <tr>
            <td> BUSINESS NO:</td>
            <td> 247 247</td>
        </tr>
        
        <tr>
            <td> ACCOUNT NUMBER: </td>
            <td> 186782#Adm No</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 school fees proforma are to be presented to the school. <br />
    iii. Fees MUST BE paid direct to the bank account or Paybill No. but 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]);