107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?php
|
|
require('../.hta_config/conf.php');
|
|
// Enable error reporting (for debugging)
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
|
|
|
|
try {
|
|
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
die("Database connection failed: " . $e->getMessage());
|
|
}
|
|
|
|
// Handle Payment Status Update
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["emiId"]) && isset($_POST["status"])) {
|
|
$emiId = $_POST["emiId"];
|
|
$status = $_POST["status"];
|
|
|
|
try {
|
|
$stmt = $db->prepare("UPDATE billing SET emi".$emiId."Status = :status WHERE customerId = :customerId");
|
|
$stmt->bindParam(':status', $status, PDO::PARAM_INT);
|
|
$stmt->bindParam(':customerId', $_POST["customerId"]);
|
|
$stmt->execute();
|
|
echo json_encode(["success" => true, "message" => "Payment status updated"]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(["success" => false, "message" => "Error updating payment status"]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Fetch Customer Billing Data
|
|
if (!isset($_GET['customerId'])) {
|
|
die("Invalid request: Customer ID is required.");
|
|
}
|
|
|
|
$customerId = $_GET['customerId'];
|
|
$stmt = $db->prepare("SELECT * FROM billing WHERE customerId = :customerId");
|
|
$stmt->bindParam(':customerId', $customerId);
|
|
$stmt->execute();
|
|
$billingData = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$billingData) {
|
|
die("No billing data found for this customer.");
|
|
}
|
|
?>
|
|
<div class="container mt-4">
|
|
<h2 class="mb-3 text-center">Customer Billing Details</h2>
|
|
<table class="table table-bordered table-striped">
|
|
<thead class="thead-dark">
|
|
<tr>
|
|
<th>#</th>
|
|
<th>EMI Date</th>
|
|
<th>Amount</th>
|
|
<th>Status</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
for ($i = 1; $i <= 12; $i++) {
|
|
$emiAmount = $billingData["emi$i"];
|
|
$emiDate = $billingData["emi{$i}Date"];
|
|
$emiStatus = $billingData["emi{$i}Status"];
|
|
|
|
if (!$emiAmount || !$emiDate) continue;
|
|
echo "<tr>
|
|
<td>{$i}</td>
|
|
<td>{$emiDate}</td>
|
|
<td>₹{$emiAmount}</td>
|
|
<td id='status-$i'>" . ($emiStatus == 1 ? "<span class='badge bg-success'>Paid</span>" : "<span class='badge bg-danger'>Unpaid</span>") . "</td>
|
|
<td>
|
|
<button class='btn btn-sm " . ($emiStatus == 1 ? "btn-secondary disabled" : "btn-primary") . "' onclick='updatePaymentStatus($i)' id='btn-$i'>Mark as Paid</button>
|
|
</td>
|
|
</tr>";
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<script>
|
|
function updatePaymentStatus(emiId) {
|
|
let customerId = "<?php echo $customerId; ?>";
|
|
let formData = new FormData();
|
|
formData.append("emiId", emiId);
|
|
formData.append("status", 1); // Set status to "1" (Paid)
|
|
formData.append("customerId", customerId);
|
|
|
|
fetch("billing-details.php", {
|
|
method: "POST",
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
document.getElementById("status-" + emiId).innerHTML = "<span class='badge badge-success'>Paid</span>";
|
|
document.getElementById("btn-" + emiId).classList.replace("btn-primary", "btn-secondary");
|
|
document.getElementById("btn-" + emiId).classList.add("disabled");
|
|
} else {
|
|
alert("Failed to update payment status!");
|
|
}
|
|
})
|
|
.catch(error => console.error("Error:", error));
|
|
}
|
|
</script>
|