DashboardService.php 2.5 KB

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