<?php

 date_default_timezone_set('Asia/Karachi');
 include('db_con.php');
 
 
 $sr_id = $_GET['id'];
 if($sr_id == 0)
 {
  $sql = "select max(sr_id) from sr";
  $statement = $connect->prepare($sql);
  $statement->execute();
  $sr_id = $statement->fetchColumn();
 }
 $tot_qty = 0;
 $tot_amt = 0;
 $tot_lqty = 0;
 $discount =0;
 $freight = 0;
 $sql = "select title_id from sr where sr_id = '".$sr_id."'";
 $statement = $connect->prepare($sql);
                      $statement->execute(); 
                      $title_id = $statement->fetchColumn();
$statement = $connect->prepare("select * from titles where  id= :title_id
    
  ");
 $statement->execute(
  array(  
   ':title_id'       =>  $title_id
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
  $heading1 = $row['heading1'];
  $heading2 = $row['heading2'];
  $heading3 = $row['heading3'];
 }
 

  $statement = $connect->prepare("select m.sr_id,m.inv_no,m.cust_id,m.chal_no,m.po_no,m.bilty_no,m.po_no,m.due_day,DATE_FORMAT(m.due_dated,'%d-%m-%Y') as due_dated,DATE_FORMAT(m.dated,'%d-%m-%Y') as dated,m.cur_stamp,m.description,m.inv_type, m.discount,m.discount_dsc, m.freight_dsc,m.cust_name,m.contact,m.freight,m.gst_pr,m.pre_balance,c.acc_name name,c.phone,o.user_name from sr m,user_detail o
    ,accounts c where o.user_id=m.user_id and c.acc_no=m.cust_id and
    m.sr_id= :sr_id LIMIT 1
    
  ");
 $statement->execute(
  array(  
   ':sr_id'       =>  $sr_id
  )
 );
 $result = $statement->fetchAll();

 
  ?>
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>Sale Return</title>
     <style>
    * {
      font-family: arial;
      font-size: 10px;
      padding: 1px;

    }
    
   
    .inv_title {
      font-size: 14px;
      text-align: center;
    }

    .main_header {
      text-align: center;
      
      line-height: 0px;
      margin-top: 20px;
      font-family: calibri;
    }

    .main_header p {
      text-align: center;
      padding: 1px;
          
      font-size: 8px;
    }

    .font-bold {
      font-weight: bold;
    }

    thead td {
      font-weight: bold;
      padding: 0;
      margin: 0;
       border-top: 2px solid black;
       border-bottom: 2px solid black;
    }
    tbody td {
      font-weight: bold;
      border-bottom: 1px solid black;
      padding: 1px;
     
    }
    .font-bold {
      font-weight: bold;
      font-size: 12px;
      

    }
    .rightside {
      text-align: right;
    }
    table.table_head td{
      border: none;
    }
    .smallfont {
      font-size: 10px;
      font-weight: bold;
    }
    .endline {
      border-top: 1px solid black;

    }

    h2 {
      font-size: 12px;
      font-weight: bold;
      border-bottom: 1px solid black;
      border-top: 1px solid black;
      padding: 5px;
      text-align: center;

    }


    .bottom-text {
      font-size: 12px;
      font-weight: bold;

    }

    .bottom_line {
      border-top: 1px solid black;
      right: 0;
    }
    .no-border {
      border: none;
    }
  </style>

  </head>
  <body onload="window.print();">
    <h2>Cash Sale Return</h2>
    <div class="main_header">
       <span class="inv_title"><?php echo $heading1;?></span>
      <p><?php echo $heading2;?></p>
      <p><?php echo $heading3;?></p>
    </div>
 <table width="100%" class="table_head">
  <?php 
  foreach($result as $row)
 {

 if($row['cust_id']> 1000)
  $contact = $row['contact'];
    $name = $row['cust_name'];
  $discount = $row['discount'];
  $freight = $row['freight'];
  $sr_id = $row['sr_id'];
  $cur_stamp = $row['cur_stamp'];
  
 echo '
 <table width="100%">
 <tr>
 <td colspan="2" width="30%" class="no-border"><b>Inv # :</b>'.$sr_id.'</td><td width="70%" colspan="2" class="rightside no-border"><b>Date:</b><span class="smallfont">'.$cur_stamp.'</span></td>
 </tr>
 </table>
 <table width="100%">
  <tr>
  <td width="100%" colspan"4" class="no-border"><b>M/S :</b> '.$name.'</td>
  </tr>
  <tr>
  <td width="100%" colspan="4" class="no-border"><b>Ph:</b>'.$contact.'</td>
  </tr>
  ';
}
  ?> 
</table>
  </table>
  <table width="100%" id="table_header">
  <thead>

  <tr>
  
  
  <td width="3%" class="font-bold">#</td>
  <td width="70%"class="font-bold">Description</td>
  
<td width="7%" class="font-bold rightside">Qty</td>
  <td width="8%" class="rightside font-bold">Rate</td> 
    <td width="12%" class="rightside font-bold">Amount</td> 
  
  </tr>
  
</thead>

<tbody>
<?php

$sql = "select d.item_no,CONCAT( t.type_name, ' - ' ,i.item_name) item_name,u.uom_name,d.ctn,d.qty,d.rate from items i,item_type t, sr_detail d,item_uom u where t.type_id=i.type_id  and d.item_no=i.item_id and i.uom=u.uom_id and
    d.sr_id= :sr_id";
$statement = $connect->prepare($sql);
$statement->execute(
  array(  
   ':sr_id'       =>  $sr_id
  )
 );
$result = $statement->fetchAll();
$count = 0;
foreach($result as $row)
 {

$amount = 0;

$tot_balance = 0;

  $qty = $row["qty"];
  
  
  $rate = $row["rate"];
   $count = $count +1;
  
  $amount = $qty * $rate;
  $tot_qty = $tot_qty + $qty;
  $tot_amt = $tot_amt + $amount;
  

  
echo '
  
<tr>
  
   <td  class="data-line">'.number_format($count, 0).'</td>
   <td   class="data-line">'.$row['item_name'].'</td>

<td  class="data-line rightside">'.number_format($qty, 0).'</td>
<td  class="data-line rightside">'.number_format($rate, 0).'</td>
<td   class="data-line rightside">'.number_format($amount, 0).'</td>
   </tr>
   ';
   
  
  
  

}
$tot_balance =  $tot_amt;

echo '
</tbody>

</table>

<table width="100%">
<tr>
<td class="rightside font-bold" >Gross Total : </td>
<td  class="total_line rightside small-font"></td>
<td class="rightside font-bold" ></td>
<td  class="total_line rightside small-font">'.number_format($tot_amt, 0).'</td>
  
</tr>
';
if($discount > 0)
{
  echo '<tr>
<td class="rightside font-bold">Discount : </td>
<td class="total_line rightside "></td>
<td  class="total_line rightside "></td>
<td  class="total_line rightside ">'.number_format($discount, 0).'</td>
  
</tr>';
}
if($freight > 0)
{
  echo '<tr>
<td  class="rightside font-bold" >Freight : </td>
<td  class="total_line rightside "></td>
<td  class="total_line rightside "></td>
<td  class="total_line rightside ">'.number_format($freight, 0).'</td>
  
</tr>';
}
if($discount > 0 || $freight > 0)
{
  $net_amt = $tot_amt - $discount + $freight;
  echo '<tr>
<td class="rightside font-bold">Net Total : </td>
<td  class="total_line rightside "></td>

<td  class="total_line rightside ">'.number_format($net_amt, 0).'</td>
  
</tr>';
}

?>

</tbody>
</table>
<div align="center" class="bottom_text">
<p style="font-size: 15px;font-weight:bold;" > بل کے بغیر کوئی واپسی یا تبدیلی نہیں۔ </p>
</div>

<hr class="bottom_line" />
</body>
</html>