<?
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  | Raw Stock Exchange Form</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-Out' ");
$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.item3.value) == '') {
        alert("Please Select Product Name");
        document.stock.item3.focus();
        return false;
      }

if ((document.stock.scomp.value) == '') {
        alert("Please Select Source Company.");
        document.stock.scomp.focus();
        return false;
      }

if ((document.stock.tcomp.value) == '') {
        alert("Please Select Target Company.");
        document.stock.tcomp.focus();
        return false;
      }





          

      if ((document.stock.qty.value) == '' || (document.stock.qty.value) == '0') {
        alert("Please Enter Quantity First!");
        document.stock.qty.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 = "s_out.php?Code2="+val;
                }
                

                
               function show_cbo2()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                      
                        location.href = "s_out.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 = "s_out.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

                }
                
  function moveToAmount() {
    let amountInput = document.getElementById("qty");
    if (amountInput) {
        amountInput.focus();
    }
}

</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 STOCK TRANSFER FORM</th>

  </tr>      
<tr>
<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 where (Name!='Seal' and Name!='Tape') 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;


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

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

$stk=$sin-$sout;



$result32 = mysqli_query($link, "select Rate from raw_details where Name='$prod' and Type='$cat' ");
$row32 = mysqli_fetch_object($result32);                              
?> 
<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><b><font size=3 color="black">Source Company:</b></td>
                        <td>
                                <select class="form-control" name="scomp" id="scomp" style=font-size="16" onchange="moveToAmount()" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Scomp']) && !isset($_POST['reset']))
                                        {
                                                $Scomp = $_GET['Scomp'];
                                                      }
                                else                        
                                       $Scomp = "";
                                        
                                        //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($Scomp == $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">Target Company:</b></td>
                        <td>
                                <select class="form-control" name="tcomp" id="tcomp" style=font-size="16" onchange="moveToAmount()" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Tcomp']) && !isset($_POST['reset']))
                                        {
                                                $Tcomp = $_GET['Tcomp'];
                                                      }
                                else                        
                                       $Tcomp = "";
                                        
                                        //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($Tcomp == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>           


            
<tr>
<td width="200"><b><font size=3 color="black">Transfer 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 >";

?>
<tr>
<td width="220"><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=''>";



echo "<tr>";
 $strvoucher= Date("Y-m-d");

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

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


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

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);       

$name = $_POST['item3'];
$cat = $_POST['item33'];
$size = $_POST['sku'];
//$aq = $_POST['aqty'];
$r = $_POST['price'];
$q = $_POST['qty'];

$ucart = $_POST['ucart'];
$tcomp = $_POST['tcomp'];
$scomp = $_POST['scomp'];
$rems = $_POST['rem'];
$tot=$r*$q;


/*
$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;
}
*/
$result = mysqli_query($link, "select MAX(T_No) as inv from raw_purchases");
while($row = mysqli_fetch_object($result))
{
$inv=$row->inv+1;
}    
$nq = $q * -1;
$ntot = $tot * -1;


mysqli_query($link, "insert into raw_purchases (Category, Name, SKU, Inv_Date, Inv_Type, Company, Qty, Rate, Total, addby, addon, Status, Inv_N, T_No) values ('$cat','$name','$size','$date','Transfer','$scomp','$nq','$r','$ntot','$usr','$the_time','1','Transfer','$inv')");

mysqli_query($link, "insert into raw_purchases (Category, Name, SKU, Inv_Date, Inv_Type, Company, Qty, Rate, Total, addby, addon, Status, Inv_N, T_No) values ('$cat','$name','$size','$date','Transfer','$tcomp','$q','$r','$tot','$usr','$the_time','1','Transfer','$inv')");


echo "<script language='javascript'>";
echo "alert('Transfered Successfully.')";
echo "</script>";

donot1:
                                              
 }




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

                       
          

?>



<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>

 




</div>



 


<div id="right">  

<div class="table-container">


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



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

  </tr>
<?
date_default_timezone_set("Asia/Karachi");
$date2=date("Y-m-d"); 

 echo "<center>";
      
                $result = mysqli_query($link, "select * from raw_purchases where addby='$usr' and Inv_Type='Transfer' and Inv_Date='$date2'  order by Id");
                                  
   
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Company</b>";
        echo "        </td>";
        
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Raw Category</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>Transfered 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->Company</td>";

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=right><font size=3 color=black>$row->Qty</td>";

?>
<td align=center>
       
 <a href="s_out.php?action=delete&&tid=<?php echo $row->T_No; ?>" 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  ?>