<?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'       =>  $title_id
  )
 );
 $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.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 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>
    * {
      font-family: arial;
      font-size: 14px;
      padding: 3px;
      font-weight: bold;

    }
    

    p {
      font-size: 11px;
    }

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

    .main_header {
      text-align: center;
      padding: 2px;
      line-height: 0px;
    }

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

    thead td {
      font-weight: bold;
      padding: 3px;
      border-bottom: 2px solid black;
      border-top: 2px solid black;
    }
    tbody td {
      
      padding: 4px;
      border-bottom: 0.5px solid black;
    }
    .font-bold {
      font-weight: bold;
    }
    .rightside {
      text-align: right;
    }
    table.table_head td{
      border: none;
    }
    .smallfont {
      font-size: 12px;
    }

    .no-border {
      border: none;
    }
   
  </style>

  </head>
  <body onload="window.print();">
    <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['inv_type']=='Cash')
  {
    $contact = $row['contact'];
    $cust_id = $row['cust_id'];
    $acc_no = "104".$cust_id;
    $name = $row['cust_name'];
  }else{$contact = $row['phone'];$name=$row['acc_name'];}
  $discount = $row['discount'];
  $freight = $row['freight'];
  $s_id = $row['s_id'];
  $cur_stamp = $row['cur_stamp'];
  $pre_balance = $row['pre_balance'];
  
 echo '
  <tr>
  <td width="10%"><b>M/S :</b></td><td width = "50%" >'.$name.'</td><td width="15%"><b>Inv # :</b></td><td width = "25%">'.$s_id.'</td>
  </tr>
  <tr>
  <td width="10%" class="font-bold"><b>Ph:</b></td><td width = "50%">'.$contact.'</td><td width="40%" colspan="2"><b>Date:</b><span class="smallfont">'.$cur_stamp.'</span></td>
  </tr>
  ';
}
  ?> 
  </table>
  <table width="100%" id="table_header">
  <thead>

  <tr>
  
  
  <td width="5%" class="font-bold">#</td>
  <td width="50%"class="font-bold">Description</td>
<td width="10%" class="font-bold rightside">Qty</td>
  <td width="15%" class="rightside font-bold">Rate</td> 
    <td width="20%" 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,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";
$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 = $count +1;
  
  $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">'.number_format($qty, 0).'</td>
<td width="15%" class="rightside ">'.number_format($rate, 0).'</td>
<td width="20%" class="rightside ">'.number_format($amount, 0).'</td>
   </tr>
   ';
   
  
  
  

}
$tot_balance =  $tot_amt;

echo '
</tbody>
</table>
<table width="100%">
<tr>
<td class="rightside no-border" width="55%">Total : </td>
<td width="10%" class="total_line rightside small-font">'.number_format($tot_qty, 0).'</td>
<td width="15%" 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 class="rightside no-border" width="55%">Discount : </td>
<td width="10%" class="total_line rightside "></td>
<td width="15%" class="total_line rightside "></td>
<td width="20%" class="total_line rightside ">'.number_format($discount, 0).'</td>
  
</tr>';
}
if($freight > 0)
{
  echo '<tr>
<td class="rightside no-border" width="55%">Freight : </td>
<td width="10%" class="rightside "></td>
<td width="15%" class="rightside "></td>
<td width="20%" class="rightside ">'.number_format($freight, 0).'</td>
  
</tr>';
}
if($discount > 0 || $freight > 0)
{
  $net_amt = $tot_amt - $discount + $freight;
  echo '<tr>
<td class="rightside no-border" width="55%">Net Total : </td>
<td width="10%" class="rightside "></td>
<td width="15%" class="rightside "></td>
<td width="20%" class="rightside ">'.number_format($net_amt, 0).'</td>
  
</tr>';
}else{$net_amt = $tot_amt;}
echo '<tr>
<td class="rightside no-border" width="55%" class="no-border">Previous Balance : </td>
<td width="10%" class="rightside "></td>
<td width="15%" class="rightside "></td>
<td width="20%" class="rightside ">'.number_format($pre_balance, 0).'</td>
  
</tr>';
$net_amt = $net_amt + $pre_balance;
echo '<tr>
<td class="rightside no-border" width="55%">Current Balance : </td>
<td width="10%" class="rightside "></td>
<td width="15%" class="rightside "></td>
<td width="20%" class="rightside ">'.number_format($net_amt, 0).'</td>
  
</tr>';
$statement = $connect->query("select m.dated,m.source,m.ref_id,d.narr,d.debit,d.credit from jv_detail d, jv m where m.jv_id=d.jv_id and d.acc_no = '".$acc_no."' order by dated desc LIMIT 5");
$totalRows = $statement->rowCount();
if($totalRows > 0)
{
  echo '<table class="ledger width="50%">
<tr><th>Date</th><th>Src</th><th>Ref</th><th>Narration</th><th>Debit</th><th>Credit</th></tr>
  ';
  foreach ($result as $row) {
    echo '
    <tr><td>'.$row["dated"].'</td><td>'.$row["source"].'</td><td>'.$row["ref_id"].'</td><td>'.$row["narr"].'</td><td>'.$row["debit"].'</td><td>'.$row["credit"].'</td></tr>
    ';
  }
  echo '</table>';

}
?>



</table>

</body>
</html>