<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/function.php");

$zdb = $_SESSION["zdbyear_bmexp"];
mysqli_select_db($conn, $zdb);

if(isset($_GET["mode"])=="true"){
    $addflag = 1;
    $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM item LIMIT 0,1 ") or die(mysqli_error($conn));
    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else{
        $qvouch = '1';
    }
}
else{
    $addflag= 0;
    $qvouch = $_GET["vouchno"];
}


if(isset($_POST["save"])){
    
    $a = 1;
    $name      = mysqli_real_escape_string($conn, $_POST["name"]);
    $unit      =$_POST["unit"];
    $cmbitem   =$_POST["cmbitem"];
    $cost      =$_POST["cpr"];
    $rate      =$_POST["rate"];
    $openbal   =$_POST["openbal"];
    $mini      =$_POST["mini"];
    $mlevel    = 1;
    
    if($name == " "){
        $a =0;
        browse("Name Cannot Be Empty");
        if($addflag ==1){
          // header("refresh: ; item.php?mode=true") ;
          header("location:javascript://history.go(-1)");
        }else{
           header("refresh: ; item.php?vouchno=$_GET[vouchno]"); 
        }
     }
    if($cmbitem == " "){
        $a =0;
        browse("Group Cannot Be Empty");
        if($addflag ==1){
           header("refresh: ; item.php?mode=true") ;
        }else{
           header("refresh: ; item.php?vouchno=$_GET[vouchno]"); 
        }
     }
    
    if ($a == 1) {
        $namechk = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$name' and code!=$qvouch") or die(mysqli_error($conn));
        if (mysqli_num_rows($namechk) > 0) {
            $a = 0;
            browse("Name Already Exist");
            if ($addflag == 1) {
                header("refresh: 1; item.php?mode=true");
            } else {
                header("refresh: 1; item.php?vouchno=$_GET[vouchno]");
            }
        }
    }
    
    
    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `item`(`code`,  `name`, `size`, `comp`, `unit`, `itemgrp`, levelno, `nameprin`, opstock, minstock, cost, rate, `username`)
                                     VALUES('$qvouch', '$name', '', '', '$unit', '$cmbitem', '$mlevel', '$name', '$openbal', '$mini', '$cost', '$rate', '$_SESSION[username]')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; item.php?mode=true");
        } else {
            $mvouch = $_POST["vouch"];
            mysqli_query($conn, "UPDATE `item` SET `name`='$name', `unit`='$unit', `itemgrp`='$cmbitem', cost='$cost', rate='$rate', opstock='$openbal', minstock='$mini',
                                           `username`='$_SESSION[username]' where code = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("location: itemmain.php");
        }
    }
}

if($addflag==0){
  $getvouch = $_GET["vouchno"];
  $result = mysqli_query($conn, "SELECT * FROM item WHERE code='$getvouch'")or die(mysqli_error($conn));
  $get = mysqli_fetch_array($result);
  
  $getitemname = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE code='$get[itemgrp]'");
  $getiname = mysqli_fetch_array($getitemname);
}
?>
<html lang="en-US">
    
    <head>
        <meta charset="UTF-8">
        <title>Change Password</title>
        <meta name="msapplication-TileColor" content="#5bc0de"/>
        <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>
        
        <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
        <link rel="stylesheet" href="assets/css/main.css"/>
        <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>

        <link rel="stylesheet" href="assets/css/theme.css">

        
        <link rel="stylesheet" href="assets/lib/uniform/themes/default/css/uniform.default.css">
        <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
        <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
        <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
        <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
        <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
        <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
        <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">
        <link rel="stylesheet" href="assets/lib/switch/static/stylesheets/bootstrap-switch.css">
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css">
        
        
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  
                 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-1669764-16', 'onokumus.com');
            ga('send', 'pageview');

        </script>

        <script src="assets/lib/modernizr-build.min.js"></script>
        <script type="text/javascript" src="js/jquery.js"></script>
	<!--[if lt IE 9]>
	    	<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
	    	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
        <script type='text/javascript'>
            function check(){
                   if(item.cmbitem.value == "") {
                      alert("GROUP CANNOT BE EMPTY");
                      item.cmbitem.focus();
                      return false;
                   }
            }
        </script>


</head>
<body onLoad="document.boq.vouchdt.focus();">
        <form action=''  name="customer" method="POST">
