<?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"); 
	$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;

	if ($_SESSION['project']=='Electric fencing') { $value = $_SESSION['square_tube']; }
	if ( ($_SESSION['project']=='Manase 358') OR ($_SESSION['project']=='Gate') OR ($_SESSION['project']=='Razor wire') OR ($_SESSION['project']=='Wall spikes') OR ($_SESSION['project']=='Farm fence') OR ($_SESSION['project']=='Free standing electric fence') OR ($_SESSION['project']=='Miscelaneous') ) { $value = $_SESSION['amount_text30']; }
	if ($_SESSION['project']=='Diamond mesh') { $value = $_SESSION['amount_text90']; }
	if ($_SESSION['project']=='Palisade standard') { $value = $_SESSION['amount_text90']; }
	if ($_SESSION['project']=='Palisade fencing') { $value = $_SESSION['palisade_text2']; }
	if ($_SESSION['project']=='Gate motor') { $value = $_SESSION['amount_text60']; }
	if ($_SESSION['project']=='Pool fence') { $value = $_SESSION['amount_text60']; }
	
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$quotation_query = "UPDATE $table_name SET `priority` = '".$_SESSION['priority']."',  `status` = 'quoted', `quotation_date` = '".$_SESSION['quotation_date']."', `followup_date` = '".$followup_date."', `re_title` = '".$_SESSION['re_text']."', `vat_text` = '".$_SESSION['VAT']."', `valid_days` = '".$_SESSION['valid_days']."', `delivery_days` = '".$_SESSION['delivery_days']."', `value` = '".$value."', `terms` = '".$_SESSION['terms']."', `picture` = '".$_SESSION['picture']."', `seller` = '".$_SESSION['seller']."' WHERE quote_no = '".$_SESSION['quote_no']."' ";
	 execsql($quotation_query);	
	//echo $quotation_query;
	
	if ($_SESSION['project']=='Electric fencing') {
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '0', 'Round bar, 8 line', '".$_SESSION['round_bar']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '1', 'Flat bar, 8 line', '".$_SESSION['flat_bar']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '2', 'Square tube, 8 line', '".$_SESSION['square_tube']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '3', 'Heavy duty square tubes brackets, 12 lines, 8 line', '".$_SESSION['heavy_duty']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '4', 'Earth loops', '".$_SESSION['earth_loops']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '5', 'Outrigger', '".$_SESSION['outrigger']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '6', 'Wash line', '".$_SESSION['wash_line']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '7', 'Strobe', '".$_SESSION['strobe']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '8', 'Anti lift bar', '".$_SESSION['anti_lift_bar']."' ) ";
		execsql($quote_query);
		
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '9', 'Ace 100 energizer', '".$_SESSION['ace_100']."' ) ";
		execsql($quote_query);
			
	}


if ( ($_SESSION['project']=='Gate motor')  OR ($_SESSION['project']=='Pool fence') )  {
	$i=0;
	while ($i<$_SESSION['line_num6']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '1', '".$_SESSION['project']."', '".$_SESSION['item_text6'.$i]."', '".$_SESSION['amount_text6'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	
	$i=0;
	while ($i<$_SESSION['line_num7']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '2', '".$_SESSION['project']."', '".$_SESSION['item_text7'.$i]."', '".$_SESSION['amount_text7'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	
	}
	
if ( ($_SESSION['project']=='Manase 358') OR ($_SESSION['project']=='Gate') OR ($_SESSION['project']=='Razor wire') OR ($_SESSION['project']=='Wall spikes') OR ($_SESSION['project']=='Farm fence') OR ($_SESSION['project']=='Free standing electric fence') OR ($_SESSION['project']=='Miscelaneous') ) { 
	$i=0;
	while ($i<$_SESSION['line_num2']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '1', '".$_SESSION['option1_text']."', '".$_SESSION['item_text2'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num3']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '11', '".$_SESSION['option1_text']."',  '".$_SESSION['item_text3'.$j]."', '".$_SESSION['amount_text3'.$j]."')";
    	execsql($line2_query);
		$j++;
	}
	
if ($_SESSION['option2_text']!='.') { 
	$i=0;
	while ($i<$_SESSION['line_num4']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '2', '".$_SESSION['option2_text']."', '".$_SESSION['item_text4'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num5']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '22', '".$_SESSION['option2_text']."',  '".$_SESSION['item_text5'.$j]."', '".$_SESSION['amount_text5'.$j]."')";
    	execsql($line2_query);
		$j++;
	}
}

if  (($_SESSION['project']=='Manase 358') OR ($_SESSION['project']=='Miscelaneous')) { 
if ($_SESSION['option3_text']!='.') { 
	$i=0;
	while ($i<$_SESSION['line_num_M4']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '3', '".$_SESSION['option3_text']."', '".$_SESSION['item_text_M4'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num_M5']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '33', '".$_SESSION['option3_text']."',  '".$_SESSION['item_text_M5'.$j]."', '".$_SESSION['amount_text_M5'.$j]."')";
    	execsql($line2_query);
		$j++;
	}
}


