<?php
include("../include/session.php");
if( !$session->isAdmin() ){
	//header("Location: ../Store/storeMenu.php");?>
<script language="javascript" type="text/javascript">
        <!--
		  window.location="denied.php";
        -->
       </script>
<?php 
}else{
	
	include("../storeHeader.php");
	
	$nameQuery = "SELECT productName FROM products WHERE productId = '".$_SESSION['productId']."'" ;
	$nameResult = execsql($nameQuery);
	$nameNum = mysql_num_rows($nameResult);
	if ($nameNum==1) {
	$_SESSION['productName'] = mysql_result($nameResult, 0, "productName");
	}
	
	
	
	
	//$_SESSION['ContactText'];
	$s = $_GET['s'];
	$p = $_GET['p'];
	$_SESSION['p']=$p;
	$_SESSION['s']=$s;
	
	
	if (isset($_GET['prev_button']) )
	   {
		 $_SESSION['p']= $_SESSION['p']-1;
	   }
		 
	if (isset($_GET['next_button']) )
	  {
		$_SESSION['p']=$_SESSION['p']+1;
	  }
	
		
	
	if ($s==1) {
		$_SESSION['store'] = $_GET['store_group'];
		$resetQuery = "UPDATE products SET stockTake = 'P' WHERE category = '". $_SESSION['store']."' AND stockTake = 'S' " ;
	    $resetResult = execsql($resetQuery);
		
		$_SESSION['type'] = $_GET['stockSelect'];
		if($_SESSION['type']=="single") {
			 $_SESSION['p'] = "3";
		}
	};
	
	if($s==2) {
	   $prepare = $_GET['prepare'];
	     
	   
	   
	   if ($prepare=="yes") {
		   $prepareQuery = "UPDATE products SET stockTake = 'P' WHERE category = '". $_SESSION['store']."'" ;
		   $prepareResult = execsql($prepareQuery);   
		   
		   $productQuery = "SELECT productId FROM products WHERE category = '". $_SESSION['store']."' AND subCategory <> 'old' AND stockTake = 'P' ORDER BY productName ASC ";
           $productResult = execsql($productQuery);
           $productNum = mysql_num_rows($productResult);
		   
		   $_SESSION['message1'] = "List is prepared. There are ".$productNum." items in the list to enter.<br><br> Click 'Next' to start entering";
	   }
	};
	
	if($s==3) {
		
	$c = $_GET['c'];
		if ($c==3) {
			$productId  = $_GET['productId'];
	        $quantity  = $_GET['quantityText'];
	        $stock  = $_GET['stock'];
			$category = $_GET['category'];
			$sub_category = $_GET['sub_category'];
            $date = date("Y-m-d, h:m:s");
			date_default_timezone_set('Africa/Khartoum');
	        $entryDate = date("Y-m-d H:i:s",time()-3600); 
			$stockTake = 'C';	
			$userId = $_SESSION['username'];
			
			$difference = $quantity- $stock;
		   
		    $reportQuery = "INSERT INTO rad_stock_count (date, productId, category, sub_category, old_stock, new_stock, user) VALUES ('".$entryDate."' ,'".$productId."','".$category."','".$sub_category."','".$stock."' ,'".$quantity."','".$userId."')";
			execsql($reportQuery);
			
			$countQuery = "UPDATE products SET quantity='".$quantity."', stockTake='".$stockTake."' WHERE productId = $productId ";
			execsql($countQuery);
			
			$entry="5";
			$reference="";
			$historyQuery = "INSERT INTO itemhistory (productId, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','".$entry."','".$reference."','".$difference."', '".$quantity."','".$date."','".$entryDate."','".$userId."')";
			
	$historyResult = mySQL_query($historyQuery);
	
	$stockLossQuery = "INSERT INTO stockloss (productId, category, amount, quantity, total, date) VALUES ('$productId', '". $_SESSION['store']."', '$difference', '$stock', '$quantity', '$date')";
	$stockLossResult = mySQL_query($stockLossQuery);
	
	
	$actionQuery = "INSERT INTO action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$userId."','Stock Count' ,'".$productId."' )";
	execsql($actionQuery);
			
		}
		
		if ($c==4) {
			$productId2  = $_GET['productId2'];
	        
			date_default_timezone_set('Africa/Khartoum');
	        $entryDate = date("Y-m-d H:i:s",time()-3600); 
			$stockTake = 'S';	
		   
		   	$countQuery = "UPDATE products SET stockTake='".$stockTake."' WHERE productId = $productId2 ";
			execsql($countQuery);
			
			
		}
		
		if ($c==5) {
			$productId  = $_GET['productId'];
	        
			$productQuery = "SELECT category, subCategory, quantity FROM products WHERE productId = '".$productId."'" ;
			$productResult = execsql($productQuery);
			
			$category = mysql_result($productResult, 0, "category");
			$sub_category = mysql_result($productResult, 0, "subCategory");
			$stock = mysql_result($productResult, 0, "quantity");
						
			$quantity  = $_GET['quantityText'];
			
            $date = date("Y-m-d, h:m:s");
			date_default_timezone_set('Africa/Khartoum');
	        $entryDate = date("Y-m-d H:i:s",time()-3600); 
			$stockTake = 'C';	
			$userId = $_SESSION['username'];
			
			$difference = $quantity- $stock;
		   
		    $reportQuery = "INSERT INTO rad_stock_count (date, productId, category, sub_category, old_stock, new_stock, user) VALUES ('".$entryDate."' ,'".$productId."','".$category."','".$sub_category."','".$stock."' ,'".$quantity."','".$userId."')";
			execsql($reportQuery);
			
			$countQuery = "UPDATE products SET quantity='".$quantity."', stockTake='".$stockTake."' WHERE productId = $productId ";
			execsql($countQuery);
			
			$entry="5";
			$reference="";
			$historyQuery = "INSERT INTO itemhistory (productId, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','".$entry."','".$reference."','".$difference."', '".$quantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);
	
	$stockLossQuery = "INSERT INTO stockloss (productId, category, amount, quantity, total, date) VALUES ('$productId', '". $_SESSION['store']."', '$difference', '$stock', '$quantity', '$date')";
		
	$stockLossResult = mySQL_query($stockLossQuery);
	
	$actionQuery = "INSERT INTO action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$userId."','Stock Count' ,'".$productId."' )";
	execsql($actionQuery);
			
		}
		
	   $productQuery = "SELECT productId, productName, category, subCategory, quantity, stockTake FROM products WHERE category = '". $_SESSION['store']."' AND subCategory <> 'old' AND stockTake = 'P'  ORDER BY productName ASC LIMIT 1";
       $productResult = execsql($productQuery);
       $productNum = mysql_num_rows($productResult);
	   
	   if ($productNum==0) {
		   $_SESSION['finished'] ="finished";
		   $a0="";
	       $a1="";
	       $a2="";
	       $a3="";
		   $a4="";
		   $a5="";
	   }
	   else
	   {
	   
	   $a0=mysql_result($productResult,0,"productId");
	   $a1=mysql_result($productResult,0,"productName");
	   $a2=mysql_result($productResult,0,"quantity");
	   $a3=mysql_result($productResult,0,"stockTake");
	   $a4=mysql_result($productResult,0,"category");
	   $a5=mysql_result($productResult,0,"subCategory");
	   
	   }
	};
	
	
	
	?>
