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


$supplier_id = $_POST['supplier_id_text'];


$table_name = $_SESSION['db_prefix']."_suppliers";
$saved_supplier_query = "SELECT * FROM $table_name WHERE supplier_id = '".$supplier_id."' ";
$saved_supplier_result = $con->query($saved_supplier_query);
$row = mysqli_fetch_assoc($saved_supplier_result);
$account_name_saved = $row['account_name'];
$credit_cash_saved = $row['credit_cash'];
$post_address_saved = $row['post_address'];
$post_place_saved = $row['post_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['contact1'];
$cell1_saved = $row['cell1'];
$email1_saved = $row['email1'];
$contact2_saved = $row['contact2'];
$cell2_saved = $row['cell2'];
$email2_saved = $row['email2'];
$web_site_saved = $row['web_site'];
$notes_saved = $row['notes'];


$account_name = $_POST['account_name_text'];
$credit_cash = $_POST['credit_cash_select'];
$post_address = $_POST['post_address_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']."_suppliers";
$supplier_query = "UPDATE $table_name SET account_name = '".$account_name."', credit_cash = '".$credit_cash."', post_address = '".$post_address."', post_place = '".$post_place."', phys_address = '".$phys_address."', phys_place = '".$phys_place."', country = '".$country."', phone = '".$phone."', fax = '".$fax."', VAT_no = '".$VAT_no."', contact1 = '".$contact1."', cell1 = '".$cell1."', email1 = '".$email1."', contact2 = '".$contact2."', cell2 = '".$cell2."', email2 = '".$email2."', web_site = '".$web_site."', notes = '".$notes."' WHERE supplier_id = '".$supplier_id."' ";
$supplier_result = $con->query($supplier_query);



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

if ($account_name_saved!=$account_name) {
	    $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '23', '".$supplier_id."' ,'".$account_name_saved."','".$account_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']."', '23', '".$supplier_id."' ,'".$credit_cash_saved."','".$credit_cash."','Status' )";
$tracking_result = $con->query($tracking_query);
}

if ($post_address_saved!=$post_address) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '23', '".$supplier_id."' ,'".$post_address_saved."','".$post_address."','Post address' )";
$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$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']."', '23', '".$supplier_id."' ,'".$notes_saved."','".$notes."','Notes' )";
$tracking_result = $con->query($tracking_query);
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../store/store_main.php?date=&branch=&subCatSelect=Tiles";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="supplier_details_edit.php?supplier_id=<?php echo $supplier_id?>"
	   </script>
        <?php		 
	   }
	   ?>

