35251-vm/db/seed.php
2025-10-26 16:46:07 +00:00

139 lines
4.7 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
function seed_customers() {
$pdo = db();
$customers = [
['name' => 'John Doe', 'email' => 'john.doe@example.com', 'plan' => 'Premium HD', 'status' => 'Active'],
['name' => 'Jane Smith', 'email' => 'jane.smith@example.com', 'plan' => 'Basic', 'status' => 'Active'],
['name' => 'Mike Johnson', 'email' => 'mike.j@example.com', 'plan' => 'Premium HD', 'status' => 'Suspended'],
['name' => 'Sarah Williams', 'email' => 'sarah.w@example.com', 'plan' => 'Family Pack', 'status' => 'Deactivated'],
];
$stmt = $pdo->prepare("INSERT INTO customers (name, email, plan, status) VALUES (:name, :email, :plan, :status) ON DUPLICATE KEY UPDATE name=VALUES(name), plan=VALUES(plan), status=VALUES(status)");
foreach ($customers as $customer) {
echo "Seeding customer: " . $customer['name'] . "...\n";
try {
$stmt->execute($customer);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error seeding customer " . $customer['name'] . ": " . $e->getMessage() . "\n";
}
}
}
function seed_plans() {
$pdo = db();
$plans = [
[
'name' => 'Basic Cable',
'price' => 29.99,
'billing_cycle' => 'monthly',
'features' => json_encode(['50+ Channels', 'Standard Definition', '1 TV Connection'])
],
[
'name' => 'Premium HD',
'price' => 49.99,
'billing_cycle' => 'monthly',
'features' => json_encode(['150+ Channels', 'High Definition', 'Includes Sports Pack', '2 TV Connections'])
],
[
'name' => 'Ultimate Fiber',
'price' => 79.99,
'billing_cycle' => 'monthly',
'features' => json_encode(['300+ Channels', '4K Ultra HD', 'All Premium Channels', 'Up to 5 TV Connections', 'On-Demand Library'])
],
];
$stmt = $pdo->prepare("INSERT INTO plans (name, price, billing_cycle, features) VALUES (:name, :price, :billing_cycle, :features) ON DUPLICATE KEY UPDATE name=VALUES(name), price=VALUES(price), billing_cycle=VALUES(billing_cycle), features=VALUES(features)");
foreach ($plans as $plan) {
echo "Seeding plan: " . $plan['name'] . "...\n";
try {
$stmt->execute($plan);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error seeding plan " . $plan['name'] . ": " . $e->getMessage() . "\n";
}
}
}
function seed_invoices() {
$pdo = db();
$customer_stmt = $pdo->query('SELECT id FROM customers');
$customer_ids = $customer_stmt->fetchAll(PDO::FETCH_COLUMN);
if (empty($customer_ids)) {
echo "No customers found to create invoices for.\n";
return;
}
$invoices = [];
$statuses = ['Paid', 'Pending', 'Overdue'];
foreach ($customer_ids as $customer_id) {
for ($i = 0; $i < 3; $i++) {
$invoices[] = [
'customer_id' => $customer_id,
'amount' => rand(2000, 10000) / 100,
'status' => $statuses[array_rand($statuses)],
'due_date' => date('Y-m-d', strtotime('+' . rand(-30, 30) . ' days')),
];
}
}
$stmt = $pdo->prepare("INSERT INTO invoices (customer_id, amount, status, due_date) VALUES (:customer_id, :amount, :status, :due_date)");
foreach ($invoices as $invoice) {
echo "Seeding invoice for customer ID: " . $invoice['customer_id'] . "...\n";
try {
$stmt->execute($invoice);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error seeding invoice: " . $e->getMessage() . "\n";
}
}
}
function seed_reports() {
$pdo = db();
$reports = [
[
'name' => 'Monthly Revenue',
'type' => 'Financial',
'data' => json_encode(['month' => 'October 2025', 'revenue' => 12345.67])
],
[
'name' => 'Active Customers',
'type' => 'User',
'data' => json_encode(['active_customers' => 123])
],
[
'name' => 'Plan Distribution',
'type' => 'Subscription',
'data' => json_encode(['Basic' => 50, 'Premium HD' => 50, 'Family Pack' => 23])
]
];
$stmt = $pdo->prepare("INSERT INTO reports (name, type, data) VALUES (:name, :type, :data)");
foreach ($reports as $report) {
echo "Seeding report: " . $report['name'] . "...\n";
try {
$stmt->execute($report);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error seeding report " . $report['name'] . ": " . $e->getMessage() . "\n";
}
}
}
seed_customers();
seed_plans();
seed_invoices();
seed_reports();