<?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;
if($sm_id > 0)
{
 
 $sql = "SELECT sname FROM sales_man where sm_id ='".$sm_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
           $sm_name = $statement->fetchColumn();
         
}else
{
  $sm_name = 'ALL';
}
  $sql = "select m.s_id,m.inv_no,m.dated,c.name,m.description, sum(d.qty*d.rate - d.qty*d.dis_rate - d.rate * d.dis_pr / 100 * d.qty)   + sum(d.qty*d.rate - d.qty*d.dis_rate - d.rate * d.dis_pr / 100 * d.qty)  * gst_pr / 100 as amount from sale m, sale_detail d, customers c where m.s_id = d.s_id  and m.cust_id=c.cust_id AND c.sm_id LIKE '%".$sm_id."%' and m.dated between '".$fdate."' and '".$tdate."' group by m.s_id order by dated,s_id";
   $statement = $connect->prepare($sql);
 $statement->execute();
  
 
 $result = $statement->fetchAll();


 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>Sales By SalesMan</title>
  <style>
    * {
      font-family: arial;
      font-size:16px;
    }
    @media print {
  @page { margin: 0; }
  body { margin: 1cm; }
}
  .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>Sales By SalesMan</b></td>
</tr>
<tr>
<td>From : <?php echo $fdate;?></td><td>To : <?php echo $tdate;?></td>
</tr>
<tr>
<td>Sales Man :</td><td><?php echo $sm_name;?>
</tr>
</table>
  <table width="100%">
  <thead>
  <tr class="head_row">
  
  <td width="5%">Sr</td>
  
<td width="10%">S_ID</td>
  <td width="10%">Inv No.</td>
  <td width="12%">Date</td>
<td width="25%">Customer</td>
<td width="25%" >Description</td>
<td width="13%" class="rightside">Amount</td>
  </tr>
  </thead>
  <tbody>
<?php
foreach($result as $row)
 {
  $count++;
  $amount = $row["amount"];
  $s_id = $row["s_id"];
   $sql = "select m.freight from  sale m where  m.s_id = '".$s_id."'";
  

   $statement = $connect->prepare($sql);
 $statement->execute();
 $freight = $statement->fetchColumn();

 $sql = "select m.discount from  sale m where m.s_id = '".$s_id."'";
  

   $statement = $connect->prepare($sql);
 $statement->execute();
 $discount = $statement->fetchColumn();  
 $amount = $amount - $discount + $freight;
  $tot_amount = $tot_amount + $amount;
echo ' 
  
<tr>
<td width="5%">'.$count.'</td>  
<td width="10%">'.$row["s_id"].'</td>
<td width="10%">'.$row["inv_no"].'</td>
<td width="12%">'.$row["dated"].'</td>
<td width="25%">'.$row["name"].'</td>
<td width="25%">'.$row["description"].' Discount : '.$discount.' Freight :'.$freight.'</td>
<td width="13%" class="rightside">'.number_format($amount, 2).'</td>
  
   
    
   </tr>
  ';
   
   
   
  }
  $tot_amount = $tot_amount - $discount + $freight;
  ?>
  </tbody>
</table>
<table width="100%">
<tr class="total">
  <td colspan="6" width="87%" class="rightside total"><b>Total : </b></td>
  
  <td width="13%" class="rightside total"><b><?php echo $tot_amount;?></b></td>
</tr>
  </table>
</body>
</html>