<?
session_start();
include('includes/config.php');
include('defs.php');

if(strlen($_SESSION['aid'])==0)
  { header('location:index.php');
}

$usr=$_SESSION['uname'];



 ?>


 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>GLAKB  | Leakage Stock Entry</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!--Function Email Availabilty---->

</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
<?php include_once("includes/navbar.php");?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
 <?php include_once("includes/sidebar.php");?>

 
  <div class="content-wrapper">

<?
$result12 = mysqli_query($link, "select Role from user_details where Name='$usr' ");
$row12=  mysqli_fetch_object($result12);
$role = $row12->Role;
if($role=='Operator')
{
$result = mysqli_query($link, "select Role from user_priv where Name='$usr' and Form='Leakage' ");
$row=  mysqli_fetch_object($result);
$role = $row->Role;
if($role=='0')
{
{
echo"<center>";
echo"<br><br><br><br><br>";
echo"<img src='users.png' width='160' height='160'><br>";
echo "<font size=6 color=red><b>ACCESS DENIED!</b><br>";
echo "<font size=4 color=darkblue><b>You Do Not Have Permission to View This Page.</b><br>";
goto a;
}
}
}
?> 


<form name="stock" action="" method="POST" onSubmit='return validateForm()'>
<style>




        .input-box {
            width: 100%;
            padding: 1px;
            margin-top: 1px;
            margin-bottom: 1px;
            border: 1px solid gainsboro;
            border-radius: 4px;
            font-size: 16px;

        }
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 98%;
}

#customers td, #customers th {
  border: 1px solid #4444;
  padding: 8px;
}

#customers tr:nth-child(even){background-color:gainsboro ;}


#customers th {
  padding-top: 8px;
  padding-bottom: 8px;
  text-align: center;
background-color:dodgerblue;
    color: white;
}
</style> 
 <style>
        #suggestions {
            border: 2px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 350px;
            max-height: 250px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions div {
            padding: 3px;
            cursor: pointer;
        }
        #suggestions div:hover,
        #suggestions .selected {
            background-color: #f0f0f0;
        }

#suggestions1 {
            border: 2px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 300px;
            max-height: 250px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions1 div {
            padding: 3px;
            cursor: pointer;
        }
        #suggestions1 div:hover,
        #suggestions1 .selected {
            background-color: #f0f0f0;
        }



    </style>  
<link rel="stylesheet" href="css/style.css">



<script language="javascript">
function validateForm() {

      if ((document.stock.warehouse.value) == '') {
        alert("Please Select Warehouse First!");
        document.stock.warehouse.focus();
        return false;
      }



if ((document.stock.item3.value) == '') {
        alert("Please Select Product Name");
        document.stock.item3.focus();
        return false;
      }


      if ((document.stock.dcn.value) == '' || (document.stock.dcn.value) == '0') {
        alert("Please Enter Quantity First!");
        document.stock.dcn.focus();
        return false;
      }

      
      return true;
    }

function moveToAmount1() {
    let amountInput = document.getElementById("dcn");
    if (amountInput) {
        amountInput.focus();
    }
}

 

        function show_cbo1()
                {
                        var val = document.getElementById('warehouse').value;
                          
                        location.href = "p_dead.php?Ware="+val;
                }
                

                


               function show_cbo2()
                {
                        var val2= document.getElementById('warehouse').value;
                         var val = document.getElementById('item3').value;
                         
                        location.href = "p_dead.php?Code="+val+"&Ware="+val2;
                }
       





                      function processkey(e)



       

         {
                        var kc = (window.Event?e.which:e.keyCode)
                        if (kc >=48 && kc<= 57 || kc ==8 || kc == 46)
                                return true
                        else
                                return false

                }
                
     
</script>


<style type="text/css">
#left                 {width:50%;height:auto;float:left;padding-top:0%;padding-left:0.5%}
#right                {width:50%;height:auto;float:right;padding-top:0%;padding-right:0.5%}

</style>
<div id="left">




 <table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="5">LEAKAGE STOCK ENTRY FORM</th>

  </tr>      


                               <td><b><font size=3 color="black">Select Warehouse:</b></td>
                        <td align=left colspan="3">
                                <select class="form-control"  name="warehouse" id="warehouse"  onchange="show_cbo1()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Ware']))
                                        {
                                                $Ware = $_GET['Ware'];
                                                
                                                
                
                                        }
                                else                        
                                        $Ware = "";
                                        
                                        //include('config.php');
                                  $result = mysqli_query($link, "select Distinct Name from warehouse order by Name");
       while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Ware == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>
                                
                       
