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

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

$date = date("Y-m-d");
$entryDate = date("Y-m-d H:i:s",time()-3600); 
?>


<?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 (isset($_GET['end_button']) )
	   { echo 'checking button';
		 $_SESSION['p']= 7;
	   }
	   
	   
		
	
	if ($s==1) {
		$_SESSION['delivery_address']=$_GET['delivery_address_text'];
	 	$_SESSION['driver']=$_GET['driver_select'];
	 	$_SESSION['vehicle']=$_GET['vehicle_select'];
	 	$_SESSION['hour']=$_GET['hour_select'];
	 	$_SESSION['min']=$_GET['min_select'];

	}
		
	if($s==2) {
	 	$i=0;
		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		while ($i<$_SESSION['line_no2']) {
			
				$quantity = $_POST['truss_no_text'.$i];
				$truss_type = $_POST['truss_type_text'.$i];
				$check_query = "SELECT store_item_id FROM $table_name WHERE job_no='".$_SESSION['job_no']."' AND truss_type='".$truss_type."' AND user =  '".$_SESSION['username']."'  ";
				$check_result = mySQL_query($check_query);
				$check_num = mysql_num_rows($check_result);
				
				if ($check_num==0) {
				
				if ($quantity!=0) {
				$item_query = "INSERT INTO $table_name (job_no, timber_type, truss_type, quantity, user ) VALUES ('".$_SESSION['job_no']."', 'Trusses', '".$truss_type."', '".$quantity."', '".$_SESSION['username']."' ) ";
				execsql($item_query);
				}
				}
				else {
				$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE  job_no='".$_SESSION['job_no']."' AND truss_type='".$truss_type."' AND user =  '".$_SESSION['username']."'  ";
				execsql($update_query);
				}
				
				if ($quantity==0) {
					 	$delete_query = "DELETE FROM $table_name WHERE  job_no='".$_SESSION['job_no']."' AND truss_type='".$truss_type."' AND user =  '".$_SESSION['username']."'  ";
						execsql($delete_query);
				}
				
			$i++;
		}
	
	 
	};
	
	if($s==3) {
		//================== bracing section entry
		
		$j=0;
		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		while ($j<$_SESSION['line_no3a']) {
			$i=0;
			while($i<$_SESSION['line_no3b'.$j]) {
				$bracing_quantity = $_POST['bracing_quantity'.$j.$i];
				$bracing_length = $_POST['bracing_length'.$j.$i];
				$bracing_item_id = $_POST['bracing_item_id'.$j.$i];
				$tot_length=($bracing_length*$bracing_quantity)/1000;
				$bracing_authorised = $_POST['bracing_authorised_text'.$j.$i];
				
				if ($bracing_authorised=='') {$bracing_authorised='std';}
				
				$check_query = "SELECT store_item_id, quantity FROM $table_name WHERE job_no='".$_SESSION['job_no']."' AND store_item_id='".$bracing_item_id."' AND truss_type='bracing' AND timber_type= '".$_SESSION['bracing_timber_type'.$j]."'  AND user =  '".$_SESSION['username']."'  ";
				//echo '<br>'.$check_query;
				$check_result = mySQL_query($check_query);
				$check_num = mysql_num_rows($check_result);
				
				$table_name12 = $_SESSION['db_prefix']."_store_items";
				$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$bracing_item_id."'  ";
				//echo '<br>'.$check_stock_query;
				$check_stock_result = mySQL_query($check_stock_query);
				$quantity = mysql_result($check_stock_result, 0, "quantity");
				
				
				if ($check_num==0) {
								
				//if ($bracing_quantity!=0) {
				$item_query = "INSERT INTO $table_name (job_no, truss_type, timber_type, store_item_id, quantity, tot_length, user , extra) VALUES ('".$_SESSION['job_no']."', 'bracing', '".$_SESSION['bracing_timber_type'.$j]."', '".$bracing_item_id."', '".$bracing_quantity."', '".$tot_length."', '".$_SESSION['username']."', '".$bracing_authorised."' ) ";
				execsql($item_query);
				//}
				}
				else {
				$update_query = "UPDATE $table_name SET quantity = '".$bracing_quantity."', tot_length = '".$tot_length."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$bracing_item_id."' AND truss_type='bracing' AND timber_type= '".$_SESSION['bracing_timber_type'.$j]."' AND user =  '".$_SESSION['username']."' ";
				execsql($update_query);
				}
				
				if ($bracing_quantity==0) {
					 	$delete_query = "DELETE FROM $table_name WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$bracing_item_id."' AND truss_type='bracing' AND timber_type= '".$_SESSION['bracing_timber_type'.$j]."' AND user =  '".$_SESSION['username']."'  ";
						execsql($delete_query);
				}
				else {				
				if ($bracing_quantity>$quantity) {
				$_SESSION['error3']='yes';
				$bracing_quantity=$quantity;
				$_SESSION['timber3_error'.$i.$j]='yes';
				$tot_length=($bracing_length*$bracing_quantity)/1000;
				$update_query = "UPDATE $table_name SET quantity = '".$bracing_quantity."', tot_length = '".$tot_length."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$bracing_item_id."' AND truss_type='bracing' AND timber_type= '".$_SESSION['bracing_timber_type'.$j]."' AND user =  '".$_SESSION['username']."'  ";
				//echo '<br>'.$update_query;
				execsql($update_query);
				}
				}	
				
				$i++;
			}
			$j++;
		}
		
		
if ($_SESSION['variations_permission']=='yes') {  
$bracing_add_line_num = $_POST['bracing_add_line_num'];
    

	$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$delete_query = "DELETE FROM $table_name WHERE  job_no = '".$_SESSION['job_no']."' AND truss_type = 'bracing' AND extra = 'yes' AND user = '".$_SESSION['username']."' ";
	execsql($delete_query);
	
$i=0;
while ($i < $bracing_add_line_num) {
	$_SESSION['bracing_length_select'.$i] = '';
	$_SESSION['bracing_length_select_index'.$i] = '';
	//$_SESSION['bracing_reason_text'.$i] = '';
	
if(isset($_POST['bracing_line_checkbox'.$i]) ) {		
		
	//echo '<br>'.$i.' of '.$bracing_add_line_num;
	$_SESSION['bracing_timber_type_select'.$i] = $_POST['bracing_product_select'.$i]+2;	
	$_SESSION['bracing_productName'.$i] = $_POST['bracing_product_name'.$i];
	$timber_type = $_SESSION['bracing_productName'.$i];
	//echo '<br>timber type:'.$timber_type;
	$_SESSION['bracing_length_select'.$i] = $_POST['length_list_number'.$i];
	$_SESSION['bracing_length_select_index'.$i] = $_POST['length_list_number_index'.$i];
	
	$_SESSION['bracing_reason_text'.$i] = $_POST['bracing_reason_text'.$i];
	$_SESSION['line_no3c']=$_SESSION['line_no3c']+1;
	$store_item_id = $_POST['bracing_length_select'.$i];
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
		
	
	$reason = $_SESSION['bracing_reason_text'.$i];
	$quantity = $_POST['bracing_additional_text'.$i];
	$tot_length=($length*$quantity)/1000;
	//echo '<br>line:'.$i.'--- '.$_SESSION['bracing_length_select'.$i].'--- '. $_SESSION['bracing_timber_type_select'.$i].'--- '.$_SESSION['bracing_length_select_index'.$i];

    $table_name12 = $_SESSION['db_prefix']."_store_items";
	$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$store_item_id."'  ";
	
	$check_stock_result = mySQL_query($check_stock_query);
	$store_quantity = mysql_result($check_stock_result, 0, "quantity");
	
    if ($quantity>$store_quantity) {
			$_SESSION['error3']='yes';
			$quantity=$store_quantity;
			$_SESSION['bracing_error'.$i]='yes';
			$quantity = $store_quantity;
			if ($quantity<0) {$quantity=0;}
		}


    $table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$additional_query = "INSERT INTO $table_name (job_no, truss_type, store_item_id, timber_type, quantity, tot_length, extra, notes_text, user ) VALUES ('".$_SESSION['job_no']."', 'bracing', '".$store_item_id."', '".$timber_type."', '".$quantity."', '".$tot_length."',  'yes', '".$reason."',  '".$_SESSION['username']."') ";
	
	execsql($additional_query);	
}
	$i++;
	}
}


	if ($_SESSION['error3']=='yes') {
			?>
                <script language="javascript" type="text/javascript">
	                   window.location="deliverJobWizbase.php?p=3&s=&all=";
                </script>
               <?php
			}
		
		}


	if($s==4) {
		//================== sundries section entry		
				
		$j=0;
		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		while ($j<$_SESSION['line_no4a']) {
			$i=0;
			while($i<$_SESSION['line_no4b'.$j]) {
				$sundries_quantity = $_POST['sundries_quantity'.$j.$i];
				$sundries_length = $_POST['sundries_length'.$j.$i];
				$sundries_item_id = $_POST['sundries_item_id'.$j.$i];
				$tot_length=($sundries_length*$sundries_quantity)/1000;
				$sundries_authorised = $_POST['sundries_authorised_text'.$j.$i];
				
				if ($sundries_authorised=='') {$sundries_authorised='std';}
				
				$check_query = "SELECT store_item_id, quantity FROM $table_name WHERE job_no='".$_SESSION['job_no']."' AND store_item_id='".$sundries_item_id."' AND truss_type='sundries' AND timber_type= '".$_SESSION['sundries_timber_type'.$j]."'  AND user =  '".$_SESSION['username']."'  ";
				//echo '<br>'.$check_query;
				$check_result = mySQL_query($check_query);
				$check_num = mysql_num_rows($check_result);
				
				$table_name12 = $_SESSION['db_prefix']."_store_items";
				$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$sundries_item_id."'  ";
				//echo '<br>'.$check_stock_query;
				$check_stock_result = mySQL_query($check_stock_query);
				$quantity = mysql_result($check_stock_result, 0, "quantity");
				
				
				if ($check_num==0) {
								
				//if ($sundries_quantity!=0) {
				$item_query = "INSERT INTO $table_name (job_no, truss_type, timber_type, store_item_id, quantity, tot_length, user , extra) VALUES ('".$_SESSION['job_no']."', 'sundries', '".$_SESSION['sundries_timber_type'.$j]."', '".$sundries_item_id."', '".$sundries_quantity."', '".$tot_length."', '".$_SESSION['username']."', '".$sundries_authorised."' ) ";
				//echo '<br>'.$j.$item_query;
				execsql($item_query);
				//}
				}
				else {
				$update_query = "UPDATE $table_name SET quantity = '".$sundries_quantity."', tot_length = '".$tot_length."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$sundries_item_id."' AND truss_type='sundries' AND timber_type= '".$_SESSION['sundries_timber_type'.$j]."' AND user =  '".$_SESSION['username']."' AND extra =  '".$sundries_authorised."'  ";
				//echo '<br>'.$update_query;
				execsql($update_query);
				}
				
				if ($sundries_quantity==0) {
					 	$delete_query = "DELETE FROM $table_name WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$sundries_item_id."' AND truss_type='sundries' AND timber_type= '".$_SESSION['sundries_timber_type'.$j]."' AND user =  '".$_SESSION['username']."'  ";
						execsql($delete_query);
				}
				else {				
				if ($sundries_quantity>$quantity) {
				$_SESSION['error4']='yes';
				$sundries_quantity=$quantity;
				$_SESSION['timber4_error'.$j.$i]='yes';
				$tot_length=($sundries_length*$sundries_quantity)/1000;
				$update_query = "UPDATE $table_name SET quantity = '".$sundries_quantity."', tot_length = '".$tot_length."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$sundries_item_id."' AND truss_type='sundries' AND timber_type= '".$_SESSION['sundries_timber_type'.$j]."' AND user =  '".$_SESSION['username']."'  ";
				//echo '<br>'.$update_query;
				execsql($update_query);
				}
				}	
				
				$i++;
			}
				
			$j++;
		}
		
		
if ($_SESSION['variations_permission']=='yes') {  
$sundries_add_line_num = $_POST['sundries_add_line_num'];
    

	$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$delete_query = "DELETE FROM $table_name WHERE  job_no = '".$_SESSION['job_no']."' AND truss_type = 'sundries' AND extra = 'yes' AND user = '".$_SESSION['username']."' ";
	execsql($delete_query);
	
$i=0;
while ($i < $sundries_add_line_num) {
	$_SESSION['sundries_length_select'.$i] = '';
	$_SESSION['sundries_length_select_index'.$i] = '';
	
if(isset($_POST['sundries_line_checkbox'.$i]) ) {		
	

	//echo '<br>'.$i.' of '.$sundries_add_line_num;
	$_SESSION['sundries_timber_type_select'.$i] = $_POST['sundries_product_select'.$i]+2;	
	$_SESSION['sundries_productName'.$i] = $_POST['sundries_product_name'.$i];
	$timber_type = $_SESSION['sundries_productName'.$i];
	//echo '<br>timber type:'.$timber_type;
	$_SESSION['sundries_length_select'.$i] = $_POST['length_list_number'.$i];
	$_SESSION['sundries_length_select_index'.$i] = $_POST['length_list_number_index'.$i];
	$_SESSION['sundries_reason_text'.$i] = $_POST['sundries_reason_text'.$i];
	$_SESSION['line_no4c']=$_SESSION['line_no4c']+1;
	$store_item_id = $_POST['sundries_length_select'.$i];
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
	
	
	$quantity = $_POST['sundries_additional_text'.$i];
	$reason = $_SESSION['sundries_reason_text'.$i];
	//echo '<br>line:'.$i.'--- '.$_SESSION['sundries_length_select'.$i].'--- '. $_SESSION['sundries_timber_type_select'.$i].'--- '.$_SESSION['sundries_length_select_index'.$i];
	$tot_length=($length*$quantity)/1000;
	


    $table_name12 = $_SESSION['db_prefix']."_store_items";
	$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$store_item_id."'  ";
	
	$check_stock_result = mySQL_query($check_stock_query);
	$store_quantity = mysql_result($check_stock_result, 0, "quantity");
	
    if ($quantity>$store_quantity) {
			$_SESSION['error4']='yes';
			$quantity=$store_quantity;
			$_SESSION['sundries_error'.$i]='yes';
			$quantity = $store_quantity;
			if ($quantity<0) {$quantity=0;}
		}


    $table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$additional_query = "INSERT INTO $table_name (job_no, truss_type, store_item_id, timber_type, quantity, tot_length, notes_text, extra, user ) VALUES ('".$_SESSION['job_no']."', 'sundries', '".$store_item_id."', '".$timber_type."', '".$quantity."', '".$tot_length."', '".$reason."', 'yes', '".$_SESSION['username']."') ";
	//echo '<br>'.$additional_query;
	execsql($additional_query);	
}
	$i++;
	}
}


	
	if ($_SESSION['error4']=='yes') {
			?>
                <script language="javascript" type="text/javascript">
	                   window.location="deliverJobWizbase.php?p=4&s=&all=";
                </script>
               <?php
			}
		
		}
			
	if($s==5) {	
		$i=0;
		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		while ($i<$_SESSION['line_no5a']) {
				$authorised = $_POST['supply_text'.$i];
				$extra = $_POST['authorised_text'.$i];
				$quantity = $_POST['quantity'.$i];
				$item_id = $_POST['item_id'.$i];	
				if ($extra=='') {$extra='std';}
				
				$check_query = "SELECT store_item_id FROM $table_name WHERE job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."' AND truss_type='Mitek' AND extra <> 'yes' ";
				$check_result = mySQL_query($check_query);
				$check_num = mysql_num_rows($check_result);
								
				$table_name12 = $_SESSION['db_prefix']."_store_items";
				$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$item_id."'  ";
				$check_stock_result = mySQL_query($check_stock_query);
				$store_quantity = mysql_result($check_stock_result, 0, "quantity");
				
				if ($check_num==0) {
				
				//if ($quantity!=0) {
				$item_query = "INSERT INTO $table_name (job_no, truss_type, timber_type, store_item_id, authorised, quantity, user, extra ) VALUES ('".$_SESSION['job_no']."', 'Mitek', 'Mitek', '".$item_id."', '".$authorised."', '".$quantity."', '".$_SESSION['username']."', '".$extra."' ) ";
				execsql($item_query);
				//}
				}
				else {
				$update_query = "UPDATE $table_name SET authorised = '".$authorised."', quantity = '".$quantity."', extra = '".$extra."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."' AND extra <> 'yes' ";
				execsql($update_query);
				}
				
				if ($quantity==0) {
					 	$delete_query = "DELETE FROM $table_name WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  ";
						execsql($delete_query);
				}
				else {
				if ($quantity>$store_quantity) {
				$_SESSION['error5']='yes';
				$quantity=$store_quantity;
				$_SESSION['mitek_error'.$i]='yes';
				$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  ";
				execsql($update_query);
				}
				}
				
			$i++;
		}
		
if ($_SESSION['variations_permission']=='yes') {   
$add_line_num = $_POST['add_line_num'];
	
	$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$delete_query = "DELETE FROM $table_name WHERE  job_no = '".$_SESSION['job_no']."' AND truss_type = 'Mitek' AND extra = 'yes' AND user = '".$_SESSION['username']."' ";
	execsql($delete_query);
	
$i=0;
while ($i < $add_line_num) {	
	
	
	
if(isset($_POST['line_checkbox'.$i]) ) {	
	$store_item_id = $_POST['product_select'.$i];
	$quantity = $_POST['mitek_additional_text'.$i];
	$reason = $_POST['mitek_additional_reason_text'.$i];

    $table_name12 = $_SESSION['db_prefix']."_store_items";
	$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$store_item_id."'  ";
	$check_stock_result = mySQL_query($check_stock_query);
	$store_quantity = mysql_result($check_stock_result, 0, "quantity");
	
    if ($quantity>$store_quantity) {
			$_SESSION['error5']='yes';
			$quantity=$store_quantity;
			$_SESSION['mitek2_error'.$i]='yes';
			$quantity = $store_quantity;
			if ($quantity<0) {$quantity=0;}
		}


    $table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$additional_query = "INSERT INTO $table_name (job_no, truss_type, store_item_id, timber_type, quantity, notes_text, extra, user ) VALUES ('".$_SESSION['job_no']."', 'Mitek', '".$store_item_id."', 'Mitek', '".$quantity."', '".$reason."', 'yes', '".$_SESSION['username']."') ";
	execsql($additional_query);	
}
	$i++;
	}
}
		
		if ($_SESSION['error5']=='yes') {
			?>
                <script language="javascript" type="text/javascript">
	                   window.location="deliverJobWizbase.php?p=5&s=&all=";
                </script>
               <?php
			}
		
	}
	
	if($s==6) {
		$i=0;
		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		while ($i<$_SESSION['line_no6']) {
			    $authorised = $_POST['supply_text'.$i];
				$extra = $_POST['authorised_text'.$i];
				$quantity = $_POST['quantity'.$i];
				$item_id = $_POST['item_id'.$i];
				
				if ($extra=='') {$extra='std';}
				
				$check_query = "SELECT store_item_id FROM $table_name WHERE job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  AND truss_type='Additional' AND extra <> 'yes' ";
				$check_result = mySQL_query($check_query);
				$check_num = mysql_num_rows($check_result);
				
				$table_name12 = $_SESSION['db_prefix']."_store_items";
				$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$item_id."'  ";
				$check_stock_result = mySQL_query($check_stock_query);
				$store_quantity = mysql_result($check_stock_result, 0, "quantity");
				
				if ($check_num==0) {
				
				//if ($quantity!=0) {
				$item_query = "INSERT INTO $table_name (job_no, truss_type, timber_type, store_item_id, quantity, user, extra ) VALUES ('".$_SESSION['job_no']."', 'Additional', 'Additional', '".$item_id."', '".$quantity."', '".$_SESSION['username']."', '".$extra."' ) ";
				//echo '<br>'.$item_query;
				execsql($item_query);
				//}
				}
				else {
				$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  AND extra <> 'yes' ";
				execsql($update_query);
				}
				
				if ($quantity==0) {
					 	$delete_query = "DELETE FROM $table_name WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  ";
						execsql($delete_query);
				}
				else {				
				if ($quantity>$store_quantity) {
				$_SESSION['error6']='yes';
				$quantity=$store_quantity;
				$_SESSION['additional_error'.$i]='yes';
				$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE  job_no='".$_SESSION['job_no']."' AND store_item_id='".$item_id."' AND user =  '".$_SESSION['username']."'  ";
				execsql($update_query);
				}
				}
				
			$i++;
		}
		
if ($_SESSION['variations_permission']=='yes') {   
$add_line_num = $_POST['add_line_num'];
	
	$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$delete_query = "DELETE FROM $table_name WHERE  job_no = '".$_SESSION['job_no']."' AND truss_type = 'Additional' AND extra = 'yes' AND user = '".$_SESSION['username']."' ";
	execsql($delete_query);
	
$i=0;
while ($i < $add_line_num) {	
	
	$store_item_id = $_POST['product_select'.$i];
	$quantity = $_POST['additional_text'.$i];
	$reason = $_POST['additional_reason_text'.$i];
	
if(isset($_POST['line_checkbox'.$i]) ) {	

    $table_name12 = $_SESSION['db_prefix']."_store_items";
	$check_stock_query = "SELECT quantity FROM $table_name12 WHERE store_item_id = '".$store_item_id."'  ";
	$check_stock_result = mySQL_query($check_stock_query);
	$store_quantity = mysql_result($check_stock_result, 0, "quantity");
	//echo '<br>quantity'.$store_quantity;
    if ($quantity>$store_quantity) {
			$_SESSION['error6']='yes';
			$quantity=$store_quantity;
			$_SESSION['additional2_error'.$i]='yes';
			$quantity = $store_quantity;
			if ($quantity<0) {$quantity=0;}
		}


    $table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$additional_query = "INSERT INTO $table_name (job_no, truss_type, store_item_id, timber_type, quantity, notes_text, extra, user ) VALUES ('".$_SESSION['job_no']."', 'Additional', '".$store_item_id."', 'Additional', '".$quantity."', '".$reason."', 'yes', '".$_SESSION['username']."') ";
	//echo '<br>'.$additional_query;
	execsql($additional_query);	
}
	$i++;
	}
}
		
		
		if ($_SESSION['error6']=='yes') {
			?>
                <script language="javascript" type="text/javascript">
	                   window.location="deliverJobWizbase.php?p=6&s=&all=";
                </script>
               <?php
			}
	
		
	}
	

