101 lines
3.9 KiB
PHP
101 lines
3.9 KiB
PHP
<?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>
|