<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php

include ("../navigation/header.php");
include ("../navigation/store_sidebar.php");

	$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['category'] = $_GET['cat_group'];
		$_SESSION['subCategory'] = $_GET['sub_cat_group'];		
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$resetQuery = "UPDATE $table_name SET stockTake = 'P' WHERE subCategory = '".$_SESSION['subCategory']."' AND stockTake = 'S' " ;
		$resetResult = execsql($resetQuery);
		
		$_SESSION['type'] = $_GET['stock_select'];
		if($_SESSION['type']=="single") {
			 $_SESSION['p'] = "3";
		}
	};
	
	if($s==2) {
	   $prepare = $_GET['prepare'];
	    
	     
	   if ($prepare=="yes") {
		   $table_name = $_SESSION['db_prefix']."_store_items";
		   $prepareQuery = "UPDATE $table_name SET stockTake = 'P' WHERE subCategory = '". $_SESSION['subCategory']."'  AND branch = '".$_SESSION['branch']."' " ;
		   $prepareResult = execsql($prepareQuery);   
		   
		   $productQuery = "SELECT store_item_id FROM $table_name WHERE subCategory = '". $_SESSION['subCategory']."'  AND branch = '".$_SESSION['branch']."' AND stockTake = 'P' ORDER BY item_name ASC ";
		   $productResult = execsql($productQuery);
           $productNum = mysql_num_rows($productResult);
		   $_SESSION['finished'] ="1";
		   $_SESSION['message1'] = "List is prepared. There are ".$productNum." items in the list to enter. Click 'Next' to start entering";
	   }
	};
	
	if($s==3) {
		
	$c = $_GET['c'];
	    /* ===== stock count */
		if ($c==3) {
			$store_item_id  = $_GET['store_item_id'];
	        $quantity  = $_GET['quantityText'];
	        
			$category = $_GET['category'];
			$subCategory = $_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'];
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$qty_query = "SELECT  quantity, price   FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
			$qty_result = mySQL_query($qty_query);
			$stock = mysql_result($qty_result, 0, "quantity");
			$price = mysql_result($qty_result, 0, "price");
			
			
			$difference = $quantity- $stock;
		    $table_name = $_SESSION['db_prefix']."_stock_count";
		    $reportQuery = "INSERT INTO $table_name (date, store_item_id, category, sub_category, old_stock, new_stock, user_name, branch) VALUES ('".$entryDate."' ,'".$store_item_id."','".$category."','".$subCategory."','".$stock."' ,'".$quantity."','".$userId."', '".$_SESSION['branch']."')";
			execsql($reportQuery);
			
			$table_name3 = $_SESSION['db_prefix']."_store_items";
			$countQuery = "UPDATE $table_name3 SET quantity='".$quantity."', stockTake='".$stockTake."' WHERE store_item_id = $store_item_id ";
			execsql($countQuery);
			
			$entry="5";
			$reference="";
			$table_name4 = $_SESSION['db_prefix']."_item_history";
			$historyQuery = "INSERT INTO $table_name4 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."','".$entry."','".$reference."','".$difference."', '".$quantity."','".$date."','".$entryDate."','".$userId."')";
			$historyResult = mySQL_query($historyQuery);
	
	$table_name5 = $_SESSION['db_prefix']."_stockloss";
	$stockLossQuery = "INSERT INTO $table_name5 (store_item_id, category, sub_category, amount, quantity, total, date, unit_price, branch) VALUES ('".$store_item_id."','".$category."','".$subCategory."', '".$difference."', '".$stock."', '".$quantity."', '".$date."', '".$price."', '".$_SESSION['branch']."')";
	$stockLossResult = mySQL_query($stockLossQuery);
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, branch, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."', '".$_SESSION['branch']."', '".$_SESSION['username']."', '21', '".$store_item_id."', '".$stock."' ,'".$quantity."', '' )";
	$trackingResult = mySQL_query($trackingQuery);	
	}	
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	   if ($_SESSION['subCategory']=="Timber") {
	   $productQuery = "SELECT store_item_id, item_name, category, subCategory, quantity, stockTake FROM $table_name WHERE subCategory = '". $_SESSION['subCategory']."' AND stockTake = 'P'  ORDER BY width, depth, length ASC LIMIT 1";
	   }
	   else {
	   $productQuery = "SELECT store_item_id, item_name, category, subCategory, quantity, stockTake FROM $table_name WHERE subCategory = '". $_SESSION['subCategory']."' AND stockTake = 'P'  ORDER BY item_name 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,"store_item_id");
	   $a1=mysql_result($productResult,0,"item_name");
	   $a2=mysql_result($productResult,0,"quantity");
	   $a3=mysql_result($productResult,0,"stockTake");
	   $a4=mysql_result($productResult,0,"category");
	   $a5=mysql_result($productResult,0,"subCategory");
	   
	   }
	
		
		
	/* ===== stock adjustment */			
	if ($c==5) {
			
			$store_item_id  = $_GET['store_item_id'];
	        $table_name = $_SESSION['db_prefix']."_store_items";
			$productQuery = "SELECT category, subCategory, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."'" ;
			$productResult = execsql($productQuery);
			$category = mysql_result($productResult, 0, "category");
			$sub_category = mysql_result($productResult, 0, "subCategory");
			$stock = mysql_result($productResult, 0, "quantity");
			$price = mysql_result($productResult, 0, "price");
						
			$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;
		    
			$table_name = $_SESSION['db_prefix']."_stock_count";
		    $reportQuery = "INSERT INTO $table_name (date, store_item_id, category, sub_category, old_stock, new_stock, user_name, branch) VALUES ('".$entryDate."' ,'".$store_item_id."','".$category."','".$sub_category."','".$stock."' ,'".$quantity."','".$userId."', '".$_SESSION['branch']."')";
			execsql($reportQuery);
			
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$countQuery = "UPDATE $table_name SET quantity='".$quantity."', stockTake='".$stockTake."' WHERE store_item_id = '".$store_item_id."' ";
			execsql($countQuery);
			
			$entry="5";
			$reference="";
			$table_name = $_SESSION['db_prefix']."_item_history";
			$historyQuery = "INSERT INTO $table_name (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."','".$entry."','".$reference."','".$difference."', '".$quantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);
	
	$table_name = $_SESSION['db_prefix']."_stockloss";
	$stockLossQuery = "INSERT INTO $table_name (store_item_id, category, sub_category, amount, quantity, total, date, unit_price, branch) VALUES ('".$store_item_id."', '". $category."', '". $sub_category."', '$difference', '$stock', '$quantity', '$date', '".$price."', '".$_SESSION['branch']."')";
		
	$stockLossResult = mySQL_query($stockLossQuery);
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, branch, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."', '".$_SESSION['branch']."', '".$_SESSION['username']."', '25', '".$store_item_id."', '".$stock."' ,'".$quantity."', '' )";
	$trackingResult = mySQL_query($trackingQuery);	
			
		}
	   
	};
	
	
	
	?>
