Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1068,18 +1068,91 @@ class AdminApiController extends BaseController
|
||||
->where('s.status = :s')->setParameter('s', Settlement::STATUS_PENDING)
|
||||
->getQuery()->getSingleScalarResult();
|
||||
|
||||
$monthStart = mktime(0, 0, 0, (int) date('n'), 1);
|
||||
$monthEnd = time();
|
||||
|
||||
$thisMonthRevenue = (int) $em->createQueryBuilder()
|
||||
->select('COALESCE(SUM(p.amountRials), 0)')
|
||||
->from(Payment::class, 'p')
|
||||
->where('p.status = :s AND p.createdAt BETWEEN :ts AND :te')
|
||||
->setParameter('s', Payment::STATUS_SUCCESS)
|
||||
->setParameter('ts', $monthStart)->setParameter('te', $monthEnd)
|
||||
->getQuery()->getSingleScalarResult();
|
||||
|
||||
$thisMonthAppointments = (int) $em->createQueryBuilder()
|
||||
->select('COUNT(a.id)')
|
||||
->from(Appointment::class, 'a')
|
||||
->where('a.createdAt BETWEEN :s AND :e')
|
||||
->setParameter('s', $monthStart)->setParameter('e', $monthEnd)
|
||||
->getQuery()->getSingleScalarResult();
|
||||
|
||||
return $this->success([
|
||||
'total_users' => $totalUsers,
|
||||
'active_doctors' => $activeDoctors,
|
||||
'total_doctors' => $totalDoctors,
|
||||
'total_clinics' => $totalClinics,
|
||||
'today_appointments' => $todayAppointments,
|
||||
'total_appointments' => $totalAppointments,
|
||||
'today_payments_count' => (int) $todayPaymentsResult['cnt'],
|
||||
'today_payments_amount' => (int) $todayPaymentsResult['total'],
|
||||
'total_payments_amount' => (int) $totalPaymentsResult['total'],
|
||||
'pending_comments' => $pendingComments,
|
||||
'pending_settlements' => $pendingSettlements,
|
||||
'total_users' => $totalUsers,
|
||||
'active_doctors' => $activeDoctors,
|
||||
'total_doctors' => $totalDoctors,
|
||||
'total_clinics' => $totalClinics,
|
||||
'today_appointments' => $todayAppointments,
|
||||
'total_appointments' => $totalAppointments,
|
||||
'today_payments_count' => (int) $todayPaymentsResult['cnt'],
|
||||
'today_payments_amount' => (int) $todayPaymentsResult['total'],
|
||||
'total_payments_amount' => (int) $totalPaymentsResult['total'],
|
||||
'pending_comments' => $pendingComments,
|
||||
'pending_settlements' => $pendingSettlements,
|
||||
'this_month_revenue' => $thisMonthRevenue,
|
||||
'this_month_appointments' => $thisMonthAppointments,
|
||||
]);
|
||||
}
|
||||
|
||||
// ── Dashboard Charts ──────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/dashboard/charts', methods: ['GET'])]
|
||||
public function dashboardCharts(): JsonResponse
|
||||
{
|
||||
$conn = $this->em->getConnection();
|
||||
$days = 30;
|
||||
$now = time();
|
||||
$start = $now - ($days * 86400);
|
||||
|
||||
$apptRows = $conn->fetchAllAssociative(
|
||||
'SELECT DATE(FROM_UNIXTIME(slot_start)) AS d, COUNT(*) AS cnt
|
||||
FROM appointments WHERE slot_start >= :s GROUP BY d ORDER BY d',
|
||||
['s' => $start]
|
||||
);
|
||||
$apptMap = array_column($apptRows, 'cnt', 'd');
|
||||
|
||||
$revRows = $conn->fetchAllAssociative(
|
||||
'SELECT DATE(FROM_UNIXTIME(created_at)) AS d, COALESCE(SUM(amount_rials), 0) AS total
|
||||
FROM payments WHERE status = :st AND created_at >= :s GROUP BY d ORDER BY d',
|
||||
['st' => 'received', 's' => $start]
|
||||
);
|
||||
$revMap = array_column($revRows, 'total', 'd');
|
||||
|
||||
$appt30d = [];
|
||||
$rev30d = [];
|
||||
for ($i = $days - 1; $i >= 0; $i--) {
|
||||
$date = date('Y-m-d', $now - $i * 86400);
|
||||
$shortDate = date('m/d', $now - $i * 86400);
|
||||
$appt30d[] = ['date' => $shortDate, 'count' => (int)($apptMap[$date] ?? 0)];
|
||||
$rev30d[] = ['date' => $shortDate, 'amount' => (int)($revMap[$date] ?? 0)];
|
||||
}
|
||||
|
||||
$statusRows = $conn->fetchAllAssociative(
|
||||
'SELECT status, COUNT(*) AS cnt FROM appointments GROUP BY status ORDER BY cnt DESC'
|
||||
);
|
||||
|
||||
$specRows = $conn->fetchAllAssociative(
|
||||
'SELECT s.name, COUNT(a.id) AS cnt
|
||||
FROM appointments a
|
||||
JOIN doctor_specialties ds ON ds.doctor_id = a.doctor_id
|
||||
JOIN specialties s ON s.id = ds.specialty_id
|
||||
GROUP BY s.id, s.name ORDER BY cnt DESC LIMIT 8'
|
||||
);
|
||||
|
||||
return $this->success([
|
||||
'appointments_30d' => $appt30d,
|
||||
'revenue_30d' => $rev30d,
|
||||
'appointment_status' => array_map(fn($r) => ['status' => $r['status'], 'count' => (int) $r['cnt']], $statusRows),
|
||||
'top_specialties' => array_map(fn($r) => ['name' => $r['name'], 'count' => (int) $r['cnt']], $specRows),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user