<?
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  | Stock Opening Balance</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='Stock-Open' ");
$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;
}
}
}

echo "                <form name='m_head' method=POST onSubmit='return validateForm()'>";


              ?>
              <html>
<head>
<style>
    .table-container {
    max-height: 500px;        /* Set max height for vertical scrolling */
    overflow-y: auto;
    overflow-x: auto;
    width: 100%;
    border: 1px solid #ccc;   /* Optional: visible border around container */
  }
#customers {
  font-family: Times-Bold;
  border-collapse: collapse;
  width: 98%;
}

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

}

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


#customers th {
  padding-top: 1px;
  padding-bottom: 1px;
  text-align: center;
  background-color:dodgerblue;
  color: white;

}


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

#customers1 td, #customers1 th {
  border: 1px solid #4444;
  padding: 5px;
}

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


#customers1 th {
  padding-top: 1px;
  padding-bottom: 1px;
  text-align: center;
  background-color:dodgerblue;
  color: white;

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

<script language="javascript" type="text/javascript">

   window.onload= function show()
                        {
                                if(document.m_head.strtit.value =="")
                                        {

                                                document.m_head.strtit.focus()
                                        }
                                        }
                                        
function formconfirm() {
return confirm("Do You want to Continue?<? echo "$coursename";?>");
}

 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 openAndPrintPDF() {
      var pdfWindow = window.open('rep_stock1.php?usr=<?php echo $usr ?>', '_blank');
    }
function validateForm1()
{
if((document.m_head1.ctype.value) == '')
{
alert("Please Select Company Name First!");
document.m_head1.ctype.focus();
return false;
}
if((document.m_head1.ware.value) == '')
{
alert("Please Select Warehouse!");
document.m_head1.ware.focus();
return false;
}

if((document.m_head1.dcn.value) == '0' )
{
alert("Please Enter DC-Number!");
document.m_head1.dcn.focus();
return false;
}

return true;
}


function validateForm()
{
if((document.m_head.ware.value) == '')
{
alert("Select Target Warehouse First!");
document.m_head.ware.focus();
return false;
}
if((document.m_head.item3.value) == '')
{
alert("Select Product Name First!");
document.m_head.item3.focus();
return false;
}



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


if((document.m_head.dp.value) == '' || (document.m_head.dp.value) == '0' )
{
alert("Please Enter Price First!");
document.m_head.dp.focus();
return false;
}

return true;
}


  function show_cbo1()
                {
                        var val = document.getElementById('item3').value;
                           var val3 = document.getElementById('ware').value;
                      
                        location.href = "add_stock.php?Code="+val+"&Ware="+val3;
                }
 function show_cbo2()
                {
                        var val = document.getElementById('ware').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "add_stock.php?Ware="+val;
                }

                


function myFunction() 
{
$x = document.getElementById("qty").value;
$y = document.getElementById("unit").value;
$c= 0;
$c=$x*$y;

document.getElementById("tqty").value=$c;


}



  </script>





 <table align="center" id="customers1">
       
<tr>
    <th colspan="6"><font size="4">PRODUCT STOCK OPENING BALANCE FORM</th>

  </tr>               
       
                       
<!DOCTYPE html>
<html>
<head>
    
    <style>
        #suggestions {
            border: 2px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 300px;
            max-height: 250px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions div {
            padding: 3px;
            cursor: pointer;
        }
        #suggestions div:hover,
        #suggestions .selected {
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>
<tr>
<td align=left><b><font size=4 color=black>Select Warehouse:</b></td>
                        <td align=left colspan="1">
                                <select class="form-control" name="ware" id="ware"  onchange="show_cbo2()">
                                <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>
                                <td colspan="4">
                                <tr>
<td><b><font size=4 color="black">Select Product Name:</b></td>
                        <td colspan="">
                                <select class="form-control" name="item3" id="item3" style=font-size="10" onchange="show_cbo1()">
                                <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 Full_Name");
                                        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>


<?
$result1 = mysqli_query($link, "select Unit_Liter, Carton_Qty, DP from products where Full_Name='$Code'");
while($row  = mysqli_fetch_object($result1))
{
$numbs=$row->Unit_Liter;
$ucart=$row->Carton_Qty;
$price=$row->DP;

}
?>



<td colspan="1"><b><font size=4 color="black">Liters / Unit:</b></td>

