<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


$job_no = $_GET['job_no_text'];


$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$saved_quote_query = "SELECT store_item_id, quantity, truss_item FROM $table_name WHERE quote_no = '".$job_no."' ";
$saved_quote_result = mySQL_query($saved_quote_query);
$saved_quote_num = mysql_num_rows($saved_quote_result);
$i=0;
while ($i<$saved_quote_num) {
$saved_store_item_id = mysql_result($saved_quote_result, $i, 'store_item_id');
$saved_quantity = mysql_result($saved_quote_result, $i, 'quantity');
$truss_item = mysql_result($saved_quote_result, $i, 'truss_item');

$store_item_id = $_GET['store_item_id'.$i];
$quantity = $_GET['quantity_text'.$i];

if ($quantity<>0) {

if ($saved_store_item_id!=$store_item_id) {
	    
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT price_2 FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$price_2 = mysql_result($name_result, 0, "price_2");
	
	   $table_name = $_SESSION['db_prefix']."_cash_job_lines";
	   $tile_update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."', price_2 = '".$price_2."' WHERE quote_no = '".$job_no."'  AND store_item_id = '".$saved_store_item_id."' ";
	   execsql($tile_update_query);
	   
	      
	   
	   $table_name = $_SESSION['db_prefix']."_store_items";
	   $name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
	   $name_result = mySQL_query($name_query);
	   $saved = mysql_result($name_result, 0, "item_name");
	  
	   
	   $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']."', '47', '".$job_no."' ,'".$saved_store_item_id."','".$store_item_id."','Item' )";
		execsql($trackingQuery);
		
		
			
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$new = mysql_result($name_result, 0, "item_name");	
		
		$note_text = 'Item was changed from '.$saved.' to '.$new.'.'; 
		$table_name = $_SESSION['db_prefix']."_quotation_notes";
		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
		execsql($notes_query);
}

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE quote_no = '".$job_no."'  AND store_item_id = '".$store_item_id."' ";
execsql($update_query);

if ($truss_item=='Timber') {
$table_name = $_SESSION['db_prefix']."_store_items";
$cap_timber_query = "SELECT depth, width, class, length FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$cap_timber_result = mySQL_query($cap_timber_query);
$cap_timber_name = mysql_result($cap_timber_result, 0, "depth").' x '.mysql_result($cap_timber_result, 0, "width").' ('.mysql_result($cap_timber_result, 0, "class").') '.' - '.mysql_result($cap_timber_result, 0, "length");
$length = mysql_result($cap_timber_result, 0, "length");
$cap_timber_length = ($length*$quantity)/1000;
	
$table_name = $_SESSION['db_prefix']."_captured_timber";
$cap_timber_query = "UPDATE $table_name SET cap_timber_length = '".$cap_timber_length."', prereserved = '".$cap_timber_length."' WHERE cap_job = '".$job_no."'  AND cap_timber_name = '".$cap_timber_name."' ";
execsql($cap_timber_query);
}

if ($truss_item=='Mitek') {
$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_query = "UPDATE $table_name SET cap_quantity = '".$quantity."' WHERE cap_job = '".$job_no."'  AND cap_item_id = '".$store_item_id."' ";
execsql($mitek_query);
}

if ($truss_item=='Sundries') {
$table_name = $_SESSION['db_prefix']."_captured_sundries";
$sundries_query = "UPDATE $table_name SET cap_quantity = '".$quantity."' WHERE cap_job = '".$job_no."'  AND cap_item_id = '".$store_item_id."' ";
execsql($sundries_query);
}

if ($saved_quantity!=$quantity) {
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$item = mysql_result($name_result, 0, "item_name");
		
		
	
	   $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']."', '48
	   ', '".$job_no."' ,'".$saved_quantity."','".$quantity."','Quantity for ".$item."' )";
	   execsql($trackingQuery);
		
		$note_text = 'Quantity for '.$item.' was changed from '.$saved_quantity.' to '.$quantity.'.'; 
		$table_name = $_SESSION['db_prefix']."_quotation_notes";
		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
		execsql($notes_query);
}
}
else {
	$table_name = $_SESSION['db_prefix']."_store_items";
	$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
	$name_result = mySQL_query($name_query);
	$item = mysql_result($name_result, 0, "item_name");
	
	$table_name = $_SESSION['db_prefix']."_cash_job_lines";
	$delete_query = "DELETE FROM $table_name WHERE quote_no = '".$job_no."'  AND store_item_id = '".$store_item_id."' ";
	execsql($delete_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$cap_timber_query = "SELECT depth, width, class, length FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
	$cap_timber_result = mySQL_query($cap_timber_query);
	$cap_timber_name = mysql_result($cap_timber_result, 0, "depth").' x '.mysql_result($cap_timber_result, 0, "width").' ('.mysql_result($cap_timber_result, 0, "class").') '.' - '.mysql_result($cap_timber_result, 0, "length");

	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$delete_query = "DELETE FROM $table_name WHERE cap_job = '".$job_no."'  AND cap_timber_name = '".$cap_timber_name."' ";
	execsql($delete_query);
	
	$table_name = $_SESSION['db_prefix']."_captured_mitek";
	$delete_query = "DELETE FROM $table_name WHERE cap_job = '".$job_no."'  AND cap_item_id = '".$store_item_id."' ";
	execsql($delete_query);
	
	$table_name = $_SESSION['db_prefix']."_captured_sundries";
	$delete_query = "DELETE FROM $table_name WHERE cap_job = '".$job_no."'  AND cap_item_id = '".$store_item_id."' ";
	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']."', '51', '".$job_no."' ,'','','".$item."' )";
	execsql($trackingQuery);
		
	$note_text = 'Item '.$item.' was deleted from the quote.'; 
	$table_name = $_SESSION['db_prefix']."_quotation_notes";
	$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
	execsql($notes_query);
	
		
}
$i++;
} 