<TR>
<td><b><font size=3 color="black">Select Product Name:</b></td>
                        <td>
                                <select class="form-control" name="item3" id="item3" style=font-size="10" onchange="show_cbo2()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Code']) && !isset($_POST['reset']))
                                        {
                                                $Code = $_GET['Code'];
                                              
                                                      }
                                else                        
                                       $Code = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Distinct Full_Name from products order by Article");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code == $row->Full_Name)
                                                        {
                                                                echo "<option value=\"$row->Full_Name\" selected=\"selected\">$row->Full_Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Full_Name\">$row->Full_Name</option>";
                                                        }
                                        }
                                ?>                            </select>


<?
$prod=$Code;
$ware=$Ware;

$result = mysqli_query($link, "select Unit_Liter, Carton_Qty, DP from products where Full_Name='$prod'");
$row1 = mysqli_fetch_object($result);             


$result12 = mysqli_query($link, "select SUM(Tqty) as stk from purchases where Name='$prod' and Warehouse='$ware'  group by Name");
 while($row12 = mysqli_fetch_array($result12))
{
$sin=$row12['stk'];
}

$result13 = mysqli_query($link, "select SUM(Tunit) as stk from cust_sales where Name='$prod' and Warehouse='$ware' group by Name");
 while($row13 = mysqli_fetch_array($result13))
{
$sout=$row13['stk'];
}

$bal=$sin-$sout;
?> 


<tr>
<td><b><font size=3 color="black">SKU:</b></td>
<?
echo "<td align=right><b><input type='text' class='input-box' name='sku' size='5' style='color: black; font-family: Times-Roman; font-size:2' id='sku' value='$row1->Unit_Liter' onkeypress='return processkey(event)' onfocus='this.select()' readonly >";
?>
<tr>
<td><b><font size=3 color="black">Unit/Carton:</b></td>
<?
echo "<td align=right><b><input type='text' class='input-box' name='ucart' size='5' style='color: black; font-family: Times-Roman; font-size:2' id='ucart' value='$row1->Carton_Qty' onkeypress='return processkey(event)' onfocus='this.select()' readonly >";
?>

<tr>


<td><b><font size=3 color=black>Likeage Unit:</b></td>

<td colspan="1">
<select class="form-control" name=iqty id=iqty onchange="moveToAmount1()">
<option value='CTN'>CTN</option>
<option value='PACKS'>PACKS</option>
<option value='LITERS'>LITERS</option>

</select>



              
<tr>
<td width="200"><b><font size=3 color="black">Likeage Quantity:</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='dcn' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='dcn' value='0' onkeypress='return processkey(event)' onfocus='this.select()' required autofocus >";



if(isset($_POST['add']))
{
$name = $_POST['item3'];
$ware = $_POST['warehouse'];
$unit = $_POST['sku'];
$ucart = $_POST['ucart'];


$qty = $_POST['dcn'];
$iqty = $_POST['iqty'];


if($iqty=='CTN')
{
$tcart=$qty;
$tunit=$ucart*$qty;
$tlit=$tunit*$unit;    
}  

if($iqty=='LITERS')
{
$tunit=$qty/$unit;
$tcart=$tunit/$ucart;
$tlit=$qty;    
}

if($iqty=='PACKS')
{
$tunit=$qty;
$tlit=$qty*$unit;    
$tcart=$qty/$ucart;
}



$sql1 = "SELECT * FROM temp_dead where Name='$name' and Warehouse='$ware'  ";
$qq = $link->query($sql1);
if ($qq->num_rows == 1) 
{
echo "<script language='javascript'>";
echo "alert('Product Already added!')";
echo "</script>";
goto donot1;
}




mysqli_query($link, "insert into temp_dead values ('','$name','$ware','$unit','$ucart','$tlit','$tunit','$tcart','$r','$tot','$usr')");
donot1:
                                              
 }




if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_dead where Id='$lid'");
if($query){
//unlink($ppicpath);
//echo "<script type='text/javascript'> document.location = 'sales.php'; </script>";
}
}

if(isset($_POST['save']))
{

date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());


$date = strtotime($_POST['strDate']);
$date = date("Y-m-d",$date);       

$rems = $_POST['rem'];


$result = mysqli_query($link, "select MAX(D_No) as inv from purchases");
while($row = mysqli_fetch_object($result))
{
$inv=$row->inv+1;
}        

$strQuery1 = "select count(*) as cnt1 from temp_dead where addby='$usr' ";
$result121 = mysqli_query($link, $strQuery1);
$rstRow = mysqli_fetch_array($result121);
$counter= $rstRow["cnt1"];
if($counter==0)
{
echo "<script language='javascript'>";
echo "alert('First Enter Details.')";
echo "</script>";
goto donot;
}


