<!doctype html>
<html  lang="en">
<?php
include '../includes/connection.php';
 $staffcode=$_GET['staffcode'];

session_start();
$branch=$_SESSION['branch_id'];
?>
<head>
       <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
        <link href="assets/DT_bootstrap.css" rel="stylesheet" media="screen">


<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>
<body style="margin: 20px;">
        <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:40px;">
        <?php
        include "../sales/receipt_header.php";
         // include('../sales/myheader.php'); 

        ?>
        <hr></br>
        <h3 style="text-align:center;">Payment receipt</h3>
        <div align="right">
        <?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);
          ?>

        </div>
        
        <div class="row">
            <strong><span style="font-size: 20px; margin-right: 100px;">Date:  <?php echo date('d/m/Y');?></span></strong><strong><span style="font-size: 20px; margin-left: 100px;">Rec No:  <?php echo $roww['receiptnoo'] ?></span></strong>
           
        </div>
        <br>
        <div class="row">
            <table width="100%" style="border: 5px; font-size: 20px;">
          <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>
          </table>
          <hr>
          <table width="100%" style="border: 5px; font-size: 22px;">
          
          <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>
        <div class="row">
            <div class="span4">
            <hr>
            <br>
            <table width="100%" style="text-align: center;">
              <tr style="font-size: 30px;">
                <td colspan="2" ><strong>Total:</strong></td>
                <td colspan="2"><strong><?php echo number_format($sum);?></strong></td>
              </tr>
              <tr style="font-size: 32px;">
                <td colspan="2"><strong>Amount Paid:</strong></td>
                <td colspan="2"><strong><?php echo number_format($amtpaid);?></strong></td>
              </tr>
              <tr style="font-size: 25px;">
                <td colspan="2"><strong>
                <?php
                if ($sum>$amtpaid) {
             ?>
             Balance:
             <?php
            }else{
              ?>
                Change:
                <?php
                }
                ?>
              </strong></td>
                <td colspan="2"><strong>
                <?php 
                if ($sum>$amtpaid) {
                echo number_format($sum-$amtpaid);
                }else{
                 echo number_format($changebal);
                 }
                 ?></strong>
                 </td>
              </tr>
            </table>
           
            </div>
            
        </div>
            <center><span style="text-align: center; font-size: 20px;"><br>SERVED BY: <?php echo $staffname;?></span></center>
        </div>
        <hr>
        
            <div class="row">
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4" style="font-size: 20px;">
            <?php include'../sales/rec_footer.php' ;?>
            </div>
            <hr>
            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
            <p align="center" style="font-size: 20px;">POS Software By:<br>
             <?php $dev_select=mysqli_fetch_array(mysqli_query($con,"select * from developer")); echo $dev_select['developer_name']; ?>(
             <?php if($_SESSION['business_id']==17) {
            echo "sales@nugsoft.com";
          }else{
            $dev_select['tel'];
            } 
            ?>
             )
          </p>
            </div>
        </div>
        
        
        </div>
        </div>
        </body>


</html>