<?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()); 
	
	$table_name = $_SESSION['db_prefix']."_customer";
    $customer_query = "UPDATE $table_name SET comp_name = trim('".$_SESSION['comp_name']."') ,
											  name_2 = trim('".$_SESSION['name_2']."') ,
											  title = trim('".$_SESSION['title']."') ,
											  bag_box =  trim('".$_SESSION['bag_box']."') ,
											  box_no = trim('".$_SESSION['box_no']."') ,
											  postal_place = trim('".$_SESSION['post_place']."') ,
											  phys_address = trim('".$_SESSION['phys_address']."') ,
											  phys_place = trim('".$_SESSION['phys_place']."') ,
											  country = trim('".$_SESSION['country']."') ,
											  phone = trim('".$_SESSION['phone']."') ,
											  phone_2 = trim('".$_SESSION['phone_2']."') ,
											  fax = trim('".$_SESSION['fax']."') ,
											  vat_no = trim('".$_SESSION['vat_no']."') , 
											  contact_1 = trim('".$_SESSION['contact_1']."') ,
											  cell_1 =  trim('".$_SESSION['cell_1']."') ,
											  email_1 = trim('".$_SESSION['email_1']."') ,
											  contact_2 = trim('".$_SESSION['contact_2']."') , 
											  cell_2 = trim('".$_SESSION['cell_2']."') ,  
											  email_2 = trim('".$_SESSION['email_2']."') ,
											  web_site = trim('".$_SESSION['web_site']."') ,
											  notes = trim('".$_SESSION['notes']."')
							WHERE cust_id = '".$_SESSION['cust_id']."' ";				  
	execsql($customer_query);
	
		
	 if ($_SESSION['account']=="cash") {
	   $_SESSION['invoice'] = '';	 
	   $_SESSION['order'] = '';
	 }
	 
	 if ($_SESSION['account']=="account") {
	   $_SESSION['receipt'] = '';
	 }
	 
	
	$table_name = $_SESSION['db_prefix']."_compiled_jobs";
	$job_query = "SELECT job_card_no FROM $table_name ORDER BY job_card_no DESC LIMIT 1";
	$job_result = mySQL_query($job_query);
	$job_num = mysql_num_rows($job_result);
	$job_card_no = mysql_result($job_result, 0, "job_card_no");
	$job_card_no = $job_card_no + 1; 
	 
	 
	
	$table_name2 = $_SESSION['db_prefix']."_quotation_accept";
	$accept_query = "INSERT INTO $table_name2 (date, quote_no, job_card_no, cust_id, account, accept_value, receipt_no, order_no, invoice_no, acceptor ) VALUES (trim('".$_SESSION['acceptance_date']."'), trim('".$_SESSION['quote_no']."'), '".$job_card_no."', trim('".$_SESSION['cust_id']."'), trim('".$_SESSION['account']."'), trim('".$_SESSION['received']."'), trim('".$_SESSION['receipt']."'), trim('".$_SESSION['order']."'), trim('".$_SESSION['invoice']."'), trim('".$_SESSION['acceptor']."') )";
	execsql($accept_query);
	
	$table_name3 = $_SESSION['db_prefix']."_quotations";
	$quote_query = "UPDATE $table_name3 SET status = 'accepted', finalised = '".$_SESSION['acceptance_date']."', job_card_no = '".$job_card_no."', value = '".$_SESSION['received']."' WHERE quote_no = '".$_SESSION['quote_no']."' ";
	execsql($quote_query);
	
	$table_name4 = $_SESSION['db_prefix']."_quotation_notes";
    $notes_query = "INSERT INTO $table_name4 (quote_number, note_date, progress_status, note_text, seller ) VALUES ('".$_SESSION['quote_no']."', '".$_SESSION['acceptance_date']."', '".trim($_SESSION['status'])."', '<b>This quote was successful, job card ".$job_card_no." created.</b>', 'system' ) ";
	execsql($notes_query);
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$details_query = "SELECT project, project_no, project_area, project_place, project_description FROM $table_name WHERE quote_no = '".$_SESSION['quote_no']."' ";
	$details_result = mySQL_query($details_query);
	$project = mysql_result($details_result, 0, "project");
	$project_no = mysql_result($details_result, 0, "project_no");
	$project_area = mysql_result($details_result, 0, "project_area");
	$project_place = mysql_result($details_result, 0, "project_place");
	$project_description = mysql_result($details_result, 0, "project_description");
	
	
	$table_name5 = $_SESSION['db_prefix']."_compiled_jobs";
	$order_query = "INSERT INTO $table_name5 (job_no, job_card_no, cust_id, date, project, project_no, project_area, project_place, project_description, value, mworks, captured, quote_note) VALUES ( trim('".$_SESSION['quote_no']."'), '".$job_card_no."', trim('".$_SESSION['cust_id']."'), '".$_SESSION['acceptance_date']."' , '".$project."', '".$project_no."', '".$project_area."', '".$project_place."',  '".$project_description."',  trim('".$_SESSION['received']."'), 'no', 'yes', '".$_SESSION['note']."'  ) ";
	execsql($order_query);
	
	$table_name6 = $_SESSION['db_prefix']."_order_history";
    $history_query = "INSERT INTO $table_name6 (job_card_number, note_date, note_text, user_name ) VALUES ('".$job_card_no."', '".$entry_date."', 'Job card ".$job_card_no." created from quote no ".$_SESSION['quote_no'].".', '".$_SESSION['username']."' ) ";
	execsql($history_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']."', '53', '".$_SESSION['quote_no']."' ,'".$_SESSION['cust_id']."','successful','".$job_card_no."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	include('../include/receive_order_alert.php');
	
	if (isset($_GET['new_button']) )
	   {
		 $action='acceptanceWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='acception.php?quote_no='.$_SESSION['quote_no'].'&job_card_no='.$job_card_no ;
	  }
	
	$_SESSION['account_type'] ="";
	 $_SESSION['type'] ="";
     $_SESSION['comp_name'] = "";
	 $_SESSION['name_2'] = "";
	 $_SESSION['title'] = "";
	 $_SESSION['bag_box'] = "";
	 $_SESSION['box_no'] = "";
	 $_SESSION['post_place'] = "";
	 $_SESSION['phys_address'] = "";
	 $_SESSION['phys_place'] = "";
	 $_SESSION['country'] = "";
	 $_SESSION['phone'] = "";
	 $_SESSION['phone_2'] = "";
	 $_SESSION['fax'] = "";
	 $_SESSION['vat_no'] = "";
	 $_SESSION['contact_1'] = "";
	 $_SESSION['cell_1'] = "";
	 $_SESSION['email_1'] = "";
	 $_SESSION['contact_2'] = "";
	 $_SESSION['cell_2'] = "";
	 $_SESSION['email_2'] = "";
	 $_SESSION['web_site'] = "";
	 $_SESSION['notes'] = "";
	 
	 $_SESSION['quote_no']="";
	 $_SESSION['cust_id']="";
	 $_SESSION['seller']="";
	 $_SESSION['acceptor']="";
	 $_SESSION['received']="";
	 $_SESSION['account']="";
	 $_SESSION['receipt']="---";
	 $_SESSION['order']="---";
	 $_SESSION['company_contact']="";
	 $_SESSION['contact_phone']="";
	 $_SESSION['project']="";
	 $_SESSION['project_no']="";
	 $_SESSION['value']="";
	 $_SESSION['acceptance_date']=date("Y-m-d");$_SESSION['account_type'] ="";
	 $_SESSION['type'] ="";
     $_SESSION['comp_name'] = "";
	 $_SESSION['name_2'] = "";
	 $_SESSION['title'] = "";
	 $_SESSION['bag_box'] = "";
	 $_SESSION['box_no'] = "";
	 $_SESSION['post_place'] = "";
	 $_SESSION['phys_address'] = "";
	 $_SESSION['phys_place'] = "";
	 $_SESSION['country'] = "";
	 $_SESSION['phone'] = "";
	 $_SESSION['phone_2'] = "";
	 $_SESSION['fax'] = "";
	 $_SESSION['vat_no'] = "";
	 $_SESSION['contact_1'] = "";
	 $_SESSION['cell_1'] = "";
	 $_SESSION['email_1'] = "";
	 $_SESSION['contact_2'] = "";
	 $_SESSION['cell_2'] = "";
	 $_SESSION['email_2'] = "";
	 $_SESSION['web_site'] = "";
	 $_SESSION['notes'] = "";
	 
	 $_SESSION['quote_no']="";
	 $_SESSION['cust_id']="";
	 $_SESSION['seller']="";
	 $_SESSION['acceptor']="";
	 $_SESSION['received']="";
	 $_SESSION['account']="";
	 $_SESSION['receipt']="---";
	 $_SESSION['order']="---";
	 $_SESSION['invoice']="---";
	 $_SESSION['company_contact']="";
	 $_SESSION['contact_phone']="";
	 $_SESSION['project_no']="";
	 $_SESSION['value']="";
	 $_SESSION['acceptance_date']=date("Y-m-d");
	 
?>

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



