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



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

$quote_no = $_GET['quote_no'];
$truss_item = $_GET['category'];
$store_item_id = $_GET['store_item_id_select'];
$qty = $_GET['qty_text'];


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

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$add_query = "INSERT INTO $table_name (quote_no, store_item_id, truss_item, quantity, price, price_2) VALUES ('".$quote_no."', '".$store_item_id."', '".$truss_item."', '".$qty."', '".$price."', '".$price_2."' ) ";
execsql($add_query);

if ($truss_item=='Timber') {
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$item_query = "SELECT item_name, width, depth, length, class, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
	$item_result = mySQL_query($item_query);
	$item_num = mysql_num_rows($item_result);
	$item_name = mysql_result($item_result, 0, "item_name");
	$depth = mysql_result($item_result, 0, "depth");
	$width = mysql_result($item_result, 0, "width");
	$class = mysql_result($item_result, 0, "class");
	$cap_timber = $depth.'x'.$width.' ('.$class.')';
	$length = mysql_result($item_result, 0, "length");
	$prereserved = ($length*$qty)/1000;
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$cap_timber_query = "INSERT INTO $table_name (cap_job, cap_timber_group, cap_timber_name, cap_timber, cap_timber_length, prereserved, cap_depth, cap_width, cap_class, cap_timber_price ) VALUES ('".$quote_no."', 'Cash sale', '".$item_name."', '".$cap_timber."', '".$length."', '".$prereserved."', '".$depth."', '".$width."', '".$class."', '".$price."' ) ";
	execsql($cap_timber_query);
	
	//$table_name = $_SESSION['db_prefix']."_authorised_timber";
//	$auth_timber_query = "INSERT INTO $table_name (job_no, auth_timber_group, auth_timber_name, store_item_id, reserved ) VALUES ('".$quote_no."', 'Cash sale', '".$item_name."', '".$store_item_id."', '".$qty."' ) ";
//	execsql($auth_timber_query);
}

if ($truss_item=='Mitek') {
	$table_name = $_SESSION['db_prefix']."_captured_mitek";
	$mitek_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price ) VALUES ('".$quote_no."', 'Cash sale', '".$store_item_id."', '".$qty."', '".$price."' ) ";
	execsql($mitek_query);
}

if ($truss_item=='Sundries') {
	$table_name = $_SESSION['db_prefix']."_captured_sundries";
	$sundries_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price ) VALUES ('".$quote_no."', 'Cash sale', '".$store_item_id."', '".$qty."', '".$price."' ) ";
	execsql($sundries_query);
}



$note_text = $qty.' x '.$item_name.' added to quote';; 
$table_name = $_SESSION['db_prefix']."_quotation_notes";
$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$quote_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
execsql($notes_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']."', '50', '".$quote_no."' , '".$store_item_id."','','".$qty."' )";
execsql($trackingQuery);


?>


<script language="javascript" type="text/javascript">
         window.location="../sales/cash_quotation_edit.php?job_no=<?php echo $quote_no ?>&authorise=0";
</script>