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

//require_once 'pdf.php';
 $html = '';
 
 $acc_no = $_GET['acc_no'];
 
 $fdate = $_GET['fdate'];
 $tdate = $_GET['tdate'];
 $inv_type = $_GET['inv_type'];
 
 if($inv_type == 'GST')
 {
  header('location:rpt_gsales_summary_preview?print=1&acc_no='.$acc_no.'&fdate='.$fdate.'&tdate='.$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.s_id, m.cust_id,m.inv_no, m.dated, m.cust_name,m.contact, a.acc_name, m.description, m.discount, m.freight, sum(qty) qty, sum(d.qty*d.rate) amount from sale_detail d, sale m, accounts a where m.s_id=d.s_id and a.acc_no=m.cust_id and m.inv_type LIKE '%".$inv_type."' and m.dated between '".$fdate."' and '".$tdate."' group by m.s_id order by m.s_id , dated asc ";
  if($acc_no > 0)
  {
   $sql = "select m.s_id, m.inv_no, m.dated,  m.cust_name,m.contact, a.acc_name, m.description, m.discount, m.freight, sum(qty) qty, sum(d.qty*d.rate) amount from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.cust_id = '".$acc_no."'  and m.inv_type LIKE '%".$inv_type."' and m.dated between '".$fdate."' and '".$tdate."' group by m.s_id order by m.s_id , dated asc ";
  }
 $statement = $connect->query($sql);
  
 
 $result = $statement->fetchAll();


 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>Sales 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><?php echo $inv_type;?> Sales 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%">S_ID</td>
  <td width="5%">Inv No.</td>
  <td width="10%">Date</td>
<td width="25%">To/Customer</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)
 {
  if($row["cust_id"] == 1011000){$name = $row["cust_name"] . ' - '. $row["contact"];}else{$name = $row["acc_name"];}
  $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["s_id"].'</td>
  <td>'.$row["inv_no"].'</td>
  <td>'.$row["dated"].'</td>
  <td>'.$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>