<?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());
	$read_date = date("Y-m-d");	
	$seller = $_GET['seller_select'];
	$status = 'pending';
	$status = 'pending';
	$notes_string=$_GET['notes_text'];			
	$notes_string = str_replace("'", "-", $notes_string);
	$notes_string = htmlentities($notes_string, ENT_QUOTES);
	
		$read_date = date("Y-m-d");
	
//$table_name = $_SESSION['db_prefix']."_presets";
//$high_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Priority high'  ";
//$high_result = mySQL_query($high_query);
//$high = mysql_result($high_result, 0, "preset_value");
//
//$table_name = $_SESSION['db_prefix']."_presets";
//$normal_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Priority normal'  ";
//$normal_result = mySQL_query($normal_query);
//$normal = mysql_result($normal_result, 0, "preset_value");
//
//$table_name = $_SESSION['db_prefix']."_presets";
//$low_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Priority low'  ";
//$low_result = mySQL_query($low_query);
//$low = mysql_result($low_result, 0, "preset_value");
		
	
//if ($_SESSION['priority']=='Hot') { $followup_date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($read_date)) . " +".$high." day"));}
//if ($_SESSION['priority']=='Warm') { $followup_date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($read_date)) . " +".$normal." day"));}
//if ($_SESSION['priority']=='Cold') { $followup_date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($read_date)) . " +".$low." day"));}

$followup_date = $read_date;

	
$_SESSION['priority'] = $_GET['priority_group'];

	  	
$_SESSION['status'] = $_GET['status_group'];
	  if ($_SESSION['status']=="successful" ) {
	
	  if ($_SESSION['name']!='Bill of quantity') {	  
		  	
	    	$_SESSION['entry_date'] = $_GET['entry_date'];
	  		$_SESSION['seller'] = $_GET['seller_select'];
	  		$_SESSION['notes'] = $_GET['notes_text'];
					
			
			$table_name = $_SESSION['db_prefix']."_quotation_notes";
    		$notes_query = "INSERT INTO $table_name (quote_number, note_date, progress_status, note_text, seller ) VALUES ( '".trim($_SESSION['quote_no'])."', '".trim($_SESSION['entry_date'])."', '".trim($_SESSION['status'])."', '".trim($_SESSION['notes'])."', '".trim($_SESSION['seller'])."' ) ";
    		execsql($notes_query);
			
				$table_name = $_SESSION['db_prefix']."_quotations";
				$status_query = "UPDATE $table_name SET status = 'quoted' WHERE quote_no = '".$_SESSION['quote_no']."' ";
				execsql($status_query);
			
		?>
            <script language="javascript" type="text/javascript">
        		window.location="acceptanceWiz0.php?quote_no=<?php echo $_SESSION['quote_no'] ?> ";
        	</script>
            <?php	
	  }
	   else {
		    $_SESSION['priority'] = $_GET['priority_group'];
		    $_SESSION['entry_date'] = $_GET['entry_date'];
	  		$_SESSION['seller'] = $_GET['seller_select'];
	  		$_SESSION['notes'] = $_GET['notes_text'];
			
			   
		       	$table_name = $_SESSION['db_prefix']."_quotation_notes";
				$notes_query = "INSERT INTO $table_name (quote_number, note_date, progress_status, note_text, seller ) VALUES ( '".trim($_SESSION['quote_no'])."', '".trim($_SESSION['entry_date'])."', '".trim($_SESSION['status'])."', 'This bill of quantity was successful', '".trim($_SESSION['seller'])."' ) ";
    			execsql($notes_query);
    			$notes_query = "INSERT INTO $table_name (quote_number, note_date, progress_status, note_text, seller ) VALUES ( '".trim($_SESSION['quote_no'])."', '".trim($_SESSION['entry_date'])."', '".trim($_SESSION['status'])."', '".trim($_SESSION['notes'])."', '".trim($_SESSION['seller'])."' ) ";
    			execsql($notes_query);
		   
				$table_name = $_SESSION['db_prefix']."_quotations";
				$status_query = "UPDATE $table_name SET status = 'accepted' WHERE quote_no = '".$_SESSION['quote_no']."' ";
				execsql($status_query);
			}	
			
			?>
        	<script language="javascript" type="text/javascript">
        		window.location="qbill.php?quote_no=<?php echo $_SESSION['quote_no'] ?> ";
        	</script>
        	<?php
			}
	 
	  else {
		   
	$table_name = $_SESSION['db_prefix']."_quotation_notes";
    $notes_query = "INSERT INTO $table_name (quote_number, priority, note_date, progress_status, note_text, seller ) VALUES ( '".trim($_SESSION['quote_no'])."', '".$_SESSION['priority']."', '".trim($_SESSION['entry_date'])."', '".trim($_SESSION['status'])."', '".$notes_string."', '".$seller."' ) ";
	execsql($notes_query);
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$update_query = "UPDATE $table_name SET priority  = '".$_SESSION['priority']."', `followup_date` = '".$followup_date."' WHERE quote_no = '".$_SESSION['quote_no']."' ";
	execsql($update_query);				
	
	if ($_SESSION['status']=="unsuccessful") {
		$table_name = $_SESSION['db_prefix']."_quotations";
		$status_query = "UPDATE $table_name SET status = 'unsuccessful' WHERE quote_no = '".$_SESSION['quote_no']."' ";
		$status = 'unsuccessful';
		execsql($status_query);
	}
	

	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '52', '".$_SESSION['quote_no']."' ,'".$_SESSION['cust_id']."','".$status."','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
    if (isset($_GET['new_button']) )
	   {
		 $action='quote_updateWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		if ($_SESSION['name']=='Bill of quantity'){
			  $action='qbill.php?quote_no='.$_SESSION['quote_no'] ;
		}
		else {   			  
		   $action='quotation.php?quote_no='.$_SESSION['quote_no'] ;
		}
	  }
	  
 
?>

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


<?php
	  }
	  ?>
