prepare($accessQuery); $stmt->execute($accessParams); $company = $stmt->fetch(); if (!$company) { json_error('الشركة غير موجودة أو ليس لديك صلاحية', 404); } $companyName = Encryption::decrypt($company['name']) ?: $company['name']; try { // 1. Gather last 3 months of data $months = []; for ($i = 0; $i < 3; $i++) { $m = date('m', strtotime("-{$i} months")); $y = date('Y', strtotime("-{$i} months")); $stmt = $db->prepare(" SELECT COUNT(*) as total_invoices, COALESCE(SUM(grand_total), 0) as revenue, COALESCE(SUM(tax_amount), 0) as tax, COALESCE(SUM(discount_total), 0) as discounts, COALESCE(AVG(grand_total), 0) as avg_invoice, SUM(CASE WHEN status = 'submitted' THEN 1 ELSE 0 END) as submitted_count, SUM(CASE WHEN status = 'extracted' THEN 1 ELSE 0 END) as pending_count FROM invoices WHERE company_id = ? AND MONTH(created_at) = ? AND YEAR(created_at) = ? "); $stmt->execute([$companyId, $m, $y]); $data = $stmt->fetch(); $data['month'] = (int)$m; $data['year'] = (int)$y; $months[] = $data; } // 2. Pending invoices count $pendingStmt = $db->prepare("SELECT COUNT(*) FROM invoices WHERE company_id = ? AND status = 'extracted'"); $pendingStmt->execute([$companyId]); $pendingCount = (int)$pendingStmt->fetchColumn(); // 3. Build AI prompt $dataJson = json_encode([ 'company_name' => $companyName, 'tin' => $company['tax_identification_number'], 'monthly_data' => $months, 'pending_invoices' => $pendingCount, ], JSON_UNESCAPED_UNICODE); $prompt = << 0) $score += 2; if (($currentMonth['submitted_count'] ?? 0) > 0) $score += 1; if ($pendingCount === 0) $score += 1; if (($currentMonth['revenue'] ?? 0) > ($prevMonth['revenue'] ?? 0)) $score += 1; $report = [ 'health_score' => min(10, $score), 'health_label' => $score >= 8 ? 'ممتاز' : ($score >= 6 ? 'جيد' : 'متوسط'), 'summary' => 'تقرير مبني على البيانات المتوفرة بدون تحليل AI.', 'insights' => ['عدد الفواتير: ' . ($currentMonth['total_invoices'] ?? 0)], 'warnings' => $pendingCount > 0 ? ["يوجد {$pendingCount} فاتورة بانتظار المراجعة"] : [], 'recommendations' => ['تأكد من إرسال جميع الفواتير المعتمدة لجوفوترا'], ]; } json_success([ 'company_id' => $companyId, 'company_name' => $companyName, 'report' => $report, 'data' => [ 'monthly_summary' => $months, 'pending_count' => $pendingCount, ], 'generated_at' => date('c'), ], 'تقرير صحة الشركة'); } catch (\Exception $e) { safe_error($e, 'reports/company-health', 'حدث خطأ في إنشاء التقرير.'); }