if ($_SESSION['option4_text']!='.') { 
	$i=0;
	while ($i<$_SESSION['line_num_K4']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '4', '".$_SESSION['option4_text']."', '".$_SESSION['item_text_K4'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num_K5']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '44', '".$_SESSION['option4_text']."',  '".$_SESSION['item_text_K5'.$j]."', '".$_SESSION['amount_text_K5'.$j]."')";
    	execsql($line2_query);
		$j++;
	}
}


if ($_SESSION['option5_text']!='.') { 
	$i=0;
	while ($i<$_SESSION['line_num_L4']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '5', '".$_SESSION['option5_text']."', '".$_SESSION['item_text_L4'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num_L5']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '55', '".$_SESSION['option5_text']."',  '".$_SESSION['item_text_L5'.$j]."', '".$_SESSION['amount_text_L5'.$j]."')";
    	execsql($line2_query);
		$j++;
	}
}

}
}

if ($_SESSION['project']=='Palisade fencing') { 
        $table_name = $_SESSION['db_prefix']."_quotation_lines";		
		$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '2', 'Devils fork', 'Devils fork', '".$_SESSION['palisade_text1']."')";
    	execsql($line_query);
		    			
		$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '7', 'Gate 1', '".$_SESSION['gate_text0']."',  '".$_SESSION['palisade_text6']."')";
    	execsql($line_query);
		
		$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '8', 'Gate 2', '".$_SESSION['gate_text1']."',  '".$_SESSION['palisade_text7']."')";
    	execsql($line_query);
		
		$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '9', 'Gate 3', '".$_SESSION['gate_text2']."',  '".$_SESSION['palisade_text8']."')";
    	execsql($line_query);

}


if ($_SESSION['project']=='Diamond mesh') { 
	$i=0;
	while ($i<6) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '1', '".$_SESSION['option8_text']."', '".$_SESSION['item_text8'.$i]."')";
    	execsql($line_query);
		$i++;
	}
	$j=0;
	while ($j<$_SESSION['line_num9']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		if ($_SESSION['amount_text9'.$j]!='.') {
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '11', '".$_SESSION['item_text9'.$j]."',  '".$_SESSION['item_text9'.$j]."', '".$_SESSION['amount_text9'.$j]."')";
    	execsql($line2_query);
		}
		$j++;
	}
}

if ($_SESSION['project']=='Palisade standard') { 
    
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
    	$line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text,  quote_item_text) VALUES ('".$_SESSION['quote_no']."', '1', '".$_SESSION['fence_height']."', 'Fence height: ".$_SESSION['fence_height']."mm')";
    	execsql($line_query);
	
	$j=0;
	while ($j<$_SESSION['line_num9']) {
		$table_name = $_SESSION['db_prefix']."_quotation_lines";
		if ($_SESSION['amount_text9'.$j]!='.') {
    	$line2_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$_SESSION['quote_no']."', '11', '".$_SESSION['item_text9'.$j]."',  '".$_SESSION['item_text9'.$j]."', '".$_SESSION['amount_text9'.$j]."')";
    	execsql($line2_query);
		}
		$j++;
	}
}
	
	$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']."', '51', '".$_SESSION['quote_no']."' ,'".$_SESSION['cust_id']."','".$_SESSION['project']."','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
    if (isset($_GET['new_button']) )
	   {
		 $action='quoteWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='quotation.php?quote_no='.$_SESSION['quote_no'] ;
	  }
	  
	include('../include/quotation_alert.php');	  
	  
	$_SESSION['company_name'] ="";
	 $_SESSION['name'] ="";
	 $_SESSION['id'] = "";
     $_SESSION['type'] ="";
     $_SESSION['contact'] = "";
	 $_SESSION['cell'] = "";
	 $_SESSION['bag_box'] = "";
	 $_SESSION['box_number'] = "";
	 $_SESSION['post_place'] = "";
	 $_SESSION['phone'] = "";
	 $_SESSION['fax'] = "";
	 $_SESSION['email'] = "";
	 $_SESSION['reference'] = "";
	 
	 $_SESSION['quote_line_1'] = "";
	 $_SESSION['quote_line_2'] = "";
	 $_SESSION['quote_line_3'] = "";
	 $_SESSION['quote_price_1'] = "";
	 $_SESSION['quote_price_2'] = "";
	 $_SESSION['quote_price_3'] = "";
	 
	 
	 $_SESSION['seller'] = "";
	 
	 
	 $_SESSION['quotation_date'] = date('Y-m-d');
	 $_SESSION['terms'] = '';
	 $_SESSION['valid_days'] = '';
	 $_SESSION['delivery_days'] = '';
	 $_SESSION['priority'] = '';
	 
	 	 
	 $_SESSION['notes'] ="";	   
	  
	 
?>

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