$sales_discount = $_GET['discount'];
echo '<br>*****'.$sales_discount;
$transport = $_GET['transport'];
$quote_value = $_GET['sub_total2'];

$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$discount_query = "SELECT sales_discount, transport FROM $table_name WHERE job_no = '".$job_no."'  ";
$discount_result = mySQL_query($discount_query);
$saved_sales_discount = mysql_result($discount_result, 0, "sales_discount");
$saved_transport = mysql_result($discount_result, 0, "transport");

$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$update_query = "UPDATE $table_name SET sales_discount = '".$sales_discount."', value = '".$quote_value."', transport = '".$transport."' WHERE job_no = '".$job_no."'  ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_quotations";
$update_query = "UPDATE $table_name SET quote_discount = '".$sales_discount."', value = '".$quote_value."' WHERE quote_no = '".$job_no."'  ";
execsql($update_query);



if ($saved_sales_discount!=$sales_discount) {
	    $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']."', '48', '".$job_no."' ,'".$saved_sales_discount."','".$sales_discount."','Discount' )";
		execsql($trackingQuery);
		
		$note_text = 'Discount was changed from '.$saved_sales_discount.' to '.$sales_discount.'.'; 
		$table_name = $_SESSION['db_prefix']."_quotation_notes";
		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
		execsql($notes_query);
}

		

if ($saved_transport!=$transport) {
	    $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']."', '48', '".$job_no."' ,'".$saved_transport."','".$transport."','Transport' )";
		execsql($trackingQuery);
		
		$note_text = 'Transport was changed from '.$saved_transport.' to '.$transport.'.'; 
		$table_name = $_SESSION['db_prefix']."_quotation_notes";
		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
		execsql($notes_query);
}


$extra_note = $_GET['note_text'];
if ($extra_note!='') {
	    $note_text = $extra_note; 
		$table_name = $_SESSION['db_prefix']."_quotation_notes";
		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$job_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
		execsql($notes_query);
}


if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../sales/quotation.php?quote_no=<?php echo $job_no?>";
		</script>
        <?php		 
	   }
	   
	if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	        window.location="cash_quotation_edit.php?job_no=<?php echo $job_no?>&authorise=0";
	   </script>
        <?php		 
	   }
	   ?>

