prepare(" SELECT id, tenant_id, status, total_images FROM invoice_batches WHERE id = ? AND uploaded_by = ? "); $stmt->execute([$batchId, $userId]); $batch = $stmt->fetch(); if (!$batch || ($decoded['role'] !== 'super_admin' && $batch['tenant_id'] !== $tenantId)) { json_error('الدفعة غير موجودة', 404); } if ($batch['status'] !== 'uploading') { json_error('تم إنهاء هذه الدفعة مسبقاً', 400); } if ($batch['total_images'] == 0) { json_error('لا يمكن إنهاء دفعة فارغة', 400); } // 2. Mark as processing $stmt = $db->prepare(" UPDATE invoice_batches SET status = 'processing', updated_at = NOW() WHERE id = ? "); $stmt->execute([$batchId]); // 3. Trigger the worker via a "Fire and Forget" HTTP request // This ensures processing starts immediately but the mobile app doesn't timeout. $workerUrl = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/api/v1/batches/process-worker'; $postData = json_encode(['batch_id' => $batchId]); $ch = curl_init($workerUrl); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Only wait 1 second curl_setopt($ch, CURLOPT_NOSIGNAL, 1); @curl_exec($ch); curl_close($ch); json_success([ 'batch_id' => $batchId, 'status' => 'processing', 'total_images' => $batch['total_images'] ], 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية');