This commit is contained in:
Suvodip
2025-02-28 20:07:48 +05:30
parent e56d4f72ab
commit c1ee1e0ba0
5 changed files with 263 additions and 186 deletions

View File

@@ -1,106 +1,130 @@
<?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"]);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['emiId'], $_POST['payStatus'])) {
header('Content-Type: application/json');
ob_end_clean(); // Clears any accidental HTML output
try {
$stmt = $db->prepare("UPDATE emi SET payStatus = :payStatus WHERE customerId = :customerId AND id = :emiId");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':payStatus', $_POST['payStatus'], PDO::PARAM_INT);
$stmt->bindParam(':emiId', $_POST['emiId'], PDO::PARAM_INT);
$stmt->execute();
echo json_encode(['status' => 'success']);
} catch (PDOException $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
exit;
}
exit;
}
// Fetch EMI data
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId ORDER BY emiDate ASC");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->execute();
$emiPlans = $stmt->fetchAll(PDO::FETCH_ASSOC);
// var_dump($emiPlans);
// 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.");
$stmt = $db->prepare("SELECT * FROM customers WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->execute();
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
// var_dump($customer);
} catch (PDOException $e) {
die('<div class="alert alert-danger text-center">Error: ' . $e->getMessage() . '</div>');
}
?>
<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">
<h2 class="mb-3">EMI Details</h2>
<div class="d-flex justify-content-between">
<div>
<p>Customer Name: <strong><?php echo $customer['name']; ?></strong></p>
<p>Mobile Number: <strong><?php echo $customer['mobile']; ?></strong></p>
<p>EMI Booking Date: <strong><?php echo $emiPlans[0]['bookingDate']; ?></strong></p>
</div>
<div>
<?php
$currentOutstanding = 0;
$totalAmount = 0;
foreach ($emiPlans as $emi) {
$totalAmount = $emi['totalAmount'];
if ($emi['payStatus'] == 0) {
$currentOutstanding += $emi['emiAmount'];
}
}
?>
<p>Total Amount: <strong><?php echo $totalAmount; ?></strong></p>
<p>Outstanding: <strong><?php echo round($currentOutstanding); ?></strong></p>
<p>Tenure: <strong><?php echo $emiPlans[0] ? $emiPlans[0]['tenure'] : 0; ?></strong></p>
<p>Frequency: <strong><?php echo $emiPlans[0] ? $emiPlans[0]['frequency'] : 0; ?></strong></p>
</div>
</div>
<table class="table table-striped table-bordered">
<thead class="bg-primary text-white text-center">
<tr>
<th>#</th>
<th>Number of EMI</th>
<th>EMI Amount</th>
<th>EMI Date</th>
<th>Amount</th>
<th>Status</th>
<th>Payment Status</th>
<th>Outstanding</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>";
}
?>
<?php foreach ($emiPlans as $emi) { ?>
<tr id="row-<?= $emi['id']; ?>">
<td><?= $emi['emiNumber']; ?></td>
<td>₹<?= number_format($emi['emiAmount'], 2); ?></td>
<td><?= date('d-m-Y', strtotime($emi['emiDate'])); ?></td>
<td>
<span id="status-<?= $emi['id']; ?>" class="badge <?= $emi['payStatus'] == 0 ? 'bg-danger' : 'bg-success'; ?>">
<?= $emi['payStatus'] == 0 ? 'Unpaid' : 'Paid'; ?>
</span>
</td>
<td>₹<?= number_format($emi['outstanding'], 2); ?></td>
<td>
<select class="form-select paymentStatus" data-emi-id="<?= $emi['id']; ?>">
<option value="1" <?= $emi['payStatus'] == 1 ? 'selected' : ''; ?>>Paid</option>
<option value="0" <?= $emi['payStatus'] == 0 ? 'selected' : ''; ?>>Unpaid</option>
</select>
</td>
</tr>
<?php } ?>
</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));
}
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll(".paymentStatus").forEach(select => {
select.addEventListener("change", function() {
let emiId = this.getAttribute("data-emi-id");
let newStatus = this.value;
fetch(window.location.href, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `emiId=${emiId}&payStatus=${newStatus}`
})
.then(response => response.json())
.then(data => {
if (data.status === "success") {
let statusBadge = document.getElementById("status-" + emiId);
statusBadge.textContent = newStatus == 1 ? "Paid" : "Unpaid";
statusBadge.className = "badge " + (newStatus == 1 ? "bg-success" : "bg-danger");
}
})
.catch(error => console.error("Error:", error));
});
});
});
</script>