<?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();
 }
 $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'       =>  2
  )
 );
 $result = $statement->fetchAll();
 
  foreach($result as $row)
 {
  $heading1 = $row['heading1'];
  $heading2 = $row['heading2'];
  $heading3 = $row['heading3'];
 }
 

  $statement = $connect->prepare("select m.s_id,m.inv_no,m.cust_id,m.s_id chal_no,m.po_no,0 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,o.user_name from sale m,user_detail o
    ,accounts c where o.user_id=m.user_id and c.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>Cash Sale Print</title>
     <style>
    * {
      font-family: arial;
      font-size: 10px;
      padding: 3px;

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

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

    .main_header p {
      text-align: center;
      padding: 2px;
          
      font-size: 10px;
    }

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

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

    }
    .rightside {
      text-align: right;
    }
    table.table_head td{
      border: 2px solid black;
    }
    .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;
    }
    table {
      border-collapse: collapse;
    }
  </style>

  </head>
  <body onload="window.print();">
    <h2>Cash Sale Invoice</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)
 {
  $acc_no = $row["cust_id"];

 echo '
 <table width="100%">
 <tr>
 <td colspan="2" width="30%" class="no-border"><b>Inv # :</b>'.$row["inv_no"].'</td><td width="70%" colspan="2" class="rightside no-border"><b>Date:</b><span class="smallfont">'.$row["dated"].'</span></td>
 </tr>
 </table>
 <table width="100%">
  <tr>
  <td width="100%" colspan"4" class="no-border"><b>M/S :</b> '.$row["cust_name"].'</td>
  </tr>
  <tr>
  <td width="100%" colspan="4" class="no-border"><b>Ph:</b>'.$row["contact"].'</td>
  </tr>
  ';
}

  ?> 
</table>
  </table>
  <table width="100%" class="table_header">
  <thead>

   <tr>
  
  
  <td width="3%" class="font-bold" align="center">#</td>
  <td width="55%"class="font-bold">Description</td>  
  <td width="10%" class="font-bold" align="center">UOM</td>
<td width="7%" 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) as item_name,i.description, u.uom_name,d.ctn,d.qty,d.rate from  items i,item_type t, sale_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.s_id= :s_id order by d.ln_no asc";
$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"];
  
  
  $rate = $row["rate"];
   $count++;
  
  $amount = $qty * $rate;
  $tot_qty = $tot_qty + $qty;
  $tot_amt = $tot_amt + $amount;
  

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

<td class="rightside">'.$row['uom_name'].'</td>
<td class="rightside">'.number_format($qty, 0).'</td>
<td  class="rightside ">'.number_format($rate, 0).'</td>
<td class="rightside ">'.number_format($amount, 0).'</td>
   </tr>
   ';  

}

echo '
  
<tr>
  
   <td colspan="3" class="rightside">Total :</td>
   
  
<td class="rightside">'.number_format($tot_qty, 0).'</td>
<td  class="rightside "></td>
<td class="rightside ">'.number_format($tot_amt  , 0).'</td>
   </tr>
   ';

if($discount > 0)
{
  echo '
<tr>
  
   <td colspan="5" class="rightside">Discount :</td>
   

<td class="rightside ">'.number_format($discount, 0).'</td>
   </tr>';
}
if($freight > 0)
{
  echo '<tr>
  
   <td colspan="5" class="rightside">Freight :</td>
   

<td class="rightside ">'.number_format($freight, 0).'</td>
   </tr>';
}
if($discount > 0 || $freight > 0)
{
  $net_amt = $tot_amt - $discount + $freight;
  echo '<tr>
  
   <td colspan="5" class="rightside">Net Total :</td>
   

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

?>

</tbody>
</table>

</body>
</html>