<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 

  $po_no = $_GET["id"];



 $qty = 0;

 $rcvd = 0;

 $tot_qty = 0;

 $tot_rcvd = 0;

 $tot_balance = 0;

 

 $count = 0;

 $balance = 0;

     



  $statement = $connect->prepare("SELECT m.v_id,m.dated,m.description,LEFT(v.name,35) as v_name,

  m.status FROM vendors v,po m   WHERE m.v_id=v.v_id and m.po_no=:po_no");

 $statement->execute(

  array(

   

   ':po_no'       =>  $po_no

  )

 );

 $result = $statement->fetchAll(); 

 

 foreach($result as $row)

 {













  











 $html .=' <style>

    @page { margin:140px 20px; }

    #header { position: fixed; left: 0px; top: -140px; right: 0px; height: 140px; text-align: center; }

    #footer { position: fixed; left: 0px; bottom: -160px; right: 0px; height: 100px; background-color: #fff; }

    #content {margin:0;font-size:13px;font-weight:normal;}

    .rightside{text-align:right;}

    #footer .page:after { content: counter(page, upper-roman); }

    

    #table_header th {

    border-top:1px solid black;

    border-bottom:1px solid black;

  }

  </style>



  <div id="header">



  <h3>Purchase Order</h3>

  <table width="100%">

  <tr>

  <td width="15%">Po No. :</td><td width="55%">'.$po_no.'</td><td width="10%" class="rightside">Date :</td><td width="20%" class="rightside">'.$row["dated"].'</td>

  </tr>

  <tr>

  <td width="15%">Vendor : </td><td  width="55%" style="font-size:12px">'.$row["v_name"].'</td><td width="10%" class="rightside">Status :</td><td width="20%" class="rightside">'.$row["status"].'</td>

  </tr>

  

  </table>

  <table width="100%" id="table_header">



  <tr>

  <th width="4%">#</th>

  <th width="8%">Item No</th>

  <th width="48%">Item Name</th> 

  

  <th width="10%" class="rightside">Qty</th>

  <th width="10%" class="rightside">Recvd</th>

  <th width="10%" class="rightside">Balance</th>







  </tr>



  </table>



  </div>

  

';

}



$statement = $connect->prepare("select b.item_no,b.item_name, qty, rcvd, balance 

from ( select a.item_no,a.item_name,sum(qty) qty, sum(rcvd) rcvd, sum(qty) - sum(rcvd) as balance 

  from ( select d.item_no,i.item_name,sum(IFNULL(d.qty,0)) as qty, 0 as rcvd

   from po_detail d, po m, items i where m.po_no=d.po_no  and i.item_no = d.item_no and m.po_no = :po_no group by item_no,item_name

   union all

   select d.item_no,i.item_name, 0 as qty, sum(IFNULL(d.qty,0)) as rcvd 

   from purchase_detail d, purchase m,items i where m.p_id=d.p_id and i.item_no=d.item_no and m.po_no = :po_no group by item_no,item_name

  ) a group by  a.item_no,a.item_name) b;");

 $statement->execute(

  array(

   

   ':po_no'       =>  $po_no

  )

 );

 $result = $statement->fetchAll(); 

foreach($result as $row)

 {

  $qty = $row["qty"];

  $rcvd = $row["rcvd"];

  $balance = $row["balance"];

  $count++;



  $tot_qty = $tot_qty + $qty;

  $tot_rcvd  = $tot_rcvd + $rcvd;



  $tot_balance = $tot_qty - $tot_rcvd;





 

  

    

  

  



$html .= '

  <div id="content">

  

  <table width="100%">

<tr>

<td width="4%">'.$count.'</td>

 

<td width="8%">'.$row["item_no"].'</td>

<td width="48%">'.$row["item_name"].'</td>

<td width="10%" class="rightside">'.number_format($qty, 2).'</td>

   <td width="10%" class="rightside">'.number_format($rcvd, 2).'</td>

    <td width="10%" class="rightside">'.number_format($balance, 2).'</td>

  

   

    

   </tr>

   

  </table>





  </div>



  <div id="footer">

  <hr />

  <table width="100%">

  <tr>

  <td width="70%" class="rightside">Total : </td>

  <td width="10%" class="rightside">'.number_format($tot_qty, 2).'</td>

  <td width="10%" class="rightside">'.number_format($tot_rcvd, 2).'</td>

  <td width="10%" class="rightside">'.number_format($tot_balance, 2).'</td>

  </tr>

  </table>

   

  </div>



        ';



}

 $pdf = new Pdf();



 $file_name = 'po.pdf';

 $pdf->setPaper('A4', 'portrait');



 $pdf->loadHtml($html);

 $pdf->render();

 $pdf->stream($file_name, array("Attachment" => false));



?>