<?php
 
 include('db_con.php');
 include('function.php');
//require_once 'pdf.php';
 $html = '';
 
 $sm_id = $_GET['sm_id'];
 
 $fdate = $_GET['fdate'];
 $tdate = $_GET['tdate'];
 
 //$store_id = $_Get['fitem_no'];
// $store_id = $_Get['titem_no'];
 
$sm_name = 'ALL';
$count = 0;
$tot_qty = 0;
$tot_amount = 0;
$tot_gst_amount = 0;
$discount = 0;
$freight = 0;
$tot_final_amount = 0;


if($sm_id > 0)
{
 
 $sql = "SELECT sname FROM sales_man where sm_id ='".$sm_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute();
           $sm_name = $statement->fetchColumn();
         
}else
{
  $sm_name = 'ALL';
}
  $sql = "select m.s_id,m.inv_no,m.dated,c.name,m.discount,m.gst_pr,m.freight,sum(d.qty) qty,sum(d.qty * d.rate) - sum(d.qty * d.rate * d.dis_pr / 100) amount from sale m,sale_detail d, customers c where m.s_id=d.s_id and m.cust_id=c.cust_id and m.gst='ON' and  m.dated between '".$fdate."' and '".$tdate."' group by s_id order by inv_no,dated desc";
   $statement = $connect->prepare($sql);
 $statement->execute();
  
 
 $result = $statement->fetchAll();


 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>Sales By SalesMan</title>
  <style>
    * {
      font-family: arial;
      font-size:10px;
    }
    @media print {
  @page { margin: 0; }
  body { margin: 0.5cm; }
}
  .rightside {text-align: right;}
  table td{   
    border-bottom: 1px solid gray;
  }
  .head_row {
    font-weight: bold;
  }
  thead td {
    font-weight: bold;
     border-bottom: 2px solid gray;
      border-top: 2px solid gray;
       background-color: :  gray;
  }
  .total {
    font-weight: bold;
     border-bottom: 1px solid gray;
      border-top: 1px solid gray;
     
  }
  </style>
</head>
<body>
<table width="100%">
<tr>
<td colspan="2"><b>GST Invoices</b></td>
</tr>
<tr>
<td>From : <?php echo $fdate;?></td><td>To : <?php echo $tdate;?></td>
</tr>
<tr>
<td></td><td>
</tr>
</table>
  <table width="100%">
  <thead>
  <tr class="head_row">
  
  <td width="3%">Sr</td>
  
<td width="5%">S_ID</td>
  <td width="10%">Inv No.</td>
  <td width="10%">Date</td>
<td width="30%">Customer</td>
<td width="7%" class="rightside">Qty</td>
<td width="10%" class="rightside">Amount</td>
<td width="10%" class="rightside">Gst Amount</td>
<td width="15%" class="rightside">Total Amount</td>
  </tr>
  </thead>
  <tbody>
<?php
foreach($result as $row)
 {
  $count++;
  $amount = $row["amount"];
  $qty = $row['qty'];
  $gst_pr = $row['gst_pr'];
  $discount = $row['discount'];
  $freight = $row['freight'];
 $amount = $amount - $discount + $freight;

 $gst_amount = $amount * $gst_pr / 100;

 $final_amount = $amount + $gst_amount;
 
 $tot_qty = $tot_qty + $qty;

 $tot_final_amount= $tot_final_amount + $final_amount;

 $tot_amount = $tot_amount + $amount;

 $tot_gst_amount = $tot_gst_amount + $gst_amount;

 
  
echo ' 
  
<tr>
<td>'.$count.'</td>  
<td>'.$row["s_id"].'</td>
<td>'.$row["inv_no"].'</td>
<td>'.$row["dated"].'</td>
<td>'.$row["name"].'</td>
<td class="rightside">'.number_format($qty, 2).'</td>
<td class="rightside">'.number_format($amount, 2).'</td>
<td class="rightside">'.number_format($gst_amount, 2).'</td>
<td class="rightside">'.number_format($final_amount, 2).'</td>
  
   
    
   </tr>
  ';
   
   
   
  }
  
  ?>
  <tr class="total">
  <td  colspan="5" class="rightside total"><b>Total : </b></td>
  
  <td class="rightside total"><b><?php echo number_format($tot_qty,2);?></b></td>
  <td class="rightside total"><b><?php echo number_format($tot_amount,2);?></b></td>
  <td class="rightside total"><b><?php echo number_format($tot_gst_amount,2);?></b></td>
  <td class="rightside total"><b><?php echo number_format($tot_final_amount,2);?></b></td>
</tr>
  </tbody>

</body>
</html>