<?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 = $con->query($saved_cust_query);
$row = mysqli_fetch_assoc($saved_cust_result);
$cust_no = $row['cust_no'];
$comp_name_saved = $row['comp_name'];
$credit_cash_saved = $row['account_type'];
$bag_box_saved = $row['bag_box'];
$box_no_saved = $row['box_no'];
$post_place_saved = $row['postal_place'];
$phys_address_saved = $row['phys_address'];
$phys_place_saved = $row['phys_place'];
$country_saved = $row['country'];
$phone_saved = $row['phone'];
$fax_saved = $row['fax'];
$VAT_no_saved = $row['vat_no'];
$contact1_saved = $row['contact_1'];
$cell1_saved = $row['cell_1'];
$email1_saved = $row['email_1'];
$contact2_saved = $row['contact_2'];
$cell2_saved = $row['cell_2'];
$email2_saved = $row['email_2'];
$web_site_saved = $row['web_site'];
$notes_saved = $row['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."' ";
$cust_result = $con->query($cust_query);



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

if ($comp_name_saved!=$comp_name) {
	    $tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($credit_cash_saved!=$credit_cash) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($bag_box_saved!=$bag_box) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($box_no_saved!=$box_no) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($post_place_saved!=$post_place) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($phys_address_saved!=$phys_address) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($phys_place_saved!=$phys_place) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($country_saved!=$country) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($phone_saved!=$phone) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($fax_saved!=$fax) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($VAT_no_saved!=$VAT_no) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($contact1_saved!=$contact1) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($cell1_saved!=$cell1) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($email1_saved!=$email1) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($contact2_saved!=$contact2) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($email2_saved!=$email2) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($cell2!=$cell2) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($web_site_saved!=$web_site) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}

if ($notes_saved!=$notes) {
$tracking_query = "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' )";
$tracking_result = $con->query($tracking_query);
}


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		 
	   }
	   ?>

