<?php
require_once 'pdf.php';
 
 include('db_con.php');
 include('function.php');
 $html = '';
 $dated  = date('Y-m-d');
 $count = 0;
        accounts_update($connect);
$parent_acc_no = $_GET["acc_no"];
$fdate = $_GET["fdate"];
$tdate = $_GET["tdate"];
$balance = 0;
  
         
  $statement = $connect->prepare("select a.acc_no,a.acc_name from accounts a where a.acc_parent = '".$parent_acc_no."'
  ");
 $statement->execute();
 $result = $statement->fetchAll(); 
 $html .=' <style>
    @page { margin:160px 20px; }
    @page {margin-bottom: 40px;}
    #header { position: fixed; left: 0px; top: -160px; right: 0px; height: 160px; text-align: center; }
    #footer { position: fixed; left: 0px; bottom: -30px; right: 0px; height: 50px; background-color: #fff; }
    #content {margin:0;font-size:13px;font-weight:normal;}
    .rightside{text-align:right;}
    .total{text-align:right;border-top:1px solid black;}
    #footer .page:after { content: counter(page, upper-roman); }
    
    #table_header th {
    border-top:1px solid black;
    border-bottom:1px solid black;
  }
  table.tot_table td{
    border-top :1px solid black;
  }
  .endline {
    border-top:0.1px solid black;
  }
  .total_line {
    border-top:1px solid gray;
  }
  
  </style>
  <div id="header">
  <h3>Sub Account Report</h3>
  <table width="100%">
  <tr>
  <td width="15%">Printed at  :</td><td width="55%">'.$dated.'</td><td width="10%" class="rightside">From </td><td width="20%" class="rightside">'.$fdate.'</td>
  </tr>
  <tr>
  <td width="15%"> </td><td  width="55%" style="font-size:12px"></td><td width="15%" class="rightside">To :</td><td width="20%" class="rightside">'.$tdate.'</td>
  </tr>
  
  </table>
  <table width="100%" id="table_header">
  <tr>
  <th width="5%">#</th>
  <th width="15%">No.</th>
  <th width="65%">Name</th> 
  
  <th width="15%" class="rightside">Balance</th>
  </tr>
  </table>
  </div>
  
';
 
$html .= '
  <div id="content">
  
  ';
foreach($result as $row)
 {
$acc_no = $row["acc_no"];
   $count = $count+1;
          $detail_statement = $connect->prepare("select m.dated, a.acc_name,m.source,m.ref_id, d.debit,d.credit from jv m, jv_detail d, accounts a where m.jv_id=d.jv_id and a.acc_no=d.acc_no and d.acc_no= '".$acc_no."' and m.dated between '".$fdate."' and '".$tdate."' order by dated");
         $detail_statement->execute(
                   );
         $detail_result = $detail_statement->fetchAll();
          $opening_statement = $connect->prepare("select sum(d.debit-d.credit) from jv_detail d, jv m where m.jv_id=d.jv_id and m.dated < '".$fdate."' and d.acc_no='".$acc_no."'");
         $opening_statement->execute(
                   );
         $opening = $opening_statement->fetchColumn();
 
  // if($count == 1){$balance = $opening + $debit - $credit; }else{$balance = $balance + $debit - $credit;}
   
 
  
    
  
 
  $html .=' <div class="endline"></div>
  <b>
  <table width="100%">
<tr>
<td width="5%">'.$count.'</td>
  
<td colspan="6" width="55%">'.$row["acc_name"].'</td>
<td width="40%">Opening : '.$opening.'</td>
</tr>
</table>
</b>
  ';
  
   $sr = 0;
   //$tot_amount = 0;
   //$tot_qty = 0;
   //$tot_dis_amount = 0;
   $tot_debit = 0;
   $tot_credit = 0;
                foreach($detail_result as $detail_row)
                  {
                      $sr = $sr+1;
                     
                      $debit = $detail_row["debit"];
                      $credit = $detail_row["credit"];
                      if($sr == 1){$balance = $opening + $debit - $credit; }else{$balance = $balance + $debit - $credit;}
                      
                      //$tot_amount = $tot_amount + $amount;
                     // $tot_qty = $tot_qty + $qty;
                     // $tot_dis_amount = $tot_dis_amount + $dis_amount;
                      $html .='
                      <table width="100%">
                      
                      <tr>                
                      
                      
                      <td width="5%">'.$sr.'</td>
                      
                      <td width="20%">'.$detail_row['dated'].'</td>
                      <td width="15%">'.$detail_row['source'].'</td>
                      <td width="15%">'.$detail_row['ref_id'].'</td>
    <td width="15%" class="rightside">'.number_format($debit, 2).'</td>
    <td width="15%" class="rightside">'.number_format($credit, 2).'</td>
    <td width="15%" class="rightside">'.number_format($balance, 2).'</td>
  
   
    
   </tr>
   </table>
   ';
   $tot_debit = $tot_debit + $debit;
   $tot_credit = $tot_credit + $credit;
                  }
                 
     $html .='
                      <table width="100%">
                      
                      <tr class="total_line">
                       
                      
                      
                     
                      <td width="55%" class="rightside">Total :</td>
    <td width="15%" class="rightside">'.number_format($tot_debit, 2).'</td>
    <td width="15%" class="rightside">'.number_format($tot_credit, 2).'</td>
    <td width="15%" class="rightside"></td>
  
   
    
   </tr>
   </table>
   ';            
                  
}
$html .='
   
  </div>
  <div id="footer">
  <hr />
  <table width="100%">
  <tr>
  <td width="70%" class="rightside"> </td><td width="30%" class="rightside"></td>
  </tr>
  </table>
   
  </div>
        ';
 $pdf = new Pdf();
 $file_name = 'sub_balance.pdf';
 $pdf->setPaper('A4', 'portrait');
 $pdf->loadHtml($html);
 $pdf->render();
 $pdf->stream($file_name, array("Attachment" => false));
?>