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

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


$quote_no = $_POST['quote_no_text'];


$table_name = $_SESSION['db_prefix']."_quotations";
$saved_enquiry_query = "SELECT * FROM $table_name WHERE quote_no = '".$quote_no."' ";
$saved_enquiry_result = mySQL_query($saved_enquiry_query);
$saved_enquiry_num = mysql_num_rows($saved_enquiry_result);


$seller_saved = mysql_result($saved_enquiry_result, 0, 'seller');
$project_saved = mysql_result($saved_enquiry_result, 0, 'project');
$project_no_saved = mysql_result($saved_enquiry_result, 0, 'project_no');
$project_area_saved = mysql_result($saved_enquiry_result, 0, 'project_area');
$project_place_saved = mysql_result($saved_enquiry_result, 0, 'project_place');
$project_description_saved = mysql_result($saved_enquiry_result, 0, 'project_description');



//$id_number = $_POST['id_number_text'];
$seller = $_POST['seller_select'];
$project = $_POST['project_select'];
$project_no = $_POST['project_no_text'];
$project_area = $_POST['project_area_select'];
$project_place = $_POST['project_place_select'];
$project_description = $_POST['project_description_text'];


$table_name = $_SESSION['db_prefix']."_quotations";
$enquiry_update_query = "UPDATE $table_name SET seller = '".$seller."', project = '".$project."', project_no = '".$project_no."', project_area = '".$project_area."', project_place = '".$project_place."', project_description = '".$project_description."'  WHERE quote_no = '".$quote_no."' ";
execsql($enquiry_update_query);

$table_name = $_SESSION['db_prefix']."_tracking_sales";

if ($seller_saved!=$seller) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$seller_saved."','".$seller."','sales rep' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($project_saved!=$project) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$project_saved."','".$project."','project' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($project_no_saved!=$project_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$project_no_saved."','".$project_no."','project no' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($project_area_saved!=$project_area) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$project_area_saved."','".$project_area."','project area' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($project_place!=$project_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$project_place_saved."','".$project_place."','project place' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($project_description_saved!=$project_description) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '67', '".$quote_no."' ,'".$project_description_saved."','".$project_description."','project description' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="quotation_request.php?quote_no=<?php echo $quote_no ?>";
	   </script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="quotation_request_edit.php?quote_no=<?php echo $quote_no ?>";
	   </script>
        <?php		 
	   }
	   ?>

