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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 

	
$table_name = $_SESSION['db_prefix']."_cash_authorisation_lines_temp";
$timber_query = "SELECT * FROM $table_name WHERE truss_item = 'Timber'  ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);
$i=0;
while ($i<$timber_num) {
$job_no = mysql_result($timber_result, $i, "job_no");
$store_item_id = mysql_result($timber_result, $i, "store_item_id");
$truss_item = mysql_result($timber_result, $i, "truss_item");
$quantity = mysql_result($timber_result, $i, "quantity");


$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT item_name, length, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = mySQL_query($name_query);
$item_name = mysql_result($name_result, 0, "item_name");
$length = mysql_result($name_result, 0, "length");
$price = mysql_result($name_result, 0, "price");

$item_length = $quantity * ($length/1000);

$table_name = $_SESSION['db_prefix']."_authorised_timber";
$timber_authorise_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no ) VALUES ('Cash sale', '".$item_name."', '".$store_item_id."', '".$quantity."', '0', '".$job_no."' ) ";
execsql($timber_authorise_query);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber_capture_query = "UPDATE $table_name SET auth_timber_length = '".$item_length."', prereserved = '0',  reserved = '".$item_length."' WHERE cap_job = '".$job_no."' AND cap_timber_name = '".$item_name."'  ";
execsql($timber_capture_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET authorised = '".$quantity."', auth_price = '".$price."' WHERE quote_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);

$i++;
}

$table_name = $_SESSION['db_prefix']."_cash_authorisation_lines_temp";
$mitek_query = "SELECT * FROM $table_name WHERE truss_item = 'Mitek'  ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);
$i=0;
while ($i<$mitek_num) {
$job_no = mysql_result($mitek_result, $i, "job_no");
$store_item_id = mysql_result($mitek_result, $i, "store_item_id");
$truss_item = mysql_result($mitek_result, $i, "truss_item");
$quantity = mysql_result($mitek_result, $i, "quantity");

$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = mySQL_query($name_query);
$price = mysql_result($name_result, 0, "price");

$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_authorise_query = "UPDATE $table_name SET auth_quantity = '".$quantity."', authorised = 'yes' WHERE cap_job = '".$job_no."' AND cap_item_id = '".$store_item_id."' ";
execsql($mitek_authorise_query);

$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$plate_query = "UPDATE $table_name SET plate_area = '0' WHERE job_no = '".$job_no."' ";
execsql($plate_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET authorised = '".$quantity."', auth_price = '".$price."' WHERE quote_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);

$i++;
}

$table_name = $_SESSION['db_prefix']."_cash_authorisation_lines_temp";
$sundries_query = "SELECT * FROM $table_name WHERE truss_item = 'Sundries'  ";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);
$i=0;
while ($i<$sundries_num) {
$job_no = mysql_result($sundries_result, $i, "job_no");
$store_item_id = mysql_result($sundries_result, $i, "store_item_id");
$truss_item = mysql_result($sundries_result, $i, "truss_item");
$quantity = mysql_result($sundries_result, $i, "quantity");

$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = mySQL_query($name_query);
$price = mysql_result($name_result, 0, "price");



$table_name = $_SESSION['db_prefix']."_captured_sundries";
$sundries_authorise_query = "UPDATE $table_name SET auth_quantity = '".$quantity."', authorised = 'yes'  WHERE cap_job = '".$job_no."' AND cap_item_id = '".$store_item_id."' ";
execsql($sundries_authorise_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET authorised = '".$quantity."', auth_price = '".$price."'  WHERE quote_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);

$i++;
}
	
$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$finalise_query = "UPDATE $table_name SET authorisation = 'yes' WHERE job_no = '".$job_no."' ";
execsql($finalise_query);				
	
$table_name = $_SESSION['db_prefix']."_cash_authorisation_lines_temp";
$delete_query = "DELETE FROM $table_name"; 
execsql($delete_query);	
	
$table_name = $_SESSION['db_prefix']."_tracking_sales";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '54', '".$_SESSION['job_no']."' ,'','','' )";
$trackingResult = mySQL_query($trackingQuery); 
	
$job_no=$_SESSION['job_no'];
	

?>

<script language="javascript" type="text/javascript">
        
	    window.location="../sales/cash_authorisation.php?job_no=<?php echo $job_no ?>";
        
       </script>




