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

//require_once 'pdf.php';
 $html = '';
 
 $acc_no = $_GET['acc_no'];
 
 $fdate = $_GET['fdate'];
 $tdate = $_GET['tdate'];
 
if($acc_no > 0)
{
 
 $sql = "SELECT acc_name from accounts where acc_no ='".$acc_no."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
           $acc_name = $statement->fetchColumn();
         
}else
{
  $acc_name = 'ALL';
}
  $sql = "select m.p_id, m.inv_no, m.dated, a.acc_name, m.gst_pr,m.fst_pr,m.ast_pr, m.description, m.discount, m.freight, sum(qty) qty, sum(d.qty*d.rate) amount from purchase_detail d, purchase m, accounts a where m.p_id=d.p_id and a.acc_no=m.v_id and m.gst='ON' and m.dated between '".$fdate."' and '".$tdate."' group by m.p_id order by m.p_id , dated asc ";
  if($acc_no > 0)
  {
   $sql = "select m.p_id, m.inv_no, m.dated, a.acc_name,  m.gst_pr,m.fst_pr,m.ast_pr, m.description, m.discount, m.freight, sum(qty) qty, sum(d.qty*d.rate) amount from purchase_detail d, purchase m, accounts a where m.p_id=d.p_id and m.v_id=a.acc_no and m.v_id = '".$acc_no."'  and m.gst='ON' and m.dated between '".$fdate."' and '".$tdate."' group by m.p_id order by m.p_id , dated asc ";
  }
 $statement = $connect->query($sql);
  
 
 $result = $statement->fetchAll();


 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>GST Purchase Summary</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>GST Purchase Summary</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>Account :</b></td><td><?php echo $acc_name;?>
</tr>
</table>
  <table width="100%">
  <thead>
  <tr class="head_row">
  
  <td width="3%">Sr</td>
  
<td width="3%">P_ID</td>
  <td width="5%">Inv No.</td>
  <td width="8%">Date</td>
<td width="18%">From/Vendor</td>
<td width="10%" >Description</td>
<td width="5%" class="rightside">Qty</td>
<td width="8%" class="rightside">Amount</td>
<td width="8%" class="rightside">GST Amount</td>
<td width="8%" class="rightside">Inv Amount</td>
<td width="8%" class="rightside">Discount</td>
<td width="8%" class="rightside">Freight</td>
<td width="8%" class="rightside">Inv Total</td>
  </tr>
  </thead>
  <tbody>
<?php
$tot_amount = 0;
$tot_discount = 0;
$tot_freight = 0;
$tot_gst_amount = 0;
$tot_fst_amount = 0;
$tot_ast_amount = 0;
$tot_inv_total = 0;
$count = 0;
$tot_qty = 0;
$tot_inv_amount = 0;
foreach($result as $row)
 {
  $gst_pr = $row["gst_pr"];
  $fst_pr = $row["fst_pr"];
  $ast_pr = $row['ast_pr'];
  $count++;
  $amount = $row["amount"];
  $discount = $row["discount"];
  $freight = $row["freight"];
  $qty = $row["qty"];
  $tot_qty = $tot_qty + $qty;

 
 if($gst_pr > 0){$gst_amount = $amount * $gst_pr / 100;}else{$gst_amount = 0;}
 if($ast_pr > 0){$ast_amount = $amount * $ast_pr / 100;}else{$ast_amount = 0;}
 if($fst_pr > 0){$fst_amount = $amount * $fst_pr / 100;}else{$fst_amount = 0;}
 $others = $freight + $ast_amount + $fst_amount;
 $inv_amount = $amount + $gst_amount;
  $tot_inv_amount = $tot_inv_amount + $inv_amount;
  $tot_discount = $tot_discount = $discount;
  $tot_freight = $tot_freight + $others;
  $tot_amount = $tot_amount + $amount;
  $tot_gst_amount = $tot_gst_amount + $gst_amount;
  $inv_total = $inv_amount + $gst_amount + $others;
  $tot_inv_total = $tot_inv_total + $inv_total;
echo ' 
  <tr>
  <td>'.$count.'</td>
  <td>'.$row["p_id"].'</td>
  <td>'.$row["inv_no"].'</td>
  <td>'.$row["dated"].'</td>
  <td>'.$row["acc_name"].'</td>
  <td>'.$row["description"].'</td>
  <td class="rightside">'.number_format($qty,0).'</td>
  <td class="rightside">'.number_format($amount,0).'</td>
  <td class="rightside">'.number_format($gst_amount,0).'</td>
  <td class="rightside">'.number_format($inv_amount,0).'</td>
  <td class="rightside">'.number_format($discount,0).'</td>
  <td class="rightside">'.number_format($freight,0).'</td>
  <td class="rightside">'.number_format($inv_total,0).'</td>
  </tr>
  ';
   
   
   
  }
  echo 
  '
    <tr class="total">
  <td colspan="6" class="rightside">Total:</td>

  <td class="rightside">'.number_format($qty,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_inv_amount,0).'</td>
  <td class="rightside">'.number_format($tot_discount,0).'</td>
  <td class="rightside">'.number_format($tot_freight,0).'</td>
  <td class="rightside">'.number_format($tot_inv_total,0).'</td>
  </tr>';
  
  ?>
  </tbody>
</table>

</body>
</html>