<?php
require_once 'pdf.php';
 
 include('db_con.php');
 $html = '';
 $tr_id = $_GET['id'];
 $totqty_in = 0;
 $totqty_out = 0;
 $totcost=0;
 $cost_amt= 0;
           
          
 
 
  $statement = $connect->prepare("select t.tr_id,t.dated,t.item_no,concat(p.type_name, ' ',i.item_name) item_name,t.cost,s.store_name, u.user_name,t.qty_in,t.qty_out from
  adjusted_stock t, items i,store s,item_type p,user_detail u where p.type_id=i.type_id and i.item_id=t.item_no and u.user_id=t.user_id and s.store_id=t.store_id and t.tr_id=:tr_id");
 $statement->execute(
  array(
   ':tr_id'       =>  $tr_id
  
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
 
 $html .=' <style>
    @page { margin:140px 20px; }
   body {font-weight:normal;font-size:11px;font-family:Helvetica}
    #header { position: fixed; left: 0px; top: -140px; right: 0px; height: 140px; text-align: center; font-weight:normal;font-size:14px;font-family:Helvetica;background-color: #fff;}
    #footer { position: fixed; left: 0px; bottom: -150px; right: 0px; height: 150px; background-color: #fff; }
   #content {margin:0 auto;}
   
    
    .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;
  }
  </style>
  <div id="header">
  <center>
  <h2 style="font-weight:normal">Store Adjustment</h2>
  </center>
  
  <table width="100%">
  <tr>
  <td width="100%">ID: '.$row["tr_id"].'  |  Date : '.$row["dated"].'</span>
  </td>
  </tr>
  <tr>
  <td width="100%"> <b>Created:</b>'.$row["user_name"].' </td>
  </tr>
  
  
  </table>
  <table width="100%" id="table_header">
  <tr>
  
  <th width="40%">Name</th>
  <th width="20%">Store</th>
  <th width="10%" class="rightside">Qty In</th>
  <th width="10%" class="rightside">Qty Out</th>
  <th width="10%" class="rightside">Cost</th>
  <th width="10%" class="rightside">CostAmt</th>
  
  </tr>
  </table>
  </div>
  
';
  $qty_in = $row["qty_in"];
  $qty_out = $row["qty_out"];
  $cost = $row["cost"];
  $totqty_in = $totqty_in + $qty_in;
  $totqty_out = $totqty_out + $qty_out;
  if($qty_in > 0){$cost_amt= $cost * $qty_in;}else{$cost_amt = $cost* $qty_out * -1; }
  $totcost = $totcost + $cost_amt;
  
$html .= '
  <div id="content">
  
  <table width="100%">
<tr>
  
<td width="40%">'.$row["item_name"].'</td>
<td width="20%">'.$row["store_name"].'</td>
<td width="10%" class="rightside">'.number_format($qty_in, 2).'</td>
<td width="10%" class="rightside">'.number_format($qty_out, 2).'</td>
<td width="10%" class="rightside">'.number_format($cost, 2).'</td>
<td width="10%" class="rightside">'.number_format($cost_amt, 0).'</td>
   
  
   
    
   </tr>
   
  </table>
';
}
$html .='
<hr style="height:-1px solid balck"/>
<table width="100%">
<tr>
<td width="70%" class="rightside">
Total  :</td><td width="10%" class="rightside">'.number_format($totqty_in, 2).'
</td><td width="10%" class="rightside">'.number_format($totqty_out, 2).'
</td>
<td width="10%" class="rightside">'.number_format($totcost, 2).'
</td>
</tr>
</table>
<br />
<br />
<table width="100%">
<tr>
<td width="50%">Verified 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 = 'item_transfer_print.pdf';
 $pdf->setPaper('A4', 'portrait');
 $pdf->loadHtml($html);
 $pdf->render();
 $pdf->stream($file_name, array("Attachment" => false));
?>