<?
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  | Dead Raw 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='Raw-Dead' ");
$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: 4px;
}

#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.item33.value) == '') {
        alert("Please Select Raw Material!");
        document.stock.item33.focus();
        return false;
      }



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


          

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

      
      return true;
    }



 

        function show_cbo1()
                {
                        var val = document.getElementById('item33').value;
                          var val2 = document.getElementById('item').value;
                      
                        location.href = "dead2.php?Code2="+val+"&Code3="+val2;
                }
                

 
  function show_cbo11()
                {
                        var val = document.getElementById('item').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "dead2.php?Code3="+val;
                }
                


               function show_cbo2()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                         var val3 = document.getElementById('item').value;
                      
                        location.href = "dead2.php?Code="+val+"&Code2="+val2+"&Code3="+val3;
                }
       
 function show_cbo3()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                         var val1 = document.getElementById('psize').value;
                      
                        location.href = "dead2.php?Code="+val+"&Code2="+val2+"&Code3="+val1;
                }





                      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">RAW DEAD STOCK</th>

  </tr>      

<TR>
<td><b><font size=3 color="black">Select Company:</b></td>
                        <td>
                                <select class="form-control" name="item" id="item" style=font-size="16" onchange="show_cbo11()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Code3']) && !isset($_POST['reset']))
                                        {
                                                $Code3 = $_GET['Code3'];
                                                      }
                                else                        
                                       $Code3 = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Name from acc_heads where (M_Head='Company' or M_Head='Supplier') order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code3 == $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 Raw Name:</b></td>
                        <td>
                                <select class="form-control" name="item33" id="item33" style=font-size="16" onchange="show_cbo1()">
                                <option value="">select</option>
                                <?php
                                if(isset($_GET['Code2']) && !isset($_POST['reset']))
                                        {
                                                $Code2 = $_GET['Code2'];
                                              
                                                      }
                                else
                                       $Code2 = "";

                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from raw_names order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code2 == $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'];
                                                  $Code2 = $_GET['Code2'];
                                                      }
                                else                        
                                       $Code = "";
                                        
                                        //include('config.php');
   $result = mysqli_query($link, "select Distinct Name from raw_details where Type='$Code2' order by Name");
                                         while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>


<?
$prod=$Code;
$cat=$Code2;
$comp=$Code3;

$result32 = mysqli_query($link, "select Rate from raw_details where Name='$prod' and Type='$cat' ");
$row32 = mysqli_fetch_object($result32);                              


$result12 = mysqli_query($link, "select SUM(Qty) as stk from raw_purchases where Name='$prod' and Category='$cat' and Company='$comp'  group by Category, Name");
 while($row12 = mysqli_fetch_array($result12))
{
$sin=$row12['stk'];
}

$result13 = mysqli_query($link, "select SUM(S_Out) as stk from raw_sales where Name='$prod' and Category='$cat' and Company='$comp'  group by Category, Name");
 while($row13 = mysqli_fetch_array($result13))
{
$sout=$row13['stk'];
}

$bal=$sin-$sout;
?> 


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

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='price' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='price' value='$row32->Rate' onfocus='this.select()' readonly>";

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

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='aqty' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='aqty' value='$bal' onfocus='this.select()' readonly>";

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

    
<?

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



if(isset($_POST['add']))
{
$name = $_POST['item3'];
$comp1 = $_POST['item'];

$cat = $_POST['item33'];
$q = $_POST['qty'];
$aq = $_POST['aqty'];
$r = $_POST['price'];

if($aq<$q)
{
echo "<script language='javascript'>";
echo "alert('Less Available Quantity.')";
echo "</script>";
goto donot1;
}


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



$tot=$r*$q;
mysqli_query($link, "insert into temp_sout values ('','$name','$cat','$size','$q','$r','$tot','$usr','$ucart','$comp1')");
donot1:
                                              
 }




if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_sout 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(Inv_No) as inv from raw_company");
while($row = mysqli_fetch_object($result))
{
$inv=$row->inv+1;
}  

$salesInv = str_pad($inv, 4, "0", STR_PAD_LEFT);
$salesInv = "RD".$salesInv;



$strQuery1 = "select count(*) as cnt1 from temp_sout 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;
}



$gtot=0;
$result = mysqli_query($link, "select * from temp_sout where addby='$usr' order by Id");
while($row = mysqli_fetch_object($result))
{
///$qty= $row->Qty * -1;
//$tot= $row->Total * -1;

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv_No, Inv_Det, Category, Name, Size, SKU, Inv_Type, S_Out, Rate, Total, addby, Company, addon, Status, D_Remarks) values ('$date','$inv','$salesInv','$row->Category','$row->Name','$row->Carton_Qty','$row->Size','Dead Stock','$row->Qty','$row->Rate','$row->Total','$usr','$row->Company','$the_time','1','$rems')");
$comp2=$row->Company;
$cat2=$row->Category;


$gtot=$row->Total+$gtot;
}
 
 //$gtot=$gtot * -1;
mysqli_query($link, "insert into raw_company (Category, Inv_Date, Inv_No, Inv_Det, Company, Inv_Type, Total, addby, addon, Remarks) values ('$cat2','$date','$inv','$salesInv','$comp2','Dead Stock','$gtot','$usr','$the_time','$rems')");

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

saveent:
echo "<script language='javascript'>";
 echo "alert('Dead Stock Entered Successfully.')";
echo "</script>";
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(Inv_No) as inv from raw_company " );
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">DEAD STOCK DETAILS</th>

  </tr>
<?

 echo "<center>";
      
                $result = mysqli_query($link, "select * from temp_sout where addby='$usr' order by Id");
                                  
 
        
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Raw Material</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>Dead Quantity</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->Category</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->Qty</td>";

?>
<td align=center>
       
 <a href="dead2.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  ?>