<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s", time()); 

$cust_id = $_POST['cust_id_text'];


$table_name = $_SESSION['db_prefix']."_customer";
$saved_cust_query = "SELECT * FROM $table_name WHERE cust_id = '".$cust_id."' ";
$saved_cust_result = mySQL_query($saved_cust_query);
$saved_cust_num = mysql_num_rows($saved_cust_result);
$cust_no = mysql_result($saved_cust_result, 0, 'cust_no');

$comp_name_saved = mysql_result($saved_cust_result, 0, 'comp_name');
$credit_cash_saved = mysql_result($saved_cust_result, 0, 'account_type');
$bag_box_saved = mysql_result($saved_cust_result, 0, 'bag_box');
$box_no_saved = mysql_result($saved_cust_result, 0, 'box_no');
$post_place_saved = mysql_result($saved_cust_result, 0, 'postal_place');
$phys_address_saved = mysql_result($saved_cust_result, 0, 'phys_address');
$phys_place_saved = mysql_result($saved_cust_result, 0, 'phys_place');
$country_saved = mysql_result($saved_cust_result, 0, 'country');
$phone_saved = mysql_result($saved_cust_result, 0, 'phone');
$fax_saved = mysql_result($saved_cust_result, 0, 'fax');
$VAT_no_saved = mysql_result($saved_cust_result, 0, 'VAT_no');
$contact1_saved = mysql_result($saved_cust_result, 0, 'contact_1');
$cell1_saved = mysql_result($saved_cust_result, 0, 'cell_1');
$email1_saved = mysql_result($saved_cust_result, 0, 'email_1');
$contact2_saved = mysql_result($saved_cust_result, 0, 'contact_2');
$cell2_saved = mysql_result($saved_cust_result, 0, 'cell_2');
$email2_saved = mysql_result($saved_cust_result, 0, 'email_2');
$web_site_saved = mysql_result($saved_cust_result, 0, 'web_site');
$notes_saved = mysql_result($saved_cust_result, 0, 'notes');


$comp_name = $_POST['comp_name_text'];
$credit_cash = $_POST['credit_cash_select'];
$bag_box = $_POST['bag_box_select'];
$box_no = $_POST['box_no_text'];
$post_place = $_POST['post_place_select'];
$phys_address = $_POST['phys_address_text'];
$phys_place = $_POST['phys_place_select'];
$country = $_POST['country_select'];
$phone = $_POST['phone_text'];
$fax = $_POST['fax_text'];
$VAT_no = $_POST['vat_text'];
$contact1 = $_POST['contact1_text'];
$cell1 = $_POST['cell1_text'];
$email1 = $_POST['email1_text'];
$contact2 = $_POST['contact2_text'];
$cell2 = $_POST['cell2_text'];
$email2 = $_POST['email2_text'];
$web_site = $_POST['web_site_text'];
$notes = $_POST['notes_text'];


$table_name = $_SESSION['db_prefix']."_customer";
$cust_query = "UPDATE $table_name SET comp_name = '".$comp_name."', account_type = '".$credit_cash."', bag_box = '".$bag_box."', box_no = '".$box_no."', postal_place = '".$post_place."', phys_address = '".$phys_address."', phys_place = '".$phys_place."', country = '".$country."', phone = '".$phone."', fax = '".$fax."', VAT_no = '".$VAT_no."', contact_1 = '".$contact1."', cell_1 = '".$cell1."', email_1 = '".$email1."', contact_2 = '".$contact2."', cell_2 = '".$cell2."', email_2 = '".$email2."', web_site = '".$web_site."', notes = '".$notes."' WHERE cust_id = '".$cust_id."' ";
execsql($cust_query);



$table_name = $_SESSION['db_prefix']."_tracking_orders";

if ($comp_name_saved!=$comp_name) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$comp_name_saved."','".$comp_name."','Account name' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($credit_cash_saved!=$credit_cash) {
		$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$credit_cash_saved."','".$credit_cash."','Status' )";
		//echo '<br>emp_first_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($bag_box_saved!=$bag_box) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$bag_box_saved."','".$bag_box."','Bag/box' )";
		//echo '<br>emp_last_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($box_no_saved!=$box_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$box_no_saved."','".$box_no."','Bag/box number' )";
		//echo '<br>emp_last_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($post_place_saved!=$post_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$post_place_saved."','".$post_place."','Post place' )";
		//echo '<br>birth_date'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($phys_address_saved!=$phys_address) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$phys_address_saved."','".$phys_address."','Physical address' )";
		//echo '<br>gender'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($phys_place_saved!=$phys_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$phys_place_saved."','".$phys_place."','Physical place' )";
		//echo '<br>birth_date'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($country_saved!=$country) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$country_saved."','".$country."','Country' )";
		//echo '<br>bag_box'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($phone_saved!=$phone) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$phone_saved."','".$phone."','Phone' )";
		//echo '<br>box_no'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($fax_saved!=$fax) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$fax_saved."','".$fax."','Fax' )";
		//echo '<br>postal_place'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($VAT_no_saved!=$VAT_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$VAT_no_saved."','".$VAT_no."','VAT no' )";
		//echo '<br>phys_address'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($contact1_saved!=$contact1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$contact1_saved."','".$contact1."','Contact 1' )";
		//echo '<br>phys_place'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($cell1_saved!=$cell1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$cell1_saved."','".$cell1."','Cell 1' )";
		//echo '<br>emergency_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($email1_saved!=$email1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$email1_saved."','".$email1."','email 1' )";
		//echo '<br>emergency_phone'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($contact2_saved!=$contact2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$contact2_saved."','".$contact2."','Contact 2' )";
		//echo '<br>position'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($email2_saved!=$email2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$email2_saved."','".$email2."','email 2' )";
		//echo '<br>pay_period'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($cell2!=$cell2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$cell2_saved."','".$cell2."','Cell 2' )";
		//echo '<br>rate'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($web_site_saved!=$web_site) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$web_site_saved."','".$web_site."','Web_site' )";
		//echo '<br>bank'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($notes_saved!=$notes) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '41', '".$cust_no."' ,'".$notes_saved."','".$notes."','Notes' )";
		//echo '<br>bank_branch'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location='account_details.php?cust_id=<?php echo $cust_id ?>';
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="account_details_edit.php?cust_id=<?php echo $cust_id?>"
	   </script>
        <?php		 
	   }
	   ?>