<!--style sheets-->
<link href="../css/wait.css" rel="stylesheet" type="text/css">    
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<link rel="stylesheet" href="../window/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="../window/modal.css" type="text/css" />

<!--jscript-->
<script type="text/javascript" src="../window/dhtmlwindow.js"></script>
<script type="text/javascript" src="../window/modal.js"></script>
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>

</head>

<body>
<div class="main_frame">
  <table id="tableMain" width="595">
    <!--begin of tableMain-->
    <tr>
      <td ><div class="page_head_frame">
        <table width="728" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="579"><?php if($_SESSION['type']=="single") {echo '<label class=label_page_head>Stock Adjustment</label>';} else {echo '<label class=label_page_head>Stock Count</label>';}?></td>
              <td width="149"><b><?php echo $_SESSION['userbranch'].'<br>branch'?></b></td>
            </tr>
          </table>
      </div>
        <br />
        <div class="wizard_paging_frame">
        <table align="left">
          <tr>
            <td><?php if ($_SESSION['p']==1) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;1</div>
                <div class="active_step_label"><br />
                  <br />
                  <br />
                  Select store/department</div>
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;1</div>
                <div class="de_active_step_label"><br />
                  <br />
                  <br />
                  Select store/department</div>
              </div>
              <?php } ?></td>
            <td><?php if ($_SESSION['p']==2) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;2</div>
                <div class="active_step_label"><br />
                  <br />
                  <br />
                  Prepare stock take</div>
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;2</div>
                <div class="de_active_step_label"><br />
                  <br />
                  <br />
                  Prepare stock take</div>
              </div>
              <?php } ?></td>
            <td><?php if ($_SESSION['p']==3) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;3</div>
                <div class="active_step_label"><br />
                  <br />
                  <br />
                  <?php if($_SESSION['type']=="single") {echo 'Stock adjustment';} else {echo 'Stock Count';}?></div>
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;3</div>
                <div class="de_active_step_label"><br />
                  <br />
                  <br />
                  <?php if($_SESSION['type']=="single") {echo 'Stock adjustment';} else {echo 'Stock Count';}?></div>
              </div>
              <?php } ?></td>
          </tr>
        </table>
        </div>
        </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>
</div>
<?php include ('../navigation/side_bar.php'); ?>
</body>
