billing2/customers/.hta_slug/list.php

165 lines
9.9 KiB
PHP

<?php
require('../.hta_config/conf.php');
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete'])) {
if (!empty($_POST['customerId']) && !empty($_POST['invoiceId'])) {
$customerId = $_POST['customerId'];
$invoiceId = $_POST['invoiceId'];
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Start Transaction
$db->beginTransaction();
// Delete from emi table
$stmt = $db->prepare("DELETE FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->execute(['customerId' => $customerId, 'invoiceId' => $invoiceId]);
// Delete from invoice table
$stmt = $db->prepare("DELETE FROM invoice WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->execute(['customerId' => $customerId, 'invoiceId' => $invoiceId]);
// Delete from customers table (only if no dependent records exist)
$stmt = $db->prepare("DELETE FROM customers WHERE customerId = :customerId");
$stmt->execute(['customerId' => $customerId]);
// Commit Transaction
$db->commit();
echo '<div class="alert alert-success">Customer deleted successfully!</div>';
} catch (Exception $e) {
$db->rollBack();
error_log("Delete Error: " . $e->getMessage()); // Logs error without exposing details
echo '<div class="alert alert-danger">Error: ' . htmlspecialchars($e->getMessage()) . '</div>';
}
} else {
echo '<div class="alert alert-warning">Invalid input! Please check your data.</div>';
}
}
?>
<div class="container-xxl mt-4">
<h3 class="mb-3 ">Customer List</h3><hr>
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover w-100" style="font-size: 13px;">
<thead class="bg-primary text-white text-center" >
<tr>
<th>Sl No</th>
<th>Name</th>
<th>Payment Start Date</th>
<th>Payment End Date</th>
<th>Number of Installments</th>
<th>Monthly Payment Date</th>
<th>Monthly Payment Amount</th>
<th>Total Amount</th>
<th>Invoice</th>
</tr>
</thead>
<tbody>
<?php
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch customer data
$stmt = $db->prepare("SELECT * FROM customers ORDER BY regDate DESC");
$stmt->execute();
$content = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch EMI data
$stmt = $db->prepare("SELECT * FROM emi ORDER BY invoiceId ASC");
$stmt->execute();
$emiContent = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch invoice data
$stmt2 = $db->prepare("SELECT * FROM invoice");
$stmt2->execute();
$invoiceContent = $stmt2->fetchAll(PDO::FETCH_ASSOC);
// Loop through each customer
$customerSerial = 1;
foreach ($content as $customer) {
// Find all invoices for the current customer
$matchingInvoices = array_filter($invoiceContent, function ($invoice) use ($customer) {
return $invoice['customerId'] === $customer['customerId'];
});
// If there are matching invoices, loop through them
if (!empty($matchingInvoices)) {
foreach ($matchingInvoices as $invoice) {
// Find corresponding EMI records for this invoice
$matchingEMIs = array_filter($emiContent, function ($emi) use ($invoice) {
return $emi['invoiceId'] === $invoice['invoiceId'];
});
// If EMI exists, use its data; otherwise, display empty fields
// $emi = reset($matchingEMIs) ?: [];
$matchingEmis = array_filter($emiContent, function ($emi) use ($invoice) {
return $emi['invoiceId'] === $invoice['invoiceId'];
});
if (!empty($matchingEmis)) {
$emi = reset($matchingEmis); // Get the first matching EMI record safely
} else {
$emi = [];
}
?>
<tr>
<td><?php echo $customerSerial++; ?></td>
<td>
<a href="/customers/billing-details/?customerId=<?php echo $customer['customerId'] . '&invoiceId=' . $invoice['invoiceId']; ?>">
<?php echo htmlspecialchars($customer['name']); ?>
</a>
</td>
<td><?php echo isset($emi['bookingDate']) ? date('d-m-Y', strtotime($emi['bookingDate'])) : 'N/A'; ?></td>
<td><?php echo isset($emi['bookingDate']) ? (new DateTime($emi['bookingDate']))->modify('+'.$emi['tenure']-'1'.'months')->format('d-m-Y') : 'N/A'; ?></td>
<td><?php echo isset($emi['tenure']) ? htmlspecialchars($emi['tenure']) : 'N/A'; ?></td>
<td><?php echo isset($emi['bookingDate']) ? date('d', strtotime($emi['bookingDate'])) .' Each Month': 'N/A'; ?></td>
<td>$<?php echo isset($emi['emiAmount']) ? htmlspecialchars($emi['emiAmount']) : '0.00'; ?></td>
<td>$<?php echo isset($emi['totalAmount']) ? htmlspecialchars($emi['totalAmount']) : '0.00'; ?></td>
<td class="d-flex flex-wrap gap-1">
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">New</a>
<a href="/customers/edit-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">Edit</a>
<a href="/customers/print-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">Print</a>
<form method="post">
<input type="hidden" name="customerId" value="<?php echo htmlspecialchars($customer['customerId']); ?>" />
<input type="hidden" name="invoiceId" value="<?php echo htmlspecialchars($invoice['invoiceId']); ?>" />
<button type="submit" name="delete" class="btn btn-danger btn-sm" style="font-size: 13px;">Delete</button>
</form>
</td>
</tr>
<?php
}
} else {
// For customers without an invoice, you can still display their info but leave invoice data empty
?>
<tr>
<td><?php echo $customerSerial++; ?></td>
<td><?php echo htmlspecialchars($customer['name']); ?></td>
<td colspan="6" class="text-center">No EMI or Invoice Data</td>
<td class="d-flex flex-wrap gap-1">
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">New</a>
<form method="post">
<input type="hidden" name="customerId" value="<?php echo htmlspecialchars($customer['customerId']); ?>" />
<input type="hidden" name="invoiceId" value="<?php echo htmlspecialchars($invoice['invoiceId']); ?>" />
<button type="submit" name="delete" class="btn btn-danger btn-sm" style="font-size: 13px;">Delete</button>
</form>
</td>
</tr>
<?php
}
}
} catch (PDOException $e) {
echo '<tr><td colspan="9" class="text-danger text-center">Error: ' . $e->getMessage() . '</td></tr>';
}
?>
</tbody>
</table>
</div>
</div>