<?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 items 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,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.pre_balance,a.acc_name,c.phone,c.stn,c.ntn,o.user_name from sale m,user_detail o,
    accounts a,customers c where o.user_id=m.user_id and a.acc_no=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>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;
    border-bottom: 1px solid black;
    
    


    }

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


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

.column {
  float: left;
  
  margin-top: 20px;
}

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

.middle {
  width: 50%;
}

.comp {
  
  font-size: 17px;
  padding: 0;
  
  font-family: Times New Roman;
  font-weight: bold;

}
.doc_type {
  float: right;
}
.address {
  width: 25%;
  text-align: left;
  
  font-family: arial;
  font-size: 12px;
  float: right;
  margin-top: 20px;
}
.comp_info {
  width: 25%;
  text-align: left;
    
  font-family: arial;
  font-size: 12px;
  float: left;
  
}

    .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;
    
      padding: 2px;
    }

    footer {
  
}

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


}

#rpt_header h2 {
  font-size: 15px;
  border-top: 2px solid black;
  border-bottom: 2px solid black;
  padding: 5px;
}
.auth {
  font-size: 14px;
}
.signtable {
  margin-top: 40px;
  text-align: center;
}
.topline {
  border-top: 1px solid black;
  
}
.cat_font {
  text-align: right;


}
table.data_table td {
  border-bottom: 1px solid black;
  font-size: 12px;
  font-family: arial;
}
  </style>

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

          
          </div>
          
          
             
      </div>
     
      <div class="comp_info">
          

          
          </div>
      </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['acc_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>DC# :</b></td><td width = "35%" class="rightside">'.$row["chal_no"].'</td>
  </tr>
  <tr><td width="15%"><b>Date :</b></td>
  <td width = "35%">'.$row["dated"].'</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%" class="data_table">
  <thead>
  <tr> 
  
  <td width="3%" class="font-bold">#</td>
  <td width="10%"class="font-bold">Type</td>
  <td colspan="2" width="40%"class="font-bold">Description</td>
  
  <td width="7%" class="font-bold rightside">UOM</td>
<td width="7%" class="font-bold rightside">Qty</td>
  <td width="10%" class="rightside font-bold">Rate</td> 
  
    <td width="12%" class="rightside font-bold">Amount</td> 
  
  </tr>
  
</thead>

<tbody>
<?php

$sql = "select d.item_no,t.type_name item_type,CONCAT(i.item_name) item_name,c.category_name,i.hs_code,u.uom_name uom,d.ctn,d.qty,d.rate,d.dis_pr from  items i,item_type t, sale_detail d,item_uom u,item_category c where t.type_id=i.type_id and d.item_no=i.item_id and i.uom=u.uom_id and c.category_id=i.category_id AND
    d.s_id= :s_id";
$statement = $connect->prepare($sql);
$statement->execute(
  array(  
   ':s_id'       =>  $s_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="3%" class="small-font">'.number_format($count, 0).'</td>
   <td width="10%" class="small-font">'.$row['item_type'].'</td>
   <td width="17%" class="small-font">'.$row['item_name'].' </td>
   <td width="29%" class="cat_font">'.$row['category_name'].' </td>
<td width="7%" class="rightside">'.$row['uom'].'</td>
<td width="7%" class="rightside">'.number_format($qty, 0).'</td>
<td width="10%" class="rightside ">'.number_format($rate, 2).'</td>

<td width="12%" class="rightside ">'.number_format($amount, 2).'</td>
   </tr>
   '; 

}


$tot_balance =  $tot_amt;
$gst_amount = $tot_amt * $gst_pr / 100;

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

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

<tr>
<td colspan="3" class="rightside font-bold" width="55%">Value Excl. GST: </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, 2).'</td>
  
</tr>
<tr>
<td colspan="3" class="rightside font-bold" width="55%">GST ('.$gst_pr.'%) :</td>
<td width="10%" class="total_line rightside small-font"></td>
<td width="10%" class="total_line rightside small-font"></td>
<td width="10%" class="total_line rightside small-font"></td>
<td width="20%" class="total_line rightside small-font">'.number_format($gst_amount, 2).'</td>
  
</tr>
';
if($discount > 0)
{
  echo '<tr>
<td colspan="3" 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="3" 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 || $gst_pr > 0)
{
  $net_amt = $tot_amt - $discount + $freight + $gst_amount;
  echo '<tr>
<td colspan="3" 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, 2).'</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>