<?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();
}
?>
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>Receipt Vouchers Preview</title>
     <style>
    * {
      font-family: arial;
      font-size: 12px;
      padding: 2px;
      font-weight: bold;

    }
    h2 {
        font-size: 20px;
    }

    

    table {
      border-collapse: collapse;
    }
    thead td {
      font-weight: bold;
      padding: 3px;
      border: 2px solid black;
    
      background-color: #5bc0de;
    }
    tbody td {
      
      padding: 3px;
      border: 0.5px solid black;
    }
    .font-bold {
      font-weight: bold;
    }
    .rightside {
      text-align: right;
    }
   
  @page {
  margin: 1cm;
}

.primary {
  color: #0275d8;
}
   
  </style>

  </head>
  <body>
   <?php

 date_default_timezone_set('Asia/Karachi');

 
 $dated = date('Y-m-d h:i a');
 $fdate = $_GET['fdate'];

 $acc_no= $_GET['cust_id'];

 $sm_id= $_GET['sm_id'];

 $tdate = $_GET['tdate'];

 $pay_status = $_GET['pay_status'];

 $pay_type = $_GET['pay_type'];

 

 $sql = "select ifnull(sname,'ALL') from sales_man where sm_id= '".$sm_id."'";
 $statement = $connect->prepare($sql);
                      $statement->execute(); 
          $sm_name = $statement->fetchColumn();

 $sql = "select acc_name from accounts where acc_no= '".$acc_no."'";
 $statement = $connect->prepare($sql);
                      $statement->execute(); 
          $acc_name = $statement->fetchColumn();
          if($acc_name=='')
{$acc_name='ALL';} 

echo
'<h2>Receipt Vouchers</h2>
<h3>From : '.$fdate.'
To: '.$tdate.'</h3>
<table width="100%">

<tr>
<td>From :</td><td colspan="2">'.$acc_name.'</td><td>Paid By:'.$pay_type.'</td><td>Status: '.$pay_status.'
sales_man : '.$sm_name.'</td>
</tr>
</table>';
if($sm_id > 0)
{
  $statement = $connect->query("select p.rec_id,p.dated,a.acc_name,p.paid_by,p.description,p.chq_date, p.pay_status, p.amount from rec_voucher p, accounts a, customers c where p.cust_id=a.acc_no and concat(104,c.cust_id) = a.acc_no and c.sm_id = '".$sm_id."' and p.pay_status LIKE '%".$pay_status."%' and paid_by LIKE '%".$pay_type."%' and p.dated between '".$fdate."' and '".$tdate."'");
$result = $statement->fetchAll();
}
else
{


$statement = $connect->query("select p.rec_id,p.dated,a.acc_name,p.paid_by,p.description,p.chq_date, p.pay_status, p.amount from rec_voucher p, accounts a where p.cust_id=a.acc_no and p.cust_id LIKE '%".$acc_no."%' and p.pay_status LIKE '%".$pay_status."%' and paid_by LIKE '%".$pay_type."%' and p.dated between '".$fdate."' and '".$tdate."'");
$result = $statement->fetchAll();
}
  ?>
  <table width="100%" id="table_header">
  <thead>

  <tr>
  
  
  <td width="3%">#</td>
  <td width="7%">ID</td>
  <td width="10%">Date</td>
  <td width="25%">From | Customer</td>
<td width="15%">Description</td>
  <td width="10%">Paid By</td>
  <td width="10%">Chq Date</td> 
    <td width="10%">Status</td> 
    <td width="10%" class="rightside">Amount</td> 
  
  </tr>
  
</thead>

<tbody>

    <?php 
$count= 0;
$tot_amount = 0;
    foreach ($result as $row) {
$amount = $row['amount'];
$tot_amount = $tot_amount + $amount;
$count++;
        echo'
  <tr>
  
  
  <td>'.$count.'</td>
  <td>'.$row['rec_id'].'</td>
  <td>'.$row['dated'].'</td>
  <td>'.$row['acc_name'].'</td>
<td>'.$row['description'].'</td>
  <td>'.$row['paid_by'].'</td>
  <td>'.$row['chq_date'].'</td> 
    <td>'.$row['pay_status'].'</td> 
    <td class="rightside">'.number_format($amount,0).'</td> 
  
  </tr>';
        
    }
    echo '
    <tr>
  
  
 
    <td colspan="8" class="rightside">Total : </td> 
    <td class="rightside">'.number_format($tot_amount,0).'</td> 
  
  </tr>';

    ?>

</tbody>
</table>
</body>
</html>