<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 

  $so_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.cust_id,m.dated,m.description,LEFT(c.name,35) as c_name,

  m.status FROM customers c,so m   WHERE m.cust_id=c.cust_id and m.so_no=:so_no");

 $statement->execute(

  array(

   

   ':so_no'       =>  $so_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>Sale Order</h3>

  <table width="100%">

  <tr>

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

  </tr>

  <tr>

  <td width="15%">Customer : </td><td  width="55%" style="font-size:12px">'.$row["c_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 so_detail d, so m, items i where m.so_no=d.so_no  and i.item_no = d.item_no and m.so_no = :so_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 sale_detail d, sale m,items i where m.s_id=d.s_id and i.item_no=d.item_no and m.so_no = :so_no group by item_no,item_name

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

 $statement->execute(

  array(

   

   ':so_no'       =>  $so_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 = 'so'.$so_no.'.pdf';

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



 $pdf->loadHtml($html);

 $pdf->render();

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



?>