<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['hrm_id'])):
header('Location:../index.php');
endif;

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



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

}
$inv_no=433;
/////!!!!!!!!!!!!!!!!!!  Open inv_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con,"  SELECT * 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 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'];
    $discount=$rowsp['discount'];
  } ////  while($rowsp=mysqli_fetch_array($query)){ 



}

?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub | Sales Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>

<link rel="stylesheet" type="text/css" media="print" href="print.css">

</head>
<body>

<div class="row">
              <div class="col-lg-12">
                  <div class="card px-2">
                      <div class="card-body">
                          <div class="container-fluid">
                            <h3 class="text-right my-5">Invoice&nbsp;&nbsp;<?php  echo $inv_no ;?></h3>
                            <hr>
                          </div>
                          <div class="container-fluid d-flex justify-content-between">
                            <div class="col-lg-3 pl-0">
                              <p class="mt-5 mb-2"><b>Admin</b></p>
                              <p>xxxx<br>xxxxx,<br>xxxxx</p>
                            </div>
                            <div class="col-lg-3 pr-0">
                              <p class="mt-5 mb-2 text-right"><b>Invoice to</b></p>
                              <p class="text-right"><?php echo $cus_name;  ?><br> xxxxxx, xxxxxx,<br> xxxxxx</p>
                            </div>
                          </div>
                          <div class="container-fluid d-flex justify-content-between">
                            <div class="col-lg-3 pl-0">
                              <p class="mb-0 mt-5">Invoice Date : <?php  echo $inv_date; ?></p>
                            
                            </div>
                          </div>
                          <div class="container-fluid mt-5 d-flex justify-content-center w-100">
                            <div class="table-responsive w-100">
                                <table class="table">
                                  <thead>
                                    <tr class="bg-dark text-white">
                                        <th>#</th>
                                        <th>Description</th>
                                        <th class="text-right">Quantity</th>
                                        <th class="text-right">Unit Price</th>
                                         <th class="text-right">Discount</th>
                                        <th class="text-right">Total</th>
                                      </tr>
                                  </thead>
                                  <tbody>
                                    <?php 
                                    $rcount=0;
                                    $sub_total=0;
                                    $inv_discount=0;
                                    $net_total=0;
$query=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()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $item_name=$row['item_name'];
                $sales_price=$row['sales_price']; 
                $qty=$row['qty'];
                $item_dis=$row['item_dis'];

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


             

 ?>

                                    <tr class="text-right">
                                      <td class="text-left">1</td>
                                      <td class="text-left"><?php echo $item_name;  ?></td>
                                       <td><?php echo $qty;  ?></td>
                                      <td><?php  echo $sales_price;  ?></td>
                                      <td><?php  echo $item_dis;  ?></td>
                                     
                                      <td><?php  echo  $tot_sales; ?></td>
                                    </tr>
                                   <?php     } 
                                   if ($discount<>0) {

                                    $inv_discount= $sub_total * $discount/100;
                                    $net_total=$sub_total - $inv_discount;


                                   }
                                   else
                                    { 
                                       $net_total=$sub_total;
                                    }

                                   ?>
                                  </tbody>
                                </table>
                              </div>
                          </div>
                          <div class="container-fluid mt-5 w-100">
                            <p class="text-right mb-2">Sub Total : <?php echo $sub_total;?></p>
                            <?php  if ($discount<>0) {  ?>
                            <p class="text-right">Discount <?php echo $discount;?>: <?php echo $inv_discount?></p>
                          <?php } ?>
                            <h4 class="text-right mb-5">Total : <?php echo $net_total; ?></h4>
                            <hr>
                          </div>
                          <div class="container-fluid w-100">
                            <a href="#" class="btn btn-primary float-right mt-4 ml-2"><i class="fa fa-print mr-1"></i>Print</a>
                            <a href="#" class="btn btn-success float-right mt-4"><i class="fa fa-share mr-1"></i>Send Invoice</a>
                          </div>
                      </div>
                  </div>
              </div>
          </div>
        </div>