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 <?php
require('../.hta_config/conf.php'); require('../.hta_config/conf.php');
// Enable error reporting (for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);
try { try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass); $db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $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'], $_POST['payStatus'])) {
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["emiId"]) && isset($_POST["status"])) { header('Content-Type: application/json');
$emiId = $_POST["emiId"]; ob_end_clean(); // Clears any accidental HTML output
$status = $_POST["status"];
try { try {
$stmt = $db->prepare("UPDATE billing SET emi".$emiId."Status = :status WHERE customerId = :customerId"); $stmt = $db->prepare("UPDATE emi SET payStatus = :payStatus WHERE customerId = :customerId AND id = :emiId");
$stmt->bindParam(':status', $status, PDO::PARAM_INT); $stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':customerId', $_POST["customerId"]); $stmt->bindParam(':payStatus', $_POST['payStatus'], PDO::PARAM_INT);
$stmt->execute(); $stmt->bindParam(':emiId', $_POST['emiId'], PDO::PARAM_INT);
echo json_encode(["success" => true, "message" => "Payment status updated"]); $stmt->execute();
} catch (PDOException $e) {
echo json_encode(["success" => false, "message" => "Error updating payment status"]); echo json_encode(['status' => 'success']);
} catch (PDOException $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
exit;
} }
exit;
}
// Fetch Customer Billing Data // Fetch EMI data
if (!isset($_GET['customerId'])) { $stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId ORDER BY emiDate ASC");
die("Invalid request: Customer ID is required."); $stmt->bindParam(':customerId', $_GET['customerId']);
} $stmt->execute();
$emiPlans = $stmt->fetchAll(PDO::FETCH_ASSOC);
// var_dump($emiPlans);
$customerId = $_GET['customerId']; $stmt = $db->prepare("SELECT * FROM customers WHERE customerId = :customerId");
$stmt = $db->prepare("SELECT * FROM billing WHERE customerId = :customerId"); $stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':customerId', $customerId); $stmt->execute();
$stmt->execute(); $customer = $stmt->fetch(PDO::FETCH_ASSOC);
$billingData = $stmt->fetch(PDO::FETCH_ASSOC); // var_dump($customer);
if (!$billingData) { } catch (PDOException $e) {
die("No billing data found for this customer."); die('<div class="alert alert-danger text-center">Error: ' . $e->getMessage() . '</div>');
} }
?> ?>
<div class="container mt-4"> <div class="container mt-4">
<h2 class="mb-3 text-center">Customer Billing Details</h2> <h2 class="mb-3">EMI Details</h2>
<table class="table table-bordered table-striped"> <div class="d-flex justify-content-between">
<thead class="thead-dark"> <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> <tr>
<th>#</th> <th>Number of EMI</th>
<th>EMI Amount</th>
<th>EMI Date</th> <th>EMI Date</th>
<th>Amount</th> <th>Payment Status</th>
<th>Status</th> <th>Outstanding</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php <?php foreach ($emiPlans as $emi) { ?>
for ($i = 1; $i <= 12; $i++) { <tr id="row-<?= $emi['id']; ?>">
$emiAmount = $billingData["emi$i"]; <td><?= $emi['emiNumber']; ?></td>
$emiDate = $billingData["emi{$i}Date"]; <td><?= number_format($emi['emiAmount'], 2); ?></td>
$emiStatus = $billingData["emi{$i}Status"]; <td><?= date('d-m-Y', strtotime($emi['emiDate'])); ?></td>
<td>
if (!$emiAmount || !$emiDate) continue; <span id="status-<?= $emi['id']; ?>" class="badge <?= $emi['payStatus'] == 0 ? 'bg-danger' : 'bg-success'; ?>">
echo "<tr> <?= $emi['payStatus'] == 0 ? 'Unpaid' : 'Paid'; ?>
<td>{$i}</td> </span>
<td>{$emiDate}</td> </td>
<td>{$emiAmount}</td> <td><?= number_format($emi['outstanding'], 2); ?></td>
<td id='status-$i'>" . ($emiStatus == 1 ? "<span class='badge bg-success'>Paid</span>" : "<span class='badge bg-danger'>Unpaid</span>") . "</td> <td>
<td> <select class="form-select paymentStatus" data-emi-id="<?= $emi['id']; ?>">
<button class='btn btn-sm " . ($emiStatus == 1 ? "btn-secondary disabled" : "btn-primary") . "' onclick='updatePaymentStatus($i)' id='btn-$i'>Mark as Paid</button> <option value="1" <?= $emi['payStatus'] == 1 ? 'selected' : ''; ?>>Paid</option>
</td> <option value="0" <?= $emi['payStatus'] == 0 ? 'selected' : ''; ?>>Unpaid</option>
</tr>"; </select>
} </td>
?> </tr>
<?php } ?>
</tbody> </tbody>
</table> </table>
</div> </div>
<script> <script>
function updatePaymentStatus(emiId) { document.addEventListener("DOMContentLoaded", function() {
let customerId = "<?php echo $customerId; ?>"; document.querySelectorAll(".paymentStatus").forEach(select => {
let formData = new FormData(); select.addEventListener("change", function() {
formData.append("emiId", emiId); let emiId = this.getAttribute("data-emi-id");
formData.append("status", 1); // Set status to "1" (Paid) let newStatus = this.value;
formData.append("customerId", customerId);
fetch("billing-details.php", { fetch(window.location.href, {
method: "POST", method: "POST",
body: formData headers: { "Content-Type": "application/x-www-form-urlencoded" },
}) body: `emiId=${emiId}&payStatus=${newStatus}`
.then(response => response.json()) })
.then(data => { .then(response => response.json())
if (data.success) { .then(data => {
document.getElementById("status-" + emiId).innerHTML = "<span class='badge badge-success'>Paid</span>"; if (data.status === "success") {
document.getElementById("btn-" + emiId).classList.replace("btn-primary", "btn-secondary"); let statusBadge = document.getElementById("status-" + emiId);
document.getElementById("btn-" + emiId).classList.add("disabled"); statusBadge.textContent = newStatus == 1 ? "Paid" : "Unpaid";
} else { statusBadge.className = "badge " + (newStatus == 1 ? "bg-success" : "bg-danger");
alert("Failed to update payment status!"); }
} })
}) .catch(error => console.error("Error:", error));
.catch(error => console.error("Error:", error)); });
} });
});
</script> </script>

