Files
Siro/backend/test_add_driver_and_car.php
2026-06-26 00:40:48 +03:00

215 lines
8.8 KiB
PHP

<?php
/**
* test_add_driver_and_car.php
* ===========================
* يضيف سائق + سيارته في قاعدة البيانات مباشرة (لأغراض الاختبار).
* يستخدم نفس التشفير ونظام إدارة الهوية مثل الإنتاج.
*
* الاستخدام:
* https://example.com/backend/test_add_driver_and_car.php?phone=96279xxxxxxx&password=1234&first_name=Ahmed&last_name=Ali&make=Hyundai&model=Elantra&year=2020&car_plate=1234&color=White
*
* جميع الحقول الاختيارية لها قيمة افتراضية.
*/
require_once __DIR__ . '/core/bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
// دعم JSON body بالإضافة إلى POST/GET
$rawBody = file_get_contents('php://input');
if ($rawBody) {
$json = json_decode($rawBody, true);
if ($json) {
$_POST = array_merge($_POST, $json);
}
}
try {
/* ================== قراءة المدخلات ================== */
$phone = filterRequest('phone');
$password = filterRequest('password');
$first_name = filterRequest('first_name');
$last_name = filterRequest('last_name');
if (empty($phone) || empty($password) || empty($first_name) || empty($last_name)) {
jsonError('Required: phone, password, first_name, last_name');
exit;
}
// توحيد الرقم (إزالة +/مسافات)
$phone = preg_replace('/[ \-\(\)\+]/', '', $phone);
// حقول السائق الاختيارية
$email = filterRequest('email') ?: $phone . '@intaleqapp.com';
$gender = filterRequest('gender') ?: 'Male';
$national_number = filterRequest('national_number') ?: '';
$birthdate = filterRequest('birthdate') ?: '1990-01-01';
$site = filterRequest('site') ?: 'testing';
$license_type = filterRequest('license_type') ?: 'private';
$employmentType = filterRequest('employmentType') ?: 'full_time';
// حقول السيارة
$make = filterRequest('make') ?: 'Toyota';
$model = filterRequest('model') ?: 'Camry';
$year = filterRequest('year') ?: '2020';
$car_plate = filterRequest('car_plate') ?: 'TEST' . random_int(100, 999);
$vin = filterRequest('vin') ?: 'VIN' . bin2hex(random_bytes(8));
$color = filterRequest('color') ?: 'White';
$color_hex = filterRequest('color_hex') ?: '#FFFFFF';
$fuel = filterRequest('fuel') ?: 'Petrol';
$owner = filterRequest('owner') ?: trim($first_name . ' ' . $last_name);
$expiration_date = filterRequest('expiration_date') ?: date('Y-m-d', strtotime('+1 year'));
/* ================== ID السائق ================== */
$driverId = 'TEST' . date('YmdHis') . random_int(1000, 9999);
/* ================== التشفير ================== */
$encPhone = $encryptionHelper->encryptData($phone);
$encEmail = $encryptionHelper->encryptData($email);
$encFirstName = $encryptionHelper->encryptData($first_name);
$encLastName = $encryptionHelper->encryptData($last_name);
$encNameArabic = $encryptionHelper->encryptData("$first_name $last_name");
$encGender = $encryptionHelper->encryptData($gender);
$encNationalNumber = $national_number ? $encryptionHelper->encryptData($national_number) : '';
$encBirthdate = $encryptionHelper->encryptData($birthdate);
$encSite = $encryptionHelper->encryptData($site);
$encOwner = $encryptionHelper->encryptData($owner);
$encCarPlate = $encryptionHelper->encryptData($car_plate);
$encVin = $encryptionHelper->encryptData($vin);
$passwordHashed = password_hash($password, PASSWORD_DEFAULT);
$con = Database::get('main');
/* ================== التحقق من التكرار ================== */
$dup = $con->prepare("SELECT id FROM driver WHERE phone = :p OR email = :e");
$dup->execute([':p' => $encPhone, ':e' => $encEmail]);
if ($dup->rowCount() > 0) {
jsonError("Phone or email already registered.");
exit;
}
$con->beginTransaction();
/* ================== 1) إدراج السائق ================== */
$sqlDriver = "
INSERT INTO driver (
id, phone, email, password, gender, license_type, national_number,
name_arabic, issue_date, expiry_date, license_categories,
address, licenseIssueDate, status, birthdate, site,
first_name, last_name, accountBank, bankCode,
employmentType, maritalStatus, fullNameMaritial, expirationDate,
created_at, updated_at
) VALUES (
:id, :phone, :email, :pwd, :gender, :license_type, :national_number,
:name_arabic, :issue_date, :expiry_date, :license_categories,
:address, :licenseIssueDate, :status, :birthdate, :site,
:first_name, :last_name, :accountBank, :bankCode,
:employmentType, :maritalStatus, :fullNameMaritial, :expirationDate,
NOW(), NOW()
)
";
$insD = $con->prepare($sqlDriver);
$insD->execute([
':id' => $driverId,
':phone' => $encPhone,
':email' => $encEmail,
':pwd' => $passwordHashed,
':gender' => $encGender,
':license_type' => $license_type,
':national_number' => $encNationalNumber,
':name_arabic' => $encNameArabic,
':issue_date' => '2020-01-01',
':expiry_date' => '2030-01-01',
':license_categories' => 'B',
':address' => $encSite,
':licenseIssueDate' => '2020-01-01',
':status' => 'pending_review',
':birthdate' => $encBirthdate,
':site' => $encSite,
':first_name' => $encFirstName,
':last_name' => $encLastName,
':accountBank' => 'yet',
':bankCode' => 'CIB',
':employmentType' => $employmentType,
':maritalStatus' => 'Single',
':fullNameMaritial' => '',
':expirationDate' => date('Y-m-d', strtotime('+5 years')),
]);
/* ================== 2) إدراج السيارة ================== */
$sqlCar = "
INSERT INTO CarRegistration (
driverID, vin, car_plate, make, model, year, expiration_date,
color, owner, color_hex, fuel,
vehicle_category_id, fuel_type_id,
isDefault, created_at, status
) VALUES (
:driverID, :vin, :car_plate, :make, :model, :year, :expiration_date,
:color, :owner, :color_hex, :fuel,
:vehicle_category_id, :fuel_type_id,
:isDefault, NOW(), 'active'
)
";
$insC = $con->prepare($sqlCar);
$insC->execute([
':driverID' => $driverId,
':vin' => $encVin,
':car_plate' => $encCarPlate,
':make' => $make,
':model' => $model,
':year' => $year,
':expiration_date' => $expiration_date,
':color' => $color,
':owner' => $encOwner,
':color_hex' => $color_hex,
':fuel' => $fuel,
':vehicle_category_id' => 1,
':fuel_type_id' => 1,
':isDefault' => 1,
]);
$carRegID = $con->lastInsertId();
/* ================== 3) توكن السائق ================== */
$token = bin2hex(random_bytes(20));
$sqlToken = "
INSERT INTO driverToken (token, captain_id, fingerPrint, created_at)
VALUES (:token, :captain_id, :fingerPrint, NOW())
";
$con->prepare($sqlToken)->execute([
':token' => $token,
':captain_id' => $driverId,
':fingerPrint' => 'test_fingerprint',
]);
/* ================== 4) توثيق رقم الهاتف ================== */
$sqlPhoneVer = "
INSERT INTO phone_verification (phone_number, driverId, email, token_code, expiration_time, is_verified, created_at)
VALUES (:phone, :driverId, :email, :token_code, DATE_ADD(NOW(), INTERVAL 1 YEAR), 1, NOW())
";
$con->prepare($sqlPhoneVer)->execute([
':phone' => $encPhone,
':driverId' => $driverId,
':email' => $encEmail,
':token_code' => $encryptionHelper->encryptData('999'),
]);
/* ================== Commit ================== */
$con->commit();
printSuccess([
'driverID' => $driverId,
'carRegID' => $carRegID,
'status' => 'success',
'message' => "Driver $first_name $last_name created successfully with status pending_review.",
]);
} catch (Exception $e) {
if (isset($con) && $con instanceof PDO && $con->inTransaction()) {
$con->rollBack();
}
error_log("[test_add_driver] " . $e->getMessage());
jsonError($e->getMessage());
}