Files
onboard/app/Application/Reports/ReportInputFactory.php

77 lines
2.6 KiB
PHP

<?php
namespace App\Application\Reports;
use App\Application\Reports\DTO\GenerateReportInput;
use App\Models\Department;
use App\Models\User;
use App\Services\DateRange;
use App\Services\DateRangeService;
use DateTimeImmutable;
final readonly class ReportInputFactory
{
public function __construct(
private DateRangeService $dateRangeService,
) {}
/**
* @param array<string, mixed> $validated
*/
public function forManualSave(User $actor, array $validated, string $reportType = 'daily'): GenerateReportInput
{
$dateRange = $this->dateRangeService->getNormalizedDateRange(
$actor,
(string) ($validated['dates'][0] ?? null),
(string) ($validated['dates'][1] ?? null),
);
$endedAt = new DateTimeImmutable($dateRange->endSql());
return new GenerateReportInput(
departmentId: (int) $validated['departmentId'],
userId: (int) $validated['userId'],
actorUserId: (int) $actor->id,
periodStart: new DateTimeImmutable($dateRange->startSql()),
periodEnd: $endedAt,
metrics: (array) ($validated['metrics'] ?? []),
observationPatients: (array) ($validated['observationPatients'] ?? []),
unwantedEvents: (array) ($validated['unwantedEvents'] ?? []),
reportId: isset($validated['reportId']) ? (int) $validated['reportId'] : null,
status: (string) ($validated['status'] ?? 'draft'),
reportType: $reportType,
autoFill: false,
rawPayload: [
...$validated,
'actorUserId' => $actor->id,
'created_at' => $dateRange->endSql(),
'sent_at' => $dateRange->endSql(),
],
createdAt: $endedAt,
sentAt: $endedAt,
);
}
public function forAutoFill(
User $scopedUser,
Department $department,
DateRange $dateRange,
string $reportType = 'daily',
): GenerateReportInput {
$endedAt = new DateTimeImmutable($dateRange->endSql());
return new GenerateReportInput(
departmentId: (int) $department->department_id,
userId: (int) ($scopedUser->rf_lpudoctor_id ?? $scopedUser->id),
actorUserId: (int) $scopedUser->id,
periodStart: new DateTimeImmutable($dateRange->startSql()),
periodEnd: $endedAt,
status: 'submitted',
reportType: $reportType,
autoFill: true,
createdAt: $endedAt,
sentAt: $endedAt,
);
}
}