<?php



require_once 'pdf.php';

 

 include('db_con.php');
if (isset($_SESSION['user_id'])) {
        $user_type = $_SESSION['user_type'];
        $user_id = $_SESSION['user_id'];
        $user_name = $_SESSION['user_name'];
        $branch_id = $_SESSION['branch_id'];
        $branch_name = $_SESSION['branch_name'];
        $store_id = $_SESSION['store_id'];
        $store_name = $_SESSION['store_name'];
        $user_acc_no = $_SESSION['user_acc_no'];
}else{
    header('Location: login');
    die();
}
 $html = '';

 $fdate = $_GET['fdate'];

 $cust_id = $_GET['cust_id'];

 $tdate = $_GET['tdate'];

 $pay_status = $_GET['pay_status'];

 $sorted = $_GET['sorted'];









                $statement = $connect->prepare("

        select r.rec_id,r.chq_no,LEFT(c.name, 24) as name,r.chq_bank,LEFT(r.chq_bank, 20) as chq_bank,r.description,r.pay_status,r.amount from

         rec_voucher r, customers c where c.cust_id=r.cust_id 

        and r.chq_date between :fdate and :tdate and pay_status = :pay_status 

        order by chq_no");

       $statement->execute(

        array(                  

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate,         

         ':pay_status'     =>  $pay_status

        )

       );



        if($pay_status =='ALL')

               {

                $statement = $connect->prepare("

        select r.rec_id,r.chq_no,LEFT(c.name, 24) as name,r.chq_bank,LEFT(r.chq_bank, 20) as chq_bank,CONCAT(r.description,' - ',r.pay_status)

        as description, r.amount from

         rec_voucher r, customers c where c.cust_id=r.cust_id 

        and r.chq_date between :fdate and :tdate

        order by chq_no");

       $statement->execute(

        array(         

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate         

        

        )

       );

               }            







         



          

        if($sorted == 1)

        {

          $statement = $connect->prepare("

        select r.rec_id,r.chq_no,r.chq_date,LEFT(c.name, 24) as name,LEFT(r.chq_bank, 15) as chq_bank,r.chq_date,r.description,r.pay_status,r.amount from

         rec_voucher r, customers c where c.cust_id=r.cust_id 

        and r.chq_date between :fdate and :tdate and pay_status = :pay_status 

        order by chq_date");

       $statement->execute(

        array(                  

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate,         

         ':pay_status'     =>  $pay_status

        )

       );



        if($pay_status =='ALL')

               {

                $statement = $connect->prepare("

        select r.rec_id,r.chq_no,r.chq_date,LEFT(c.name, 24) as name,LEFT(r.chq_bank, 20) as chq_bank,r.chq_date,CONCAT(r.description,' - ',r.pay_status)

        as description, r.amount from

         rec_voucher r, customers c where c.cust_id=r.cust_id 

        and r.chq_date between :fdate and :tdate

        order by chq_date");

       $statement->execute(

        array(         

         ':fdate'       =>  $fdate,

         ':tdate'       =>  $tdate

         

        

        )

       );

               }

        }



         

  

 $result = $statement->fetchAll();

 



 $html .=' <style>

    @page { margin:80px 20px; }

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

    #footer { position: fixed; left: 0px; bottom: -60px; right: 0px; height: 60px; 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">



  

  <table width="100%">

  <tr>

  <td width="40%">Cheques Receivable '.$pay_status.'</td><td width="10%"></td>

  <td width="5%" class="rightside">From :</td><td width="12%" class="rightside">'.$fdate.'</td>

  <td width="10%"></td><td width="8%" class="rightside">To :</td><td width="15%" class="rightside">'.$tdate.'</td>

  </tr>

  <tr>

  

  </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="8%">rec_id</th>

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

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

  <th width="35%">From | Customer</th>

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

 

  

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

 







  </tr>



  </table>



  </div>

  

';

foreach($result as $row)

 {

 

  $amount = $row["amount"];

$html .= '

  <div id="content">

  

  <table width="100%" cellpadding="0" cellspacing="0">

<tr>

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

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

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

<td width= "35%">'.$row["name"].'</td>

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



   <td width="10%" class="rightside">'.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>



        ';



}

$html .='<hr />';

 $pdf = new Pdf();



 $file_name = 'rec_voucher.pdf';

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



 $pdf->loadHtml($html);

 $pdf->render();

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



?>