<?php 
  if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "To access this page, Login is required";
    header('location: login.php');
  }  ?>

<!DOCTYPE>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}
.top-top {float: left; font-size: x-small;color:#848482; width: 100%; padding: 10px; margin-bottom: 1%;}
.col-returned-a {  float: left;  width: 30%;  padding: 15px; }
.col-returned-b{  float: left;  width: 70%;  padding: 15px; }
.returned { float: left; width: 100%;  background: white; padding: 15px; border-bottom: 2px solid #7CB9E8;}
.returned-a { float: left; width: 100%;  background: white; padding: 15px;  border-bottom: 2px solid #7CB9E8;}

input[type=search-products], select{ width: 100%;  padding: 8px;  border: 1px solid #ccc;  border-radius: 5px; background: white;  margin: auto; color: #7CB9E8;margin-top: 0%; font-weight: bold; font-size: x-small; }

input[type=search]:focus {  border:none; padding: 7px; }

.nav-checkout-return{ text-align: right; margin-top: 5%; margin-bottom: 10%; width: 100%;  }
.checkout-return { padding: 5px; border: 1px solid #7CB9E8 ; color: #7CB9E8; background: none; border-radius: 5px; }
.checkout-return:hover { padding: 8px; border: 0px solid #7CB9E8 ; color: white; background:#7CB9E8; border-radius: 5px; }


.time{ font-size: x-small; }

.returned a { float: right; color: #7CB9E8; }
.returned p {padding: 10px;}
/* Clear floats after the col-returneds */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .col-returned-b, .col-returned-a {  width: 100%;   } }

</style>
</head>
<body>


<div class="row">

<div class="top-top">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;"> Home </a>
<a>// Accept returned Items</a>
</div>



  <div class="col-returned-a">
  <nav class="returned">
    <hr style="opacity: .1;">

<p>Total items to be returned 
<a> <?php $sql = "SELECT * FROM items_received where status = 0 AND  time >= CURDATE() AND YEAR(time) = YEAR(CURRENT_DATE) ";     
$result = mysqli_query($conn,$sql); $rowcount=mysqli_num_rows($result);
  printf(" %d \n ",$rowcount);?></a>
</p>

<nav class="nav-checkout-return">
<button class="checkout-return"  onclick="location.href = 'index.php?link=001returned'; return false;">Checkout Returned Items</button>
</nav>

<hr style="opacity: .1;">
<p> <i class="fa fa-calendar cal"> </i> <a class="time"><?php $date = new DateTime(); echo $date->format('l, F jS, Y'); ?> </a></p>

  </nav>
  </div>
  
  <div class="col-returned-b">
  <nav class="returned-a">

   <?php include( "returned-sales-search.php"); ?>
   <?php include( "returned-sales.php"); ?>

  </nav>
  </div>

</div>

</body>
</html>

<script>
function opensales() {   document.getElementById("returnItems").style.display = "block"; }
function closesales() {   document.getElementById("returnItems").style.display = "none";  }
</script>
<style type="text/css">
.bottom-total-a { float: left; width: 50%; padding: 15px; font-size: x-small; }
.bottom-total-b { float: left; width: 50%; padding: 15px; font-size: x-small; }

.bottom-total-b h3 {text-align: right;}
.form-popup {  display: none; margin-top: 2%;  z-index: 9; }

.f1 {float: left; width: 33.33%; padding: 10px;}
.f2 {float: left; width: 33.33%; padding: 10px;}
.f3 {float: left; width: 33.33%; padding: 10px;}

input[type=text],[type=file], select { width: 100%;  padding: 8px;  border: 1px solid #ccc;  border-radius: 4px;   box-sizing: border-box;  margin-top: 6px;  margin-bottom: 16px;  resize: vertical; background:whitesmoke; color:#848482; font-weight: bold; font-size: x-small; }

input[type=number] { width: 100%;  padding: 8px;  border: 1px solid #ccc;  border-radius: 4px;   box-sizing: border-box;  margin-top: 6px;  margin-bottom: 16px;  resize: vertical; background:white; color:#848482; font-weight: bold; font-size: x-small; }

input[type=submit] {padding: 8px; background: skyblue; color: white; float: right; border: none; border-radius: 3px;}

/* Add styles to the loan container */
.return-items {width:55%; margin: auto; padding: 15px;  background-color: white;  box-shadow: 2px 3px 3px 2px gray; position:absolute; top: 0; right: 10; margin-right: 3%; margin-top: 10%; margin-bottom: 2%;   }

.fa-close { float: right; color: red; margin-top:0%; font-size: large; }

.table { margin-right: 3%;  overflow-x: auto;scrollbar-width: thin; }

@media screen and (max-width:600px) {
  .return-items { width: 95%; height: auto;  margin-right: 0%; margin-top: 25%;  }
  .bottom-total-b, .bottom-total-a { width: 50%; float: left; } 
  .c1 , .c2 {float: left; width: 100%;}
  .f1, .f2, .f3 { width: 100%; float: left;  }
  .bottom-total-a , .bottom-total-b { width: 100%; float: left; }



  }

 </style>
<div class="form-popup" id="returnItems">
  <nav class="return-items">
<h4> <i class="fa fa-shopping-basket" style="margin-right: 5%; margin-left: 1%;"></i>Return Goods Form <a onclick="closesales()"><i class="fa fa-close"></i> </a> </h4>

<?php  
if (isset($_POST["submit"])) {
$id = $_POST['id'];
$sql = "SELECT * FROM product_sales where id = '$id'  order by Id  DESC";   
$result = mysqli_query($conn,$sql);  $row = mysqli_fetch_object($result);
echo "<script type= 'text/javascript'> opensales() </script>";

 ?>


<form method="post"  action="" >
  <nav class="f1">
  <label>Customer Name</label>
  <input type="text" name="customer" value="<?php echo $row->customer;?>" readonly>
  </nav>

 <nav class="f2">
  <label>Product Name</label>
  <input type="text" name="item" value="<?php echo $row->name;?>" readonly>
</nav>

  <nav class="f2">
  <label>Product Size</label>
  <input type="text" name="size" value="<?php echo $row->size;?>" readonly>
</nav>

  <nav class="f1">
<label>Unit Price</label>
<input type="text" name="price" Ksh value="<?php echo $row->price;?>" readonly>
</nav>

  <nav class="f3">
<label>Released Quantity</label>
  <input type="text" name="quantity" value="<?php echo $row->quantity;?>"  readonly>
</nav>

<nav class="f3">
<label>Returned Quantity</label>
<input type="number" name="quantity_1" placeholder="3" value="1" >
</nav>

<input type="hidden" name="receipt_no" value="<?php echo $row->receipt_no;?>"  readonly>
<input type="hidden" name="code" value="<?php echo $row->code;?>" readonly>



<input type="submit" name="submit-1" value="Return Item">

</form>

<?php 
$receipt_no = $_POST['receipt_no'];
$sql = "SELECT * FROM earnings where receipt_no = '$receipt_no'  order by Id  DESC";   
$result = mysqli_query($conn,$sql); $row = mysqli_fetch_object($result); ?>

<nav class="bottom-total-a">
   <p><b> Receipt No</b> - <?php echo $row->receipt_no;?>   </p>
    <p><b> Served by</b> - <?php echo $row->session;?>   </p>
    <p><b> Date/time</b> - <?php echo date_format( new DateTime($row->time),"d,M, Y \t H:m:s");?>   </p>

</nav>

 <?php  }?>
</nav>
</div>


<?php include 'inc/config.php'; 
if (isset($_POST["submit-1"])) { $receipt_no = $_POST['receipt_no'];

$username =  $_SESSION['username'];  $customer= $_POST['customer'];   $item= $_POST['item'];  $size= $_POST['size'];   $code= $_POST['code']; 
$quantity= $_POST['quantity'];  $quantity_1 = $_POST['quantity_1'];  $price= $_POST['price'];  $totalprice = $price * $quantity_1; 

 $res = mysqli_query($db,"SELECT * FROM `products` WHERE code = '$code'");
 $r = mysqli_fetch_assoc($res);  $previous_quantity = $r['quantity'];    $new_quantity = ( $previous_quantity + $quantity_1 );

$sql_a = "SELECT * FROM items_received WHERE item='$item' AND size = '$size' AND customer = '$customer'";
$res_a = mysqli_query($db, $sql_a);

if (mysqli_num_rows($res_a) > 0) { echo "<script type= 'text/javascript'>alert('Sorry ! request to return items for [ $customer ] has arleady been made');</script>"; }

else if ($quantity_1 > $quantity )  { 
     echo "<script type= 'text/javascript'>alert('Sorry [ $username ] Quantity to be returned exceeds the released items');</script>";
          }

else {
    $sql = "INSERT INTO items_received (customer, item, size, price, totalprice,  quantity, quantity_1, receipt_no, session ) 
    VALUES ('$customer' ,'$item', '$size', '$price',  '$totalprice', '$quantity', '$quantity_1', '$receipt_no', '$username' )";


     $sql_2 = "UPDATE products SET quantity  = '$new_quantity' WHERE code = '$code'"; 


    // execute query
if (($db->query($sql) === TRUE) AND ($db->query($sql_2) === TRUE)  ){
echo "<script type= 'text/javascript'>alert('Accept [ $customer ] - return of $quantity_1 $item  - $size  back? .');</script>"; 
echo "<script type='text/javascript'>location.href = 'index.php?link=101returned';</script>"; }

else{ echo "<script type= 'text/javascript'>alert('sorry  ! system is unable to receive-back items purchased by [$customer].');</script>";  }


   }
  }
  
?> 