billing2/my-account/.hta_slug/emi-details.php

99 lines
4.1 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 AND invoiceId = :invoiceId ORDER BY emiDate ASC");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':invoiceId', $_GET['invoiceId']);
$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>
<p>EMI Booking Date: <strong><?php echo $emiPlans[0]['invoiceId']; ?></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>
</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>
</tr>
<?php } ?>
</tbody>
</table>
</div>