Files
ISP-Billing/delete_orphaned_customers.php
2026-01-16 12:06:59 +01:00

99 lines
3.2 KiB
PHP

<?php
/**
* Script to safely delete orphaned customers
* This script deletes customers that have no relationships in other tables
*/
include "init.php";
echo "=== CUSTOMER DELETION SCRIPT ===\n";
echo "Date: " . date('Y-m-d H:i:s') . "\n\n";
// List of customer IDs to delete (confirmed orphaned customers)
$customersToDelete = [
419, 425, 426, 429, 431, 433, 436, 437, 438, 439, 440, 448, 450, 452, 455, 458, 459, 467, 471, 474, 475, 476, 477, 480, 481, 482, 485, 490
];
echo "Customers to delete: " . count($customersToDelete) . "\n\n";
$deletedCount = 0;
$errorCount = 0;
foreach ($customersToDelete as $customerId) {
try {
// Get customer info before deletion
$customer = ORM::for_table('tbl_customers')->find_one($customerId);
if (!$customer) {
echo "❌ Customer ID $customerId not found\n";
$errorCount++;
continue;
}
echo "Deleting: ID $customerId - {$customer['username']} ({$customer['fullname']})... ";
// Double-check: Verify no relationships exist
$hasRelations = false;
// Check tbl_user_recharges
$recharges = ORM::for_table('tbl_user_recharges')->where('customer_id', $customerId)->count();
if ($recharges > 0) {
echo "❌ HAS RELATIONS (tbl_user_recharges: $recharges records) - SKIPPING\n";
$errorCount++;
continue;
}
// Check tbl_transactions
$transactions = ORM::for_table('tbl_transactions')->where('username', $customer['username'])->count();
if ($transactions > 0) {
echo "❌ HAS RELATIONS (tbl_transactions: $transactions records) - SKIPPING\n";
$errorCount++;
continue;
}
// Check tbl_customers_fields
$fields = ORM::for_table('tbl_customers_fields')->where('customer_id', $customerId)->count();
if ($fields > 0) {
echo "❌ HAS RELATIONS (tbl_customers_fields: $fields records) - SKIPPING\n";
$errorCount++;
continue;
}
// Check tbl_payment_gateway
$payments = ORM::for_table('tbl_payment_gateway')->where('username', $customer['username'])->count();
if ($payments > 0) {
echo "❌ HAS RELATIONS (tbl_payment_gateway: $payments records) - SKIPPING\n";
$errorCount++;
continue;
}
// Safe to delete - proceed with deletion
$customer->delete();
echo "✅ DELETED\n";
$deletedCount++;
} catch (Exception $e) {
echo "❌ ERROR: " . $e->getMessage() . "\n";
$errorCount++;
}
}
echo "\n=== DELETION SUMMARY ===\n";
echo "Total customers processed: " . count($customersToDelete) . "\n";
echo "Successfully deleted: $deletedCount\n";
echo "Errors/Skipped: $errorCount\n";
if ($deletedCount > 0) {
echo "\n✅ Deletion completed successfully!\n";
echo "Removed $deletedCount orphaned customers from the database.\n";
} else {
echo "\n⚠️ No customers were deleted.\n";
}
echo "\n=== VERIFICATION ===\n";
$remainingCustomers = ORM::for_table('tbl_customers')->count();
echo "Remaining customers in database: $remainingCustomers\n";
echo "\n=== DELETION COMPLETE ===\n";
?>