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()); }