This commit is contained in:
Suvodip
2025-03-08 17:01:20 +05:30
parent e97d30ce72
commit b813a79fe8
7 changed files with 229 additions and 126 deletions

View File

@@ -3,32 +3,85 @@
$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>';
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId");
// Get customer EMI details
$customerId = $_GET['customerId'];
$invoiceId = $_GET['invoiceId'];
$adPaymentAmount = floatval($_POST['adPaymentAmount']);
$adPaymentDate = $_POST['adPaymentDate'];
$adPaymentSource = $_POST['adPaymentSource'];
$adPaymentTran = $_POST['adPaymentTran'];
$adPaymentRemarks = $_POST['adPaymentRemarks'];
// Fetch EMIs of the customer (Assuming we have emiNumber in ascending order)
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId ORDER BY emiNumber DESC");
$stmt->bindParam(':customerId', $customerId);
$stmt->bindParam(':invoiceId', $invoiceId);
$stmt->execute();
$emiRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$emiRecords) {
echo '<div class="alert alert-danger">No EMI records found.</div>';
exit;
}
$remainingAmount = $adPaymentAmount;
foreach ($emiRecords as $index => &$emi) {
if ($remainingAmount <= 0) break;
$emiId = $emi['id'];
$emiAmount = floatval($emi['emiAmount']);
if ($remainingAmount >= $emiAmount) {
// If additional amount is greater or equal to EMI, pay it off
$payAmount = $emiAmount;
$remainingAmount -= $emiAmount;
$newEmiAmount = 0; // EMI is fully paid
} else {
// If additional amount is less than EMI, reduce this EMI
$payAmount = $remainingAmount;
$newEmiAmount = $emiAmount - $remainingAmount;
$remainingAmount = 0; // No more amount left to deduct
}
// Update EMI record with reduced amount
$updateStmt = $db->prepare("UPDATE emi SET emiAmount = :newEmiAmount, payStatus = CASE WHEN emiAmount = 0 THEN 1 ELSE 0 END WHERE id = :emiId");
$updateStmt->bindParam(':newEmiAmount', $newEmiAmount);
$updateStmt->bindParam(':emiId', $emiId);
$updateStmt->execute();
}
// Insert additional payment record
$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', $customerId);
$stmt3->bindParam(':invoiceId', $invoiceId);
$stmt3->bindParam(':adPaymentAmount', $adPaymentAmount);
$stmt3->bindParam(':adPaymentDate', $adPaymentDate);
$stmt3->bindParam(':adPaymentSource', $adPaymentSource);
$stmt3->bindParam(':adPaymentTran', $adPaymentTran);
$stmt3->bindParam(':adPaymentRemarks', $adPaymentRemarks);
if ($stmt3->execute()) {
echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> applied successfully.</div>';
} else {
echo '<div class="alert alert-danger">Failed to record the additional payment.</div>';
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
try {
$stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->bindParam(':invoiceId', $_GET['invoiceId']);
$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();
@@ -48,11 +101,16 @@
$stmt->bindParam(':payStatus', $_POST['payStatus'], PDO::PARAM_INT);
$stmt->bindParam(':emiId', $_POST['emiId'], PDO::PARAM_INT);
$stmt->execute();
// Ensure no previous output before JSON
echo json_encode(['status' => 'success']);
exit; // Terminate script after sending JSON
} catch (PDOException $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
exit;
}
}
try {
// Fetch EMI data
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId ORDER BY emiDate ASC");
@@ -113,7 +171,7 @@
<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>
<p>Frequency: <strong><?php echo $invoiceData['frequency'] ?></strong></p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Additional Payment</button>
</div>
</div>
@@ -126,7 +184,6 @@
<th>EMI Amount</th>
<th>EMI Date</th>
<th>Payment Status</th>
<th>Outstanding</th>
<th>Action</th>
</tr>
</thead>
@@ -141,7 +198,6 @@
<?= $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>
@@ -153,7 +209,7 @@
</tbody>
</table>
<?php
if($emiPlans[0]['adPaymentAmount'] !== null) { ?>
if($emiPlans[0]['adPaymentAmount'] > 0 ) { ?>
<h3 class="mb-3">Additional Payment Details</h3>
<table class="table table-striped table-bordered">
<thead class="bg-primary text-white text-center">
@@ -161,20 +217,16 @@
<th>Amount</th>
<th>Payment Date</th>
<th>Transaction Id</th>
<th>Payment Status</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php foreach ($emiPlans as $emi) { if($emi['emiAmount'] === null) { ?>
<?php foreach ($emiPlans as $emi) { if($emi['adPaymentAmount'] !== null && $emi['emiAmount'] == 0) { ?>
<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>
<td><?= $emi['adPaymentRemarks']; ?></td>
</tr>
<?php } }?>
</tbody>
@@ -192,7 +244,7 @@
<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>
<input oninput="addAutoAddRemarks();" type="text" id="adPaymentAmount" name="adPaymentAmount" class="form-control" value="<?= htmlspecialchars($emiData['emiAmount']); ?>" required>
</div>
<div class="form-group">
<label for="adPaymentDate">Payment Date:</label>
@@ -223,12 +275,12 @@
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".paymentStatus").forEach(select => {
select.addEventListener("change", function() {
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" },
@@ -236,14 +288,30 @@
})
.then(response => response.json())
.then(data => {
console.log(data); // Debugging output
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");
if (statusBadge) {
statusBadge.textContent = newStatus == 1 ? "Paid" : "Unpaid";
statusBadge.className = "badge " + (newStatus == 1 ? "bg-success" : "bg-danger");
}
} else {
alert("Error updating status: " + data.message);
}
})
.catch(error => console.error("Error:", error));
});
});
});
</script>
function addAutoAddRemarks() {
const emiAmount = <?= json_encode($emiData['emiAmount']); ?>;
const addPayAmount = parseFloat(document.getElementById('adPaymentAmount').value) || 0;
const extraAmount = addPayAmount - emiAmount;
document.getElementById('adPaymentRemarks').value =
`EMI Amount: ${emiAmount}. Extra ${extraAmount > 0 ? extraAmount.toFixed(2) : "0.00"} Settled in last EMI `;
}
</script>