<?
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  | Add Raw Material</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='Add-Raw' ");
$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.item33.value) == '') {
        alert("Please Select Raw Material!");
        document.stock.item33.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.price.value) == '' || (document.stock.price.value) == '0') {
        alert("Please Enter Price First!");
        document.stock.price.focus();
        return false;
      }

      
      return true;
    }



 
                
function openAndPrintPDF() {
            // Open the PDF in a new tab
            var pdfWindow = window.open('rep_invoice.php?usr=<?php echo $usr ?>', '_blank');

          
          
        }

        function show_cbo1()
                {
                        var val = document.getElementById('item33').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "add_raw.php?Code2="+val;
                }
                

                
               function show_cbo2()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                      
                        location.href = "add_raw.php?Code="+val+"&Code2="+val2;
                }
       
 function show_cbo3()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                         var val1 = document.getElementById('psize').value;
                      
                        location.href = "add_raw.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">ADD RAW MATERIAL INITIAL STOCK DETAILS</th>

  </tr>      
<tr>
<tr>
                               <td><b><font size=3 color="black">Select Raw Name:</b></td>
                       <td colspan="3">
                        
                                <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 colspan="3">
                                <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>


<?
$result14 = mysqli_query($link, "select Carton_Qty, Unit_Liter, Size from products where Full_Name='$Code' " );
while($row12  = mysqli_fetch_object($result14))
{
$ucart=$row12->Carton_Qty;
$ulit=$row12->Unit_Liter;
$size=$row12->Size;

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

    
<?

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

?>
  <td width="200"><b><font size=3 color="black">Unit / Carton:</b></td>

    
<?

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

?>
<td colspan="2">
<tr>
<td width="200"><b><font size=3 color="black">Price / Quantity:</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='0' onkeypress='return processkey(event)' onfocus='this.select()' required >";

?>
<td colspan="2">
<?


if(isset($_POST['add']))
{
$cat = $_POST['item33'];
$name = $_POST['item3'];
$sku = $_POST['sku'];
$ucart = $_POST['ucart'];
$q = $_POST['qty'];
$r = $_POST['price'];


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

$tot=$r*$q;

if($cat=='Seal')
{
mysqli_query($link, "insert into temp_raw values ('','Seal','$cat','$sku','$ucart','$size','$q','$r','$tot','$usr')");
goto donot1;
}

if($cat=='Tape')
{
mysqli_query($link, "insert into temp_raw values ('','Tape','$cat','$sku','$ucart','$size','$q','$r','$tot','$usr')");
goto donot1;
}


mysqli_query($link, "insert into temp_raw values ('','$name','$cat','$sku','$ucart','$size','$q','$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_raw 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);       
$supp = $_POST['item'];
$rems = $_POST['rem'];

if(empty($_POST['item'])){
echo "<script language='javascript'>";
echo "alert('Kindly Select Company Name.')";
echo "</script>";
goto donot;
}
else
{

$result = mysqli_query($link, "select MAX(Inv_No) as inv from r_details");
while($row = mysqli_fetch_object($result))
{
$inv=$row->inv+1;
}        
$prawInv = str_pad($inv, 4, "0", STR_PAD_LEFT);
$prawInv = "OB".$prawInv;


$strQuery1 = "select count(*) as cnt1 from temp_raw 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;
$qty1=0;
$result = mysqli_query($link, "select * from temp_raw where addby='$usr' order by Id");
while($row = mysqli_fetch_object($result))
{
//mysqli_query($link, "insert into raw (Company, Category, Name, SKU,  Size, Description, Rate, R_Date, addby, addon) values ('$supp','$row->Category','$row->Name','$row->SKU','$row->Size','$row->Ucart','$row->Rate','$date','$usr','$the_time')");
$gtot=$row->Total+$gtot;
$cat=$row->Category;
$qty1=$row->Qty;
if($qty1>0)
{
mysqli_query($link, "insert into raw_purchases (Category, Name, Size, Inv_No, Inv_Det, Inv_Date, Inv_Type, Company, Qty, Rate, Total, addby, addon, Status, Inv_N, SKU) values ('$row->Category','$row->Name','$row->Size','$inv','$prawInv','$date','OB','$supp','$row->Qty','$row->Rate','$row->Total','$usr','$the_time','1','OB','$row->SKU')");

$result47 = mysqli_query($link, "update raw_details set Rate='$row->Rate', R_Date='$date' where Name = '$row->Name' and Type='$row->Category' ");


}

}
 


 mysqli_query($link, "insert into r_details (Category, Inv_Date, Inv_No, Inv_Det, Company, Inv_Type, Total, G_Total, addby, addon, Remarks, Status, Inv_N) values ('Op. Bal','$date','$inv','$prawInv','$supp','OB','$gtot','$gtot','$usr','$the_time','$rems','1','OB')");

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

saveent:
echo "<script language='javascript'>";
 echo "alert('Raw Material Added 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">COMPANY DETAILS</th>

  </tr> 


             
<TR>
<td><b><font size=3 color="black">Select Target Company / Supplier:</b></td>
                        <td>
                                <select class="form-control" name="item" id="item" style=font-size="16" >
                                <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($Code == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>

<tr>
<?
$result13 = mysqli_query($link, "select MAX(Inv_No) as inv from r_details " );
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>Stock Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
     
                                        }
                                else
                                        {
  AddDate("<b><font size=3 color=black>Stock 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   />";



 ?>
 


<tr>
<td width="290"><b><font size=3 color="black">Reference Number / 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="2">

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




 </table>
</div>



 


<div id="right">  

<div class="table-container">


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



<tr>
    <th colspan="9"><font size="5">RAW MATERIAL DETAILS</th>

  </tr>
<?

 echo "<center>";
      
                $result = mysqli_query($link, "select * from temp_raw 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=left>";
        echo "                <b><font size=3 color=darkblue>Product Name</b>";
        echo "        </td>";
          
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Product Size</b>";
        echo "        </td>";
        
           echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>SKU</b>";
        echo "        </td>";
        
echo "        <td align=right>";
        echo "                <b><font size=3 color=darkblue>Quantity</b>";
        echo "        </td>";
  echo "        <td align=right>";
        echo "                <b><font size=3 color=darkblue>Rate</b>";
        echo "        </td>";       
 echo "        <td align=right>";
        echo "                <b><font size=3 color=darkblue>Total</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=center><font size=3 color=black>$row->SKU</td>";

echo "<td align=right><font size=3 color=black>$row->Qty</td>";
echo "<td align=right><font size=3 color=black>$row->Rate</td>";
echo "<td align=right><font size=3 color=black>$row->Total</td>";

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