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. Post-Response Processing (Background) // Manually send response instead of json_success() because it calls exit; header('Content-Type: application/json; charset=utf-8'); http_response_code(200); echo json_encode([ 'success' => true, 'data' => [ 'batch_id' => $batchId, 'status' => 'processing', 'total_images' => $batch['total_images'] ], 'message' => 'تم إنهاء الدفعة بنجاح وبدء المعالجة الفورية', 'timestamp' => date('c') ], JSON_UNESCAPED_UNICODE); // This allows the mobile app to get an instant success message, // while the server continues processing the AI extraction in the background. if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } // Ensure the script keeps running ignore_user_abort(true); set_time_limit(300); // Give it 5 minutes $queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending'"); $queueStmt->execute([$batchId]); $items = $queueStmt->fetchAll(); foreach ($items as $item) { InvoiceProcessor::processQueueItem((int)$item['id']); } exit; // End here