clinicRepo->findByUser($user); if ($clinic === null) { return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'کلینیک یافت نشد', 404); } $clinicId = $clinic->getId(); $todayStart = strtotime('today midnight'); $todayEnd = strtotime('tomorrow midnight') - 1; $monthStart = strtotime('first day of this month midnight'); $from = $request->query->get('from') ? (int) $request->query->get('from') : $monthStart; $to = $request->query->get('to') ? (int) $request->query->get('to') : time(); // آمار نوبت‌های امروز و این ماه $stats = $this->em->createQuery(' SELECT COUNT(a.id) AS today_appointments, SUM(CASE WHEN a.slotStart >= :monthStart THEN 1 ELSE 0 END) AS this_month_appointments FROM App\Appointment\Entity\Appointment a JOIN App\Doctor\Entity\Doctor d WITH a.doctor = d JOIN App\Clinic\Entity\Clinic c WITH d MEMBER OF c.doctors WHERE c.id = :clinicId AND a.slotStart >= :todayStart AND a.slotStart <= :todayEnd ')->setParameters([ 'clinicId' => $clinicId, 'todayStart' => $todayStart, 'todayEnd' => $todayEnd, 'monthStart' => $monthStart, ])->getOneOrNullResult() ?? []; // شمارش کل نوبت‌های این ماه (query جداگانه) $monthCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a JOIN App\Doctor\Entity\Doctor d WITH a.doctor = d JOIN App\Clinic\Entity\Clinic c WITH d MEMBER OF c.doctors WHERE c.id = :clinicId AND a.slotStart >= :monthStart ')->setParameters([ 'clinicId' => $clinicId, 'monthStart' => $monthStart, ])->getSingleScalarResult(); // تعداد دعوتنامه‌های در انتظار $pendingInvitations = (int) $this->em->createQuery(' SELECT COUNT(i.id) FROM App\ClinicInvitation\Entity\ClinicDoctorInvitation i WHERE i.clinic = :clinic AND i.status = :status ')->setParameters([ 'clinic' => $clinic, 'status' => 'pending', ])->getSingleScalarResult(); // ۵ نوبت امروز این کلینیک $todayAppts = $this->em->createQuery(' SELECT a.uuid, u.realName AS patient_name, d.name AS doctor_name, a.slotStart AS slot_start, a.status FROM App\Appointment\Entity\Appointment a JOIN a.doctor d JOIN a.user u JOIN App\Clinic\Entity\Clinic c WITH d MEMBER OF c.doctors WHERE c.id = :clinicId AND a.slotStart >= :todayStart AND a.slotStart <= :todayEnd ORDER BY a.slotStart ASC ')->setMaxResults(5)->setParameters([ 'clinicId' => $clinicId, 'todayStart' => $todayStart, 'todayEnd' => $todayEnd, ])->getArrayResult(); // لیست پزشکان با شمارش نوبت امروز $doctors = $this->em->createQuery(' SELECT d.uuid, d.name, COUNT(a.id) AS today_count FROM App\Doctor\Entity\Doctor d JOIN App\Clinic\Entity\Clinic c WITH d MEMBER OF c.doctors LEFT JOIN App\Appointment\Entity\Appointment a WITH a.doctor = d AND a.slotStart >= :todayStart AND a.slotStart <= :todayEnd WHERE c.id = :clinicId GROUP BY d.id ')->setParameters([ 'clinicId' => $clinicId, 'todayStart' => $todayStart, 'todayEnd' => $todayEnd, ])->getArrayResult(); $smsBalance = $this->smsWalletService->getBalance('clinic', $clinicId); $uniquePatients = $this->patientRecordRepo->countUnique('clinic', $clinicId, $from, $to); $revenuePeriod = $this->patientSessionRepo->sumRevenue('clinic', $clinicId, $from, $to); return $this->success([ 'clinic' => [ 'uuid' => $clinic->getUuid(), 'name' => $clinic->getName(), 'is_active' => $clinic->isActive(), 'logo' => $clinic->getClinicLogo(), ], 'stats' => [ 'total_doctors' => count($doctors), 'today_appointments' => (int) ($stats['today_appointments'] ?? 0), 'this_month_appointments' => $monthCount, 'pending_invitations' => $pendingInvitations, 'sms_wallet_balance' => $smsBalance, 'unique_patients_count' => $uniquePatients, 'revenue_period_rials' => $revenuePeriod, ], 'period' => ['from' => $from, 'to' => $to], 'today_appointments' => $todayAppts, 'doctors' => $doctors, ]); } // ── Doctor Dashboard ───────────────────────────────────────────────────── #[Route('/api/v1/dashboard/doctor', methods: ['GET'])] #[IsGranted('ROLE_DOCTOR')] public function doctor(Request $request, #[CurrentUser] User $user): JsonResponse { $doctor = $this->doctorRepo->findByUser($user); if ($doctor === null) { return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'دکتر یافت نشد', 404); } $doctorId = $doctor->getId(); $todayStart = strtotime('today midnight'); $todayEnd = strtotime('tomorrow midnight') - 1; $tmrStart = strtotime('tomorrow midnight'); $tmrEnd = strtotime('tomorrow midnight') + 86399; $monthStart = strtotime('first day of this month midnight'); $from = $request->query->get('from') ? (int) $request->query->get('from') : $monthStart; $to = $request->query->get('to') ? (int) $request->query->get('to') : time(); // آمار $todayCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctor' => $doctor, 's' => $todayStart, 'e' => $todayEnd]) ->getSingleScalarResult(); $tmrCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctor' => $doctor, 's' => $tmrStart, 'e' => $tmrEnd]) ->getSingleScalarResult(); $monthCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :doctor AND a.slotStart >= :s ')->setParameters(['doctor' => $doctor, 's' => $monthStart]) ->getSingleScalarResult(); // میانگین و تعداد امتیاز $ratingRow = $this->em->createQuery(' SELECT AVG((r.waitingTimeAtClinic + r.accuracyOfDiagnosis + r.doctorBehavior + r.clinicCleanliness + r.doctorExpertise) / 5.0) AS avg_score, COUNT(r.id) AS total FROM App\Rating\Entity\Rate r WHERE r.doctor = :doctor ')->setParameter('doctor', $doctor)->getOneOrNullResult() ?? []; // نوبت‌های امروز $todayAppts = $this->em->createQuery(' SELECT a.uuid, u.realName AS patient_name, u.mobileNumber AS patient_mobile, a.slotStart AS slot_start, a.status FROM App\Appointment\Entity\Appointment a JOIN a.user u WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ORDER BY a.slotStart ASC ')->setMaxResults(10)->setParameters([ 'doctor' => $doctor, 's' => $todayStart, 'e' => $todayEnd, ])->getArrayResult(); // کلینیک‌های عضو $clinics = $this->em->createQuery(' SELECT c.uuid, c.name, c.clinicLogo AS logo FROM App\Clinic\Entity\Clinic c JOIN c.doctors d WHERE d.id = :doctorId ')->setParameter('doctorId', $doctorId)->getArrayResult(); $smsBalance = $this->smsWalletService->getBalance('doctor', $doctorId); $uniquePatients = $this->patientRecordRepo->countUnique('doctor', $doctorId, $from, $to); $revenuePeriod = $this->patientSessionRepo->sumRevenue('doctor', $doctorId, $from, $to); return $this->success([ 'doctor' => [ 'uuid' => $doctor->getUuid(), 'name' => $doctor->getName(), 'degree' => $doctor->getDegree(), ], 'stats' => [ 'today_appointments' => $todayCount, 'tomorrow_appointments' => $tmrCount, 'this_month_appointments' => $monthCount, 'avg_rating' => $ratingRow['avg_score'] ? round((float) $ratingRow['avg_score'], 1) : null, 'total_ratings' => (int) ($ratingRow['total'] ?? 0), 'sms_wallet_balance' => $smsBalance, 'unique_patients_count' => $uniquePatients, 'revenue_period_rials' => $revenuePeriod, ], 'period' => ['from' => $from, 'to' => $to], 'today_appointments' => $todayAppts, 'clinics' => $clinics, ]); } // ── Secretary Dashboard ────────────────────────────────────────────────── #[Route('/api/v1/dashboard/secretary', methods: ['GET'])] #[IsGranted('ROLE_SECRETARY')] public function secretary(#[CurrentUser] User $user): JsonResponse { $activeCtx = $this->contextRepo->findByUser($user); $dbUuid = $activeCtx?->getDbUuid(); if ($dbUuid === null) { return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'دسترسی منشی تنظیم نشده', 403); } // تعیین scope بر اساس db_uuid فعال $clinic = $this->clinicRepo->findByUuid($dbUuid); if ($clinic !== null) { return $this->secretaryClinicDashboard($user, $clinic); } $doctor = $this->doctorRepo->findByUuid($dbUuid); if ($doctor !== null) { $rel = $this->secretaryRepo->findActiveBySecretaryForDoctor($user, $doctor); if ($rel === null) { return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'دسترسی منشی تنظیم نشده', 403); } return $this->secretaryDoctorDashboard($user, $rel); } return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'context نامعتبر است', 403); } private function secretaryDoctorDashboard(User $user, DoctorSecretary $rel): JsonResponse { $doctor = $rel->getDoctor(); $permissions = $rel->getPermissions(); $canView = (bool) ($permissions['resources']['appointments']['view'] ?? false); $todayStart = strtotime('today midnight'); $todayEnd = strtotime('tomorrow midnight') - 1; $tmrStart = strtotime('tomorrow midnight'); $tmrEnd = strtotime('tomorrow midnight') + 86399; $todayCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctor' => $doctor, 's' => $todayStart, 'e' => $todayEnd]) ->getSingleScalarResult(); $tmrCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctor' => $doctor, 's' => $tmrStart, 'e' => $tmrEnd]) ->getSingleScalarResult(); $todayAppts = []; if ($canView) { $todayAppts = $this->em->createQuery(' SELECT a.uuid, u.realName AS patient_name, u.mobileNumber AS patient_mobile, a.slotStart AS slot_start, a.status FROM App\Appointment\Entity\Appointment a JOIN a.user u WHERE a.doctor = :doctor AND a.slotStart >= :s AND a.slotStart <= :e ORDER BY a.slotStart ASC ')->setMaxResults(10)->setParameters([ 'doctor' => $doctor, 's' => $todayStart, 'e' => $todayEnd, ])->getArrayResult(); } return $this->success([ 'scope' => 'doctor', 'doctor' => [ 'uuid' => $doctor->getUuid(), 'name' => $doctor->getName(), 'degree' => $doctor->getDegree(), ], 'permissions' => $permissions, 'stats' => [ 'today_appointments' => $todayCount, 'tomorrow_appointments' => $tmrCount, ], 'today_appointments' => $todayAppts, ]); } private function secretaryClinicDashboard(User $user, \App\Clinic\Entity\Clinic $clinic): JsonResponse { $rel = $this->secretaryRepo->findActiveBySecretaryForClinic($user, $clinic); if ($rel === null) { return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'دسترسی منشی تنظیم نشده', 403); } $permissions = $rel->getPermissions(); $canView = (bool) ($permissions['resources']['appointments']['view'] ?? false); $todayStart = strtotime('today midnight'); $todayEnd = strtotime('tomorrow midnight') - 1; $tmrStart = strtotime('tomorrow midnight'); $tmrEnd = strtotime('tomorrow midnight') + 86399; $doctors = $this->secretaryRepo->findDoctorsBySecretaryInClinic($user, $clinic); $todayCount = 0; $tmrCount = 0; $todayAppts = []; if (!empty($doctors)) { $todayCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor IN (:doctors) AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctors' => $doctors, 's' => $todayStart, 'e' => $todayEnd]) ->getSingleScalarResult(); $tmrCount = (int) $this->em->createQuery(' SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor IN (:doctors) AND a.slotStart >= :s AND a.slotStart <= :e ')->setParameters(['doctors' => $doctors, 's' => $tmrStart, 'e' => $tmrEnd]) ->getSingleScalarResult(); if ($canView) { $todayAppts = $this->em->createQuery(' SELECT a.uuid, u.realName AS patient_name, u.mobileNumber AS patient_mobile, a.slotStart AS slot_start, a.status, d.name AS doctor_name FROM App\Appointment\Entity\Appointment a JOIN a.user u JOIN a.doctor d WHERE a.doctor IN (:doctors) AND a.slotStart >= :s AND a.slotStart <= :e ORDER BY a.slotStart ASC ')->setMaxResults(20)->setParameters([ 'doctors' => $doctors, 's' => $todayStart, 'e' => $todayEnd, ])->getArrayResult(); } } return $this->success([ 'scope' => 'clinic', 'clinic' => [ 'uuid' => $clinic->getUuid(), 'name' => $clinic->getName(), ], 'permissions' => $permissions, 'stats' => [ 'today_appointments' => $todayCount, 'tomorrow_appointments' => $tmrCount, ], 'today_appointments' => $todayAppts, ]); } }