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

//require_once 'pdf.php';
 $html = '';
 
 $acc_no = $_GET['acc_no'];
 
 $fdate = $_GET['fdate'];
 $tdate = $_GET['tdate'];
 
 
 //$store_id = $_Get['fitem_no'];
// $store_id = $_Get['titem_no'];
 
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.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.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.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.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>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>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="5%">Sr</td>
  
<td width="5%">P_ID</td>
  <td width="5%">Inv No.</td>
  <td width="10%">Date</td>
<td width="25%">From/Vendor</td>
<td width="11%" >Description</td>
<td width="5%" class="rightside">Qty</td>
<td width="8%" class="rightside">Amount</td>
<td width="8%" class="rightside">Discount</td>
<td width="8%" class="rightside">Freight</td>
<td width="10%" class="rightside">Inv Total</td>
  </tr>
  </thead>
  <tbody>
<?php
$tot_amount = 0;
$tot_discount = 0;
$tot_freight = 0;
$count = 0;
$tot_qty = 0;
$tot_inv_amount = 0;
foreach($result as $row)
 {
  $count++;
  $amount = $row["amount"];
  $discount = $row["discount"];
  $freight = $row["freight"];
  $qty = $row["qty"];
  $tot_qty = $tot_qty + $qty;
 $inv_amount = $amount - $discount + $freight;
  $tot_inv_amount = $tot_inv_amount + $inv_amount;
  $tot_discount = $tot_discount = $discount;
  $tot_freight = $tot_freight + $freight;
  $tot_amount = $tot_amount + $amount;
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($discount,0).'</td>
  <td class="rightside">'.number_format($freight,0).'</td>
  <td class="rightside">'.number_format($inv_amount,0).'</td>
  </tr>
  ';
   
   
   
  }
  echo 
  '
    <tr class="total">
  <td colspan="6" class="rightside">Total:</td>

  <td class="rightside">'.number_format($tot_qty,0).'</td>
  <td class="rightside">'.number_format($tot_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_amount,0).'</td>
  </tr>';
  
  ?>
  </tbody>
</table>

</body>
</html>