/** * seed_data.php * Populates the database with comprehensive demo data. * Access via: http://localhost/warung-app/backend/api/seed_data.php */ // Mock REQUEST_METHOD for CLI compatibility if (php_sapi_name() === 'cli' && !isset($_SERVER['REQUEST_METHOD'])) { $_SERVER['REQUEST_METHOD'] = 'GET'; } require_once 'db.php'; // Safe mode: only run if requested via ?run=true or if run from CLI if (php_sapi_name() !== 'cli' && ($_GET['run'] ?? '') !== 'true') { die(json_encode([ "status" => "info", "message" => "To confirm seeding, append ?run=true to the URL. CAUTION: This will clear existing data in some tables." ])); } // Disable foreign key checks for clearing data $conn->query("SET FOREIGN_KEY_CHECKS = 0"); $tables_to_clear = [ 'order_items', 'orders', 'cart_items', 'user_favorites', 'user_addresses', 'notifications', 'products', 'kategori', 'warung_verifications', 'vouchers', 'voucher_batches', 'ticket_messages', 'support_tickets', 'broadcasts', 'system_audit_log', 'warung', 'users', 'akun_warung' ]; foreach ($tables_to_clear as $t) { if (!$conn->query("TRUNCATE TABLE $t")) { // Fallback to DELETE if TRUNCATE fails (due to FK) $conn->query("DELETE FROM $t"); } } $conn->query("SET FOREIGN_KEY_CHECKS = 1"); $results = []; // 1. Seed Categories $categories = ['Makanan', 'Minuman', 'Sembako', 'Elektronik', 'Kebutuhan Rumah']; foreach ($categories as $cat) { $conn->query("INSERT INTO kategori (nama_kategori) VALUES ('$cat')"); } $results[] = "Categories seeded."; // 2. Seed Owners & Warung $password = password_hash('warung123', PASSWORD_DEFAULT); $owners = [ ['Siti Aminah', '081234567890', 'Warung Bu Siti', 'warung-bu-siti', 'Toko kelontong lengkap dan murah', 'Jakarta', 'Jl. Sudirman No. 1'], ['Budi Santoso', '081234567891', 'Warung Berkah Budi', 'warung-berkah-budi', 'Sedia sembako dan gas LPG', 'Bandung', 'Jl. Asia Afrika No. 42'], ['Dewi Lestari', '081234567892', 'Warung Sejahtera', 'warung-sejahtera', 'Minuman segar dan snack', 'Surabaya', 'Jl. Tunjungan No. 10'] ]; foreach ($owners as $o) { $conn->query("INSERT INTO akun_warung (nama_lengkap, no_hp, password_hash) VALUES ('$o[0]', '$o[1]', '$password')"); $owner_id = $conn->insert_id; $conn->query("INSERT INTO warung (owner_id, nama_warung, slug, deskripsi, kota, alamat, telepon, foto_url, is_active) VALUES ($owner_id, '$o[2]', '$o[3]', '$o[4]', '$o[5]', '$o[6]', '$o[1]', 'https://plus.unsplash.com/premium_photo-1664115403213-94c39712169b?w=500&auto=format&fit=crop&q=60', 1)"); // Seed Verification for each warung $conn->query("INSERT INTO warung_verifications (warung_id, status, notes) VALUES ($conn->insert_id, 'approved', 'Verified via seeding')"); } $results[] = "Owners, Warungs, and Verifications seeded."; // 3. Seed Customers $customers = [ ['User Demo 1', '089988877701'], ['User Demo 2', '089988877702'], ['User Demo 3', '089988877703'], ['User Demo 4', '089988877704'], ['User Demo 5', '089988877705'] ]; foreach ($customers as $c) { // Current users table might still have 'role' column, let's keep it 'customer' $conn->query("INSERT INTO users (nama_lengkap, no_hp, password_hash) VALUES ('$c[0]', '$c[1]', '$password')"); $user_id = $conn->insert_id; // Seed Address $conn->query("INSERT INTO user_addresses (user_id, label, address, is_primary) VALUES ($user_id, 'Rumah', 'Alamat Demo No. ' . $user_id, 1)"); // Seed Notification $conn->query("INSERT INTO notifications (user_id, title, message, type) VALUES ($user_id, 'Selamat Datang!', 'Terima kasih telah bergabung di Lentera Warung.', 'system')"); } $results[] = "Customers, Addresses, and Notifications seeded."; // 4. Seed Products (Global or Linked to category) $products = [ [1, 'Nasi Goreng Spesial', 'NGS001', 12000, 15000, 50, 10, 'https://images.unsplash.com/photo-1603133872871-0c9a29586161?w=500&auto=format&fit=crop&q=60'], [1, 'Mie Ayam Bakso', 'MAB001', 10000, 13000, 40, 5, 'https://images.unsplash.com/photo-1593593922896-8041c96d75c0?w=500&auto=format&fit=crop&q=60'], [2, 'Es Teh Manis', 'ETM001', 2000, 3500, 100, 20, 'https://images.unsplash.com/photo-1556679343-c7306c1976bc?w=500&auto=format&fit=crop&q=60'], [2, 'Kopi Susu Gula Aren', 'KSG001', 5000, 8000, 80, 15, 'https://images.unsplash.com/photo-1541167760496-162955ed8a9f?w=500&auto=format&fit=crop&q=60'], [3, 'Beras Ramos 5kg', 'BR5KG', 55000, 62000, 20, 5, 'https://images.unsplash.com/photo-1586201375761-83865001e31c?w=500&auto=format&fit=crop&q=60'], [3, 'Minyak Goreng 2L', 'MG2L', 28000, 32000, 30, 10, 'https://images.unsplash.com/photo-1474979266404-7eaacbcd87c5?w=500&auto=format&fit=crop&q=60'], [4, 'Lampu LED 10W', 'LED10W', 15000, 19500, 15, 3, 'https://images.unsplash.com/photo-1550684848-fac1c5b4e853?w=500&auto=format&fit=crop&q=60'], [5, 'Sabun Cuci Piring 800ml', 'SCP800', 12000, 14500, 25, 5, 'https://images.unsplash.com/photo-1583947581924-860bda6a26df?w=500&auto=format&fit=crop&q=60'] ]; foreach ($products as $p) { $conn->query("INSERT INTO products (kategori_id, nama, barcode, harga_modal, harga_jual, stok, min_stok, foto_url) VALUES ($p[0], '$p[1]', '$p[2]', $p[3], $p[4], $p[5], $p[6], '$p[7]')"); } $results[] = "Products seeded."; // 5. Seed Orders $statuses = ['baru', 'diproses', 'dikirim', 'selesai', 'batal']; for ($i = 1; $i <= 5; $i++) { $customer_id = $i; $status = $statuses[array_rand($statuses)]; $total = 0; $conn->query("INSERT INTO orders (customer_id, status, total_harga, catatan_pesanan) VALUES ($customer_id, '$status', 0, 'Pesanan demo #$i')"); $order_id = $conn->insert_id; // Add 1-2 items per order $item_count = rand(1, 2); for ($j = 0; $j < $item_count; $j++) { $product_id = rand(1, 8); $qty = rand(1, 3); // Fetch price $res = $conn->query("SELECT harga_jual FROM products WHERE id = $product_id"); $prod = $res->fetch_assoc(); $price = $prod['harga_jual']; $subtotal = $price * $qty; $total += $subtotal; $conn->query("INSERT INTO order_items (order_id, product_id, jumlah, harga_satuan, subtotal) VALUES ($order_id, $product_id, $qty, $price, $subtotal)"); } // Update total order $conn->query("UPDATE orders SET total_harga = $total WHERE id = $order_id"); } $results[] = "Orders and Order Items seeded."; // 6. Seed Support Tickets $subjects = ['Tanya promo', 'Komplain barang rusak', 'Lupa password', 'Warung tidak buka']; for ($i = 1; $i <= 3; $i++) { $sub = $subjects[array_rand($subjects)]; $conn->query("INSERT INTO support_tickets (user_id, subject, status, priority) VALUES ($i, '$sub', 'open', 'medium')"); $ticket_id = $conn->insert_id; $conn->query("INSERT INTO ticket_messages (ticket_id, sender_id, message, is_admin_reply) VALUES ($ticket_id, $i, 'Halo, saya ingin bertanya tentang $sub.', 0)"); } $results[] = "Support Tickets seeded."; // 7. Seed Vouchers $conn->query("INSERT INTO voucher_batches (prefix, duration_days, count, notes) VALUES ('PROMO2024', 30, 10, 'Seeded Promo Batch')"); $batch_id = $conn->insert_id; for ($i = 1; $i <= 5; $i++) { $code = 'PROMO2024-' . str_pad($i, 3, '0', STR_PAD_LEFT); $conn->query("INSERT INTO vouchers (batch_id, code, is_redeemed) VALUES ($batch_id, '$code', 0)"); } $results[] = "Voucher Batch and Codes seeded."; // 8. Seed Audit Log $conn->query("INSERT INTO system_audit_log (user_id, action, target, details) VALUES (1, 'DATABASE_SEED', 'System', 'Database seeded with demo data')"); $results[] = "Final audit log created."; header('Content-Type: application/json'); echo json_encode([ "status" => "success", "message" => "Database seeded successfully!", "details" => $results ], JSON_PRETTY_PRINT); $conn->close(); ?>