180 lines
5.9 KiB
PHP
180 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\AttendanceTracking;
|
|
|
|
use App\Models\AttendanceData;
|
|
use App\Models\AttendanceTracking;
|
|
use App\Models\Configuration;
|
|
use App\Models\Student;
|
|
use App\Models\StudentClass;
|
|
use Carbon\Carbon;
|
|
|
|
class AttendanceTrackingService
|
|
{
|
|
protected string $semester;
|
|
protected string $schoolYear;
|
|
|
|
public function __construct(
|
|
protected Student $studentModel,
|
|
protected StudentClass $studentClassModel,
|
|
protected AttendanceData $attendanceDataModel,
|
|
protected AttendanceTracking $attendanceTrackingModel,
|
|
protected Configuration $configModel,
|
|
protected AttendanceMailerService $attendanceMailerService,
|
|
protected ViolationRuleEngineService $violationRuleEngine,
|
|
protected AttendanceParentLookupService $parentLookupService,
|
|
protected AttendanceEmailComposerService $emailComposerService,
|
|
protected AttendanceNotificationLogService $notificationLogService,
|
|
protected AttendanceViolationStudentResolverService $studentResolverService,
|
|
protected AttendanceCaseQueryService $attendanceCaseQueryService,
|
|
protected AttendanceNotificationWorkflowService $attendanceNotificationWorkflowService,
|
|
protected AttendanceCommunicationSupportService $attendanceCommunicationSupportService,
|
|
protected AttendancePendingViolationService $attendancePendingViolationService
|
|
) {
|
|
$this->semester = (string) ($this->configModel->getConfig('semester') ?? '');
|
|
$this->schoolYear = (string) ($this->configModel->getConfig('school_year') ?? '');
|
|
}
|
|
|
|
public function getPendingViolations(?string $schoolYearParam = null, ?string $semesterParam = null): array
|
|
{
|
|
return $this->attendancePendingViolationService->getPendingViolations(
|
|
$this->schoolYear,
|
|
$schoolYearParam,
|
|
$semesterParam
|
|
);
|
|
}
|
|
|
|
public function getStudentCase(
|
|
int $studentId,
|
|
?string $codeParam = null,
|
|
?string $incidentDate = null,
|
|
bool $includeNotified = false,
|
|
bool $pendingOnly = false,
|
|
?string $schoolYearFilter = null,
|
|
?string $semesterFilter = null
|
|
): array {
|
|
return $this->attendanceCaseQueryService->getStudentCase(
|
|
$studentId,
|
|
$codeParam,
|
|
$incidentDate,
|
|
$includeNotified,
|
|
$pendingOnly,
|
|
$schoolYearFilter,
|
|
$semesterFilter,
|
|
$this->schoolYear,
|
|
$this->semester
|
|
);
|
|
}
|
|
|
|
public function getNotifiedViolations(?string $schoolYearParam = null, ?string $semesterParam = null): array
|
|
{
|
|
return $this->attendanceCaseQueryService->getNotifiedViolations(
|
|
$schoolYearParam,
|
|
$semesterParam,
|
|
$this->schoolYear,
|
|
$this->semester
|
|
);
|
|
}
|
|
|
|
public function compose(
|
|
int $studentId,
|
|
string $code = 'ABS_1',
|
|
string $variant = 'default',
|
|
?string $incidentDate = null
|
|
): array {
|
|
return $this->attendanceCommunicationSupportService->compose(
|
|
$studentId,
|
|
$code,
|
|
$variant,
|
|
$incidentDate
|
|
);
|
|
}
|
|
|
|
public function parentsInfo(int $studentId): array
|
|
{
|
|
return $this->attendanceCommunicationSupportService->parentsInfo($studentId);
|
|
}
|
|
|
|
public function record(array $data): array
|
|
{
|
|
return $this->attendanceNotificationWorkflowService->record($data);
|
|
}
|
|
|
|
public function sendAutoEmails(?string $variantOverride = null): array
|
|
{
|
|
$classStudentsQuery = $this->studentClassModel->query();
|
|
if (method_exists($this->studentClassModel, 'scopeActive')) {
|
|
$classStudentsQuery->active();
|
|
}
|
|
|
|
$classStudents = $classStudentsQuery
|
|
->where('school_year', $this->schoolYear)
|
|
->get()
|
|
->toArray();
|
|
|
|
if (empty($classStudents)) {
|
|
return [
|
|
'success' => true,
|
|
'sent' => 0,
|
|
'skipped' => 0,
|
|
'errors' => 0,
|
|
'details' => [],
|
|
];
|
|
}
|
|
|
|
$studentIds = array_column($classStudents, 'student_id');
|
|
|
|
$students = $this->studentModel->query()
|
|
->whereIn('id', $studentIds)
|
|
->where('is_active', 1)
|
|
->get()
|
|
->toArray();
|
|
|
|
$attendanceQuery = $this->attendanceDataModel->query()
|
|
->whereIn('student_id', $studentIds)
|
|
->where('school_year', $this->schoolYear)
|
|
->where('date', '>=', Carbon::today()->subWeeks(5)->format('Y-m-d'));
|
|
|
|
$this->violationRuleEngine->applyUnreportedAttendanceFilter(
|
|
$attendanceQuery,
|
|
$this->attendanceDataModel->getTable()
|
|
);
|
|
|
|
$attendanceData = $attendanceQuery
|
|
->whereRaw("LOWER(TRIM(status)) IN ('absent','late')")
|
|
->orderByDesc('date')
|
|
->get()
|
|
->toArray();
|
|
|
|
$violations = $this->violationRuleEngine->computeViolations(
|
|
$students,
|
|
$attendanceData,
|
|
$this->schoolYear,
|
|
null,
|
|
$attendanceData
|
|
);
|
|
|
|
return $this->attendanceNotificationWorkflowService->sendAutoEmails($violations, $variantOverride);
|
|
}
|
|
|
|
public function sendEmailManual(array $data): array
|
|
{
|
|
$sid = (int) $data['student_id'];
|
|
$code = (string) $data['code'];
|
|
$ymdRaw = (string) ($data['incident_date'] ?? '');
|
|
$ymd = $ymdRaw !== '' ? substr($ymdRaw, 0, 10) : '';
|
|
|
|
$violationDates = $this->attendanceCommunicationSupportService->getViolationDatesForStudent(
|
|
$sid,
|
|
$code,
|
|
$ymd
|
|
);
|
|
|
|
return $this->attendanceNotificationWorkflowService->sendEmailManual($data, $violationDates);
|
|
}
|
|
|
|
public function saveNotificationNote(array $data): array
|
|
{
|
|
return $this->attendanceNotificationWorkflowService->saveNotificationNote($data);
|
|
}
|
|
} |