<?php

 date_default_timezone_set('Asia/Karachi');
 include('db_con.php');
 
 
 $s_id = $_GET['id'];
 if($s_id == 0)
 {
  $sql = "select max(s_id) from sale";
  $statement = $connect->prepare($sql);
  $statement->execute();
  $s_id = $statement->fetchColumn();
 }
 $sql = "select hs_code from sale where s_id= '".$s_id."' LIMIT 1";
  $statement = $connect->prepare($sql);
  $statement->execute();
  $hs_code = $statement->fetchColumn();
 $tot_qty = 0;
 $tot_amt = 0;
 $tot_lqty = 0;
 $discount =0;
 $freight = 0;
 $sql = "select title_id from sale where s_id = '".$s_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.s_id,m.inv_no,m.chal_no,m.po_no,dt_no,m.bilty_no,m.po_no,m.description,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.hs_code, m.pre_balance,c.name,c.phone,c.address,c.stn,c.ntn,o.user_name from sale m,user_detail o,customers c where o.user_id=m.user_id and c.cust_id=m.cust_id and 
    m.s_id= :s_id LIMIT 1
    
  ");
 $statement->execute(
  array(  
   ':s_id'       =>  $s_id
  )
 );
 $result = $statement->fetchAll();
 
 
  ?>
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>GST 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;
    border-bottom: 1px solid black;
    
    


    }

    .content , p {
    page-break-inside: avoid;
  }



    .rightside {
      text-align: right;
    }

    #data_table  {

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

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

    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;
  
}

.inv_title {
  width: 100%;

}
.comp_info {
  line-height: 16px;
  border-bottom: 1px solid black;
}
.address {
  font-size: 12px;
  text-align: left;
}
table.data_table td {
  border-bottom: 1px solid black;
  font-size: 14px;
  font-family: arial;
}
  </style>

  </head>
  <body onload="window.print();">
    <div class="content">
     <div align="center">

    <h2 class="inv_title">COMMERCIAL BILL</h2>
  </div>
  <div class="comp_info">
        
    <h3><?php echo $heading1;?></h3>
    <h6><?php echo $heading2;?></h6>
    <h6><?php echo $heading3;?></h6>
    <h6><?php echo $stn;?></h6>
    <h6><?php echo $ntn;?></h6>
  
  </div>
 <table width="100%" class="page_header">
  <?php 
  foreach($result as $row)
 {
  
  $gst_pr = $row['gst_pr'];

 if($row['inv_type']=='Cash')
  {
    $contact = $row['contact'];
    $name = $row['cust_name'];
  }else{$contact = $row['phone'];$name=$row['name'];}
  $discount = $row['discount'];
  $freight = $row['freight'];
  $s_id = $row['s_id'];
  $stn = $row['stn'];
  $ntn = $row['ntn'];
  $cur_stamp = $row['cur_stamp'];
  
 echo '
  <tr><td width="15%"><b>S.T Inv # :</b></td>
  <td width = "35%">'.$row["inv_no"].'</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>Date :</b></td>
  <td width = "35%">'.$row["dated"].'</td>
  <td width="15%" class="rightside"><b>STN :</b></td><td width = "35%" class="rightside">'.$stn.'</td>
  </tr>
  <tr><td width="15%"><b>M/s :</b></td>
  <td width = "35%">'.$name.'</td>
  <td width="15%" class="rightside"><b>NTN :</b></td><td width = "35%" class="rightside">'.$ntn.'</td>
  </tr>
  <tr><td width="15%"><b>Address :</b></td>
  <td width = "35%">'.$row["address"].'</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>DTRE # :</b></td>
  <td width = "35%"> '.$row["dt_no"].'</td>
  

  <td width="15%" class="rightside"><b>HS Code :</b></td>
  <td width = "35%" class="rightside"> '.$row["hs_code"].'</td>
  
  </tr>
  ';
}
  ?> 
  </table>
  <table width="100%" class="data_table">
  <thead>
  <tr> 
  
  <td width="5%" class="font-bold">#</td>
  
  <td width="30%"class="font-bold">Description</td>
  
  <td width="8%" class="font-bold rightside">UOM</td>
<td width="8%" class="font-bold rightside">Qty</td>
  <td width="10%" class="rightside font-bold">Rate</td> 
    <td width="12%" class="rightside font-bold">Ex. Amt</td> 
  <td width="12%" class="rightside font-bold">GST(<?php echo $gst_pr;?>%)</td> 
    <td width="15%" class="rightside font-bold">Inc. Amt</td> 
  
  </tr>
  
</thead>

<tbody>
<?php

$sql = "select d.item_no,t.type_name item_type,CONCAT(i.item_name,' ',t.type_name, ' ' ,b.brand_name, ' ', c.color_name) as item_name ,u.uom_name uom,d.item_detail,d.ctn,d.qty,d.rate from  items i,item_type t, sale_detail d,item_uom u,item_color c, item_brand b where t.type_id=i.type_id and d.item_no=i.item_id and b.brand_id=i.brand_id and i.uom=u.uom_id and c.color_id=i.color_id AND
    d.s_id= :s_id";
$statement = $connect->prepare($sql);
$statement->execute(
  array(  
   ':s_id'       =>  $s_id
  )
 );
$result = $statement->fetchAll();
$count = 0;
$tot_gst_rate = 0;
$tot_amount = 0;
foreach($result as $row)
 {

$amount = 0;


$tot_balance = 0;

  $qty = $row["qty"];
  $dis_pr = 0;
  
  $rate = $row["rate"];
   $count = $count +1;
  $dis_rate = $rate * $dis_pr / 100;
  $rate = $rate - $dis_rate;
  $gst_rate = $rate * $gst_pr / 100;
  $gst_rate = $gst_rate * $qty;
  $amount = $qty * $rate;
  $tot_amount = $tot_amount + $amount;
  $item_amount =  $amount + $gst_rate;
  $tot_qty = $tot_qty + $qty;
  $tot_amt = $tot_amt + $item_amount;
  $tot_gst_rate = $tot_gst_rate + $gst_rate;

  
echo '



<tr>
  
   <td class="small-font">'.number_format($count, 0).'</td>
   
   <td class="small-font">'.$row['item_detail'].' </td>
   
<td class="rightside">'.$row['uom'].'</td>
<td class="rightside">'.number_format($qty, 0).'</td>
<td class="rightside ">'.number_format($rate, 2).'</td>
<td class="rightside ">'.number_format($amount, 2).'</td>
<td class="rightside ">'.number_format($gst_rate, 2).'</td>
<td class="rightside ">'.number_format($item_amount, 2).'</td>
   </tr>
   '; 

}



$gst_amount = $tot_amt * $gst_pr / 100;

echo '
</tbody>
</table>
<footer>  

<table width="100%" class"footer_table">

<tr>
  
   <td width="43%" colspan="3" class="small-font rightside">Total:</td>
   
   
   

<td  width="8%" class="rightside">'.number_format($tot_qty, 0).'</td>
<td  width="10%" class="rightside "></td>
<td  width="12%" class="rightside ">'.number_format($tot_amount, 2).'</td>
<td  width="12%" class="rightside ">'.number_format($tot_gst_rate, 2).'</td>
<td  width="15%" class="rightside ">'.number_format($tot_amt, 2).'</td>
   </tr>
';




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

?>
</table>

</footer>
</body>
</html>