<?php
session_start();
include('../includes/dbcon.php');

$userid=$_SESSION['hrm_id'];
 $user_name=$_SESSION['hrm_name'];

if (isset($_GET['inv_no'])) { 
  $inv_no= trim($_GET['inv_no']);
} 



if (isset($_POST['inv_no'])) { 
$inv_no= trim($_POST['inv_no']);

}


/////!!!!!!!!!!!!!!!!!!  Open inv_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con,"  SELECT inv_header.*,customer_master.cus_name,customer_master.address,customer_master.cus_code,stock_locations.loc_name,invoice_types.des,employee_master.rep FROM inv_header
 LEFT JOIN customer_master ON inv_header.cus_id=customer_master.cus_id
 LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id 
  LEFT JOIN invoice_types ON inv_header.invtype_id=invoice_types.invtype_id 
   LEFT JOIN employee_master ON inv_header.emp_id=employee_master.emp_id
  where inv_id='$inv_no'  ")or die(mysqli_error($con));
$count=mysqli_num_rows($query);

if ($count>0)
{

  while($rowsp=mysqli_fetch_array($query)){     
    //$grnd_no=$rowd['grnd_no'];  
    $inv_date=$rowsp['inv_date'];
    $loc_name=$rowsp['loc_name']; 
    $cus_name=$rowsp['cus_name'];
   // $invd_ref=$rowsp['invd_ref'];
    $note=$rowsp['note'];
     $edate1=$rowsp['edate'];
    // $note=$rowsp['note'];
    $discount=$rowsp['discount'];
    $des=$rowsp['des'];
     $emp_name=$rowsp['rep'];
      $address=$rowsp['address'];
      $cus_code=$rowsp['cus_code'];
  } ////  while($rowsp=mysqli_fetch_array($query)){ 



}




?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="custom.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

</head>
<body>
  <div id="invoiceSection">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="row main-section">
<div class="col-md-12 col-sm-12 header">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<h1> Sales Invoice Duplicate</h1>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 text-right">
	<p><?php echo $des;  ?></p>
<p>Invoice No:<?php echo $inv_no;  ?></p>
<span>Invoice Date :<?php echo  $edate1; ?></span>
</div>
</div>
</div>
<div class="col-md-12 col-sm-12 content">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<p>To.</p>
<p><?php echo $cus_name;  ?></p>
<p><?php echo $address;  ?></p>
<p><?php echo $cus_code;  ?></p>
</div>
<div class="col-md-6 col-sm-6 col-xs-6 text-right">
<p>Rep :<?php echo $emp_name;  ?> </p>
<p>Branch : <?php echo $loc_name;  ?></p>
</div>
</div>
</div>
<div class="col-md-12 col-sm-12 text-right">
<table class="table">
<thead>
<tr>
  <th>#</th>
<th>Part No</th>
<th class="text-left">Description</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<?php

$sub_total=0;
          $inv_discount=0;
          $net_total=0;
          $total_qty=0;
          $ncount=0;


$queryd=mysqli_query($con," SELECT * FROM inv_detail
LEFT JOIN item_master ON item_master.item_id=inv_detail.item_id where inv_id='$inv_no' ")or die(mysqli_error($con));
          while($row=mysqli_fetch_array($queryd)){               
              //$rcount = $rcount + 1;
              $item_name=$row['item_name'];
              $qty=$row['qty'];
              $sales_price=$row['sales_price'];
              $description=$row['description'];
              $item_dis=$row['item_dis'];

              $tot_sales=($sales_price * $row['qty'])-($sales_price * $row['qty'] *  $item_dis/100);
              $sub_total= $sub_total + $tot_sales ;

      $total_qty=$total_qty + $row['qty'];
      $ncount=$ncount+1;
?>

 <td class="text-left"><?php echo $ncount;  ?></td>
<td><?php  echo  $item_name;   ?></td>
<td class="text-left"><?php  echo  $description;   ?></td>
<td><?php echo number_format($qty,0);  ?></td>
<td><?php echo  number_format($sales_price,2);  ?></td>
<td><?php echo  number_format($tot_sales,2);  ?></td>
</tr>

  <?php  }
 if ($discount<>0) {

                                    $inv_discount= $sub_total * $discount/100;
                                    $net_total=$sub_total - $inv_discount;
                                   }
                                   else
                                    { 
                                       $net_total=$sub_total;
                                    }


   ?>
</tbody>
          <tr class="text-right">
                                      <td class="text-left"></td>
                                       <td class="text-left"></td>
                                      <td class="text-left">Total</td>
                                      <td><span style="font-weight:bold"><u><?php echo number_format($total_qty,0);  ?></u></span></td>
                                      <td>  </td>
                                      <td><span style="font-weight:bold"><u><?php echo number_format($net_total,2);  ?></u></span></td>

                </tr>
</table>
</div>

</div>
</div>
</div>
</div>
</div>
</body>
</html>