<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include('../include/session.php');
include ("../navigation/header.php");
include ("../navigation/store_sidebar.php");

	
	$s =  $_GET['s'];
	$p = $_GET['p'];
	$error = $_SESSION['error'];
	$_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['emp_id']=$_GET['emp_id_select'];
		  
		  	$table_name2 = $_SESSION['db_prefix']."_employee";
			$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$_SESSION['emp_id']."' ";
			$employee_result = mySQL_query($employee_query);
			$employee_num = mysql_num_rows($employee_result);	
			$_SESSION['employee_name'] = mysql_result($employee_result, 0, "emp_first_name").' '.mysql_result($employee_result, 0, "emp_last_name"); 
		
	}
		
	if($s==2) {
		$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
		$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
		execsql($delete2_query);
		$_SESSION['stock_error']=0;
		$line_num=$_POST['line_num'];
		$i=0;
		while ($i<$line_num) {
			$job_card_no = $_SESSION['job_card_no'];
			$store_item_id = $_POST['store_item_id_text'.$i];
			$store_item_name = $_POST['store_item_name_text'.$i];
			$quantity = $_POST['supply_text'.$i];
			$price = $_POST['price_text'.$i];
			$user = $_SESSION['username'];
			$order_line_id = $_POST['order_line_id'.$i];
			
			
			if (($store_item_id>0) AND ($store_item_id<100000)) {
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
			$stock_result = mySQL_query($stock_query);
			$stock = mysql_result($stock_result, 0, "quantity");
			
			if ($quantity>$stock)  {
			    $table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			    $deliver_query = "INSERT INTO $table_name (order_line_id, job_card_no, store_item_id, quantity, price, error, user_name ) VALUES ('".$order_line_id."', '".$job_card_no."', '".$store_item_id."','".$stock."', '".$price."', '1', '".$user."' ) ";
				$_SESSION['stock_error']=1;
			}
			else {
				 $table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			     $deliver_query = "INSERT INTO $table_name (order_line_id, job_card_no, store_item_id, quantity, price, error, user_name ) VALUES ('".$order_line_id."', '".$job_card_no."', '".$store_item_id."','".$quantity."', '".$price."', '0', '".$user."' ) ";
			}
			}
			
			if ($store_item_id>100000) {
			
			$table_name = $_SESSION['db_prefix']."_store_items_special";
			$stock_query = "SELECT quantity, supplied_quantity FROM $table_name WHERE special_store_item_id = '".$store_item_id."' GROUP BY special_store_item_id ";
			$stock_result = mySQL_query($stock_query);
			$stock = mysql_result($stock_result, 0, "quantity")-mysql_result($stock_result, 0, "supplied_quantity");
			
			
			if ($quantity>$stock)  {
			    $table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			    $deliver_query = "INSERT INTO $table_name (job_card_no, store_item_id, quantity, price, error, user_name ) VALUES ('".$job_card_no."', '".$store_item_id."','".$stock."', '".$price."', '1', '".$user."' ) ";
				$_SESSION['stock_error']=1;
			}
			else {
				 $table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			     $deliver_query = "INSERT INTO $table_name (job_card_no, store_item_id, quantity, price, error, user_name ) VALUES ('".$job_card_no."', '".$store_item_id."','".$quantity."', '".$price."', '0', '".$user."' ) ";
			}
			}
			
			
			if ($store_item_id==0) {
			
			if ($quantity>0)  {
				$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			    $deliver_query = "INSERT INTO $table_name (job_card_no, store_item_id, item_name, quantity, price, error, user_name ) VALUES ('".$job_card_no."', '".$store_item_id."', '".$store_item_name."', '0', '".$price."', '1', '".$user."' ) ";	
				$_SESSION['stock_error']=1;
			}
			else {				
			$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
			$deliver_query = "INSERT INTO $table_name (job_card_no, store_item_id, item_name, quantity, price, error, user_name ) VALUES ('".$job_card_no."', '".$store_item_id."', '".$store_item_name."', '".$quantity."', '".$price."', '0', '".$user."' ) ";	
			}
			}
			execsql($deliver_query);
			$i++;	
		}
		if ($_SESSION['stock_error']==1) { ?>
          <script language="javascript" type="text/javascript">
            window.location="m_supplyJobWizbase.php?p=2&s=";
</script>
<?php }
		};
	
	if($s==3) {
		
		}
	

	
	?>

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<link href="../css/wait.css" rel="stylesheet" type="text/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="../jscript/jquery.1.4.2.js"></script>
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></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/alert_message.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  
<script type="text/javascript" src="../window/dhtmlwindow.js"></script>
<script type="text/javascript" src="../window/modal.js"></script>
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>



<div class="main_frame">

<div class="page_head_frame">
                <label class="label_page_head">Store supply M-works </label>
                 
                <label class="label_page_head_2">job card:&nbsp;&nbsp;<?php echo $_SESSION['job_card_no'] ?>&nbsp;&nbsp;<?php echo $_SESSION['name'] ?></label>
</div>               
       <table >
     	<tr>
      	<td >         
        <div class="wizard_paging_frame">
        <table align="left">
          <tr>
            <td width="56"><?php if ($_SESSION['p']==1) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;1</div></div>
                <div class="active_step_label">
                  Job details
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;1</div></div>
                <div class="de_active_step_label">
                  Job details
              </div>
              <?php } ?></td>
            <td width="72"><?php if ($_SESSION['p']==2) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;2</div></div>
                <div class="active_step_label">
                  Item list
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;2</div></div>
                <div class="de_active_step_label">
                  Item list
              </div>
              <?php } ?></td>
            <td width="68"><?php if ($_SESSION['p']==3) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;3</div></div>
                <div class="active_step_label">
                 Finish
              </div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;3</div></div>
                <div class="de_active_step_label">
                   Finish
              </div>
              <?php } ?></td>
            
            
            
            
          </tr>
        </table>
        </div>
        </td>
    </tr>
    <tr>
      <td><?php 
	if ($_SESSION['p']==1) { include("m_supplyJobWiz1.php"); }
	if ($_SESSION['p']==2) { include("m_supplyJobWiz2.php"); }
	if ($_SESSION['p']==3) { include("m_supplyJobWiz3.php"); }
	
	?></td>
    </tr>
  </table>
</div>

<?php include ('../navigation/side_bar.php'); ?>