$t_pak=0;
$t_cart=0;
$t_lit=0;

$result = mysqli_query($link, "select * from temp_dead where addby='$usr' order by Id");
while($row = mysqli_fetch_object($result))
{
$t_lit= $row->Qty * -1;
$t_pak= $row->Tqty * -1;
$t_cart= $row->Carton * -1;

mysqli_query($link, "insert into purchases (Inv_Date, Inv_Type, Name, Unit, Carton_Qty, Qty, Tqty, Carton, addby, addon, Warehouse, Inv_N, D_No) values ('$date','Likeage Stock','$row->Name','$row->Size','$row->U_Cart','$t_lit','$t_pak','$t_cart','$row->addby','$the_time','$row->Warehouse','Likeage Stock','$inv')");
}
 

mysqli_query($link, "delete from temp_dead where addby='$usr' ");



echo "<script language='javascript'>";
 echo "alert('Likeage Stock Entered Successfully.')";
echo "</script>";
$link = "<script>window.open('p_dead.php','_self')</script>";
echo $link;

donot:
}
 
    
        
        





                       
          

?>



<tr>
<td colspan="4" align="center">


<input type="submit" name="add" value="Save Entry" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">
</form>
</table>


<form method="post">
<table align="center" id="customers">
<br> 
<tr>
    <th colspan="4"><font size="5">DATE DETAILS</th>

  </tr> 



<tr>
<?
$result13 = mysqli_query($link, "select MAX(D_No) as inv from purchases " );
while($row13  = mysqli_fetch_object($result13))
{
$inv=$row13->inv+1;
}

 $strvoucher= Date("Y-m-d");

if(isset($_POST['strDate']) && !isset($_POST['reset']) )
                                        {

   $strvoucher=$_POST['strDate'];
 AddDate("<b><font size=3 color=black>Entry Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
     
                                        }
                                else
                                        {
  AddDate("<b><font size=3 color=black>Entry Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
                                           }

echo " Auto-No. <input type='text' id='strinv' name='strinv' class='widerHeight' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:20px;' value='$inv' readonly   />";


$result12 = mysqli_query($link, "select SUM(Total) as bal from temp_pur where addby='$usr' ");
$row = mysqli_fetch_object($result12);

 ?>
 


<tr>
<td ><b><font size=3 color="black">Remarks (If any):</b></td>
<td>
<?
echo "<b><input type='text' name='rem' class='input-box' size='15' style='color: black; font-family: Times-Roman; font-size:2' id='rem' value=''>";
?>

<tr>            
<td align="center" colspan="1" >

 <input type="submit" name="save" value="Save Details" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">
 <td align="center"> 



 </table>
</div>



 


<div id="right">  

<div class="table-container">


<table align="center" id="customers">



<tr>
    <th colspan="9"><font size="5">LIKEAGE STOCK DETAILS</th>

  </tr>
<?

 echo "<center>";
      
                $result = mysqli_query($link, "select * from temp_dead where addby='$usr' order by Id");
                                  
 
        
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Warehouse</b>";
        echo "        </td>";
           
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Product Name</b>";
        echo "        </td>";
          
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>SKU</b>";
        echo "        </td>";
        
       
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>CTN</b>";
        echo "        </td>";
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>PACKS</b>";
        echo "        </td>";
         
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>LTRS</b>";
        echo "        </td>";
 
               
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Deletion</b>";
        echo "        </td>";

 echo"  </td>";

        echo "</tr>";

       






$gtot=0;
$count=0;
$one=1;
while($row = mysqli_fetch_object($result))
{
$count=$one+$count;
$gtot=$row->Total+$gtot;
echo "<td align=center><font size=3 color=black>$row->Warehouse</td>";
echo "<td align=left><font size=3 color=black>$row->Name</td>";
echo "<td align=center><font size=3 color=black>$row->Size</td>";
echo "<td align=right><font size=3 color=black>$row->Carton</td>";
echo "<td align=right><font size=3 color=black>$row->Tqty</td>";
echo "<td align=right><font size=3 color=black>$row->Qty</td>";

?>
<td align=center>
       
 <a href="p_dead.php?action=delete&&tid=<?php echo $row->Id; ?>" style="color:red;" title="Delete Record" onclick="return confirm('Do you really want to delete this record?');"><i class="fa fa-trash" aria-hidden="true"></i> </a>

<?
echo "</tr>";
}


?>

</table>
</div>

       



<?

           


a:

               
        include "footer.php";
?>
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- bs-custom-file-input -->
<script src="../plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
$(function () {
  bsCustomFileInput.init();
});
</script>
</body>
</html>
<?php  ?>