<script language="JavaScript" src="gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<!--form validation-->
<script type="text/javascript" src="../css/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<script language="javascript" type="text/javascript" >
<!--
function confirmRefresh() {
var okToRefresh = confirm("Do you really want to cancel this entry?");
var url='specificNewEntry.php'
if (okToRefresh)
	{
			window.location=url;
	}
}
// -->
</script>
<script language="JavaScript">
  var needToConfirm = true;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      return "You did not finish the wizard.\nIf you leave now your data will not be saved.";
  }
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
</script>
<table id="tableMain" width="595">
  <!--begin of tableMain-->
  <tr>
    <td ><div class="headerbox2">Stock Count</div>
      <br />
      <p class="stepbar">
        <?php if ($_SESSION['p']==1) { echo "<label class=step>"; }  else { echo"<label class=stepd>"; } ?>
        Step 1<br />
        <font size="1">Store</font>
        </label>
        <?php if ($_SESSION['p']==2) { echo "<label class=step>"; }  else { echo"<label class=stepd>"; } ?>
        Step 2<br />
        <font size="1">Prepare</font>
        </label>
        <?php if ($_SESSION['p']==3) { echo "<label class=step>"; }  else { echo"<label class=stepd>"; } ?>
        Step 3<br />
        <font size="1">Count</font>
        </label>
        
        <label class="steptext">
          <?php if ($_SESSION['p']==1) { echo "Select store."; } else { echo "Stock count for: &nbsp;&nbsp;&nbsp;&nbsp; ".$_SESSION['store']; } ?>
        </label>
      </p>
      <br /></td>
    <!--center of tableMain-->
  </tr>
  <tr>
    <td><!--center of tableMain-->
      <?php 
	if ($_SESSION['p']==1) { include("stockCountWiz1.php"); }
	if ($_SESSION['p']==2) { include("stockCountWiz2.php"); }
	if ($_SESSION['p']==3) { include("stockCountWiz3.php"); }
	//if ($_SESSION['p']==4) { include("stockCountWiz4.php"); }
	//if ($_SESSION['p']==5) { include("stockCountWiz5.php"); }
    ?></td>
  </tr>
</table>
<?php

	}
	include("../footer.php");

?>
