<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 

 $sr_id = $_GET['id'];







           $sql = "SELECT sum(qty) FROM sr_detail WHERE sr_id ='".$sr_id."'";

            $statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

           $tot_qty = $statement->fetchColumn();



            $sql = "SELECT sum(qty*rate) FROM sr_detail WHERE sr_id ='".$sr_id."'";

            $statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

           $tot_amt = $statement->fetchColumn();









  $statement = $connect->prepare("

    select m.sr_id,m.dated,m.cust_id,LEFT(c.name,27)as name,LEFT(m.description,45) as description,m.inv_type,

    i.item_name,d.qty,d.rate,(d.qty*d.rate) as amount from sr m,sr_detail d, items i,customers c

    where m.sr_id=d.sr_id and d.item_no=i.item_no and m.cust_id=c.cust_id and m.sr_id = :sr_id

  ");

 $statement->execute(

  array(  

   ':sr_id'       =>  $sr_id

  )

 );

 $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;}

    .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;

    

  }

  .smallfont{

      font-size:11px;

    }

  </style>



  <div id="header">



  <h3>Wholesale Return</h3>

  <table width="100%">

  <tr>

  <td width="7.5%">Cust ID:</td><td width = "7.5%">'.$row["cust_id"].'</td><td width="12%">Inv No.:</td><td width = "8%">'.$row["sr_id"].'</td><td width="8%">Date :</td><td width="20%" class="rightside">'.$row["dated"].'</td><td width="15%" class="rightside">Inv Type :</td><td width="22%"class="rightside">'.$row["inv_type"].'</td>

  </tr>



  <tr>

  <td width="15%">Customer :</td><td width = "30%"  class="smallfont">'.$row["name"].'</td><td width="8%">Desc:</td><td width="47%" colspan="5" class="smallfont">'.$row["description"].'</td>

  </tr>

  

  

  </table>

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



  <tr>

  <th width="5%">Sr</th>

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

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

  <th width="15%"class="rightside">Rate</th>

  <th width="20%" class="rightside">Amount</th>

 







  </tr>



  </table>



  </div>



  

';



}

$count = 0;

foreach($result as $row)

 {

  $qty = $row["qty"];

  $rate = $row["rate"];

   $count = $count +1;

  

  $amount = $row["amount"];

$html .= '

  <div id="content">

  

  <table width="100%">

<tr>

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

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



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

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

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

  

   

    

   </tr>

   

  </table>





  </div>





  <div id="footer">

  <hr />

  <table width="100%">

  <tr>

  <td width="70%" class="rightside"> </td><td width="30%" class="rightside"></td>

  </tr>

  </table>

    <span class="page"></span>

  </div>

 

        ';



}



$html .='<hr style= "height:1px; " />



<table width="100%">

<tr>

<td class="rightside" width="55%">Total :</td><td class ="rightside" width="10%">'.$tot_qty.'</td>

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



</tr>



</table>



';

 $pdf = new Pdf();



 $file_name = 'Sale_Return.pdf';

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



 $pdf->loadHtml($html);

 $pdf->render();

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



?>