View File

@ -1,100 +0,0 @@
<?php
require('../.hta_config/conf.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$customerId = $_POST['customerId'];
$totalAmount = $_POST['totalAmount'];
$tenure = min($_POST['tenure'], 18); // Limit tenure to 18 months
$firstEmiDate = $_POST['firstEmiDate'];
$emiAmount = $totalAmount / $tenure;
// Prepare the query dynamically
$columns = ['customerId', 'totalAmount', 'tenure', 'emiAmount', 'firstEmiDate'];
$placeholders = [':customerId', ':totalAmount', ':tenure', ':emiAmount', ':firstEmiDate'];
$params = [
':customerId' => $customerId,
':totalAmount' => $totalAmount,
':tenure' => $tenure,
':emiAmount' => $emiAmount,
':firstEmiDate' => $firstEmiDate
];
// Add EMI columns dynamically based on tenure
for ($i = 1; $i <= $tenure; $i++) {
$emiDate = date('Y-m-d', strtotime("+$i months", strtotime($firstEmiDate)));
$columns[] = "emi$i";
$columns[] = "emi{$i}Date";
$placeholders[] = ":emi$i";
$placeholders[] = ":emi{$i}Date";
$params[":emi$i"] = $emiAmount;
$params[":emi{$i}Date"] = $emiDate;
}
// Construct SQL statement
$sql = "INSERT INTO billing (" . implode(',', $columns) . ") VALUES (" . implode(',', $placeholders) . ")";
$stmt = $db->prepare($sql);
// Execute the query
if ($stmt->execute($params)) {
echo '<div class="alert alert-success">New EMI plan for <strong>' . htmlspecialchars($customerId) . '</strong> saved successfully.</div>';
} else {
echo '<div class="alert alert-danger">Error executing statement: ' . $stmt->errorInfo()[2] . '</div>';
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
// Fetch customer details
$customer = null;
if (!empty($_GET['customerId'])) {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT * FROM customers WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $_GET['customerId']);
if ($stmt->execute()) {
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
?>
<div class="container mt-4">
<h2>EMI Calculation</h2>
<?php if (!empty($customer)): ?>
<p><strong>Name:</strong> <?= htmlspecialchars($customer['name']) ?></p>
<p><strong>Mobile:</strong> <?= htmlspecialchars($customer['mobile']) ?></p>
<?php else: ?>
<div class="alert alert-warning">Customer not found.</div>
<?php endif; ?>
<form method="POST">
<input type="hidden" name="customerId" value="<?= htmlspecialchars($_GET['customerId'] ?? '') ?>">
<div class="mb-3">
<label for="totalAmount" class="form-label">Total Amount:</label>
<input type="number" class="form-control" id="totalAmount" name="totalAmount" required>
</div>
<div class="mb-3">
<label for="tenure" class="form-label">Tenure (Months):</label>
<input type="number" class="form-control" id="tenure" name="tenure" min="1" max="18" required>
</div>
<div class="mb-3">
<label for="firstEmiDate" class="form-label">First EMI Date:</label>
<input type="date" class="form-control" id="firstEmiDate" name="firstEmiDate" required>
</div>
<button type="submit" class="btn btn-primary">Save EMI</button>
</form>
</div>

View File

@ -0,0 +1,149 @@
<?php
require('../.hta_config/conf.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['customerId'])) {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$totalAmount = $_POST['totalAmount'];
$tenure = $_POST['tenure'];
$firstEmiDate = $_POST['firstEmiDate'];
$totalAmount = $_POST['totalAmount'];
$frequency = $_POST['frequency'];
$bookingDate = $_POST['bookingDate'];
$paymentMode = $_POST['paymentMode'];
// EMI Calculation
$emiAmount = round($totalAmount / $tenure, 2);
$outstandingAmount = $totalAmount;
$emiDate = new DateTime($firstEmiDate); // Set initial date
$stmt = $db->prepare("INSERT INTO emi (customerId, emiNumber, emiAmount, emiDate, totalAmount, outstanding, tenure, frequency, bookingDate, paymentMode) VALUES (:customerId, :emiNumber, :emiAmount, :emiDate, :totalAmount, :outstanding, :tenure, :frequency, bookingDate, :paymentMode)");
for ($i = 1; $i <= $tenure; $i++) {
$outstandingAmount -= $emiAmount;
$emiDateFormatted = $emiDate->format('Y-m-d');
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->bindParam(':emiNumber', $i);
$stmt->bindParam(':emiAmount', $emiAmount);
$stmt->bindParam(':emiDate', $emiDateFormatted);
$stmt->bindParam(':totalAmount', $totalAmount);
$stmt->bindParam(':outstanding', $outstandingAmount);
$stmt->bindParam(':tenure', $tenure);
$stmt->bindParam(':frequency', $frequency);
$stmt->bindParam(':bookingDate', $bookingDate);
$stmt->bindParam(':paymentMode', $paymentMode);
$stmt->execute();
// Move to the next month's EMI date
if($frequency === 'Weekly'){
$emiDate->modify('+1 week');
} elseif($frequency === 'Monthly'){
$emiDate->modify('+1 month');
}
}
echo '<div class="alert alert-success">New EMI Plan Saved Successfully!</div>';
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
$customer = null;
if (!empty($_GET['customerId'])) {
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT * FROM customers WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $_GET['customerId']);
if ($stmt->execute()) {
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
}
$stmt = $db->prepare("SELECT * FROM address WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $_GET['customerId']);
if ($stmt->execute()) {
$customerAddress = $stmt->fetch(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
?>
<div class="container mt-4">
<h2>Create Invoice</h2>
<?php if (!empty($customer)): ?>
<p><strong>Mobile:</strong> <?= htmlspecialchars($customer['mobile']) ?></p>
<?php else: ?>
<div class="alert alert-warning">Customer not found.</div>
<?php endif; ?>
<form method="POST">
<div class="d-flex justify-content-between gap-4">
<div class="w-100">
<div class="mb-3">
<label for="totalAmount" class="form-label">Customer:</label>
<input disabled type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($customer['name']); ?>">
</div>
<div class="mb-3">
<label for="totalAmount" class="form-label">Address:</label>
<input disabled class="form-control" type="text" value="<?= 'City: ' . $customerAddress['city'] .' District: '. $customerAddress['district'] . ' State: ' .$customerAddress['state'] . ' Pin Code: ' . $customerAddress['pinCode']; ?>">
</div>
<div class="mb-3">
<label for="totalAmount" class="form-label">Total Amount:</label>
<input type="number" class="form-control" id="totalAmount" name="totalAmount" required>
</div>
<div class="mb-3">
<label for="tenure" class="form-label">Tenure (Weekly or Months):</label>
<input type="number" class="form-control" id="tenure" name="tenure" min="1" max="18" required>
</div>
<div class="mb-3">
<label for="frequency" class="form-label">Frequency:</label>
<select name="frequency" class="form-control"required>
<option value="">-Select-</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
</select>
</div>
<div class="d-flex w-100 gap-2">
<div class="mb-3 w-100">
<label for="bookingDate" class="form-label">Invoice Date:</label>
<input type="date" class="form-control" id="bookingDate" name="bookingDate" required>
</div>
<div class="mb-3 w-100">
<label for="firstEmiDate" class="form-label">Due Date:</label>
<input type="date" class="form-control" id="firstEmiDate" name="firstEmiDate" required>
</div>
</div>
</div>
<div class="w-100">
<div class="mb-3">
<label for="paymentMode" class="form-label">Payment Mode:</label>
<select name="paymentMode" class="form-control"required>
<option value="">-Select-</option>
<option value="UPI">UPI</option>
<option value="Credit Card">Credit Card</option>
<option value="Debit Card">Debit Card</option>
</select>
</div>
<div class="mb-3">
<label for="currency" class="form-label">Currency:</label>
<select name="currency" class="form-control"required>
<option value="">-Select-</option>
<option value="USD">USD</option>
<option value="INR">INR</option>
</select>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Save EMI</button>
</form>
</div>

View File

@ -5,7 +5,7 @@
<h2 class="mb-3 text-center">Customer List</h2> <h2 class="mb-3 text-center">Customer List</h2>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-bordered table-striped table-hover"> <table class="table table-bordered table-striped table-hover">
<thead class="thead-dark"> <thead class="bg-primary text-white text-center">
<tr> <tr>
<th>Sl No</th> <th>Sl No</th>
<th>Name</th> <th>Name</th>
@ -33,8 +33,8 @@
<td><?php echo htmlspecialchars($customer['email']); ?></td> <td><?php echo htmlspecialchars($customer['email']); ?></td>
<td> <td>
<a href="/customers/edit/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Edit</a> <a href="/customers/edit/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Edit</a>
<a href="/customers/emi/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Bill</a> <a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Invoice</a>
<a href="/customers/billing-details/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Billing Info</a> <a href="/customers/billing-details/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Installment Details</a>
</td> </td>
</tr> </tr>
<?php <?php

View File

@ -0,0 +1,4 @@
<?php
require('../.hta_config/conf.php');
?>