<!doctype html>
<html class="no-js" lang="en">
<?php
session_start();
$branch=$_SESSION['branch_id'];
?>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php 
  include '../includes/connection.php';
  $staffcode=$_GET['staffcode'];
  ?>

<script type="text/javascript">
      window.onload = function() { window.print();
        var is_chrome = function () { return Boolean(window.chrome); }
            if(is_chrome) 
            {
            setTimeout(function(){document.location.href = "new_order.php";}, 0); 
            //give them 10 seconds to print, then close
            }
            else
            {
           setTimeout(function(){document.location.href = "new_order.php";}, 0);
            }
        }
   
 </script>
</head>
<?php
if ($_SESSION['business_id']==17) {
  ?>
<body style="font-family:times-new roman; display: inline-block;">

  <?php
}else{
  ?>
<body style="font-family: Courier New, Courier,Lucida Sans Typewriter, Lucida Typewriter, monospace; font-stretch: expanded; display: inline-block;">
  

  <?php
}
?>
        <div class="row">
        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
        </div>
        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10" style="margin-top:16px;">
       
        <div class="row" style=" font-size: 20px; ">
        <?php include('../sales/myheader.php'); ?>
          <div>
          <?php
          $sql = "SELECT full_name FROM staff where staff_code='$staffcode'";
$result = mysqli_query($conn, $sql);
           $result = mysqli_query($conn, $sql);
           if (mysqli_num_rows($result) > 0) {
   $row = mysqli_fetch_assoc($result);
   $staffname=$row['full_name'];
   
    
} else {
  $staffname="";
    echo "0 results";
}

$sql = "SELECT id, (select max(id) from sales)as salesid, (select receipt_no from sales where id=salesid)as receiptnoo, (select product_name from products where id=sales_details.product_id)as product_name, (select sum(quantity) from sales_details where order_id=salesid)as quantityy, quantity,  (select price from products where id=sales_details.product_id) as unit, (select paid from sales where id=sales_details.order_id)as total FROM sales_details where order_id=(select max(order_id) from sales_details where branch_id='$branch')";

$sql2 = "SELECT id, (select max(id) from sales)as salesid, (select receipt_no from sales where id=salesid)as receiptnoo, (select product_name from products where id=sales_details.product_id)as product_name, (select count(id) from sales_details where order_id=salesid)as quantityy, quantity,  (select price from products where id=sales_details.product_id) as unit, (select paid from sales where id=sales_details.order_id)as amtpaid,(select changebal from sales where id=sales_details.order_id)as changebal,(select total_price from sales where id=sales_details.order_id)as total,selling_price FROM sales_details where order_id=(select max(order_id) from sales_details where branch_id='$branch')";
$result = mysqli_query($conn, $sql);
 $roww = mysqli_fetch_assoc($result);
          ?>
        <br>
        <br>
        <hr style="border: none;">
        <div>
        <?php
         $resultckb = mysqli_query($conn, $sql2);
         $rowckb = mysqli_fetch_assoc($resultckb);
        if ($rowckb['amtpaid']<=0) {
          ?>
          Invoice No:  <?php echo $roww['receiptnoo'] ;

        }else{
          ?>
          Rec No:  <?php echo $roww['receiptnoo'] ;

        }
        ?>
        
        </div>
        
   </div>
    </div>
    <div>
        <br>
        Date:  <?php echo date('d/m/Y')?>
        
   </div>
        <hr style="margin: 0; border-style: dashed;">
        <div>
        <br>
        <table width="100%" style="border-style: dotted; border: 2px;">
          <thead style="text-align: left;">
            <th width="40%">ITEM</th>
            <th width="20%">QTY</th>
           
            <th width="20%">PRICE</th>
            <th width="20%">AMOUNT</th>
          </thead>
        <div>
          
        </div>
        </table>
        
   </div>
   <hr style="margin: 0; border-style: dashed;">
   <div>
   <br>
         <table width="100%" style="border-style: dotted; border: 2px;">
          <tbody>
          <?php
          $result2 = mysqli_query($conn, $sql2);
          while($row = mysqli_fetch_assoc($result2)) {
            ?>
            <tr>
              <td width="40%"><?php echo $row['product_name'];?></td>
              <td width="20%"><?php echo $row['quantity']?></td>
              <td width="20%"><?php echo number_format($row['selling_price']);?></td>
              <td width="20%"><?php echo number_format($row['selling_price']*$row['quantity']);?></td>
            </tr>
            <?php
           $sum=$row['total'];
           $amtpaid=$row['amtpaid'] + $row['changebal'];
           $changebal=$row['changebal'];
          }
          ?>
            
          </tbody>
        </table>
        </div>
        <hr style="margin: 0; border-style: dashed;">
        <div>
        
         <table width="100%" style="border-style: dotted; border: 2px;">
          <thead style="text-align: left; font-stretch: expanded; ">
            <th colspan="4" width="80%" style=" transform: scale(1,2); font-size: 16px;"><h5>Total:</h5></th>
            <th style=" transform: scale(1,2); font-size: 16px;"><?php echo number_format($sum);?></th>
          </thead>

            <thead style="text-align: left; font-stretch: expanded;">
            <th colspan="4" width="80%" style=" transform: scale(1,2); font-size: 16px;">Amount Paid:</th>
            <th style=" transform: scale(1,2); font-size: 16px;"><?php echo number_format($amtpaid);?></th>
           </thead>
            <?php  

            if ($sum>$amtpaid) {
             ?>
             <thead style="text-align: left; font-stretch: expanded;">

            <th colspan="4" width="80%" style=" transform: scale(1,1.5); font-size: 20px;"><br>Balance:</th>
            <th style=" transform: scale(1,1.5); font-size: 16px;"><br><?php echo number_format($sum-$amtpaid);?></th>
          </thead>
             <?php
            }else{
              ?>
             <thead style="text-align: left; font-stretch: expanded;">
            <th colspan="4" width="80%" style=" transform: scale(1,1.5); font-size: 16px;"><br>Change:</th>
            <th style=" transform: scale(1,1.5); font-size: 16px;"><br><?php echo number_format($changebal);?></th>
            </thead>
              <?php
            }
            ?>
             
          
        </table>
        </table>
        </div>
        <hr style="margin: 5px; border-style: dashed;">
        <div>
        <br>
          <p align="center" style="font-size: 16px;">Served By: <?php echo $staffname;?></p>
          
         <?php include'../sales/rec_footer.php' ;?>          
          
        </div>
        <hr style="border-style: dashed;">
        <div>
          <p align="center">POS Software By:<br> <?php $dev_select=mysqli_fetch_array(mysqli_query($con,"SELECT * from developer where id=1")); echo $dev_select['developer_name']; ?>(
          <?php if($_SESSION['business_id']==17) {
            echo "sales@nugsoft.com";
          }else{
            echo $dev_select['tel'];
            } 
            ?>
          )
          </p>
        </div>
   </div>
   </div>
        
        
        
        
</body>
<?php

?>
</html>