billing2/customers/.hta_slug/billing-details.php

131 lines
5.5 KiB
PHP

<?php
require('../.hta_config/conf.php');
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
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;
}
// 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);
$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">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>Number of EMI</th>
<th>EMI Amount</th>
<th>EMI Date</th>
<th>Payment Status</th>
<th>Outstanding</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?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>
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>