<?php 
include('../include/session.php');
$job_no = $_GET['job_no'];

$sub_category=$_GET['cat'];
$quantity=$_GET['qty_text'];
$store_item_id = $_GET['store_item_id_radio'];
$bracing_num = $_GET['bracing_num'];
$sundries_num = $_GET['bracing_num'];


if ($sub_category=='Bracing') {
$table_name = $_SESSION['db_prefix']."_list_bracing";
$name_query = "SELECT item_name FROM $table_name WHERE item_id = '".$bracing_num."' ";
$name_result = mySQL_query($name_query);
$bracing_name = mysql_result($name_result, 0, "item_name");



if ($sub_category='Bracing') {
	$table_name = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
	$check_query = "SELECT auth_id FROM $table_name WHERE auth_timber_group='bracing' AND auth_timber_name= '".$bracing_name."' AND job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
	//echo '<br>'.$check_query;
					$check_result = mySQL_query($check_query);
					$check_num = mysql_num_rows($check_result);
					//echo '<br>'.$check_num;
					
				if ($check_num==0) {
						$timber_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no, user) VALUES ('bracing','".$bracing_name."','".$store_item_id."', '".$quantity."', '0', '".$job_no."', '".$_SESSION['username']."' )";
					//echo '<br>'.$timber_query;
						execsql($timber_query);
				}
		
		else {
			?>
            <script>
			alert('This item is already in the list and will not be added to the list.\r\n\r\nIf you require additional quantity, you should edit the quantity in the list. ');
			</script>
            <?php 
		}
}
}


if ($sub_category=='Sundries') {
$table_name = $_SESSION['db_prefix']."_list_sundries";
$name_query = "SELECT item_name FROM $table_name WHERE item_id = '".$sundries_num."' ";
$name_result = mySQL_query($name_query);
$sundries_name = mysql_result($name_result, 0, "item_name");



if ($sub_category='sundries') {
	$table_name = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
	$check_query = "SELECT auth_id FROM $table_name WHERE auth_timber_group='sundries' AND auth_timber_name= '".$sundries_name."' AND job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
	//echo '<br>'.$check_query;
					$check_result = mySQL_query($check_query);
					$check_num = mysql_num_rows($check_result);
					//echo '<br>'.$check_num;
					
				if ($check_num==0) {
						$timber_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no, user) VALUES ('sundries','".$sundries_name."','".$store_item_id."', '".$quantity."', '0', '".$job_no."', '".$_SESSION['username']."' )";
					//echo '<br>'.$timber_query;
						execsql($timber_query);
				}
		
		else {
			?>
            <script>
			alert('This item is already in the list and will not be added to the list.\r\n\r\nIf you require additional quantity, you should edit the quantity in the list. ');
			</script>
            <?php 
		}
}
}

?>
		
		   <script language="javascript" type="text/javascript">
         window.location="../production/authorisation_edit.php?job_no=<?php echo $job_no ?>&screen=0&edit=1&sub_category=<?php if ($sub_category=='Bracing') { echo '#bracing'; } else { echo '#sundries2';}?>";
</script>
