<?php
require_once 'pdf.php';
 
 include('db_con.php');
 $html = '';
 
 $sr_id = $_GET['id'];
 $tot_qty = 0;
 $tot_ctn = 0;
 $tot_amt = 0;
 $discount = 0;
 $gst_amt = 0;
 $sql = "select title_id from sr where sr_id = '".$sr_id."'";
 $statement = $connect->prepare($sql);
                      $statement->execute(); 
                      $title_id = $statement->fetchColumn();
$statement = $connect->prepare("select * from titles where  id= :title_id
    
  ");
 $statement->execute(
  array(  
   ':title_id'       =>  $title_id
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
  $heading1 = $row['heading1'];
  $heading2 = $row['heading2'];
  $heading3 = $row['heading3'];
 }
  $statement = $connect->prepare("select m.sr_id,m.inv_no,m.chal_no,m.bilty_no,m.due_day,m.due_dated,DATE_FORMAT(m.dated,'%d-%m-%Y') as dated,m.description,m.inv_type,m.discount,m.freight,m.gst_pr,c.name,c.phone,e.first_name,
    d.item_no,o.store_name,CONCAT(i.item_name,' ',t.type_name, ' ' ,b.brand_name, ' ', l.color_name) as item_name,u.uom_name,d.ctn,d.qty,d.rate,d.dis_pr,d.dis_rate from sr_detail d,sr m,item_uom u,
    customers c,sales_man s,employees e, items  i,store o, item_brand b,item_type t,item_color l where m.sr_id=d.sr_id and i.brand_id=b.brand_id and i.color_id=l.color_id and t.type_id=i.type_id and m.emp_id=e.emp_id and s.sm_id=m.sm_id and d.item_no=i.item_id and i.uom=u.uom_id and c.cust_id=m.cust_id and o.store_id=d.store_id and
    m.sr_id= :sr_id
    
  ");
 $statement->execute(
  array(  
   ':sr_id'       =>  $sr_id
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
 
 $html .=' <style>
 * {
 font-family : Helvetica;
}
    @page { margin:160px 20px; }
    #header { position: fixed; left: 0px; top: -160px; right: 0px; height: 160px; text-align: center;background-color: #fff; }
    #footer { position: fixed; left: 0px; bottom: -160px; right: 0px; height: 300px; background-color: #fff; }
    #content {margin:0;}
    .rightside{text-align:right;}
    #footer .page:after { content: "page " counter(page) " of " counter(pages);}
    
    #table_header th {
    border-top:1px solid black;
    border-bottom:1px solid black;
  }
  .small-font {
  font-size:12px;
  font-weight:normal;
  font-family:Helvetica;
}
.font-bold {
  font-size:13px;
  font-weight:normal;
  font-family:Helvetica;
}
.signtable {
  text-align:center;
}
.topline {
  border-top:0.1px solid black;
}
.brline
{
  border-top:0.1px solid black;
}
 h5, h6{
  margin: 0;
  padding: 0;
}
#main-header {
  font-size:12px;
}
  </style>
  <div id="header">
    <div id="heading">
    <br />
  <h5>'.$heading1.'</h5>
  <h6>'.$heading2.'</h6>
  <h6>'.$heading3.'</h6>
  </div>
  
  <table id="main-header" width="100%">
  <tr>
  <td width="15%">No. :</td><td width = "50%">'.$row["inv_no"].'</td><td width="15%">Date :</td><td width = "20%">'.$row["dated"].'</td>
  </tr>
  <tr>
  <td width="15%">M/S :</td><td width = "50%">'.$row["name"].'</td><td width="15%"></td><td width = "20%"></td>
  </tr>
  <tr>
  <td width="15%">Phone No. :</td><td width = "50%">'.$row["phone"].'</td><td width="15%"></td><td width = "20%"></td>
  </tr>
  <tr>
  <td width="15%">Delivered By :</td><td width = "50%">'.$row["first_name"].'</td><td width="15%">Bilty No. :</td><td width = "20%">'.$row["bilty_no"].'</td>
  </tr>
  
  </table>
  <table width="100%" id="table_header">
  <tr>
  <th width="5%" class="font-bold">Sr</th>
  
  <th width="51%" class="font-bold">Item Name</th>
  <th width="20%" class="font-bold">Store</th>
  <th width="7%" class="font-bold">UOM</th>
  
  <th width="7%" class="rightside font-bold">Ctn</th>
  <th width="10%" class="rightside font-bold">Qty</th>
  
  </tr>
  </table>
  </div>
  
';
}
$count = 0;
$amount = 0;
foreach($result as $row)
 {
  $qty = $row["qty"];
  $ctn = $row["ctn"];
   $count = $count +1;
  
  $tot_qty = $tot_qty + $qty;
  $tot_ctn = $tot_ctn + $ctn;
$html .= '
  <div id="content">
  
  <table width="100%">
<tr>
  <td width="5%" class="small-font">'.$count.'</td>
  
<td width="51%" class="small-font">'.$row["item_name"].'</td>
<td width="20%" class="small-font">'.$row["store_name"].'</td>
<td width="7%" class="small-font">'.$row["uom_name"].'</td>
<td width="7%" class="rightside small-font">'.$row["ctn"].'</td>
<td width="10%" class="rightside small-font">'.number_format($qty, 2).'</td>
  
  
   
    
   </tr>
   
  </table>
  </div>
  ';
 
  $html .='
  <div id="footer">
 <div class="brline"></div>
<table width="100%">
<tr>
<td width="61%" class="rightside small-font">Total:</td>
<td class ="rightside small-font" width="7%">'.number_format($tot_ctn, 2).'</td>
<td class ="rightside small-font" width="10%">'.number_format($tot_qty, 2).'</td>
</tr>
</table>
<div class="brline"></div>
<br />
<br />
<br />
<br />
<table width="100%" class="signtable">
<tr>
<td width="15%"></td><td class="small-font topline" width="20%">Delivered By</td><td width="20%"></td><td width="20%" class="small-font topline">Received By</td><td width="25%"></td><td width="20%" class="small-font topline">Checked By</td><td width="25%"></td>
</tr>
</table>
  
  </div>
 
        ';
}
 $pdf = new Pdf();
 $file_name = 'chalanA4.pdf';
 $pdf->setPaper('A4', 'portrait');
 $pdf->loadHtml($html);
 $pdf->render();
 $pdf->stream($file_name, array("Attachment" => false));
?>