<?
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  | Product Transfer 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='Transfer' ");
$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;
}
}
}
?>




<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>   
<link rel="stylesheet" href="css/style.css">
<?


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);      
$w1 = $_POST['warehouse'];
$name = $_POST['item3'];
$stk = $_POST['stock'];
$w2 = $_POST['warehouse1'];
$qty = $_POST['qty'];
$iqtyy = $_POST['iqty'];

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

 
$result1 = mysqli_query($link,"select * from products where Full_Name = '$name'");
while($rstRow = mysqli_fetch_array($result1))
{
$grade=$rstRow['Grade'];
$ucart=$rstRow['Carton_Qty'];
$unit=$rstRow['Unit_Liter'];
}



if($w1==$w2)
{
echo "<script language='javascript'>";
echo "alert('Source and Target Warehouses can not be same!')";
echo "</script>";
goto donot1;
}





if($qty>$stk || $qty==0)
{
echo "<script language='javascript'>";
echo "alert('Less Stock or Zero Quantity!')";
echo "</script>";
goto donot1;
}




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

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

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







$nqty = $tcart * -1;
$npacks = $tunit * -1;
$nliters = $tlit * -1;


mysqli_query($link, "insert into purchases (Inv_Date, Inv_Type, Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, addby, addon, Inv_N, Warehouse, T_No) values ('$date','Transfer','$name','$grade','$ucart','$unit','$nliters','$npacks','$nqty', '$usr', '$the_time', 'Transfer', '$w1','$inv')");

mysqli_query($link, "insert into purchases (Inv_Date, Inv_Type, Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, addby, addon, Inv_N, Warehouse, T_No) values ('$date','Transfer','$name','$grade','$ucart','$unit','$tlit','$tunit','$tcart', '$usr', '$the_time', 'Transfer', '$w2','$inv')");

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

donot1:
                                              
 }




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

                   
          

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

<script language="javascript">



function validate()
{
if((document.stock.warehouse.value) == '')
{
alert("Kindly Select Source Warehouse!");
document.stock.warehouse.focus();
return false;
}


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

if((document.stock.warehouse1.value) == '')
{
alert("Kindly Select Target Warehouse!");
document.stock.warehouse1.focus();
return false;
}

if((document.stock.qty.value) == '' || (document.stock.qty.value) == 0)
{
alert("Kindly Enter Transfer Quantity!");
document.stock.qty.focus();
return false;
}

return true;
}


window.onload= function show()
                        {
                                if(document.stock.scan_code.value=="" || document.stock.scan_code.value==0 )
                                        {

                                                document.stock.scan_code.focus()
                                        }
                                        }
 function formconfirm() {
return confirm("You Clicked the Button. Continue?<? echo "$coursename";?>");
}
                

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

                
               function show_cbo2()
                {
                      var val = document.getElementById('warehouse').value;
                           //var val3 = document.getElementById('strDate').value;
                         var val2 = document.getElementById('item3').value;
                      
                        location.href = "transfer.php?Ware="+val+"&Code="+val2;
                }
       
  function show_cbo3()
                {
                      var val = document.getElementById('warehouse').value;
                           var val3 = document.getElementById('warehouse1').value;
                         var val2 = document.getElementById('item3').value;
                      var val4 = document.getElementById('iqty').value;
                        location.href = "transfer.php?Ware="+val+"&Code="+val2+"&Ware1="+val3+"&iqty1="+val4;
                }

function show_cbo4()
{
var val = document.getElementById('warehouse').value;

var val2 = document.getElementById('item3').value;
var val4 = document.getElementById('iqty').value;

location.href = "transfer.php?Ware="+val+"&Code="+val2+"&iqty1="+val4;
}



