<?php 
include('../include/session.php');

date_default_timezone_set('Africa/Khartoum');
$entryDate = date("Y-m-d H:i:s",time()-3600); 


$job_no = $_POST['job_no'];
$mitek_num = $_POST['mitek_num'];
$bracing_num = $_POST['bracing_num'];
$sundries_num = $_POST['sundries_num'];
$additional_num = $_POST['additional_num'];

$i=0;
while ($i<$mitek_num) {
	$cap_item_id = $_POST['cap_item_id'.$i];
	$edit_value = $_POST['mitek_edit'.$i];
	$reason_text = $_POST['reason_text'.$i];
	
		$table_name = $_SESSION['db_prefix']."_captured_mitek";
		$check_query = "SELECT (auth_quantity-delivered-de_authorised) AS outstanding FROM $table_name WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "outstanding");
		if ($edit_value>0) {
	       	$table_name = $_SESSION['db_prefix']."_tracking_production";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$job_no."' ,'".$cap_item_id."','".$recorded_value."','".($recorded_value-$edit_value)."' )";
			$trackingResult = mySQL_query($trackingQuery);
		}
		
		$table_name = $_SESSION['db_prefix']."_captured_mitek";
		$mitek_query = "UPDATE $table_name SET de_authorised =  de_authorised+'".$edit_value."', notes_text = '".$reason_text."' WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		
		execsql($mitek_query);
	
	$i++;
}

$j=0;
while ($j<$bracing_num) {
	$bracing_name = $_POST['bracing_name'.$j];
	$bracing_reason_text = $_POST['bracing_reason_text'.$j];
	$bracing_store_item_id = $_POST['bracing_store_item_id'.$j];
	$edit_value = $_POST['bracing_edit'.$j];
	
		$table_name = $_SESSION['db_prefix']."_authorised_timber";
		$check_query = "SELECT (reserved-de_authorised) AS reserved FROM $table_name WHERE auth_timber_name = '".$bracing_name."' AND job_no = '".$job_no."' AND store_item_id!=0 ";
		execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "reserved");
		if ($edit_value>0) {
	       	$table_name = $_SESSION['db_prefix']."_tracking_production";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '82', '".$job_no."' ,'".$bracing_name."','".$recorded_value."','".($recorded_value-$edit_value)."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
		
	$table_name = $_SESSION['db_prefix']."_authorised_timber";
	$bracing_query = "UPDATE $table_name SET de_authorised = de_authorised+'".$edit_value."', notes_text = '".$bracing_reason_text."'  WHERE auth_timber_name = '".$bracing_name."' AND job_no = '".$job_no."' AND store_item_id = '".$bracing_store_item_id."' ";
	execsql($bracing_query);
	
	$j++;
}

$j=0;
while ($j<$sundries_num) {
	$sundries_reason_text = $_POST['sundries_reason_text'.$j];
	$sundries_store_item_id = $_POST['sundries_store_item_id'.$j];
	$sundries_name = $_POST['sundries_name'.$j];
	$edit_value = $_POST['sundries_edit'.$j];
	
	$table_name = $_SESSION['db_prefix']."_authorised_timber";
		$check_query = "SELECT (reserved--de_authorised) AS reserved FROM $table_name WHERE auth_timber_name = '".$sundries_name."' AND job_no = '".$job_no."' AND store_item_id!=0 ";
		execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "reserved");
		if ($edit_value>0) {
	       	$table_name = $_SESSION['db_prefix']."_tracking_production";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '82', '".$job_no."' ,'".$sundries_name."','".$recorded_value."','".($recorded_value-$edit_value)."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
		
	$table_name = $_SESSION['db_prefix']."_authorised_timber";
	$sundries_query = "UPDATE $table_name SET de_authorised = de_authorised+'".$edit_value."', notes_text = '".$sundries_reason_text."' WHERE auth_timber_name = '".$sundries_name."' AND job_no = '".$job_no."' AND store_item_id = '".$sundries_store_item_id."' ";
	execsql($sundries_query);
	
	$j++;
}

$m=0;
while ($m<$additional_num) {
	$cap_item_id = $_POST['additional_item'.$m];
	$edit_value = $_POST['additional_edit'.$m];
	$add_reason_text = $_POST['add_reason_text'.$m];
		
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
		$check_query = "SELECT (auth_quantity-delivered-de_authorised) AS outstanding FROM $table_name WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "outstanding");
		
		if ($edit_value>0) {
	       	$table_name = $_SESSION['db_prefix']."_tracking_production";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '83', '".$job_no."' ,'".$cap_item_id."','".$recorded_value."','".($recorded_value-$edit_value)."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
		$additional_query = "UPDATE $table_name SET de_authorised = de_authorised+'".$edit_value."', notes_text = '".$add_reason_text."'  WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		execsql($additional_query);
	
	$m++;
}

$_SESSION['edit_start']=0;

		if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	  window.location="../production/production_main.php";
		</script>
        <?php		 
	   }
	          
       if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	    window.location="de_authorisation_edit.php?job_no=<?php echo $job_no?>"
	   </script>
        <?php		 
	   }
	   ?>

