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

	
		
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	
	$mm = date("m");
	$yy = 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");	
	
	
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$id_query = "SELECT quote_no  FROM $table_name ORDER BY quote_id DESC LIMIT 1 ";
	$id_query;
	$id_result = mySQL_query($id_query);
	$id_result_num = mysql_num_rows($id_result);
	if ($id_result_num!=0) {
	$quote_no = mysql_result($id_result, 0, "quote_no");
	}else {
		$quote_no='';
	}
	
	
	
	if ( substr($quote_no, 2, 2)!=$mm ) {
	  $number="000";
	}
	else {
	  $number = substr($quote_no, 4, 3);
	}
	
	$quote_no = $yy.$mm.$number+1;
		 
	$table_name = $_SESSION['db_prefix']."_quotations";
	$quotation_query = "INSERT INTO $table_name (`quote_no`, `cust_id`, `type`, `status`, `seller`, `request_date`, `company_contact`, `contact_phone`, `project`, `project_no`, `project_area`,  `project_place`, `project_description`) VALUES ('$quote_no', '".trim($_SESSION['id'])."', '".trim($_SESSION['type'])."', 'request', '".trim($_SESSION['seller'])."',  '".trim($_SESSION['receive_date'])."', '".trim($_SESSION['comp_contact'])."', '".trim($_SESSION['contact_phone'])."', '".trim($_SESSION['project'])."', '".trim($_SESSION['project_no'])."', '".trim($_SESSION['project_area'])."', '".trim($_SESSION['project_place'])."', '".trim($_SESSION['description'])."');";
	execsql($quotation_query);
	
	
	$table_name = $_SESSION['db_prefix']."_customer";
$customer_query = "UPDATE $table_name SET contact_1 = '".trim($_SESSION['comp_contact'])."', cell_1 = '".trim($_SESSION['contact_phone'])."' WHERE cust_id = '".trim($_SESSION['id'])."' ";
execsql($customer_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']."', '50', '".$quote_no."' ,'".$_SESSION['id']."','".$_SESSION['project']."','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	
	$_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['comp_contact'] = "";
	 $_SESSION['contact_phone'] = "";
	 $_SESSION['project_no'] = "";
	 $_SESSION['project_area'] = "";
	 $_SESSION['project_place'] = "";
	 $_SESSION['description'] = "";
	 $_SESSION['seller'] = "";
	 $_SESSION['seller'] = "";
	 $_SESSION['receive_date'] = date('Y-m-d');
	 
	 
	 	
    if (isset($_GET['new_button']) )
	   {
		 $action='quoteRequestWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='quotation_request.php?quote_no='.$quote_no ;
	  }

?>



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