function getStock() {
            var code = document.getElementById("scan_code").value.trim();
            var warehouse = document.getElementById("warehouse").value;

            if(code !== "" && warehouse !== "") {
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "get_stock.php", true);
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        try {
                            var data = JSON.parse(xhr.responseText);

                            document.getElementById("product_name").value = data.product_name;
                            document.getElementById("stock").value = data.quantity;
                        } catch (e) {
                            document.getElementById("product_name").value = "Not Found";
                            document.getElementById("stock").value = "0";
                        }

                        // Reset scan box for next scan
 var scanBox = document.getElementById("scan_code");
    scanBox.focus();
    scanBox.select();

                       // document.getElementById("scan_code").value = "";
                        //document.getElementById("scan_code").focus();
                    }
                };
                xhr.send("code=" + code + "&warehouse=" + warehouse);
            }
        }

        // Detect Enter key (barcode scanner usually sends Enter)
        function handleEnter(event) {
            if (event.key === "Enter") {
                event.preventDefault(); // stop form submission
                getStock();
            }
        }







                      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>

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

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

  </tr>    

   <?

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


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

                                       ?>
                       
<td><b><font size=3 color="black">Transfer No.:</b></td>
<td>
<?

echo "<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 align=left ><b><font size=3 color=black>Select Source 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 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'];
                                                      }
                                else                        
                                       $Code = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select 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>




<tr>
<td><b><font size=3 color="black">Quantity Unit:</b></td>
                        <td colspan="3">
                                <select class="form-control" name="iqty" id="iqty" style=font-size="10" onchange="show_cbo4()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['iqty1']) && !isset($_POST['reset']))
                                        {
                                                $iqty1 = $_GET['iqty1'];
                                                      }
                                else                        
                                       $iqty1 = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Name from unts order by Id");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($iqty1 == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>





<?

if($iqty1=='CTN')
{
$rst1 = mysqli_query($link, "select sum(Carton) as s_in from purchases where Name='$Code' and Warehouse='$Ware' ");
while($row1  = mysqli_fetch_object($rst1))
{
$sin=$row1->s_in;
}
$rst2 = mysqli_query($link, "select sum(Carton) as s_out from cust_sales where Name='$Code' and Warehouse='$Ware'");
while($row2  = mysqli_fetch_object($rst2))
{
$sout=$row2->s_out;
}
$stk = $sin - $sout;
}

elseif($iqty1=='PACKS')
{
$rst1 = mysqli_query($link, "select sum(Tqty) as s_in from purchases where Name='$Code' and Warehouse='$Ware' ");
while($row1  = mysqli_fetch_object($rst1))
{
$sin=$row1->s_in;
}
$rst2 = mysqli_query($link, "select sum(Tunit) as s_out from cust_sales where Name='$Code' and Warehouse='$Ware'");
while($row2  = mysqli_fetch_object($rst2))
{
$sout=$row2->s_out;
}
$stk = $sin - $sout;
}

elseif($iqty1=='LITERS')
{
$rst1 = mysqli_query($link, "select sum(Qty) as s_in from purchases where Name='$Code' and Warehouse='$Ware' ");
while($row1  = mysqli_fetch_object($rst1))
{
$sin=$row1->s_in;
}
$rst2 = mysqli_query($link, "select sum(Qty) as s_out from cust_sales where Name='$Code' and Warehouse='$Ware'");
while($row2  = mysqli_fetch_object($rst2))
{
$sout=$row2->s_out;
}
$stk = $sin - $sout;
}

?>




    

    
<tr>

<td><b><font size=3 color="black">Available Stock (Cartons):</b></td>

  <?

echo "<td align=right><b><input type='text' class='input-box' name='stock' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='stock' value='$stk' readonly >";

?>
<td colspan="2">
    

                
<TR>

<td align=left ><b><font size=3 color=black>Select Target Warehouse:</b></td>
                        <td align=left colspan="3">
                                <select class="form-control"  name="warehouse1" id="warehouse1" onchange="show_cbo3()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Ware1']))
                                        {
                                                $Ware1 = $_GET['Ware1'];
                                                
                                                
                
                                        }
                                else                        
                                        $Ware1 = "";
                                        
                                        //include('config.php');
                                  $result = mysqli_query($link, "select Distinct Name from warehouse order by Name");
       while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Ware1 == $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">Quantity (In Cartons):</b></td>

    
<?

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

?>


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


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

</table>




 </table>
</div>



 


<div id="right">  

<div class="table-container">


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



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

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

 echo "<center>";
      
                $result = mysqli_query($link, "select * from 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>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>Transfer ID</b>";
        echo "        </td>";
       
         
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Quantity (CTN)</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=left><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->T_No</td>";

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

?>
<td align=center>
       
 <a href="transfer.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 Transfer?');"><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  ?>