<?php 
    // continue the session
    session_start();
    
    // Add includes to PHP files that I will need to access
    include 'php_only_scripts/database.php';
    include 'php_only_scripts/presentation.php';
    include 'php_only_scripts/stocked_material_presentation.php';
    include 'php_only_scripts/note_presentation.php';

    // Init some variables
    $userRow=null;

    // Assume NOT gtg
    $gtg = false;

    // Create and Setup the database
    $db = new Database();
    $db->setupTmsDB();

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser(session_id());
            
    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser == null){
        exit;
    }
    elseif (mysqli_num_rows($currentUser)==1){
        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();
        
        // Determine if this user has access to this page
        if ($userRow["UserLevel"]=='TIMECARD ONLY' || 
            $userRow["SUEmployee"]!=1 ||
            $userRow["AccountStatus"]!='ACTIVE'){
            // redirect to the invalid access page
            echo ('<script>window.location = "invalid_access.php";</script>');

            exit;
        }
        
        // Set the good to go until they log out
        $gtg = true;
        
        // Determine if BOTH the user name AND password has been set
        if (isset($_POST['formSubmitted']) && 
            htmlspecialchars($_POST['formSubmitted'])=='TRUE'){
            // Determine if this user can even modify
            if ($userRow["CanModify"]==0){
                // redirect to the companies page
                echo ('<script>window.location = "machines.php";</script>');
            }
            else {
                // Set the local variables from what was posted
                $materialStockID = htmlspecialchars($_POST['materialStockID'], ENT_QUOTES, 'UTF-8');
                $materialID = htmlspecialchars($_POST['materialID'], ENT_QUOTES, 'UTF-8');
                $name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');
                $location = htmlspecialchars($_POST['location'], ENT_QUOTES, 'UTF-8');
                $type = htmlspecialchars($_POST['type'], ENT_QUOTES, 'UTF-8');
                $dimension1 = htmlspecialchars($_POST['dimension1'], ENT_QUOTES, 'UTF-8');
                $dimension2 = htmlspecialchars($_POST['dimension2'], ENT_QUOTES, 'UTF-8');
                $dimension3 = htmlspecialchars($_POST['dimension3'], ENT_QUOTES, 'UTF-8');
                $diameter = htmlspecialchars($_POST['diameter'], ENT_QUOTES, 'UTF-8');
                $wallThickness = htmlspecialchars($_POST['wallThickness'], ENT_QUOTES, 'UTF-8');
                $lowWarpValue = htmlspecialchars($_POST['lowWarpValue'], ENT_QUOTES, 'UTF-8');
                $highWarpValue = htmlspecialchars($_POST['highWarpValue'], ENT_QUOTES, 'UTF-8');
                $amount = htmlspecialchars($_POST['amount'], ENT_QUOTES, 'UTF-8');
                $units = htmlspecialchars($_POST['units'], ENT_QUOTES, 'UTF-8');
                $lastReorderedDate = htmlspecialchars($_POST['lastReorderedDate'], ENT_QUOTES, 'UTF-8');
                $averageLeadTimeInDays = htmlspecialchars($_POST['averageLeadTimeInDays'], ENT_QUOTES, 'UTF-8');
                $notes = htmlspecialchars($_POST['permanentNotes'], ENT_QUOTES, 'UTF-8');
                $checkFrequencyInDays = htmlspecialchars($_POST['checkFrequencyInDays'], ENT_QUOTES, 'UTF-8');
                $lowReorderLevel = htmlspecialchars($_POST['lowReorderLevel'], ENT_QUOTES, 'UTF-8');
                $highReorderLevel = htmlspecialchars($_POST['highReorderLevel'], ENT_QUOTES, 'UTF-8');
                $vendor = htmlspecialchars($_POST['vendor'], ENT_QUOTES, 'UTF-8');

                // Let's create the insert query off of what we have
                $sql='INSERT INTO materialstock (';
                if ($name!=null) {$sql = $sql . 'Name';}
                if ($location!=null) {$sql = $sql . ', Location';}
                if ($type!=null) {$sql = $sql . ', Type';}
                if ($materialID!=null) {$sql = $sql . ', MaterialID';}
                if ($dimension1!=null) {$sql = $sql . ', Dimension1';}
                if ($dimension2!=null) {$sql = $sql . ', Dimension2';}
                if ($dimension3!=null) {$sql = $sql . ', Dimension3';}
                if ($diameter!=null) {$sql = $sql . ', Diameter';}
                if ($wallThickness!=null) {$sql = $sql . ', WallThickness';}
                if ($lowWarpValue!=null) {$sql = $sql . ', LowWarpValue';}
                if ($highWarpValue!=null) {$sql = $sql . ', HighWarpValue';}
                if ($amount!=null) {$sql = $sql . ', Amount';}
                if ($units!=null) {$sql = $sql . ', Units';}
                if ($lastReorderedDate!=null) {$sql = $sql . ', LastReorderedDate';}
                if ($averageLeadTimeInDays!=null) {$sql = $sql . ', AverageLeadTimeInDays';}
                if ($notes!=null) {$sql = $sql . ', Notes';}
                if ($checkFrequencyInDays!=null) {$sql = $sql . ', CheckFrequencyInDays';}
                if ($lowReorderLevel!=null) {$sql = $sql . ', LowReorderLevel';}
                if ($highReorderLevel!=null) {$sql = $sql . ', HighReorderLevel';}
                if ($vendor!=null) {$sql = $sql . ', VendorID';}
                $sql=$sql .') VALUES (';
                if ($name!=null) {$sql = $sql  . '"' . $name . '"';}
                if ($location!=null) {$sql = $sql . ', "' . $location . '"';}
                if ($type!=null) {$sql = $sql . ', "' . $type . '"';}
                if ($materialID!=null) {$sql = $sql . ', ' . $materialID;}
                if ($dimension1!=null) {$sql = $sql . ', ' . $dimension1;}
                if ($dimension2!=null) {$sql = $sql . ', ' . $dimension2;}
                if ($dimension3!=null) {$sql = $sql . ', ' . $dimension3;}
                if ($diameter!=null) {$sql = $sql . ', ' . $diameter;}
                if ($wallThickness!=null) {$sql = $sql . ', ' . $wallThickness;}
                if ($lowWarpValue!=null) {$sql = $sql . ', ' . $lowWarpValue;}
                if ($highWarpValue!=null) {$sql = $sql . ', ' . $highWarpValue;}
                if ($amount!=null) {$sql = $sql . ', ' . $amount;}
                if ($units!=null) {$sql = $sql . ', "' . $units . '"';}
                if ($lastReorderedDate!=null) {$sql = $sql . ', "' . $lastReorderedDate . '"';}
                if ($averageLeadTimeInDays!=null) {$sql = $sql . ', ' . $averageLeadTimeInDays;}
                if ($notes!=null) {$sql = $sql . ', "' . $notes . '"';}
                if ($checkFrequencyInDays!=null) {$sql = $sql . ', ' . $checkFrequencyInDays;}
                if ($lowReorderLevel!=null) {$sql = $sql . ', ' . $lowReorderLevel;}
                if ($highReorderLevel!=null) {$sql = $sql . ', ' . $highReorderLevel;}
                if ($vendor!=null) {$sql = $sql . ', ' . $vendor;}
                $sql=$sql .');';

//echo ('<script type="text/javascript">alert(\'' . $sql . '\');</script>');

                // Attempt to run the query and evaluate the response
                if ($db->queryDB($sql, $userRow, "addMaterialStock Form occurance 1")){
                    // Get the most recent ID (machine)
                    $newMaterialStockID = mysqli_insert_id($db->getDBConnection());

                    // Add any new notes that the user added
                    addNewNotesToDB($db, $userRow, $newMaterialStockID, $userRow["UserID"], "MATERIAL");
                    
                    // redirect to the stocked materials page
                    echo ('<script>window.location = "stockedMaterials.php";</script>');
                }
                else {
                    echo '<script>alert("Stocked Material was NOT added to the database. Please check the data and try again.");</script>';
                    // TODO: How do I want to display a failure to the user?
                    // echo '<font color="red">Task was NOT added to the database. <br> Please check the data and try again.</font>';
                }

                // Change the form back to NOT being submitted
                $_SESSION['formSubmitted']='FALSE';
            }
        }
        else {
            // Set the form submitted session variable to FALSE
            $_POST['formSubmitted']='FALSE';
        }
    }
