<?php
 
 include('db_con.php');

//require_once 'pdf.php';
 $html = '';
 
 $fdate = $_GET['fdate'];
 $tdate = $_GET['tdate'];
 $category_id = $_GET['category_id'];

 if($category_id > 0)
 {
  $sql = "select category_name from item_category where category_id = '".$category_id."'";
  $statement = $connect->query($sql);
  $category_name = $statement->fetchColumn();
 }else{$category_name = 'ALL';}

 if($category_id > 0)
 {
  $sql = "select  concat(t.type_name , ' ' , i.item_name) description, m.gst_pr, d.qty, d.rate from purchase_detail d, purchase m, items i, item_type t  where m.p_id=d.p_id and t.type_id = i.type_id and i.item_id = d.item_no and i.category_id = '".$category_id."' and m.dated between '".$fdate."' and '".$tdate."'  order by i.item_name asc ";
 }else{
  $sql = "select  concat(t.type_name , ' ' , i.item_name) description, m.gst_pr, d.qty, d.rate from purchase_detail d, purchase m, items i, item_type t  where m.p_id=d.p_id and t.type_id = i.type_id and i.item_id = d.item_no and m.dated between '".$fdate."' and '".$tdate."'  order by i.item_name asc ";
 }
 
  
 $statement = $connect->query($sql);
  
 
 $result = $statement->fetchAll();


 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>Purchase Detail</title>
  <style>
    * {
      font-family: calibri;
      padding: 3px;
      line-height: 12px;
     
    }
    @media print {
  @page { margin: 0;size:landscape; }
  body { margin: 1cm; }
}
  .rightside {text-align: right;}
  table td{   
    border-bottom: 1px solid gray;
    font-size: 12px;
  }
  .head_row {
    font-weight: bold;
  }
  thead td {
    font-weight: bold;
     border-bottom: 2px solid gray;
      border-top: 2px solid gray;
       background-color :  #f3f3f3;
  }
.total {
  font-weight: bold;
}
  </style>

</head>
<body>
  <h3>Purchase Detail</h3>
<table width="100%">

<tr>
<td><b>From : </b><?php echo $fdate;?></td><td><b>To :</b> <?php echo $tdate;?></td>
</tr>
<tr>
<td><b>Category : </b></td><td> <?php echo $category_name;?></td>
</tr>

</table>
  <table width="100%">
  <thead>
  <tr class="head_row">
  
  <td width="5%">Sr</td>
<td width="50%" >Description</td>
<td width="5%" class="rightside">Qty</td>
<td width="10%" class="rightside">Rate</td>
<td width="10%" class="rightside">Amount</td>
<td width="10%" class="rightside">GST</td>
<td width="10%" class="rightside">Item Amount</td>

  </tr>
  </thead>
  <tbody>
<?php
$tot_amount = 0;
$tot_discount = 0;
$tot_freight = 0;
$count = 0;
$tot_qty = 0;
$tot_item_amount = 0;
$tot_gst_amount = 0;

foreach($result as $row)
 {
 
  $count++;
  
  $qty = $row["qty"];
  $rate = $row["rate"];
  $amount = $qty * $rate;
  $gst_pr = $row["gst_pr"];
  $gst_amount = $amount * $gst_pr / 100;
  $tot_qty = $tot_qty + $qty;
  $item_amount = $amount + $gst_amount;

  $tot_amount = $tot_amount + $amount;
  $tot_gst_amount = $tot_gst_amount + $gst_amount;
  $tot_item_amount = $tot_item_amount + $item_amount;
 
echo ' 
  <tr>
  <td>'.$count.'</td> 
  
  <td>'.$row["description"].'</td>
  <td class="rightside">'.number_format($qty,0).'</td>
  <td class="rightside">'.number_format($rate,0).'</td>
  <td class="rightside">'.number_format($amount,0).'</td>
  <td class="rightside">'.number_format($gst_amount,0).'</td>
  <td class="rightside">'.number_format($item_amount,0).'</td>
  
  </tr>
  ';
   
   
   
  }
  echo 
  '
   <tr class="total">
  <td colspan="2" class="rightside">TOTAL:</td> 
  
 
  <td class="rightside">'.number_format($qty,0).'</td>
  <td class="rightside">'.number_format($rate,0).'</td>
  <td class="rightside">'.number_format($tot_amount,0).'</td>
  <td class="rightside">'.number_format($tot_gst_amount,0).'</td>
  <td class="rightside">'.number_format($tot_item_amount,0).'</td>
  
  </tr>';
  
  ?>
  </tbody>
</table>

</body>
</html>