<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 $fdate = $_GET['fdate'];

 $cust_id = $_GET['cust_id'];

 $tdate = $_GET['tdate'];

 $pay_status = $_GET['pay_status'];

 $sorted = $_GET['sorted'];







 if($cust_id == 'ALL')

 {

   header('location:rpt_rec_voucher_all.php?print=1&cust_id='.$cust_id.'&fdate='.$fdate.'&tdate='.$tdate.'&pay_status='.$pay_status.'&sorted='.$sorted.'');

 }











           $sql = "SELECT name FROM customers WHERE cust_id ='".$cust_id."' ";

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

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

           $cust_name = $statement->fetchColumn();



         

              

                $statement = $connect->prepare("

        select rec_id,chq_no,chq_date,description,pay_status,amount from rec_voucher where

        cust_id = :cust_id and dated between :fdate and :tdate and pay_status = :pay_status 

        order by chq_no");

       $statement->execute(

        array(

         ':cust_id'       =>  $cust_id,         

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate,

         ':pay_status'       =>  $pay_status

        )

       );



        if($pay_status =='ALL')

               {

                $statement = $connect->prepare("

        select rec_id,chq_no,chq_date,CONCAT(description,' - ',pay_status) as description,amount from rec_voucher where

        cust_id = :cust_id and dated between :fdate and :tdate order by chq_no");

       $statement->execute(

        array(

         ':cust_id'       =>  $cust_id,

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate

        )

       );

               }



               if($sorted == 1)

               {



                $statement = $connect->prepare("

        select rec_id,chq_no,chq_date,description,pay_status,amount from rec_voucher where

        cust_id = :cust_id and dated between :fdate and :tdate and pay_status = :pay_status 

        order by chq_date");

       $statement->execute(

        array(

         ':cust_id'       =>  $cust_id,         

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate,

         ':pay_status'       =>  $pay_status

        )

       );



        if($pay_status =='ALL')

               {

                $statement = $connect->prepare("

        select rec_id,chq_no,chq_date,CONCAT(description,' - ',pay_status) as description,amount from rec_voucher where

        cust_id = :cust_id and dated between :fdate and :tdate order by chq_date");

       $statement->execute(

        array(

         ':cust_id'       =>  $cust_id,

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate

        )

       );

               }





               }

              





              

  

 $result = $statement->fetchAll();



 

  





 



 $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, table {margin:0;font-size:12px;}

    .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>Cheques '.$pay_status.'</h3>

  <table width="100%">

  <tr>

  <td width="25%">ID :</td><td width="20%">'.$cust_id.'</td><td width="55%" class="rightside">From :</td><td width="20%" class="rightside">'.$fdate.'</td>

  </tr>

  <tr>

  <td width="10%">Name : </td><td width="70%" style="font-size:10px">'.$cust_name.'</td><td width="20%" class="rightside">To :</td><td width="20%" class="rightside">'.$tdate.'</td>

  </tr>

  <tr>

  <td width="10%"></td><td width="20%"></td><td width="70%" class="rightside"></td><td width="20%" class="rightside"></td>

  </tr>

  </table>

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



  <tr>

  <th width="7%">rec_id</th>

  <th width="10%">chq_no</th>

  <th width="10%">chq_date</th>

  <th width="61%">Description</th>

  

  <th width="10%">Amount</th>

 







  </tr>



  </table>



  </div>

  

';

foreach($result as $row)

 {

 

  $amount = $row["amount"];

$html .= '

  <div id="content">

  

  <table width="100%">

<tr>

  <td width="7%">'.$row["rec_id"].'</td>

<td width="10%">'.$row["chq_no"].'</td>

<td width="12%">'.$row["chq_date"].'</td>

<td width="61%">'.$row["description"].'</td>



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

  

   

    

   </tr>

   

  </table>





  </div>



  <div id="footer">

  <table width="100%">

  <tr>

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

  </tr>

  </table>

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

  </div>



        ';



}

 $pdf = new Pdf();



 $file_name = 'rec_voucher.pdf';

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



 $pdf->loadHtml($html);

 $pdf->render();

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



?>