?>



<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<!--reference to main.css removed-->
<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="../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>  



<div class="main_frame" style="height:auto; min-height:550px;"  >
  <table width="595" align="left" id="tableMain">
  <!--begin of tableMain-->
  <tr>
    <td >
    <div class="page_head_frame">
      <table width="681" height="22" border="0">
        <tr>
          <td width="538"><label class="label_page_head">Deliver job </label>&nbsp;<label class="label_page_head_2"><?php echo $_SESSION['name'] ?></label> </td>
          <td width="132"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$_SESSION['job_no']; ?></label></td>
        </tr>
      </table>
      
      </div>
      <br /><br /><br /><br />

      <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 class="active_step_label"><br /><br /><br />
      Job details</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 />
      Job details</div></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 class="active_step_label"><br /><br /><br />
      Trusses</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 />
      Trusses</div></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 class="active_step_label"><br /><br /><br />
      Bracing timber</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 />
      Bracing timber</div></div>
      <?php } ?>
      </td>
      <td width="68">
      <?php if ($_SESSION['p']==4) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;4</div>
      <div class="active_step_label"><br /><br /><br />
      Sundries timber</div></div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;4</div>
      <div class="de_active_step_label"><br /><br /><br />
      Sundries timber</div></div>
      <?php } ?>
      </td>
      <td width="59">
      <?php if ($_SESSION['p']==5) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;5</div>
      <div class="active_step_label"><br /><br /><br />
      eCo products</div></div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;5</div>
      <div class="de_active_step_label"><br /><br /><br />
      eCo products</div></div>
      <?php } ?>
       </td>
       <td width="30">
      <?php if ($_SESSION['p']==6) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;6</div>
      <div class="active_step_label"><br /><br /><br />
      Additional sundries</div></div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;6</div>
      <div class="de_active_step_label"><br /><br /><br />
      Additional sundries</div></div>
      <?php } ?>
       </td>
       <td width="30">
      <?php if ($_SESSION['p']==7) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;7</div>
      <div class="active_step_label"><br /><br /><br />
      Finish</div></div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;7</div>
      <div class="de_active_step_label"><br /><br /><br />
      Finish</div></div>
      <?php } ?>
       </td>
      </tr>
      </table>
      </div>
      </td>
          </tr>
          <tr>
            <td><?php 
	if ($_SESSION['p']==1) { include("deliverJobWiz1.php"); }
	if ($_SESSION['p']==2) { include("deliverJobWiz2.php"); }
	if ($_SESSION['p']==3) { include("deliverJobWiz3.php"); }
	if ($_SESSION['p']==4) { include("deliverJobWiz4.php"); }
	if ($_SESSION['p']==5) { include("deliverJobWiz5.php"); }
	if ($_SESSION['p']==6) { include("deliverJobWiz6.php"); }
	if ($_SESSION['p']==7) { include("deliverJobWiz7.php"); }
	//if ($_SESSION['p']==8) { include("deliverJobWiz8.php"); }
	
    ?></td>
          </tr>
    </table>
    </div>
   



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