?>
        
﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Task Management System - Add Material Stock</title>

    <meta name="keywords" content="GIS,data,testing,IT" />
    <meta name="description" content="EES Divisions." />

    <script src="./SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
    <script src="./javascript/general.js" type="text/javascript"></script>
    <script src="./javascript/notes.js" type="text/javascript"></script>
    <script src="scripts/jpicker-1.1.6/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="scripts/jpicker-1.1.6/jpicker-1.1.6.min.js" type="text/javascript"></script>
    <script language="JavaScript" src="scripts/gen_validatorv4.js" type="text/javascript"></script>
    
    <link rel="icon" href="images/ees_ico.ico"/> 
    
    <link href="scripts/jpicker-1.1.6/css/jPicker-1.1.6.css" rel="stylesheet" type="text/css"/>
    <link href="./SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
    <link href="./SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
    <link rel = "stylesheet" type="text/css" href="./css/contentpage.css" >

</head>

<body class="thrColElsHdr">

<div id="container">
  
<!-- start of banner wrap --->

<div id="topContent">
  
<div>
   <div class="MenuBarHorizontal" id="navBar">  <ul id="TopNav" class="MenuBarHorizontal">

    <script type="text/javascript">
    <!--
    var MenuBar2 = new Spry.Widget.MenuBar("TopNav", {imgDown:"./SpryAssets/SpryMenuBarDownHover.gif", imgRight:"./SpryAssets/SpryMenuBarRightHover.gif"});
    //-->
    </script>

    </div>
  </div>
