diff --git a/public/userportal/add-new-owner.php b/public/userportal/add-new-owner.php new file mode 100644 index 0000000..edf42d4 --- /dev/null +++ b/public/userportal/add-new-owner.php @@ -0,0 +1,48 @@ +prepare("SELECT owner_id FROM property_owners WHERE tax_code = ?"); +$checkQuery->bind_param("s", $tax_code); +$checkQuery->execute(); +$checkQuery->store_result(); + +if ($checkQuery->num_rows > 0) { + echo json_encode(["success" => false, "message" => "Questo codice fiscale esiste già!"]); + exit; +} + +// Inserisci il nuovo proprietario +$query = $conn->prepare("INSERT INTO property_owners (user_id, first_name, last_name, company_name, tax_code, email, phone, address, postal_code, city, province, country, owner_type, role, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); +$query->bind_param("issssssssssssss", $user_id, $first_name, $last_name, $company_name, $tax_code, $email, $phone, $address, $postal_code, $city, $province, $country, $owner_type, $role, $notes); + +if ($query->execute()) { + echo json_encode([ + "success" => true, + "message" => "Proprietario salvato con successo!", + "owner_id" => $conn->insert_id, + "owner_name" => $owner_type == "company" ? $company_name : "$first_name $last_name", + "tax_code" => $tax_code + ]); +} else { + echo json_encode(["success" => false, "message" => "Errore nell'inserimento."]); +} + +$conn->close(); diff --git a/public/userportal/add-owner-to-home.php b/public/userportal/add-owner-to-home.php index bf1635b..e0145b1 100644 --- a/public/userportal/add-owner-to-home.php +++ b/public/userportal/add-owner-to-home.php @@ -9,7 +9,7 @@ include('include/headscript.php'); $conn = new mysqli($servername, $username, $password, $database); if ($conn->connect_error) { - die("Errore di connessione: " . $conn->connect_error); + die(json_encode(["success" => false, "message" => "Errore di connessione: " . $conn->connect_error])); } // Recupera i dati inviati tramite POST @@ -18,19 +18,33 @@ $owner_id = isset($_POST['owner_id']) ? intval($_POST['owner_id']) : 0; $ownership_percentage = isset($_POST['ownership_percentage']) ? floatval($_POST['ownership_percentage']) : null; $notes = isset($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null; -// Verifica che tutti i dati necessari siano presenti -if ($idhome <= 0 || $owner_id <= 0) { - die("Errore: Dati mancanti."); +// Verifica che tutti i dati siano presenti +if ($idhome <= 0 || $owner_id <= 0 || is_null($ownership_percentage)) { + die(json_encode(["success" => false, "message" => "Errore: Dati mancanti."])); } -// Controlla se il proprietario è già associato a questa casa -$queryCheck = $conn->prepare("SELECT * FROM home_owners WHERE home_id = ? AND owner_id = ?"); +// Controlla se il proprietario è già associato alla casa +$queryCheck = $conn->prepare("SELECT 1 FROM home_owners WHERE home_id = ? AND owner_id = ?"); $queryCheck->bind_param('ii', $idhome, $owner_id); $queryCheck->execute(); $resultCheck = $queryCheck->get_result(); if ($resultCheck->num_rows > 0) { - die("Errore: Il proprietario è già associato a questa casa."); + die(json_encode(["success" => false, "message" => "Errore: Il proprietario è già associato a questa casa."])); +} + +// Calcola la somma attuale delle percentuali di proprietà +$queryTotal = $conn->prepare("SELECT SUM(ownership_percentage) FROM home_owners WHERE home_id = ?"); +$queryTotal->bind_param('i', $idhome); +$queryTotal->execute(); +$queryTotal->bind_result($currentTotal); +$queryTotal->fetch(); +$queryTotal->close(); + +$totalAfterInsert = $currentTotal + $ownership_percentage; + +if ($totalAfterInsert > 100) { + die(json_encode(["success" => false, "message" => "Errore: La somma totale dei proprietari supererebbe il 100%."])); } // Inserisce il nuovo proprietario nella tabella home_owners @@ -38,19 +52,18 @@ $queryInsert = $conn->prepare(" INSERT INTO home_owners (home_id, owner_id, ownership_percentage, notes, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW()) "); + if ($queryInsert === false) { - die("Errore nella preparazione della query: " . $conn->error); + die(json_encode(["success" => false, "message" => "Errore nella preparazione della query: " . $conn->error])); } $queryInsert->bind_param('iids', $idhome, $owner_id, $ownership_percentage, $notes); if ($queryInsert->execute()) { - // Reindirizza alla pagina di gestione dell'immobile - header("Location: assign-owners.php?idhome=$idhome&success=1"); - exit; + echo json_encode(["success" => true, "message" => "Proprietario aggiunto con successo."]); } else { - die("Errore nell'inserimento: " . $queryInsert->error); + die(json_encode(["success" => false, "message" => "Errore nell'inserimento: " . $queryInsert->error])); } -// Chiude la connessione +$queryInsert->close(); $conn->close(); diff --git a/public/userportal/assign-owners.php b/public/userportal/assign-owners.php index 1cdffae..71bc6d0 100644 --- a/public/userportal/assign-owners.php +++ b/public/userportal/assign-owners.php @@ -77,6 +77,8 @@ $resultAvailableOwners = $queryAvailableOwners->get_result(); + + @@ -105,42 +107,46 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
num_rows > 0) { ?> - - - - - - - - - - - - fetch_assoc()) { ?> +
+
NomeCodice Fiscale%NoteAzioni
+ - - - - - - + + + + + - - -
- - - - - - NomeCodice Fiscale%NoteAzioni
+ + + fetch_assoc()) { + $totalPercentage += $owner['ownership_percentage']; + echo " + " . htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']) . " + " . htmlspecialchars($owner['tax_code']) . " + " . htmlspecialchars($owner['ownership_percentage']) . " + " . htmlspecialchars($owner['notes']) . " + + + + "; + } + ?> + + + + Totale % Proprietà: + % + + + + +
+

Nessun proprietario associato.

@@ -151,7 +157,7 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
-
Aggiungi Proprietari
+
Assegna Proprietari
@@ -171,17 +177,24 @@ $resultAvailableOwners = $queryAvailableOwners->get_result(); ?> +
+ +
- +
- + + +
@@ -198,26 +211,293 @@ $resultAvailableOwners = $queryAvailableOwners->get_result(); + + + + - - if (confirm('Sei sicuro di voler rimuovere questo proprietario?')) { - $.post('remove-owner-from-home.php', { - owner_id: ownerId, - idhome: idhome - }, function(response) { - location.reload(); + + + + + + + + + + \ No newline at end of file diff --git a/public/userportal/get-available-owners.php b/public/userportal/get-available-owners.php new file mode 100644 index 0000000..8d59c98 --- /dev/null +++ b/public/userportal/get-available-owners.php @@ -0,0 +1,40 @@ +connect_error) { + die("Errore di connessione: " . $conn->connect_error); +} + +$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0; + +// Recupera tutti i proprietari disponibili per l'assegnazione +$queryAvailableOwners = $conn->prepare(" + SELECT owner_id, first_name, last_name, company_name, tax_code + FROM property_owners + WHERE user_id = ? + AND owner_id NOT IN ( + SELECT owner_id FROM home_owners WHERE home_id = ? + ) +"); +$queryAvailableOwners->bind_param('ii', $iduserlogin, $idhome); +$queryAvailableOwners->execute(); +$resultAvailableOwners = $queryAvailableOwners->get_result(); + +// Genera il nuovo dropdown +echo ''; + +$conn->close(); diff --git a/public/userportal/get-owners.php b/public/userportal/get-owners.php new file mode 100644 index 0000000..f0240b3 --- /dev/null +++ b/public/userportal/get-owners.php @@ -0,0 +1,72 @@ +connect_error) { + die("Errore di connessione: " . $conn->connect_error); +} + +$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0; + +$queryOwners = $conn->prepare(" + SELECT + po.owner_id, + po.first_name, + po.last_name, + po.company_name, + po.tax_code, + po.email, + ho.ownership_percentage, + ho.notes + FROM + home_owners AS ho + INNER JOIN + property_owners AS po ON ho.owner_id = po.owner_id + WHERE + ho.home_id = ? +"); +$queryOwners->bind_param('i', $idhome); +$queryOwners->execute(); +$resultOwners = $queryOwners->get_result(); + +$totalPercentage = 0; + +echo ' + + + + + + + + + + '; + +while ($owner = $resultOwners->fetch_assoc()) { + $totalPercentage += $owner['ownership_percentage']; + echo " + + + + + + "; +} + +echo ' + + + + + + + +
NomeCodice Fiscale%NoteAzioni
" . htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']) . "" . htmlspecialchars($owner['tax_code']) . "" . htmlspecialchars($owner['ownership_percentage']) . "" . htmlspecialchars($owner['notes']) . " + +
Totale % Proprietà:' . $totalPercentage . '%
'; + +$queryOwners->close(); +$conn->close(); diff --git a/public/userportal/homes-list.php b/public/userportal/homes-list.php index 021d57e..8723776 100644 --- a/public/userportal/homes-list.php +++ b/public/userportal/homes-list.php @@ -316,7 +316,7 @@ $result = $stmt->get_result(); Foto - Nome + Nome di Riferimento Città / Nazione Indirizzo Proprietari diff --git a/public/userportal/mainphoto/2-cstech-1740489387.jpg b/public/userportal/mainphoto/2-cstech-1740489387.jpg new file mode 100644 index 0000000..89eb344 Binary files /dev/null and b/public/userportal/mainphoto/2-cstech-1740489387.jpg differ