196 lines
11 KiB
PHP
196 lines
11 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">
|
|
<div class="d-flex justify-content-between">
|
|
<h3 class="">Customer List</h3>
|
|
<button onclick="exportTableToExcel()" class="btn btn-sm text-white align-items-center" style="background-color: #374151; height: fit-content;">Export to Excel</button>
|
|
</div><hr>
|
|
|
|
<div class="table-responsive">
|
|
<table id="myTable" class="table table-bordered table-striped table-hover w-100" style="font-size: 13px;">
|
|
<thead class="text-white text-center" style="background-color: #374151;">
|
|
<tr>
|
|
<th>Sl No</th>
|
|
<th>Name</th>
|
|
<th>Start Date</th>
|
|
<th>End Date</th>
|
|
<th>Installments</th>
|
|
<th>Monthly Payment Date</th>
|
|
<th>Monthly Payment Amount</th>
|
|
<th>Total Amount</th>
|
|
<th>Action</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 gap-1">
|
|
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-sm text-white" style="font-size: 13px; background-color: #374151;">New</a>
|
|
<a href="/customers/edit-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-sm text-white" style="font-size: 13px; background-color: #374151;">Edit</a>
|
|
<a href="/customers/print-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-sm text-white" style="font-size: 13px; background-color: #374151;">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 gap-1">
|
|
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-sm text-white" style="font-size: 13px; background-color: #374151;">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>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
|
|
<script>
|
|
function exportTableToExcel() {
|
|
let table = document.getElementById("myTable");
|
|
let rows = table.querySelectorAll("tr");
|
|
let data = [];
|
|
|
|
rows.forEach((row, rowIndex) => {
|
|
let rowData = [];
|
|
let cells = row.querySelectorAll("th, td");
|
|
|
|
cells.forEach((cell, cellIndex) => {
|
|
if (cellIndex !== 8) { // Excluding the "Invoice" column (Index 8)
|
|
rowData.push(cell.innerText);
|
|
}
|
|
});
|
|
|
|
data.push(rowData);
|
|
});
|
|
|
|
let wb = XLSX.utils.book_new();
|
|
let ws = XLSX.utils.aoa_to_sheet(data); // Convert array to sheet
|
|
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
|
|
|
|
// Save the file
|
|
XLSX.writeFile(wb, "TableData.xlsx");
|
|
}
|
|
</script>
|