|
|
@@ -2,14 +2,23 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Enums\AppointmentStatusEnum;
|
|
|
+use App\Enums\PartnerAgreementTypeEnum;
|
|
|
use App\Enums\UserTypeEnum;
|
|
|
+use App\Models\Appointment;
|
|
|
use App\Models\User;
|
|
|
use Carbon\Carbon;
|
|
|
+use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
|
class ReportService
|
|
|
{
|
|
|
+ private const USOS_CONVENIO_STATUSES = [
|
|
|
+ AppointmentStatusEnum::CONFIRMADO,
|
|
|
+ AppointmentStatusEnum::CONCLUIDO,
|
|
|
+ ];
|
|
|
+
|
|
|
public function getCounters(): array
|
|
|
{
|
|
|
$now = Carbon::now();
|
|
|
@@ -25,6 +34,7 @@ class ReportService
|
|
|
->whereYear('excluded_at', $now->year)
|
|
|
->whereMonth('excluded_at', $now->month)
|
|
|
->count(),
|
|
|
+ 'usos_convenio' => Appointment::whereIn('status', self::USOS_CONVENIO_STATUSES)->count(),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -106,6 +116,69 @@ class ReportService
|
|
|
->get();
|
|
|
}
|
|
|
|
|
|
+ public function getUsosConvenioPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
|
|
|
+ {
|
|
|
+ return $this->usosConvenioQuery($filters)->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getAllUsosConvenio(array $filters = []): Collection
|
|
|
+ {
|
|
|
+ return $this->usosConvenioQuery($filters)->get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function formatUsoConvenio(Appointment $appointment): array
|
|
|
+ {
|
|
|
+ $criadoEm = $appointment->requested_at ?? $appointment->created_at;
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'id' => $appointment->id,
|
|
|
+ 'convenio' => $appointment->partnerAgreement?->company_name,
|
|
|
+ 'type' => $appointment->partnerAgreement?->type?->value,
|
|
|
+ 'servico' => $appointment->partnerAgreementService?->name,
|
|
|
+ 'associado' => $appointment->user?->name,
|
|
|
+ 'criado_em' => $criadoEm?->format('d/m/Y H:i'),
|
|
|
+ 'agendado_em' => $appointment->date
|
|
|
+ ? $appointment->date->format('d/m/Y') . ($appointment->time
|
|
|
+ ? ' ' . Carbon::parse($appointment->time)->format('H:i')
|
|
|
+ : '')
|
|
|
+ : null,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function usoConvenioTipoLabel(?string $type): string
|
|
|
+ {
|
|
|
+ return match ($type) {
|
|
|
+ PartnerAgreementTypeEnum::AGREEMENT->value => 'Convênio Médico',
|
|
|
+ PartnerAgreementTypeEnum::PARTNER->value => 'Parceiro',
|
|
|
+ default => '',
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private function usosConvenioQuery(array $filters = []): Builder
|
|
|
+ {
|
|
|
+ $query = Appointment::query()
|
|
|
+ ->with([
|
|
|
+ 'user:id,name',
|
|
|
+ 'partnerAgreement:id,company_name,type',
|
|
|
+ 'partnerAgreementService:id,name',
|
|
|
+ ])
|
|
|
+ ->whereIn('status', self::USOS_CONVENIO_STATUSES)
|
|
|
+ ->orderByRaw('COALESCE(appointments.requested_at, appointments.created_at) DESC');
|
|
|
+
|
|
|
+ if (!empty($filters['search'])) {
|
|
|
+ $term = '%' . mb_strtolower($filters['search']) . '%';
|
|
|
+ $query->where(function ($q) use ($term) {
|
|
|
+ $q->whereHas('partnerAgreement', fn($p) => $p->whereRaw('UNACCENT(LOWER(company_name)) LIKE UNACCENT(?)', [$term]))
|
|
|
+ ->orWhereHas('partnerAgreementService', fn($s) => $s->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term]))
|
|
|
+ ->orWhereHas('user', fn($u) => $u->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term]))
|
|
|
+ ->orWhereRaw("TO_CHAR(appointments.date, 'DD/MM/YYYY') LIKE ?", [$term])
|
|
|
+ ->orWhereRaw("TO_CHAR(COALESCE(appointments.requested_at, appointments.created_at), 'DD/MM/YYYY') LIKE ?", [$term]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query;
|
|
|
+ }
|
|
|
+
|
|
|
public function getAllExclusoesMes(): Collection
|
|
|
{
|
|
|
$now = Carbon::now();
|