<?
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 New Products</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-Prod' ");
$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='voucher' action='' onSubmit='return validateForm()' method=POST >";


              ?>
              <html>
<head>
<style>
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 80%;
}

#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;
}



 #suggestions {
            border: 1px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 250px;
            max-height: 150px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions div {
            padding: 1px;
            cursor: pointer;
        }
        #suggestions div:hover,
        #suggestions .selected {
            background-color: #f0f0f0;
        }
</style>   
<link rel="stylesheet" href="css/style.css">
<script language="javascript" type="text/javascript">

   window.onload= function show()
                        {
                                if(document.voucher.name.value =="")
                                        {

                                                document.voucher.name.focus()
                                        }
                                        }
                                        
function formconfirm() {
return confirm("Do You want to Continue?<? echo "$coursename";?>");
}
function openAndPrintPDF() 
{
var pdfWindow = window.open('rep_prod.php?usr=<?php echo $usr ?>', '_blank');
}

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 validateForm()
{

if((document.voucher.comp.value) == '')
{
alert("Please Select Company Name First!");
document.voucher.comp.focus();
return false;
}
if((document.voucher.name.value) == '')
{
alert("Please Enter Title First!");
document.voucher.name.focus();
return false;
}


if((document.voucher.grade.value) == '')
{
alert("Please Select Grade!");
document.voucher.grade.focus();
return false;
}



if((document.voucher.ptype.value) == '')
{
alert("Please Select Product Packing!");
document.voucher.ptype.focus();
return false;
}


if((document.voucher.lit.value) == '' || (document.voucher.lit.value) == 0 )
{
alert("Please Enter Liters per Unit!");
document.voucher.lit.focus();
return false;
}

if((document.voucher.ucart.value) == '' || (document.voucher.ucart.value) == 0 )
{
alert("Please Enter Unit Per Carton!");
document.voucher.ucart.focus();
return false;
}

if((document.voucher.rrate.value) == '' || (document.voucher.rrate.value) == 0 )
{
alert("Please Enter Retial Price!");
document.voucher.rrate.focus();
return false;
}



if((document.voucher.drate.value) == '' || (document.voucher.drate.value) == 0 )
{
alert("Please Enter Dealer Price!");
document.voucher.drate.focus();
return false;
}

if((document.voucher.wrate.value) == '' || (document.voucher.wrate.value) == 0 )
{
alert("Please Enter Whole Sale Price!");
document.voucher.wrate.focus();
return false;
}

if((document.voucher.pcmo.value) == '')
{
alert("Please Select Product Type!");
document.voucher.pcmo.focus();
return false;
}


return true;
}


function show_cbo11()
                {
                        var val = document.getElementById('comp').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "add_prod.php?M_Headc="+val;
                }
                function show_cbo12()
                {
                        var val = document.getElementById('comp').value;
                           var val3 = document.getElementById('cat').value;
                      
                        location.href = "add_prod.php?M_Headc="+val+"&Cate="+val3;
                }

  </script>


 <table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="5">ADD PRODUCT INFORMATION</th>

  </tr>               
 
<?
$result = mysqli_query($link, "SELECT MAX(Item_Code) as fno FROM products ");
while($row = mysqli_fetch_object($result))
{
$fno = $row->fno;
}


$fno1 = $fno+1;
//$fcode=$cde1."-".$fno1;
 ?>

<td colspan="4">
<b><font size=3 color="black">Item Code: PD000-</b>
<input type='text' size='6' name="strid" id='strid' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:20px;' value="<?php echo $fno1 ?>" readonly >

