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

250 lines
13 KiB
PHP

<?php
require('../.hta_config/conf.php');
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($_SERVER['REQUEST_METHOD'] === 'POST'){
try{
$stmt3 = $db->prepare("INSERT INTO emi (customerId, invoiceId, adPaymentAmount, adPaymentDate, adPaymentSource, adPaymentTran, adPaymentRemarks, payStatus) VALUES (:customerId, :invoiceId, :adPaymentAmount, :adPaymentDate, :adPaymentSource, :adPaymentTran, :adPaymentRemarks, 1)");
$stmt3->bindParam(':customerId', $_GET['customerId']);
$stmt3->bindParam(':invoiceId', $_GET['invoiceId']);
$stmt3->bindParam(':adPaymentAmount', $_POST['adPaymentAmount']);
$stmt3->bindParam(':adPaymentDate', $_POST['adPaymentDate']);
$stmt3->bindParam(':adPaymentSource', $_POST['adPaymentSource']);
$stmt3->bindParam(':adPaymentTran', $_POST['adPaymentTran']);
$stmt3->bindParam(':adPaymentRemarks', $_POST['adPaymentRemarks']);
if($stmt3->execute()){
echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> Paid Successfully.</div>';
}else{
echo '<div class="alert alert-danger">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> Payment Faild.</div>';
}
} catch(PDOException $e){
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
try {
$stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->execute();
$invoiceData = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt2 = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId");
$stmt2->bindParam(':customerId', $_GET['customerId']);
$stmt2->execute();
$emiData = $stmt2->fetch(PDO::FETCH_ASSOC);
// var_dump($emiData);
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
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()]);
}
}
try {
// 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>');
}
$currentOutstanding = 0;
$totalAmount = 0;
$bookingDate = null;
$currentOutstanding = 0;
$totalAmount = 0;
$bookingDate = null;
$tenure = null;
$frequency = null;
foreach ($emiPlans as $emi) {
$totalAmount = $emi['totalAmount'];
if ($bookingDate === null && !empty($emi['bookingDate'])) {
$bookingDate = $emi['bookingDate'];
}
if ($emi['payStatus'] == 0) {
$currentOutstanding += $emi['emiAmount'];
}
if ($tenure === null && isset($emi['tenure'])) {
$tenure = $emi['tenure'];
}
if ($frequency === null && isset($emi['frequency'])) {
$frequency = $emi['frequency'];
}
}
$currentOutstanding = round($currentOutstanding);
$tenure = $tenure !== null ? $tenure : 0;
$frequency = $frequency !== null ? $frequency : 0;
?>
<div class="container mt-4">
<h3 class="mb-3">EMI Details</h3>
<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 $bookingDate ? htmlspecialchars($bookingDate) : 'N/A'; ?></strong></p>
<p>Invoice Id: <strong><?php echo $emiPlans[0]['invoiceId']; ?></strong></p>
</div>
<div>
<p>Total Amount: <strong>$<?php echo $totalAmount; ?></strong></p>
<p>Outstanding: <strong>$<?php echo $currentOutstanding; ?></strong></p>
<p>Tenure: <strong><?php echo $tenure; ?></strong></p>
<p>Frequency: <strong><?php echo $frequency; ?></strong></p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Additional Payment</button>
</div>
</div>
<hr >
<div class="d-flex flex-column gap-3">
<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) { if($emi['emiAmount'] !== null){ ?>
<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>
<?php
if($emiPlans[0]['adPaymentAmount'] !== null) { ?>
<h3 class="mb-3">Additional Payment Details</h3>
<table class="table table-striped table-bordered">
<thead class="bg-primary text-white text-center">
<tr>
<th>Amount</th>
<th>Payment Date</th>
<th>Transaction Id</th>
<th>Payment Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($emiPlans as $emi) { if($emi['emiAmount'] === null) { ?>
<tr id="row-<?= $emi['id']; ?>">
<td>$<?= $emi['adPaymentAmount']; ?></td>
<td><?= $emi['adPaymentDate']; ?></td>
<td><?= $emi['adPaymentTran']; ?></td>
<td>
<span id="status-<?= $emi['id']; ?>" class="badge <?= $emi['payStatus'] == 0 ? 'bg-danger' : 'bg-success'; ?>">
<?= $emi['payStatus'] == 0 ? 'Unpaid' : 'Paid'; ?>
</span>
</td>
</tr>
<?php } }?>
</tbody>
</table>
<?php } ?>
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered"> <!-- This centers the modal -->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel">Additional Payment</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" class="">
<div class="form-group">
<label for="adPaymentAmount">Payment Amount:</label>
<input type="text" id="adPaymentAmount" name="adPaymentAmount" class="form-control" value="<?= $emiData['emiAmount']; ?>" required>
</div>
<div class="form-group">
<label for="adPaymentDate">Payment Date:</label>
<input type="date" id="adPaymentDate" name="adPaymentDate" class="form-control" required>
</div>
<div class="form-group">
<label for="adPaymentTran">Transaction ID:</label>
<input type="text" id="adPaymentTran" name="adPaymentTran" class="form-control" required>
</div>
<div class="form-group">
<label for="adPaymentSource">Payment Source:</label>
<input type="text" id="adPaymentSource" name="adPaymentSource" class="form-control" required>
</div>
<div class="form-group">
<label for="adPaymentRemarks">Remarks:</label>
<input type="text" id="adPaymentRemarks" name="adPaymentRemarks" class="form-control" >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success btn-block mt-2">Save Payment</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</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>