<?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_customer_query = "SELECT * FROM $table_name WHERE cust_id = '".$cust_id."' ";
$saved_customer_result = mySQL_query($saved_customer_query);
$saved_customer_num = mysql_num_rows($saved_customer_result);

$comp_name_saved = mysql_result($saved_customer_result, 0, 'comp_name');
$account_type_saved = mysql_result($saved_customer_result, 0, 'account_type');
$name_2_saved = mysql_result($saved_customer_result, 0, 'name_2');
$title_saved = mysql_result($saved_customer_result, 0, 'title');
$bag_box_saved = mysql_result($saved_customer_result, 0, 'bag_box');
$box_no_saved = mysql_result($saved_customer_result, 0, 'box_no');
$postal_place_saved = mysql_result($saved_customer_result, 0, 'postal_place');
$phys_address_saved = mysql_result($saved_customer_result, 0, 'phys_address');
$phys_place_saved = mysql_result($saved_customer_result, 0, 'phys_place');
$country_saved = mysql_result($saved_customer_result, 0, 'country');
$phone_saved = mysql_result($saved_customer_result, 0, 'phone');
$fax_saved = mysql_result($saved_customer_result, 0, 'fax');
$vat_no_saved = mysql_result($saved_customer_result, 0, 'vat_no');
$web_site_saved = mysql_result($saved_customer_result, 0, 'web_site');
$contact_1_saved = mysql_result($saved_customer_result, 0, 'contact_1');
$cell_1_saved = mysql_result($saved_customer_result, 0, 'cell_1');
$email_1_saved = mysql_result($saved_customer_result, 0, 'email_1');
$contact_2_saved = mysql_result($saved_customer_result, 0, 'contact_2');
$cell_2_saved = mysql_result($saved_customer_result, 0, 'cell_2');
$email_2_saved = mysql_result($saved_customer_result, 0, 'email_2');
$notes_saved = mysql_result($saved_customer_result, 0, 'notes');





$comp_name = $_POST['comp_name_text'];
$account_type = $_POST['account_type_select'];
$name_2 = $_POST['name_2_text'];
$title = $_POST['title_select'];
$bag_box = $_POST['bag_box_select'];
$box_no = $_POST['box_no_text'];
$postal_place = $_POST['postal_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_no_text'];
$web_site = $_POST['web_site_text'];
$contact_1 = $_POST['contact_1_text'];
$cell_1 = $_POST['cell_1_text'];
$email_1 = $_POST['email_1_text'];
$contact_2 = $_POST['contact_2_text'];
$cell_2 = $_POST['cell_2_text'];
$email_2 = $_POST['email_2_text'];
$notes = $_POST['notes_text'];

$table_name = $_SESSION['db_prefix']."_customer";
$employee_query = "UPDATE $table_name SET comp_name = '".$comp_name."', account_type = '".$account_type."', name_2 = '".$name_2."', title = '".$title."', bag_box = '".$bag_box."', box_no = '".$box_no."', postal_place = '".$postal_place."', phys_address = '".$phys_address."', phys_place = '".$phys_place."', country = '".$country."', phone = '".$phone."', fax = '".$fax."', vat_no = '".$vat_no."', web_site = '".$web_site."', contact_1 = '".$contact_1."', cell_1 = '".$cell_1."', email_1 = '".$email_1."', contact_2 = '".$contact_2."', cell_2 = '".$cell_2."', email_2 = '".$email_2."', notes = '".$notes."' WHERE cust_id = '".$cust_id."' ";
execsql($employee_query);


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

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']."', '55', '".$cust_id."' ,'".$comp_name_saved."','".$comp_name."','Company name' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($name_2_saved!=$name_2) {
		$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$name_2_saved."','".$name_2."','First name' )";
		//echo '<br>emp_first_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($account_type_saved!=$account_type) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$account_type_saved."','".$account_type."','Account type' )";
		//echo '<br>emp_last_name'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($title_saved!=$title) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$title_saved."','".$title."','Title' )";
		//echo '<br>birth_date'.$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']."', '55', '".$cust_id."' ,'".$bag_box_saved."','".$bag_box."','Bag/box' )";
		//echo '<br>gender'.$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']."', '55', '".$cust_id."' ,'".$box_no_saved."','".$box_no."','Bag/box no' )";
		//echo '<br>box_no'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($postal_place_saved!=$postal_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$postal_place_saved."','".$postal_place."','Postal place' )";
		//echo '<br>postal_place'.$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']."', '55', '".$cust_id."' ,'".$phys_address_saved."','".$phys_address."','Physical address' )";
		//echo '<br>phys_address'.$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']."', '55', '".$cust_id."' ,'".$phys_place_saved."','".$phys_place."','Physical place' )";
		//echo '<br>phys_place'.$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']."', '55', '".$cust_id."' ,'".$country_saved."','".$country."','Country' )";
		//echo '<br>emergency_name'.$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']."', '55', '".$cust_id."' ,'".$phone_saved."','".$phone."','Phone' )";
		//echo '<br>emergency_phone'.$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']."', '55', '".$cust_id."' ,'".$fax_saved."','".$fax."','Fax' )";
		//echo '<br>position'.$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']."', '55', '".$cust_id."' ,'".$vat_no_saved."','".$vat_no."','Vat no' )";
		//echo '<br>pay_period'.$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']."', '55', '".$cust_id."' ,'".$web_site_saved."','".$web_site."','Web site' )";
		//echo '<br>rate'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($contact_1_saved!=$contact_1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$contact_1_saved."','".$contact_1."','Contact 1' )";
		//echo '<br>bank'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($cell_1_saved!=$cell_1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$cell_1_saved."','".$cell_1."','Cell 1' )";
		//echo '<br>bank_branch'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($email_1_saved!=$email_1) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$email_1_saved."','".$email_1."','email 1' )";
		//echo '<br>bank_account'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($contact_2_saved!=$contact_2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$contact_2_saved."','".$contact_2."','Contact 2' )";
		//echo '<br>clothing_size'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($cell_2_saved!=$cell_2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$cell_2_saved."','".$cell_2."','Cell 2' )";
		//echo '<br>shoe_size'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($email_2_saved!=$email_2) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '55', '".$cust_id."' ,'".$email_2_saved."','".$email_2."','email 2' )";
		//echo '<br>cell'.$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']."', '55', '".$cust_id."' ,'".$notes_saved."','".$notes."','Notes' )";
		//echo '<br>notes'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}




if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../sales/sales_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="customer_details_edit.php?cust_id=<?php echo $cust_id?>"
	   </script>
        <?php		 
	   }
	   ?>

