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
require('../.hta_config/conf.php');
// Enable error reporting (for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$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"]) && isset($_POST["status"])) {
$emiId = $_POST["emiId"];
$status = $_POST["status"];
try {
$stmt = $db->prepare("UPDATE billing SET emi".$emiId."Status = :status WHERE customerId = :customerId");
$stmt->bindParam(':status', $status, PDO::PARAM_INT);
$stmt->bindParam(':customerId', $_POST["customerId"]);
$stmt->execute();
echo json_encode(["success" => true, "message" => "Payment status updated"]);
} catch (PDOException $e) {
echo json_encode(["success" => false, "message" => "Error updating payment status"]);
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;
}
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);
// Fetch Customer Billing Data
if (!isset($_GET['customerId'])) {
die("Invalid request: Customer ID is required.");
}
$customerId = $_GET['customerId'];
$stmt = $db->prepare("SELECT * FROM billing WHERE customerId = :customerId");
$stmt->bindParam(':customerId', $customerId);
$stmt->execute();
$billingData = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$billingData) {
die("No billing data found for this customer.");
$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 text-center">Customer Billing Details</h2>
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<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>#</th>
<th>Number of EMI</th>
<th>EMI Amount</th>
<th>EMI Date</th>
<th>Amount</th>
<th>Status</th>
<th>Payment Status</th>
<th>Outstanding</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 1; $i <= 12; $i++) {
$emiAmount = $billingData["emi$i"];
$emiDate = $billingData["emi{$i}Date"];
$emiStatus = $billingData["emi{$i}Status"];
if (!$emiAmount || !$emiDate) continue;
echo "<tr>
<td>{$i}</td>
<td>{$emiDate}</td>
<td>{$emiAmount}</td>
<td id='status-$i'>" . ($emiStatus == 1 ? "<span class='badge bg-success'>Paid</span>" : "<span class='badge bg-danger'>Unpaid</span>") . "</td>
<td>
<button class='btn btn-sm " . ($emiStatus == 1 ? "btn-secondary disabled" : "btn-primary") . "' onclick='updatePaymentStatus($i)' id='btn-$i'>Mark as Paid</button>
</td>
</tr>";
}
?>
<?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>
function updatePaymentStatus(emiId) {
let customerId = "<?php echo $customerId; ?>";
let formData = new FormData();
formData.append("emiId", emiId);
formData.append("status", 1); // Set status to "1" (Paid)
formData.append("customerId", customerId);
fetch("billing-details.php", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById("status-" + emiId).innerHTML = "<span class='badge badge-success'>Paid</span>";
document.getElementById("btn-" + emiId).classList.replace("btn-primary", "btn-secondary");
document.getElementById("btn-" + emiId).classList.add("disabled");
} else {
alert("Failed to update payment status!");
}
})
.catch(error => console.error("Error:", error));
}
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>

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>
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<thead class="thead-dark">
<thead class="bg-primary text-white text-center">
<tr>
<th>Sl No</th>
<th>Name</th>
@ -33,8 +33,8 @@
<td><?php echo htmlspecialchars($customer['email']); ?></td>
<td>
<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/billing-details/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">Billing Info</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">Installment Details</a>
</td>
</tr>
<?php

View File

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