<?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'];
  $stn = $row['heading4'];
  $ntn = $row['heading5'];
 }
 

  $statement = $connect->prepare("select m.sr_id,m.inv_no,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,a.acc_name,c.phone,o.user_name from sr m,user_detail o,
    accounts a,customers c where o.user_id=m.user_id and a.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 Invoice</title>
     <style>
        * {
          padding: 0;
          margin: 0;
          font-family: arial;
          font-weight: bold;

        }
    #rpt_header {
     width:100%;
    margin:0 auto;
    display:inline-block;
    text-align:center;
    }

    .content , p {
    page-break-inside: avoid;
  }
.column {
  float: left;
  width: 33.33%;
}


.row:after {
  content: "";
  display: table;
  clear: both;
  line-height: 0;
}

.column {
  float: left;
  line-height: 15px;
}

.left, .right {
  width: 25%;
}

.middle {
  width: 50%;
}

.comp {
  
  font-size: 20px;
  padding: 10px;
  
  font-family: Times New Roman;
  font-weight: bold;
}
.doc_type {
  float: right;
}
.address {
  width: 100%;
  text-align: center;
  line-height: 15px;
  
  font-family: arial;
  font-size: 12px;
}

    .rightside {
      text-align: right;
    }

    #data_table  {

    }
    thead td {
      font-weight: bold;
      font-size: 15px;
      border-bottom: 2px solid black;
      border-top: 2px solid black;  
      padding: 4px;
      font-family: arial;    
    }

    tbody td {
      font-size: 14px;
      font-weight: bold;
    }

    footer {
  
}

@media print {
  footer {
    position: fixed;
    bottom: 60px;
    border-top: 2px solid black;
    width: 100%;
  }


}
.signtable {
  margin-top: 40px;
  text-align: center;
}
.topline {
  border-top: 1px solid black;
  
}
  </style>

  </head>
  <body onload="window.print();">
    <div class="content">
      <div id="rpt_header">
          
       <div class="row">
          <div class="column doc_type">BILL Return</div>
          <div class="column logo"><img style="margin-top: 10px;float: left;width:120px;height:40px" src="images/final_logo.png"><br/><br/><br /></div>
          <div class="column comp"><?php echo $heading1;?></div>
          
             
      </div>
      <p class="address"><?php echo $heading2;?></p>  
      <p class="address"><?php echo $heading3;?></p>   
    

      </div>
    
 <table width="100%" class="page_header">
  <?php 
  foreach($result as $row)
 {

 if($row['inv_type']=='Cash')
  {
    $contact = $row['contact'];
    $name = $row['cust_name'];
  }else{$contact = $row['phone'];$name=$row['acc_name'];}
  $discount = $row['discount'];
  $freight = $row['freight'];
  $sr_id = $row['sr_id'];
  $cur_stamp = $row['cur_stamp'];
  
 echo '
  <tr><td width="15%"><b>Inv # :</b></td>
  <td width = "35%">'.$sr_id.'</td>
  <td width="15%" class="rightside"><b>DC# :</b></td><td width = "35%" class="rightside">'.$row["chal_no"].'</td>
  </tr>
  <tr><td width="15%"><b>Date :</b></td>
  <td width = "35%">'.$cur_stamp.'</td>
  <td width="15%" class="rightside"><b>PO# :</b></td><td width = "35%" class="rightside">'.$row["po_no"].'</td>
  </tr>
  <tr><td width="15%"><b>M/s :</b></td>
  <td width = "35%">'.$name.'</td>
  <td width="15%" class="rightside"><b>Phone :</b></td><td width = "35%" class="rightside">'.$row["contact"].'</td>
  </tr>
  ';
}
  ?> 
  </table>
  <table width="100%" id="data_table">
  <thead>

  <tr>
  
  
  <td width="5%" class="font-bold">#</td>
  <td width="50%"class="font-bold">Description</td>
  <td width="10%" class="font-bold rightside">UOM</td>
<td width="10%" class="font-bold rightside">Qty</td>
  <td width="10%" class="rightside font-bold">Rate</td> 
    <td width="15%" 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 uom,d.ctn,d.qty,d.rate, d.dis_pr 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"];
  $dis_pr = $row["dis_pr"];
  
  
  $rate = $row["rate"];
   $count = $count +1;
    $dis_rate = $rate * $dis_pr / 100;
    $rate = $rate - $dis_rate;
  
  $amount = $qty * $rate;
  $tot_qty = $tot_qty + $qty;
  $tot_amt = $tot_amt + $amount;
  

  
echo '



<tr>
  
   <td width="5%" class="small-font">'.number_format($count, 0).'</td>
   <td width="50%" class="small-font">'.$row['item_name'].'</td>
<td width="10%" class="rightside">'.$row['uom'].'</td>
<td width="10%" class="rightside">'.number_format($qty, 0).'</td>
<td width="10%" class="rightside ">'.number_format($rate, 0).'</td>
<td width="15%" class="rightside ">'.number_format($amount, 0).'</td>
   </tr>
   ';
   
  
  
  

}
$tot_balance =  $tot_amt;

echo '
<tbody>
</table>
<footer>  
<table width="100%" class"footer_table">

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

}
echo '
<table width="100%" class="signtable">
<tr>
</td><td class="small-font topline" width="33%">Prepared By</td>
</td><td class="small-font" width="33%"></td>
<td width="34%" class="topline">Received By</td>
</tr>
</table>';

?>
</table>

</footer>



</body>
</html>