<tr>
<td align=left ><b><font size=3 color=black>Select Company:</b></td>
                        <td align=left >
                                <select class="form-control" name="comp" id="comp"  onchange="show_cbo11()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['M_Headc']))
                                        {
                                                $M_Headc = $_GET['M_Headc'];
                                                
                                                
                
                                        }
                                else                        
                                        $M_Headc = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from acc_heads where M_Head='Company' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($M_Headc == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>


<td align=left ><b><font size=3 color=black>Select Product Category:</b></td>
                        <td align=left colspan="3">
                                <select class="form-control" name="cat" id="cat"  onchange="show_cbo12()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Cate']))
                                        {
                                                $Cate = $_GET['Cate'];
                                                
                                                
                
                                        }
                                else                        
                                        $Cate = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from category order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Cate == $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="220"><b><font size=3 color="black">Product Title:</b></td>
<td><input type="text" name="name" size="30" onkeydown="return tab(event)"  /></td>

<td width="200"><b><font size=3 color="black">API:</b></td>
<td><input type="text" name="api" size="30" onkeydown="return tab(event)"  /></td>
<tr>

 <td align=left><b><font size=3 color=black>Grade:</b></td>
                        <td align=left>
                                <select class="form-control" name="grade" id="grade" style=font-size="16" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['M_Headc']))
                                        {
                                                $M_Headc = $_GET['M_Headc'];
                                                
                                                
                
                                        }
                                else                        
                                        $M_Headc = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from grades order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($M_Headc == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>

<td><b><font size=3 color="black">Product Packing:</font></b></td>
<td>
<select class="form-control" name=ptype>
<option value='Pack'>Pack</option>
<option value='Bucket'>Bucket</option>

<option value='Drum'>Drum</option>

<tr>

<td>
<b><font size=3 color="black">Litres per Unit:</b>

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

?>


<td>
<b><font size=3 color="black">Unit per Carton:</b>

<?
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='0' onkeypress='return processkey(event)'>";

?>
<tr>
<td><b><font size=3 color="black">Size:</font></b></td>
<td>
<select class="form-control" name=pack>
<option value='3L X 4'>3L X 4</option>
<option value='4L X 4'>4L X 4</option>
<option value='6L X 3'>6L X 3</option>
<option value='1L X 12'>1L X 12</option>
<option value='1.5L X 12'>1.5L X 12</option>
<option value='700ml X 12'>700ml X 12</option>
<option value='500ml X 24'>500ml X 24</option>
<option value='6L X 1'>6L X 1</option>
<option value='8L X 1'>8L X 1</option>
<option value='8L X 2'>8L X 2</option>

<option value='10L X 1'>10L X 1</option>
<option value='20L X 1'>20L X 1</option>
<option value='208L X 1'>208L X 1</option>
<option value='210L X 1'>210L X 1</option>


</select>



<td>
<b><font size=3 color="black">Purchase Cost (Per Litre):</b>

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

?>

<tr>
<td>
<b><font size=3 color="black">Retail Price (Per Litre):</b>

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

?>


<td>
<b><font size=3 color="black">Dealer Price (Per Litre):</b>

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

?>
<tr>
<td>
<b><font size=3 color="black">Whole Sale Price (Per Litre):</b>

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




$strvoucher= Date("Y-m-d");
if(isset($_POST['strDate']) && !isset($_POST['reset']) )
{
$strvoucher=$_POST['strDate'];
AddDate("<b><font size=3 color=black>Price Update Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
else
{
AddDate("<b><font size=3 color=black>Price Update Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}


?>
<tr>
<td align=left ><b><font size=3 color=black>Select Product Type:</b></td>
                        <td align=left colspan="3">
                                <select class="form-control" name="pcmo" id="pcmo" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Pcm']))
                                        {
                                                $Pcm = $_GET['Pcm'];
                                                
                                                
                
                                        }
                                else                        
                                        $Pcm = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from pcmo order by Id");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Pcm == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>


<tr>
    <td colspan="4" 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;">
</table>
</form>

<form method="post">

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

  <td colspan="2" align="right">
<input type="submit" name="search" value="Search" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">


<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">Open Product List</span></button>

 <?

       

if(isset($_POST['search']))
{
$link1 = "<script>window.open('prodview.php','_self')</script>";
echo $link1;
}




if(isset($_POST['save']))
{
date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());
$rdate = strtotime($_POST['strDate']);
$rdate = date("Y-m-d",$rdate);
$comp = $_POST['comp'];

$name = $_POST['name'];
$api = $_POST['api'];
$grade = $_POST['grade'];
$ptype = $_POST['ptype'];
$lit = $_POST['lit'];
$ucart = $_POST['ucart'];
$pack = $_POST['pack'];
$prate = $_POST['prate'];
$rrate = $_POST['rrate'];
$wrate = $_POST['wrate'];
$drate = $_POST['drate'];
$cate = $_POST['cat'];
$pcm = $_POST['pcmo'];



$fname=$name." ".$grade." ".$lit."L";

$sql1 = "SELECT * FROM products where Full_Name='$fname' ";
$q = $link->query($sql1);
if ($q->num_rows == 1) 
{
echo "<script language='javascript'>";
echo "alert('Product Details already Exists!')";
echo "</script>";
goto donothing;
}

$result = mysqli_query($link, "SELECT MAX(Item_Code) as fno FROM products ");
while($row = mysqli_fetch_object($result))
{
$fno = $row->fno;
}
$idd = $fno+1;

mysqli_query($link, "insert into products (Company, Name, Item_Code, Full_Name, Item_Type, API, Size, Grade, Carton_Qty, Unit_Liter, DP, R_Date, R_Price, W_Price, D_Price, addby, addon, Sufix, Category, Category2, Carton) values ('$comp','$name','$idd','$fname','$ptype','$api','$pack','$grade','$ucart','$lit','$prate','$rdate','$rrate','$wrate','$drate','$usr','$the_time','PD000','$cate','$pcm','$fname')");


echo "<script language='javascript'>";
echo "alert('Information added Successfully!')";
echo "</script>";
//$link1 = "<script>window.open('add_prod.php','_self')</script>";
//echo $link1; 
donothing:
}
           
                 



 echo "</center>";
  echo "
                                </form>
                                </table>
                        ";
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  ?>