106 lines
3.8 KiB
PHP
106 lines
3.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/config.php';
|
|
require_once __DIR__ . '/includes/Security.php';
|
|
|
|
echo "=== اختبار نظام إدارة الطلبات ===\n\n";
|
|
|
|
try {
|
|
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
|
|
|
|
if ($conn->connect_error) {
|
|
throw new Exception("فشل الاتصال: " . $conn->connect_error);
|
|
}
|
|
|
|
// إنشاء مستخدم للاختبار
|
|
$stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
|
|
$username = "test_user_" . time(); // اسم مستخدم فريد
|
|
$email = "test_" . time() . "@example.com";
|
|
$password = Security::hashPassword("test123");
|
|
|
|
$stmt->bind_param("sss", $username, $email, $password);
|
|
$stmt->execute();
|
|
$user_id = $conn->insert_id;
|
|
|
|
echo "تم إنشاء مستخدم للاختبار (ID: $user_id)\n";
|
|
|
|
// إنشاء منتج للاختبار
|
|
$stmt = $conn->prepare("INSERT INTO products (name, description, price, stock) VALUES (?, ?, ?, ?)");
|
|
$name = "زيت الورد الطبيعي";
|
|
$description = "زيت ورد طبيعي 100% للعناية بالبشرة";
|
|
$price = 199.99;
|
|
$stock = 30;
|
|
|
|
$stmt->bind_param("ssdi", $name, $description, $price, $stock);
|
|
$stmt->execute();
|
|
$product_id = $conn->insert_id;
|
|
|
|
echo "تم إنشاء منتج للاختبار (ID: $product_id)\n";
|
|
|
|
// إنشاء طلب جديد
|
|
$stmt = $conn->prepare("INSERT INTO orders (user_id, total_amount, status) VALUES (?, ?, ?)");
|
|
$total = 199.99;
|
|
$status = 'pending';
|
|
|
|
$stmt->bind_param("ids", $user_id, $total, $status);
|
|
$stmt->execute();
|
|
$order_id = $conn->insert_id;
|
|
|
|
echo "تم إنشاء طلب جديد (ID: $order_id)\n";
|
|
|
|
// إضافة عناصر الطلب
|
|
$stmt = $conn->prepare("INSERT INTO order_items (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
|
|
$quantity = 1;
|
|
|
|
$stmt->bind_param("iiid", $order_id, $product_id, $quantity, $price);
|
|
$stmt->execute();
|
|
|
|
echo "تم إضافة عناصر الطلب\n";
|
|
|
|
// استرجاع معلومات الطلب
|
|
$query = "
|
|
SELECT o.*, oi.quantity, oi.price as item_price, p.name as product_name, u.username
|
|
FROM orders o
|
|
JOIN order_items oi ON o.id = oi.order_id
|
|
JOIN products p ON oi.product_id = p.id
|
|
JOIN users u ON o.user_id = u.id
|
|
WHERE o.id = ?
|
|
";
|
|
|
|
$stmt = $conn->prepare($query);
|
|
$stmt->bind_param("i", $order_id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$order = $result->fetch_assoc();
|
|
|
|
echo "\nتفاصيل الطلب:\n";
|
|
echo "رقم الطلب: " . $order['id'] . "\n";
|
|
echo "المستخدم: " . $order['username'] . "\n";
|
|
echo "المنتج: " . $order['product_name'] . "\n";
|
|
echo "الكمية: " . $order['quantity'] . "\n";
|
|
echo "السعر: " . $order['item_price'] . " جنيه\n";
|
|
echo "الإجمالي: " . $order['total_amount'] . " جنيه\n";
|
|
echo "الحالة: " . $order['status'] . "\n";
|
|
|
|
// تحديث حالة الطلب
|
|
$stmt = $conn->prepare("UPDATE orders SET status = ? WHERE id = ?");
|
|
$new_status = 'delivered';
|
|
|
|
$stmt->bind_param("si", $new_status, $order_id);
|
|
$stmt->execute();
|
|
|
|
echo "\nتم تحديث حالة الطلب إلى: delivered\n";
|
|
|
|
// تنظيف البيانات
|
|
$conn->query("DELETE FROM order_items WHERE order_id = $order_id");
|
|
$conn->query("DELETE FROM orders WHERE id = $order_id");
|
|
$conn->query("DELETE FROM products WHERE id = $product_id");
|
|
$conn->query("DELETE FROM users WHERE id = $user_id");
|
|
|
|
echo "\nتم تنظيف بيانات الاختبار بنجاح\n";
|
|
|
|
$conn->close();
|
|
|
|
} catch (Exception $e) {
|
|
echo "خطأ: " . $e->getMessage() . "\n";
|
|
}
|