<?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_name2 = $_SESSION['db_prefix']."_quotation_accept";
	$accept_query = "INSERT INTO $table_name2 (date, quote_no, cust_id, account, accept_value, receipt_no, order_no, invoice_no, acceptor, quote_type ) VALUES (trim('".$_SESSION['acceptance_date']."'), trim('".$_SESSION['quote_no']."'), trim('".$_SESSION['cust_id']."'), trim('".$_SESSION['account']."'), trim('".$_SESSION['received']."'), trim('".$_SESSION['receipt']."'), trim('".$_SESSION['order']."'), trim('".$_SESSION['invoice']."'), trim('".$_SESSION['acceptor']."'), trim('".$_SESSION['quote_type']."') )";
	execsql($accept_query);
	
	$table_name3 = $_SESSION['db_prefix']."_quotations";
	$quote_query = "UPDATE $table_name3 SET status = 'accepted', finalised = '".$_SESSION['acceptance_date']."' 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, note_text, seller ) VALUES ('".$_SESSION['quote_no']."', '".$_SESSION['acceptance_date']."', '<b>This quote was successful, ".$_SESSION['quote_type']." created.</b>', 'system' ) ";
    execsql($notes_query);
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$details_query = "SELECT project_no, project_place, project_description, designer FROM $table_name WHERE quote_no = '".$_SESSION['quote_no']."' ";
	$details_result = mySQL_query($details_query);
	$project_no = mysql_result($details_result, 0, "project_no");
	$project_place = mysql_result($details_result, 0, "project_place");
	$project_description = mysql_result($details_result, 0, "project_description");
	$designer = mysql_result($details_result, 0, "designer");
	
	
if ($_SESSION['quote_type']=='Project') {	
	$table_name5 = $_SESSION['db_prefix']."_captured_jobs";
	$order_query = "INSERT INTO $table_name5 (job_no, cust_id, date, project_no, project_place, project_description, designer, value, captured, quote_note, install_by, job_type ) VALUES ( trim('".$_SESSION['quote_no']."'), trim('".$_SESSION['cust_id']."'), '".$_SESSION['acceptance_date']."', '".$project_no."', '".$project_place."',  '".$project_description."', '".$designer."',  trim('".$_SESSION['received']."'), 'yes', '".$_SESSION['note']."', '".$_SESSION['install']."', '".$_SESSION['quote_type']."' ) ";
	execsql($order_query);	
}

if ($_SESSION['quote_type']=='Cash sale') {	
			$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
			$cash_job_query = "UPDATE $table_name SET accepted = 'yes', authorisation = 'yes' WHERE job_no = '".$_SESSION['quote_no']."' ";
			execsql($cash_job_query);
			

$table_name = $_SESSION['db_prefix']."_captured_timber";
$cap_timber_query = "SELECT cap_timber_name, cap_timber_length FROM $table_name WHERE cap_job = '".$_SESSION['quote_no']."' ";
$cap_timber_result = mySQL_query($cap_timber_query);
$cap_timber_num = mysql_num_rows($cap_timber_result);

$s=0;
while ($s<$cap_timber_num) {
	$cap_timber_name = mysql_result($cap_timber_result, $s, "cap_timber_name");
	$cap_timber_length = mysql_result($cap_timber_result, $s, "cap_timber_length");
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$store_item_query = "SELECT store_item_id, length FROM $table_name WHERE item_name = '".$cap_timber_name."' ";
	$store_item_result = mySQL_query($store_item_query);
	$store_item_num = mysql_num_rows($store_item_result);
	$store_item_id = mysql_result($store_item_result, 0, "store_item_id");
	$length = mysql_result($store_item_result, 0, "length")/1000;
	
	$quantity = $cap_timber_length / $length;
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$captured_timber_query = "UPDATE $table_name SET auth_timber_length = cap_timber_length, reserved = prereserved WHERE cap_timber_name = '".$cap_timber_name."' AND cap_job = '".$_SESSION['quote_no']."' ";
	execsql($captured_timber_query);
	
	$table_name = $_SESSION['db_prefix']."_authorised_timber";
	$auth_timber_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, job_no ) VALUES ('Cash sale', '".$cap_timber_name."', '".$store_item_id."', '".$quantity."', '".$_SESSION['quote_no']."' ) ";
	execsql($auth_timber_query);
	
	$s++;
}
	
	
$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_query = "SELECT cap_item_id FROM $table_name WHERE cap_job = '".$_SESSION['quote_no']."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

$s=0;
while ($s<$mitek_num) {
	$cap_item_id = mysql_result($mitek_result, $s, "cap_item_id");
	$table_name = $_SESSION['db_prefix']."_captured_mitek";
	$mitek_auth_query = "UPDATE $table_name SET auth_quantity = cap_quantity, authorised = 'yes' WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$_SESSION['quote_no']."' ";
	execsql($mitek_auth_query);		
$s++;			
}

$table_name = $_SESSION['db_prefix']."_captured_sundries";
$sundries_query = "SELECT cap_item_id FROM $table_name WHERE cap_job = '".$_SESSION['quote_no']."' ";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);

$s=0;
while ($s<$sundries_num) {
	$cap_item_id = mysql_result($sundries_result, $s, "cap_item_id");
	$table_name = $_SESSION['db_prefix']."_captured_sundries";
	$sundries_auth_query = "UPDATE $table_name SET auth_quantity = cap_quantity, authorised = 'yes' WHERE cap_item_id = '".$cap_item_id."'  AND cap_job = '".$_SESSION['quote_no']."' ";
	execsql($sundries_auth_query);		
$s++;			
}

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$cash_job_query = "SELECT store_item_id FROM $table_name WHERE quote_no = '".$_SESSION['quote_no']."' ";
$cash_job_result = mySQL_query($cash_job_query);
$cash_job_num = mysql_num_rows($cash_job_result);

$s=0;
while ($s<$cash_job_num) {
	$store_item_id = mysql_result($cash_job_result, $s, "store_item_id");
	$table_name = $_SESSION['db_prefix']."_cash_job_lines";
	$cash_job_auth_query = "UPDATE $table_name SET authorised = quantity WHERE store_item_id = '".$store_item_id."' ";
	execsql($cash_job_auth_query);		
$s++;			
}

}
	
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$_SESSION['quote_no']."' ,'".$_SESSION['cust_id']."','successful', '".$_SESSION['quote_type']."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	include('../include/receive_order_alert.php');
	
	if ($_SESSION['quote_type']=='Cash sale') {
	   	 $action='quotation.php?quote_no='.$_SESSION['quote_no'] ;;
	   }
		 
	if ($_SESSION['quote_type']=='Project') {
	  	$action='acception.php?quote_no='.$_SESSION['quote_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['install'] = "";
	 
	 $_SESSION['quote_no']="";
	 $_SESSION['cust_id']="";
	 $_SESSION['seller']="";
	 $_SESSION['designer']="";
	 $_SESSION['acceptor']="";
	 $_SESSION['received']="";
	 $_SESSION['account']="";
	 $_SESSION['receipt']="---";
	 $_SESSION['order']="---";
	 $_SESSION['company_contact']="";
	 $_SESSION['contact_phone']="";
	 $_SESSION['project_no']="";
	 $_SESSION['value']="";
	 $_SESSION['acceptance_date']=date("Y-m-d");
	 $_SESSION['quote_type']="";
	 $_SESSION['quote_no']="";
	 $_SESSION['cust_id']="";
	 $_SESSION['seller']="";
	 $_SESSION['designer']="";
	 $_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>