<?
echo "<td align=left><b><input type='text' name='unit' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='unit' value='$numbs' onkeypress='return processkey(event)'>";


?>


<td colspan="1"><b><font size=4 color="black">Carton Units:</b></td>

<?
echo "<td align=left><b><input type='text' name='ucart' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='ucart' value='$ucart' onkeypress='return processkey(event)'>";


?>

<tr>


<td><b><font size=4 color=black>Price/Litre:</b></td>
<?
echo "<td colspan=5><b><input type='text' name='dp' class='widerHeight' size='15' style='color: black; font-family: Times-Roman; font-size:2' id='dp' value='$price' onkeypress='return processkey(event)' >";
?>
<tr>


<td><b><font size=4 color=black>Stock Taken In:</b></td>

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

</select>
<td colspan="4">

<tr>
<td><b><font size=4 color=black>Quantity:</b></td>
<?

echo "<td align=left colspan=5><b><input type='text' name='tqty' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:44' id='tqty' value='0' onkeypress='return processkey(event)' onfocus='this.select()' required >";
?>
<tr>

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


 
</form>
</table>

<form name=form2 method="post">
  <table align="center" id="customers">
<tr>

<?
$strvoucher= Date("Y-m-d");
if(isset($_POST['strDate']) && !isset($_POST['reset']) )
{
$strvoucher=$_POST['strDate'];
AddDate("<b><font size=4 color=black>Stock Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
else
{
AddDate("<b><font size=4 color=black>Stock Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
?>  
 
<input type="submit" name="save1" value="Save All Stock" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">
<td colspan="3" align="left">
      <span class="icon text-white-50">
      <i class="fas fa-book"></i>
    </span>
 <button class="btn btn-info btn-icon-split" onclick="openAndPrintPDF()"><span class="text">View Report</span></button>
 


 </table>

 <?



if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_obal 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");
$date = date('d-m-Y (h:i:s a)', time());

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


$cname=$_POST['item3'];
$ware=$_POST['ware'];
$ucart=$_POST['ucart'];
$unit=$_POST['unit'];
$qty=$_POST['tqty'];
$rate=$_POST['dp'];
$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;
}

$ntot=$rate*$tlit;






$result1 = mysqli_query($link, "select Grade, Sufix, Item_Code from products where Full_Name='$Code'");
while($row  = mysqli_fetch_object($result1))
{
$grade=$row->Grade;
$suff=$row->Sufix;
$icode=$row->Item_Code;

}



$sql1 = "SELECT * FROM temp_cinv where Name='$cname' ";
$q = $link->query($sql1);
if ($q->num_rows == 1) 
{
echo "<script language='javascript'>";
echo "alert('Product Already added!')";
echo "</script>";
goto donothing;
}



mysqli_query($link, "insert into temp_obal (Warehouse, Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, Rate, Total, addby) values ('$ware','$cname','$grade','$ucart','$unit','$tlit','$tunit','$tcart','$rate','$ntot','$usr')");
  
//mysqli_query($link, "insert into temp_pur (Name, Company, Code, C_Name, P_Group, Packing) values ('$name','$comp','$cid','$cname','$pgroup','$pack')");
  

  echo "<script language='javascript'>";
echo "alert('Added Successfully!')";
echo "</script>";

  donothing:         
         }
?>
<form method="POST">
  <div class="table-container">
<table align="center" id="customers">
   
                        <?



 $date2=Date("Y-n-j");
$result = mysqli_query($link, "select * from temp_obal where addby='$usr' order by Id");
?>
<td style="background-color: dodgerblue; color: white;">S.No</td>
<td style="background-color: dodgerblue; color: white;">Warehouse</td>

<td style="background-color: dodgerblue; color: white;">Product Name</td>
<td style="background-color: dodgerblue; color: white;">Grade</td>
<td style="background-color: dodgerblue; color: white;">Litres/Unit</td>
<td style="background-color: dodgerblue; color: white;">Units/Carton</td>

<td style="background-color: dodgerblue; color: white;">Quantity (Cartons)</td>
<td style="background-color: dodgerblue; color: white;">Quantity (Units)</td>
<td style="background-color: dodgerblue; color: white;">Quantity (Liters)</td>

<td style="background-color: dodgerblue; color: white;">Rate</td>
<td style="background-color: dodgerblue; color: white;">Tot-Amt</td>
<td style="background-color: dodgerblue; color: white;">Deletion</td>

<?
               
           
       
        echo "</tr>";
        $count=0;
        $one=1;
$stot=0;
$tqty=0;
$aqty=0;
$tcarts=0;
                                        while($row = mysqli_fetch_object($result))
                                        {
                                        $count=$one+$count;

$stot = $row->Total+$stot;
$aqty=$row->Qty+$aqty;
$tqty=$row->Tqty+$tqty;
$tcarts=$row->Carton+$tcarts;


echo "<td align=center><font size=3 color=black><b>$count</b></td>";
echo "<td align=left><font size=3 color=black><b>$row->Warehouse</b></td>";

echo "<td align=left><font size=3 color=black><b>$row->Name</b></td>";
echo "<td align=center><font size=3 color=black>$row->Grade</td>";
echo "<td align=center><font size=3 color=black>$row->Unit</td>";
echo "<td align=center><font size=3 color=black>$row->Carton_Qty</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>";

echo "<td align=right><font size=3 color=black><b>$row->Rate</td>";
echo "<td align=right><font size=3 color=black>$row->Total</td>";
?>
<td align=center>
 <a href="add_stock.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>
</td>
<?
echo "<tr>";
}
echo "        <td align=center colspan=6>";
echo "<b><font size=3 color=darkblue>Grand Totals</b>";

echo "<td align=right><font size=3 color=black><b>$tcarts</b></td>";
echo "<td align=right><font size=3 color=black><b>$tqty</b></td>";
echo "<td align=right><font size=3 color=black><b>$aqty</b></td>";

echo "<td align=center></td>";

echo "<td align=right><font size=3 color=black><b>$stot</b></td>";
echo "<td align=center></td>";

$ntot1=$stot-$gdis-$gcom;



?>
</form>
</table>
</div>
<style type="text/css">
#left                 {width:52%;height:auto;float:left;padding-top:0%;padding-left:0.5%;font-weight:bold;}
#right                {width:48%;height:auto;float:right;padding-top:0%;padding-right:0.5%;font-weight:bold;}

</style>




               

<?

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


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

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



$sql2 = "SELECT * FROM temp_obal where addby='$usr' ";
$q = $link->query($sql2);
if ($q->num_rows == 0) 
{
echo "<script language='javascript'>";
echo "alert('No Records Found!')";
echo "</script>";
goto donot;
}

//$result = mysqli_query($link, "select max(Inv_No) as v_no2 from p_details ");
//$row =  mysqli_fetch_object($result);
//$v_no2 = $row->v_no2 + 1;




$ttot = 0;
$gtot = 0;
$ntot = 0;
$ctot = 0;
$stot = 0;
$atax1 = 0;
$result = mysqli_query($link, "select * from temp_obal where addby='$usr' order by Id");
while($row = mysqli_fetch_array($result))
{

$ttot = $row['Total']+$ttot;
$fname = $row['Name'];
$wareh = $row['Warehouse'];
//$pack = mysqli_real_escape_string($link, $pack);
//$cname = mysqli_real_escape_string($link, $cname);

$result42 = mysqli_query($link, "select Company from products where Full_Name='$fname'");
while($row42 = mysqli_fetch_array($result42))
{
$comp = $row42['Company'];
}


mysqli_query($link, "insert into purchases (Company, Inv_Date, Inv_Type, Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, Rate, Total, N_Total, addby, addon, Warehouse, Inv_N) values ('$comp','$invdate','Op. Balance','$row[Name]','$row[Grade]','$row[Carton_Qty]','$row[Unit]','$row[Qty]','$row[Tqty]','$row[Carton]','$row[Rate]','$row[Total]','$row[Total]','$usr','$date','$row[Warehouse]','POB')");

}



//mysqli_query($link, "insert into p_details (Company, Inv_No, Inv_Date, Inv_Type, Total, N_Total, G_Total, addby, addon, Warehouse, Inv_N) values ('$comp','$v_no2','$invdate','Op. balance','$ttot','$ttot','$ttot','$usr','$date','$wareh', 'POB')");


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





echo "<script language='javascript'>";
echo "alert('Stock Details Saved Successfully!')";
echo "</script>";
$link = "<script>window.open('add_stock.php','_self')</script>";
echo $link;

donot:
}

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