billing2/customers/.hta_slug/generate-invoice.php

150 lines
7.1 KiB
PHP

<?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>