DashboardService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserStatusEnum;
  4. use App\Enums\UserTypeEnum;
  5. use App\Models\PartnerAgreement;
  6. use App\Models\User;
  7. use App\Models\UserAccessLog;
  8. use App\Models\Appointment;
  9. use Illuminate\Support\Facades\Auth;
  10. class DashboardService
  11. {
  12. public function getStats(): array
  13. {
  14. return [
  15. 'total_associados' => User::where('type', UserTypeEnum::ASSOCIADO)->count(),
  16. 'associados_ativos' => User::where('type', UserTypeEnum::ASSOCIADO)->where('status', UserStatusEnum::ACTIVE)->count(),
  17. 'parceiros' => PartnerAgreement::count(),
  18. 'contratos_a_vencer' => PartnerAgreement::whereNotNull('contract_end')
  19. ->whereBetween('contract_end', [now()->toDateString(), now()->addDays(30)->toDateString()])
  20. ->count(),
  21. 'novos_mes' => PartnerAgreement::whereMonth('created_at', now()->month)->whereYear('created_at', now()->year)->count(),
  22. 'associados_pendentes' => User::where('type', UserTypeEnum::ASSOCIADO)->where('status', UserStatusEnum::PENDING)->count(),
  23. 'ultimos_acessos' => UserAccessLog::distinct('user_id')->count('user_id'),
  24. 'associados_com_acesso_app' => UserAccessLog::whereHas('user', fn ($q) => $q->where('type', UserTypeEnum::ASSOCIADO))
  25. ->distinct('user_id')
  26. ->count('user_id'),
  27. ];
  28. }
  29. public function getPartnerStats(): array
  30. {
  31. $partnerAgreementId = PartnerAgreement::where('user_id', Auth::id())->value('id');
  32. return [
  33. 'authorization' => Appointment::where('partner_agreement_id', $partnerAgreementId)
  34. ->where('status', 'pendente')
  35. ->count(),
  36. 'scheduling' => Appointment::query()
  37. ->leftJoin(
  38. 'partner_agreement_services',
  39. 'partner_agreement_services.id',
  40. '=',
  41. 'appointments.partner_agreement_service_id'
  42. )
  43. ->where('appointments.partner_agreement_id', $partnerAgreementId)
  44. ->where('partner_agreement_services.requires_scheduling', true)
  45. ->where('appointments.status', 'pendente')
  46. ->count(),
  47. //aqui e o count total sem status e sem nada
  48. 'completed' => Appointment::where('partner_agreement_id', $partnerAgreementId)
  49. ->count(),
  50. //status recusado
  51. 'not_authorized' => Appointment::where('partner_agreement_id', $partnerAgreementId)
  52. ->where('status', 'recusado')
  53. ->count(),
  54. ];
  55. }
  56. }