<?php
require_once 'pdf.php';
 
 include('db_con.php');
 $html = '';
 $jv_id = $_GET['id'];
 $tot_debit = 0;
 $tot_credit= 0;
           
   $statement = $connect->prepare("select * from titles where  id= :title_id
    
  ");
 $statement->execute(
  array(  
   ':title_id'       =>  1
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
  $heading1 = $row['heading1'];
  $heading2 = $row['heading2'];
  $heading3 = $row['heading3'];
 }
        
 
 
  $statement = $connect->prepare("
  select m.jv_id,m.dated,m.jv_no,a.acc_name, b.branch_name,m.source,m.description,d.acc_no,d.narr,
  d.debit,d.credit
  from jv m,jv_detail d ,branch b,accounts a where b.branch_id=m.branch_id and
  a.acc_no=d.acc_no and m.jv_id=d.jv_id and m.jv_id=:jv_id");
 $statement->execute(
  array(
   ':jv_id'       =>  $jv_id
  
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
 
 $html .=' <style>
    @page { margin:230px 20px; }
    body {font-weight:normal;font-size:14px;font-family:Helvetica}
    #header { position: fixed; left: 0px; top: -230px; right: 0px; height: 230px; text-align: center; font-weight:normal;font-size:14px;font-family:Helvetica}
    #footer { position: fixed; left: 0px; bottom: -160px; right: 0px; height: 100px; background-color: #fff; }
   
    .title {font-size:15px;font-weight:normal;font-family:Helvetica;}
    .title_small {font-size:10px;font-weight:normal;font-family:Helvetica;}
    .rightside{text-align:right;}
    #footer .page:after { content: counter(page, upper); }
    
    #table_header th {
    border-top:1px solid black;
    border-bottom:1px solid black;
    padding:4px;
  }
  .dataline {
    border-bottom:1px solid gray;
  }
  .total {
    border-top:1px solid gray;
  }
  </style>
  <div id="header">
  <center>
  <h2 style="font-weight:normal">Journal Voucher</h2>
  </center>
<table width= "100%">
<tr>
  <td width="60%" class="title">'.$heading1.'</td><td width="20%"></td><td width="20%"> </td></tr>
  <tr><td class="title_small">'.$heading2.' </td></tr>
  <tr><td class="title_small">'.$heading3.'</td></tr>
  
  </table>
  <hr style="height:-1px solid balck"/>
  
  <table width="100%">
  <tr>
  <td width="25%">Jv No :</td><td width="20%">'.$row["jv_no"].'</td><td width="45%" class="rightside">Date :</td><td width="30%" class="rightside">'.$row["dated"].'</td>
  </tr>
  <tr>
  <td width="10%">Branch : </td><td width="70%">'.$row["branch_name"].'</td><td width="20%" class="rightside"></td><td width="10%" class="rightside"></td>
  </tr>
  <tr>
  <td width="90%" colspan="3">'.$row["description"].'</td><td width="10%" class="rightside"></td>
  </tr>
  
  </table>
  <table width="100%" id="table_header">
  <tr>
  <th width="15%">Acc_No</th>
  <th width="35%">Account Name</th>
  <th width="30%">Narration</th>
  <th width="15%" class="rightside">Debit</th>
  <th width="15%" class="rightside">Credit</th>
  </tr>
  </table>
  </div>
  
';
  $debit = $row["debit"];
  $credit = $row["credit"];
  $tot_debit = $tot_debit + $debit;
  $tot_credit = $tot_credit + $credit;
$html .= '
  <div id="content">
  
  <table style="width:100%;
  overflow-x:hidden">
<tr>
  <td width="15%" class="dataline">'.$row["acc_no"].'</td>
<td width="35%" class="dataline">'.$row["acc_name"].'</td>
<td width="30%" class="dataline">'.$row["narr"].'</td>
<td width="15%" class="rightside dataline">'.number_format($debit, 2).'</td>
   <td width="15%" class="rightside dataline">'.number_format($credit, 2).'</td>
  
   
    
   </tr>
   
  </table>
';
}
$html .='
<table width="100%">
<tr>
<td width="70%" class="rightside">Total:</td>
<td width="15%" class="rightside total">'.number_format($tot_debit, 2).'</td>
<td width="15%" class="rightside total">'.number_format($tot_credit, 2).'</td>
</tr>
</table>
<br />
<br />
<table width="100%">
<tr>
<td width="33%">Prepared By: __________</td><td>Checked By:__________</td><td>Approved By:__________</td>
</tr>
  </div>
  <div id="footer">
  <table width="100%">
  <tr>
  <td width="70%" class="rightside"> </td><td width="30%" class="rightside"></td>
  </tr>
  </table>
   
  </div>
        ';
 $pdf = new Pdf();
 $file_name = 'jv_print.pdf';
 $pdf->setPaper('A4', 'portrait');
 $pdf->loadHtml($html);
 $pdf->render();
 $pdf->stream($file_name, array("Attachment" => false));
?>