</div>
<!-- end of banner wrap --->

  <!-- end #header -->
  <br class="clearfloat" />
  <div id="sidebar1">
      <?php 
        // Determine if we are GTG
        if ($gtg){
          // Render the appropriate presentation for the tasks sidebar
          getSidebar($userRow, 'equipment');
        }
      ?>       
      <br class="clearfloat" />
  <!-- end #sidebar1 -->
  </div>
  
  <!-- start #mainContent -->
  <div id="mainContent">
    <div class="maintitle">Task Management System - Add Material Stock</div>
    <div class="breadcrumb"> <a href="../index.html"> Home </a> > <a href="stockedMaterials.php"> Task Management System </a> > <a href="addMaterialStock.php"> Add Material Stock </a></div>

    <br>
    <?php
        // Determine if they are GTG
        if ($gtg){
            echo getMaterialStock($db, $userRow, null);
        }
        else {
            // Set the login failed to true
            $_SESSION['failedLogin']='TRUE';

            // Add a session variable that is this page URL
            $_SESSION['pageToGoTo']=$_SERVER['REQUEST_URI'];

            // redirect to the login page
            echo ('<script>window.location = ".";</script>');
        }
    ?>       
    
  <!-- end #mainContent -->
  
  <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
  <br class="clearfloat" />
  
  <!-- start #footer --></div>
   <div id="footer">
    <p>© 2018 Evolve Engineering and Software. EES, Evolve Engineering and Software, EES Logo are registered marks of Evolve Engineering and Software.</p>
  <!-- end #footer -->
   </div>
<!-- end #container -->
</div>

</body>

</html>