prepare(" SELECT id, status, total_images FROM invoice_batches WHERE id = ? AND tenant_id = ? AND uploaded_by = ? "); $stmt->execute([$batchId, $tenantId, $userId]); $batch = $stmt->fetch(); if (!$batch) { 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]); // In a real production environment, you would dispatch a job to a queue worker here. // For now, the queue worker is a cron job that checks the `invoice_processing_queue` table. json_success([ 'batch_id' => $batchId, 'status' => 'processing', 'total_images' => $batch['total_images'] ], 'تم إنهاء الدفعة بنجاح وإرسالها للمعالجة');