<?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-aa{ padding:15px; text-align: left; width: 100%; font-size: x-small; color: #848482; margin-bottom: 1%;}
td {  padding:8px;border-bottom: 1px solid #ddd !important; font-size: xx-small; }

th {  padding:10px;border: 1px solid #ddd !important; background:#7CB9E8; color: white; opacity: .8;text-align: left;  font-size: x-small;
 border-spacing: 1;}

table { margin: auto; border-spacing: 1; width: 100%; float: left;  }
.table { width: 100%;  padding: 5px;  overflow-x: auto; scrollbar-width: thin; height: 90%;}
 
.time  { font-size: xx-small; }

.sales-head { width: 100%; float: left; padding: 10px; background:#F5F5DC; font-size: small; font-weight: bolder; color:#007AA5;  }



 input[type=search-records] { border-radius: 5px;  width: 100%;  padding: 10px;  margin: 5px 0 22px 0;  border: none;  background:white; border:  1px solid  #f1f1f1;
}

input[type=search-records]:focus { background-color: #ddd;  outline: none;}



.col-sales-a {  float: left;  width: 30%;  padding: 15px;; }
.col-sales-b {  float: left;  width: 70%;  padding: 15px; }

.sales { float: left; width: 100%; background: white; padding: 15px; border-bottom: 2px solid #7CB9E8; }

.sales a { color: #7CB9E8; float: right; }
.sales p { padding: 10px; }
.sales-head a { float: right; font-weight: bolder; font-size: x-small; }
/* Clear floats after the col-saless */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .col-sales-a, .col-sales-b{  width: 100%;   }
  .sales p {padding: 10px; font-size: x-small;} }

</style>
</head>
<body>


<div class="row">

<div class="top-aa">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;">Home </a>
<a>// Sales - Monthly report </a>
</div>


  <div class="col-sales-a">

  <nav class="sales-head"><p> Station<a>  <?php  $username =  $_SESSION['username'];
  $result = mysqli_query($db, "SELECT * FROM users WHERE Username = '$username' " );            
  while ($row = mysqli_fetch_array($result))  {
    echo "<a>".$row['Branch']."</a>" ;      }?>  </a></p> 
  </nav>
  <nav class="sales">


  <p style="padding: 0px;"><i class="fa fa-calendar cal"></i> <a style="font-weight: bold;" class="time"><?php $date = new DateTime(); echo $date->format(' F jS, Y'); ?> </a></p>

    <hr style="opacity: .1;">
    <p>Items released  <a> <?php $sql = "SELECT * FROM product_sales ";     
$result = mysqli_query($conn,$sql); $rowcount=mysqli_num_rows($result);
  printf(" %d \n ",$rowcount);?></a> </p>
    <hr style="opacity: .1;">

 <p>Quantity Released  <a> 
 <?php  $sql = "select SUM(quantity) as quantity from product_sales where time >= CURDATE() AND YEAR(time) = YEAR(CURRENT_DATE)";  
 $result = $db->query($sql);  $row = $result->fetch_array();{  $sum = $row['quantity'];} echo $sum;?>  </a>
 </p>
  
<hr style="opacity: .1;">

    <p>Returned items <a> <?php $sql = "SELECT * FROM product_sales ";     
$result = mysqli_query($conn,$sql); $rowcount=mysqli_num_rows($result);
  printf(" %d \n ",$rowcount);?></a> </p>

    <hr style="opacity: .1;">




</nav>
  </div>
  
  <div class="col-sales-b">
  <nav class="sales">
  <?php include 'monthlysale-search.php'; ?>

<div class='table'>
<?php  require_once "inc/config.php";
 $sql = "SELECT * FROM product_sales where MONTH(time) >= MONTH(CURDATE()) AND YEAR(time) = YEAR(CURRENT_DATE)  order by time DESC";
   if($result = $mysqli->query($sql)){
   if($result->num_rows > 0){
   echo "<table >";
   echo "<thead>";
   echo "<tr class='t1'>";
   echo "<th>Customer Name</th>";
   echo "<th>items released</th>";
  echo "<th>Item Size</th>";
   echo "<th>Quantity</th>";
    echo "<th>Unit price</th>";
  echo "<th>Total Price</th>";
   echo "<th>Receipt_No</th>";
   echo "<th>Served by</th>";
   echo "<th>Time</th>";
     echo "</tr>";
   echo "</thead>";
   echo "<tbody>";
    while($row = $result->fetch_array()){
   echo "<tr>";
  echo "<td>" . $row['customer'] . "</td>";
   echo "<td>" . $row['name'] . "</td>";
   echo "<td>" . $row['size'] . "</td>";
   echo "<td>" . $row['quantity'] . "</td>";
       echo "<td>" . $row['price'] . "</td>";
         echo "<td>" . $row['unit_totalprice'] . "</td>";
echo
  "<td>
<form method ='post'> 
<input type='hidden' name='receipt_no' value=".$row['receipt_no']." />
<button class='receipt' type='submit' name='submit'  ><u> " . $row['receipt_no'] . " </u></button>
</form>
  </td>";

      echo "<td>" . $row['session'] . "</td>";
  echo "<td  style='float: right; margin-right: 2%; color:orange; font-weight:bold; '  >" . date_format( new DateTime ($row['time'] ),"d M, Y H: m: s"). "</td>";

 echo "</tr>";
         }
    echo "</tbody>";                            
    echo "</table>";
   // Free result set
       $result->free();
            } else{ echo "<p class='no-results'><em>Opps! no records.</em></p>";}
  
   } else{ echo "ERROR: Could not able to execute $sql. " . $mysqli->error;}
   // Close connection
       $mysqli->close();?>
</div>

  </nav>
  </div>
</div>

</body>
</html>
<script>
function opensales() {   document.getElementById("customerItems").style.display = "block"; }
function closesales() {   document.getElementById("customerItems").style.display = "none";  }
</script>
<style type="text/css">
.receipt {padding: auto; border: none; background: white; color: skyblue; }



.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; }

.c1 {float: left; width: 33.33%; padding: 10px;}
.c2 {float: left; width: 33.33%; padding: 10px;}

/* Add styles to the loan container */
.items-count {width:65%; 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: 0%; 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) {
  .items-count { width: 95%; height: auto;  margin-right: 0%; margin-top: 20%;  }
  .bottom-total-b, .bottom-total-a { width: 50%; float: left; } 
  .c1 , .c2 {float: left; width: 100%;}
  }

 </style>
<div class="form-popup" id="customerItems">
  <nav class="items-count">
<h4> <i class="fa fa-user" style="margin-right: 5%; margin-left: 1%;"></i> Sales Receipt  <a onclick="closesales()"><i class="fa fa-close"></i> </a> </h4>

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

 ?>
<nav class="table">
<table >
 <tr>

  <th>Items</th>
  <th>Size</th>
  <th>Quantity</th>
  <th>Unit price</th>
  <th>Total Price</th>
  <th>Time released</th>
</tr>  

<?php while($row = mysqli_fetch_object($result)){ ?>  

<tr> 

<td><?php echo $row->name;?></td>
<td><?php echo $row->size;?></td>
<td><?php echo $row->quantity;?></td>
<td><?php echo $row->price;?></td>
<td><?php echo $row->unit_totalprice;?></td>
<td style="color:#848482; font-weight: bold;" ><?php echo date_format( new DateTime($row->time),"d,M, Y \t H:m:s");?></td>
 </tr>       
 <?php  }?>
</table>
</nav>

<?php  $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> Customer name</b> - <?php echo $row->customer;?>   </p>
   <p><b> Receipt No</b> - <?php echo $row->receipt_no;?>   </p>
    <p><b> Served by</b> - <?php echo $row->session;?>   </p>

</nav>

<nav class="bottom-total-b">  

<p style="text-align: right; font-size: x-small;">Cash paid  - Ksh. <?php echo $row->cash;?> /= 
  <a>Mpesa paid  - Ksh. <?php echo $row->mpesa;?> /= </a> </p>

<hr style="opacity:.1">
<h3>Grand Total  - Ksh. <?php echo $row->amount;?> /=   </h3>
<hr style="opacity:.1">

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