changed assegna proprietario
This commit is contained in:
parent
a08f3f5e8c
commit
764f95ce34
48
public/userportal/add-new-owner.php
Normal file
48
public/userportal/add-new-owner.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
include('include/db_connect.php'); // Connessione al DB
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
$user_id = $_POST['user_id'];
|
||||||
|
$owner_type = $_POST['owner_type'];
|
||||||
|
$first_name = $_POST['first_name'] ?? null;
|
||||||
|
$last_name = $_POST['last_name'] ?? null;
|
||||||
|
$company_name = $_POST['company_name'] ?? null;
|
||||||
|
$tax_code = $_POST['tax_code'];
|
||||||
|
$email = $_POST['email'];
|
||||||
|
$phone = $_POST['phone'] ?? null;
|
||||||
|
$address = $_POST['address'] ?? null;
|
||||||
|
$postal_code = $_POST['postal_code'] ?? null;
|
||||||
|
$city = $_POST['city'] ?? null;
|
||||||
|
$province = $_POST['province'] ?? null;
|
||||||
|
$country = $_POST['country'] ?? null;
|
||||||
|
$role = $_POST['role'] ?? null;
|
||||||
|
$notes = $_POST['notes'] ?? null;
|
||||||
|
|
||||||
|
// Verifica se il codice fiscale esiste già
|
||||||
|
$checkQuery = $conn->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();
|
||||||
@ -9,7 +9,7 @@ include('include/headscript.php');
|
|||||||
$conn = new mysqli($servername, $username, $password, $database);
|
$conn = new mysqli($servername, $username, $password, $database);
|
||||||
|
|
||||||
if ($conn->connect_error) {
|
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
|
// 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;
|
$ownership_percentage = isset($_POST['ownership_percentage']) ? floatval($_POST['ownership_percentage']) : null;
|
||||||
$notes = isset($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
|
$notes = isset($_POST['notes']) ? $conn->real_escape_string($_POST['notes']) : null;
|
||||||
|
|
||||||
// Verifica che tutti i dati necessari siano presenti
|
// Verifica che tutti i dati siano presenti
|
||||||
if ($idhome <= 0 || $owner_id <= 0) {
|
if ($idhome <= 0 || $owner_id <= 0 || is_null($ownership_percentage)) {
|
||||||
die("Errore: Dati mancanti.");
|
die(json_encode(["success" => false, "message" => "Errore: Dati mancanti."]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Controlla se il proprietario è già associato a questa casa
|
// Controlla se il proprietario è già associato alla casa
|
||||||
$queryCheck = $conn->prepare("SELECT * FROM home_owners WHERE home_id = ? AND owner_id = ?");
|
$queryCheck = $conn->prepare("SELECT 1 FROM home_owners WHERE home_id = ? AND owner_id = ?");
|
||||||
$queryCheck->bind_param('ii', $idhome, $owner_id);
|
$queryCheck->bind_param('ii', $idhome, $owner_id);
|
||||||
$queryCheck->execute();
|
$queryCheck->execute();
|
||||||
$resultCheck = $queryCheck->get_result();
|
$resultCheck = $queryCheck->get_result();
|
||||||
|
|
||||||
if ($resultCheck->num_rows > 0) {
|
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
|
// 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)
|
INSERT INTO home_owners (home_id, owner_id, ownership_percentage, notes, created_at, updated_at)
|
||||||
VALUES (?, ?, ?, ?, NOW(), NOW())
|
VALUES (?, ?, ?, ?, NOW(), NOW())
|
||||||
");
|
");
|
||||||
|
|
||||||
if ($queryInsert === false) {
|
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);
|
$queryInsert->bind_param('iids', $idhome, $owner_id, $ownership_percentage, $notes);
|
||||||
|
|
||||||
if ($queryInsert->execute()) {
|
if ($queryInsert->execute()) {
|
||||||
// Reindirizza alla pagina di gestione dell'immobile
|
echo json_encode(["success" => true, "message" => "Proprietario aggiunto con successo."]);
|
||||||
header("Location: assign-owners.php?idhome=$idhome&success=1");
|
|
||||||
exit;
|
|
||||||
} else {
|
} 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();
|
$conn->close();
|
||||||
|
|||||||
@ -77,6 +77,8 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
|
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
|
||||||
<link href="assets/css/icons.css" rel="stylesheet" type="text/css">
|
<link href="assets/css/icons.css" rel="stylesheet" type="text/css">
|
||||||
<link href="assets/css/style.css" rel="stylesheet" type="text/css">
|
<link href="assets/css/style.css" rel="stylesheet" type="text/css">
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="fixed-left">
|
<body class="fixed-left">
|
||||||
@ -105,42 +107,46 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<?php if ($resultOwners->num_rows > 0) { ?>
|
<?php if ($resultOwners->num_rows > 0) { ?>
|
||||||
<table class="table table-bordered">
|
<div id="ownersTable">
|
||||||
<thead>
|
<table class="table table-bordered">
|
||||||
<tr>
|
<thead>
|
||||||
<th>Nome</th>
|
|
||||||
<th>Codice Fiscale</th>
|
|
||||||
<th>%</th>
|
|
||||||
<th>Note</th>
|
|
||||||
<th>Azioni</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<?php while ($owner = $resultOwners->fetch_assoc()) { ?>
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<th>Nome</th>
|
||||||
<?php
|
<th>Codice Fiscale</th>
|
||||||
echo htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']);
|
<th>%</th>
|
||||||
if ($owner['company_name']) {
|
<th>Note</th>
|
||||||
echo ' (' . htmlspecialchars($owner['company_name']) . ')';
|
<th>Azioni</th>
|
||||||
}
|
|
||||||
?>
|
|
||||||
</td>
|
|
||||||
<td><?php echo htmlspecialchars($owner['tax_code']); ?></td>
|
|
||||||
<td><?php echo htmlspecialchars($owner['ownership_percentage']); ?></td>
|
|
||||||
<td><?php echo htmlspecialchars($owner['notes']); ?></td>
|
|
||||||
<td>
|
|
||||||
<?php if (!$disableEditing) { ?>
|
|
||||||
<button class="btn btn-danger btn-sm remove-owner-btn" data-id="<?php echo $owner['owner_id']; ?>">
|
|
||||||
<i class="fas fa-trash-alt"></i> Rimuovi
|
|
||||||
</button>
|
|
||||||
<?php } ?>
|
|
||||||
</td>
|
|
||||||
|
|
||||||
</tr>
|
</tr>
|
||||||
<?php } ?>
|
</thead>
|
||||||
</tbody>
|
<tbody id="ownersTableBody">
|
||||||
</table>
|
<?php
|
||||||
|
$totalPercentage = 0;
|
||||||
|
while ($owner = $resultOwners->fetch_assoc()) {
|
||||||
|
$totalPercentage += $owner['ownership_percentage'];
|
||||||
|
echo "<tr>
|
||||||
|
<td>" . htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']) . "</td>
|
||||||
|
<td>" . htmlspecialchars($owner['tax_code']) . "</td>
|
||||||
|
<td class='ownership-percentage'>" . htmlspecialchars($owner['ownership_percentage']) . "</td>
|
||||||
|
<td>" . htmlspecialchars($owner['notes']) . "</td>
|
||||||
|
<td>
|
||||||
|
<button class='btn btn-danger btn-sm remove-owner-btn' data-id='" . $owner['owner_id'] . "'>
|
||||||
|
<i class='fas fa-trash-alt'></i> Rimuovi
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</tbody>
|
||||||
|
<tfoot>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2"><strong>Totale % Proprietà:</strong></td>
|
||||||
|
<td id="totalOwnership" class="font-weight-bold text-center"><?php echo $totalPercentage; ?>%</td>
|
||||||
|
<td colspan="2"></td>
|
||||||
|
</tr>
|
||||||
|
</tfoot>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
<?php } else { ?>
|
<?php } else { ?>
|
||||||
<p class="text-muted">Nessun proprietario associato.</p>
|
<p class="text-muted">Nessun proprietario associato.</p>
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
@ -151,7 +157,7 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
<?php if (!$disableEditing) { ?>
|
<?php if (!$disableEditing) { ?>
|
||||||
<div class="card mb-4">
|
<div class="card mb-4">
|
||||||
<div class="card-header bg-info text-white">
|
<div class="card-header bg-info text-white">
|
||||||
<h5 class="mb-0">Aggiungi Proprietari</h5>
|
<h5 class="mb-0">Assegna Proprietari</h5>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form id="addOwnerForm" method="POST" action="add-owner-to-home.php">
|
<form id="addOwnerForm" method="POST" action="add-owner-to-home.php">
|
||||||
@ -171,17 +177,24 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
?>
|
?>
|
||||||
</option>
|
</option>
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
|
<option value="new_owner">➕ Aggiungi nuovo proprietario</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="ownershipPercentage">Percentuale di Proprietà</label>
|
<label for="ownershipPercentage">Percentuale di Proprietà</label>
|
||||||
<input type="number" step="0.01" class="form-control" id="ownershipPercentage" name="ownership_percentage" placeholder="Inserisci la percentuale (es. 50.00)">
|
<input type="number" step="0.01" class="form-control" id="ownershipPercentage"
|
||||||
|
name="ownership_percentage" placeholder="Inserisci la percentuale (es. 50.00)"
|
||||||
|
oninput="validatePercentage()" required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="notes">Note</label>
|
<label for="notes">Note</label>
|
||||||
<textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Aggiungi eventuali note"></textarea>
|
<textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Aggiungi eventuali note"></textarea>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-success mt-3">Aggiungi</button>
|
<!-- Messaggio di errore -->
|
||||||
|
<small id="percentageError" class="text-danger" style="display:none;">La somma totale non può superare il 100%.</small>
|
||||||
|
<button type="submit" id="addOwnerBtn" class="btn btn-success mt-3">Aggiungi</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -198,26 +211,293 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
<?php include('include/footer.php'); ?>
|
<?php include('include/footer.php'); ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- Modal Aggiungi Nuovo Proprietario -->
|
||||||
|
<div class="modal fade" id="newOwnerModal" tabindex="-1" aria-labelledby="newOwnerModalLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog modal-lg"> <!-- Più largo per migliorare la leggibilità -->
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="newOwnerModalLabel">Aggiungi Nuovo Proprietario</h5>
|
||||||
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||||
|
<span aria-hidden="true">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<form id="newOwnerForm">
|
||||||
|
<input type="hidden" name="user_id" value="<?php echo $iduserlogin; ?>">
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<!-- Tipo Proprietario -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="ownerType">Tipo Proprietario</label>
|
||||||
|
<select class="form-control" id="ownerType" name="owner_type" required>
|
||||||
|
<option value="individual">Persona Fisica</option>
|
||||||
|
<option value="company">Azienda</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Codice Fiscale / P. IVA -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="taxCode">Codice Fiscale / Partita IVA</label>
|
||||||
|
<input type="text" class="form-control" id="taxCode" name="tax_code" required>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Nome -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="firstName">Nome</label>
|
||||||
|
<input type="text" class="form-control" id="firstName" name="first_name">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Cognome -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="lastName">Cognome</label>
|
||||||
|
<input type="text" class="form-control" id="lastName" name="last_name">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Nome Azienda -->
|
||||||
|
<div class="col-md-12">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="companyName">Nome Azienda</label>
|
||||||
|
<input type="text" class="form-control" id="companyName" name="company_name" disabled>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Email -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="email">Email</label>
|
||||||
|
<input type="email" class="form-control" id="email" name="email" required>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Telefono -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="phone">Telefono</label>
|
||||||
|
<input type="text" class="form-control" id="phone" name="phone">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Indirizzo -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="address">Indirizzo</label>
|
||||||
|
<input type="text" class="form-control" id="address" name="address">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- CAP -->
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="postalCode">CAP</label>
|
||||||
|
<input type="text" class="form-control" id="postalCode" name="postal_code">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Città -->
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="city">Città</label>
|
||||||
|
<input type="text" class="form-control" id="city" name="city">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Provincia -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="province">Provincia</label>
|
||||||
|
<input type="text" class="form-control" id="province" name="province">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Nazione (Select2) -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="country">Nazione</label>
|
||||||
|
<select class="form-control" id="country" name="country">
|
||||||
|
<option value="">-- Seleziona --</option>
|
||||||
|
<?php
|
||||||
|
$queryCountries = $conn->query("SELECT id, name FROM auth_countries ORDER BY name");
|
||||||
|
while ($country = $queryCountries->fetch_assoc()) {
|
||||||
|
echo "<option value='{$country['id']}'>" . htmlspecialchars($country['name']) . "</option>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Ruolo -->
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="role">Ruolo</label>
|
||||||
|
<input type="text" class="form-control" id="role" name="role">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Note -->
|
||||||
|
<div class="col-md-12">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="notes">Note</label>
|
||||||
|
<textarea class="form-control" id="notes" name="notes" rows="3"></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-success">Salva Proprietario</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="assets/js/jquery.min.js"></script>
|
<script src="assets/js/jquery.min.js"></script>
|
||||||
<script src="assets/js/bootstrap.min.js"></script>
|
<script src="assets/js/bootstrap.min.js"></script>
|
||||||
<script>
|
<!-- Include Select2 -->
|
||||||
$(document).on('click', '.remove-owner-btn', function() {
|
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
|
||||||
const ownerId = $(this).data('id');
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
|
||||||
const idhome = "<?php echo $idhome; ?>";
|
|
||||||
|
|
||||||
if (confirm('Sei sicuro di voler rimuovere questo proprietario?')) {
|
<script>
|
||||||
$.post('remove-owner-from-home.php', {
|
$(document).ready(function() {
|
||||||
owner_id: ownerId,
|
// Attiva Select2 per il campo "Nazione"
|
||||||
idhome: idhome
|
$("#country").select2({
|
||||||
}, function(response) {
|
width: '100%',
|
||||||
location.reload();
|
placeholder: "Seleziona un paese...",
|
||||||
|
allowClear: true
|
||||||
|
});
|
||||||
|
|
||||||
|
// Selezione del proprietario nel dropdown
|
||||||
|
$("#ownerSelect").change(function() {
|
||||||
|
if ($(this).val() === "new_owner") {
|
||||||
|
$("#newOwnerModal").modal("show"); // Apri il modale
|
||||||
|
$(this).val(""); // Resetta il valore del dropdown
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Abilita/disabilita i campi in base al tipo di proprietario
|
||||||
|
$("#ownerType").change(function() {
|
||||||
|
if ($(this).val() === "company") {
|
||||||
|
$("#companyName").prop("disabled", false);
|
||||||
|
$("#firstName, #lastName").prop("disabled", true).val("");
|
||||||
|
} else {
|
||||||
|
$("#companyName").prop("disabled", true).val("");
|
||||||
|
$("#firstName, #lastName").prop("disabled", false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Invio dati AJAX per aggiungere il nuovo proprietario
|
||||||
|
$("#newOwnerForm").submit(function(e) {
|
||||||
|
e.preventDefault(); // Evita il refresh della pagina
|
||||||
|
let formData = $(this).serialize();
|
||||||
|
|
||||||
|
$.post("add-new-owner.php", formData, function(response) {
|
||||||
|
let result = JSON.parse(response);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
Swal.fire({
|
||||||
|
icon: "success",
|
||||||
|
title: "Proprietario aggiunto!",
|
||||||
|
text: result.message,
|
||||||
|
timer: 1500,
|
||||||
|
showConfirmButton: false
|
||||||
|
}).then(() => {
|
||||||
|
$("#newOwnerModal").modal("hide"); // Chiudi il modale
|
||||||
|
|
||||||
|
// Aggiungi il nuovo proprietario al dropdown
|
||||||
|
$("#ownerSelect").prepend(`<option value="${result.owner_id}" selected>
|
||||||
|
${result.owner_name} - ${result.tax_code}
|
||||||
|
</option>`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Swal.fire({
|
||||||
|
icon: "error",
|
||||||
|
title: "Errore!",
|
||||||
|
text: result.message
|
||||||
|
});
|
||||||
|
}
|
||||||
}).fail(function() {
|
}).fail(function() {
|
||||||
alert('Errore durante la rimozione del proprietario.');
|
Swal.fire({
|
||||||
|
icon: "error",
|
||||||
|
title: "Errore!",
|
||||||
|
text: "Si è verificato un problema."
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
function getTotalOwnership() {
|
||||||
|
let total = 0;
|
||||||
|
document.querySelectorAll(".ownership-percentage").forEach(function(el) {
|
||||||
|
total += parseFloat(el.textContent) || 0;
|
||||||
|
});
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validatePercentage() {
|
||||||
|
let totalOwnership = getTotalOwnership();
|
||||||
|
let newPercentage = parseFloat(document.getElementById("ownershipPercentage").value) || 0;
|
||||||
|
let remainingPercentage = 100 - totalOwnership;
|
||||||
|
|
||||||
|
let errorMsg = document.getElementById("percentageError");
|
||||||
|
let submitBtn = document.getElementById("addOwnerBtn");
|
||||||
|
|
||||||
|
if (newPercentage > remainingPercentage) {
|
||||||
|
errorMsg.style.display = "block";
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
} else {
|
||||||
|
errorMsg.style.display = "none";
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disabilita il form se già 100%
|
||||||
|
let addOwnerForm = document.getElementById("addOwnerForm");
|
||||||
|
let addOwnerBtn = document.getElementById("addOwnerBtn");
|
||||||
|
if (getTotalOwnership() >= 100) {
|
||||||
|
addOwnerForm.style.display = "none";
|
||||||
|
document.getElementById("totalOwnership").style.backgroundColor = "#28a745"; // Verde
|
||||||
|
document.getElementById("totalOwnership").style.color = "#fff";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
<script>
|
||||||
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
let totalOwnership = 0;
|
||||||
|
|
||||||
|
// Somma tutte le percentuali di proprietà
|
||||||
|
document.querySelectorAll(".ownership-percentage").forEach(function(el) {
|
||||||
|
totalOwnership += parseFloat(el.textContent) || 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Seleziona il campo nel footer
|
||||||
|
let totalCell = document.getElementById("totalOwnership");
|
||||||
|
|
||||||
|
// Imposta il valore e il colore di sfondo in base al totale
|
||||||
|
totalCell.textContent = totalOwnership.toFixed(2) + "%";
|
||||||
|
if (totalOwnership >= 100) {
|
||||||
|
totalCell.style.backgroundColor = "#28a745"; // Verde
|
||||||
|
totalCell.style.color = "#fff";
|
||||||
|
} else {
|
||||||
|
totalCell.style.backgroundColor = "#ffc107"; // Arancione
|
||||||
|
totalCell.style.color = "#212529";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$(document).on('click', '.remove-owner-btn', function() {
|
$(document).on('click', '.remove-owner-btn', function() {
|
||||||
const ownerId = $(this).data('id');
|
const ownerId = $(this).data('id');
|
||||||
@ -276,7 +556,83 @@ $resultAvailableOwners = $queryAvailableOwners->get_result();
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
<script>
|
||||||
|
document.getElementById("addOwnerForm").addEventListener("submit", function(e) {
|
||||||
|
e.preventDefault(); // Previene il refresh della pagina
|
||||||
|
|
||||||
|
let formData = new FormData(this);
|
||||||
|
|
||||||
|
fetch("add-owner-to-home.php", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
})
|
||||||
|
.then(response => response.json()) // Converti la risposta in JSON
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
Swal.fire({
|
||||||
|
icon: "success",
|
||||||
|
title: "Proprietario aggiunto!",
|
||||||
|
text: data.message,
|
||||||
|
timer: 1500,
|
||||||
|
showConfirmButton: false
|
||||||
|
}).then(() => {
|
||||||
|
// Aggiorna la tabella dei proprietari
|
||||||
|
fetch("get-owners.php?idhome=<?php echo $idhome; ?>")
|
||||||
|
.then(response => response.text())
|
||||||
|
.then(html => {
|
||||||
|
document.getElementById("ownersTable").innerHTML = html;
|
||||||
|
});
|
||||||
|
|
||||||
|
// **Aggiorna il dropdown con i proprietari disponibili**
|
||||||
|
fetch("get-available-owners.php?idhome=<?php echo $idhome; ?>")
|
||||||
|
.then(response => response.text())
|
||||||
|
.then(html => {
|
||||||
|
document.getElementById("ownerSelectContainer").innerHTML = html;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Resetta il form
|
||||||
|
document.getElementById("addOwnerForm").reset();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Swal.fire({
|
||||||
|
icon: "error",
|
||||||
|
title: "Errore!",
|
||||||
|
text: data.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Errore AJAX:", error);
|
||||||
|
Swal.fire({
|
||||||
|
icon: "error",
|
||||||
|
title: "Errore!",
|
||||||
|
text: "Si è verificato un problema con la richiesta."
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
function validatePercentage() {
|
||||||
|
let totalOwnership = 0;
|
||||||
|
document.querySelectorAll(".ownership-percentage").forEach(function(el) {
|
||||||
|
totalOwnership += parseFloat(el.textContent) || 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
let newPercentage = parseFloat(document.getElementById("ownershipPercentage").value) || 0;
|
||||||
|
let remainingPercentage = 100 - totalOwnership;
|
||||||
|
|
||||||
|
let errorMsg = document.getElementById("percentageError");
|
||||||
|
let submitBtn = document.getElementById("addOwnerBtn");
|
||||||
|
|
||||||
|
if (newPercentage > remainingPercentage) {
|
||||||
|
errorMsg.style.display = "block";
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
} else {
|
||||||
|
errorMsg.style.display = "none";
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
40
public/userportal/get-available-owners.php
Normal file
40
public/userportal/get-available-owners.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
include('include/headscript.php');
|
||||||
|
|
||||||
|
// Connessione al database
|
||||||
|
$conn = new mysqli($servername, $username, $password, $database);
|
||||||
|
|
||||||
|
if ($conn->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 '<select class="form-control" id="ownerSelect" name="owner_id" required>';
|
||||||
|
echo '<option value="">-- Seleziona --</option>';
|
||||||
|
while ($availableOwner = $resultAvailableOwners->fetch_assoc()) {
|
||||||
|
echo '<option value="' . $availableOwner['owner_id'] . '">';
|
||||||
|
echo htmlspecialchars($availableOwner['first_name'] . ' ' . $availableOwner['last_name']);
|
||||||
|
if ($availableOwner['company_name']) {
|
||||||
|
echo ' (' . htmlspecialchars($availableOwner['company_name']) . ')';
|
||||||
|
}
|
||||||
|
echo ' - ' . htmlspecialchars($availableOwner['tax_code']);
|
||||||
|
echo '</option>';
|
||||||
|
}
|
||||||
|
echo '</select>';
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
72
public/userportal/get-owners.php
Normal file
72
public/userportal/get-owners.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
include('include/headscript.php');
|
||||||
|
$conn = new mysqli($servername, $username, $password, $database);
|
||||||
|
|
||||||
|
if ($conn->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 '<table class="table table-bordered">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Nome</th>
|
||||||
|
<th>Codice Fiscale</th>
|
||||||
|
<th>%</th>
|
||||||
|
<th>Note</th>
|
||||||
|
<th>Azioni</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>';
|
||||||
|
|
||||||
|
while ($owner = $resultOwners->fetch_assoc()) {
|
||||||
|
$totalPercentage += $owner['ownership_percentage'];
|
||||||
|
echo "<tr>
|
||||||
|
<td>" . htmlspecialchars($owner['first_name'] . ' ' . $owner['last_name']) . "</td>
|
||||||
|
<td>" . htmlspecialchars($owner['tax_code']) . "</td>
|
||||||
|
<td class='ownership-percentage'>" . htmlspecialchars($owner['ownership_percentage']) . "</td>
|
||||||
|
<td>" . htmlspecialchars($owner['notes']) . "</td>
|
||||||
|
<td>
|
||||||
|
<button class='btn btn-danger btn-sm remove-owner-btn' data-id='" . $owner['owner_id'] . "'>
|
||||||
|
<i class='fas fa-trash-alt'></i> Rimuovi
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo '</tbody>
|
||||||
|
<tfoot>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2"><strong>Totale % Proprietà:</strong></td>
|
||||||
|
<td id="totalOwnership" class="font-weight-bold text-center">' . $totalPercentage . '%</td>
|
||||||
|
<td colspan="2"></td>
|
||||||
|
</tr>
|
||||||
|
</tfoot>
|
||||||
|
</table>';
|
||||||
|
|
||||||
|
$queryOwners->close();
|
||||||
|
$conn->close();
|
||||||
@ -316,7 +316,7 @@ $result = $stmt->get_result();
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Foto</th>
|
<th>Foto</th>
|
||||||
<th>Nome</th>
|
<th>Nome di Riferimento</th>
|
||||||
<th>Città / Nazione</th>
|
<th>Città / Nazione</th>
|
||||||
<th>Indirizzo</th>
|
<th>Indirizzo</th>
|
||||||
<th>Proprietari</th>
|
<th>Proprietari</th>
|
||||||
|
|||||||
BIN
public/userportal/mainphoto/2-cstech-1740489387.jpg
Normal file
BIN
public/userportal/mainphoto/2-cstech-1740489387.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Loading…
x
Reference in New Issue
Block a user