<div class="row">
<div class="col-lg-6" style='width:50%;float:center;'>
    <div class="box dark">
        <header>
            <div class="icons"><i class="fa fa-edit"></i></div>
            <h5>Create User</h5>
            <!-- .toolbar -->
            <div class="toolbar">
                <ul class="nav">
                    <li><input type='submit' name='submit' id='save' value='Save' class="btn btn-primary"></li>
                    <li><input class="btn btn-danger" type="button" onClick="window.location.href='itemmain.php'" value="Exit" /></li>
                    <li>
                        <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </li>
                </ul>
            </div>
            <!-- /.toolbar -->
        </header>
        <div id="div-1" class="accordion-body collapse in body">
            <div class="form-horizontal">
              <input type="hidden" size="6" maxlength="6" id="mvouch" maxlength="6" name="mvouch" value="<?php echo ($addflag == 0) ? $get['vouchno'] : $qvouch; ?>" />      
              <div class="form-group">
                <label class="control-label col-lg-4">Item No</label>
                   <div class="col-lg-8"> 
                       <input type="text" size="4" maxlength="4" id="code"  name="vouch" value="<?php echo ($addflag == 0) ? $get['code'] : $qvouch; ?>" class="form-control" readonly style ="width:60px;"/>
                     
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Name</label>
                   <div class="col-lg-8"> 
                        <input type="text" size="40" id="name" name="name" value="<?php echo ($addflag == 0) ? $get['name'] : ""; ?>" class="form-control" required tabindex="1" autofocus />
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Unit</label>
                   <div class="col-lg-8"> 
                       <input type="text" size="10" maxlength="6" id="unit" name="unit" value="<?php echo ($addflag == 0) ? $get['unit'] : ""; ?>" class="form-control" required tabindex="1" style ="width:60px;"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Item Group </label>
                   <div class="col-lg-8"> 
                       <select  style="width:332px;" id="cmbitem" name="cmbitem" class="form-control" tabindex="2">
                              <option value="<?php echo ($addflag == 0) ? $getiname['code'] : " "; ?>"><?php echo ($addflag == 0) ? $getiname["name"] : ""; ?></option>
                           <?php
                             $mown = mysqli_query($conn, "SELECT code, `name` FROM `itemgrp`") or die(mysqli_error($conn));
                              while($trow = mysqli_fetch_array($mown)){
                                     echo "<option value=$trow[code]>$trow[name]</option>";
                                    }
                            ?>
                        </select>

                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Cost Price </label>
                   <div class="col-lg-8"> 
                      <input type="text"  size="11" maxlength="11" name="cpr" value="<?php echo ($addflag == 0) ? $get['cost'] : "0.00"; ?>"  double="(10,2)" class="form-control" tabindex="3" style ="width:120px;text-align: right;"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Rate</label>
                   <div class="col-lg-8"> 
                       <input type="text" name="rate" value="<?php echo ($addflag == 0) ? $get['rate'] : "0.00"; ?>" maxlength="11" size="11" double="(10,2)" class="form-control" tabindex="4"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Opening Stock</label>
                   <div class="col-lg-8"> 
                       <input type="text" name="openbal" value="<?php echo ($addflag == 0) ? $get['opstock'] : "0"; ?>" maxlength="15" size="10"  class="form-control" tabindex="5"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Opening Stock</label>
                   <div class="col-lg-8"> 
                       <input type="text" name="openbal" value="<?php echo ($addflag == 0) ? $get['opstock'] : "0"; ?>" maxlength="15" size="10"  class="form-control" tabindex="5"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Minimun Stock</label>
                   <div class="col-lg-8"> 
                       <input type="text" name="mini" value="<?php echo ($addflag == 0) ? $get['minstock'] : 0 ; ?>" maxlength="15" class="form-control" tabindex="6"/>
                   </div> 
              </div> 
              <div class="form-group">
                <label class="control-label col-lg-4">Minimun Stock</label>
                   <div class="col-lg-8"> 
                       <input type="text" name="mini" value="<?php echo ($addflag == 0) ? $get['minstock'] : 0 ; ?>" maxlength="15" class="form-control" tabindex="6"/>
                   </div> 
              </div> 
              </div> 
              </div> 
              </div> 
              </div> 

	            
	</form>

</body>
</html>
