first
This commit is contained in:
100
app/Services/Attendance/AttendanceRecordingService.php
Normal file
100
app/Services/Attendance/AttendanceRecordingService.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Attendance;
|
||||
|
||||
use App\Models\Attendance;
|
||||
use App\Models\Election;
|
||||
use App\Models\ElectionSetting;
|
||||
use App\Models\PusatMengundi;
|
||||
use App\Models\StaffAssignment;
|
||||
use App\Models\User;
|
||||
use App\Services\Ppm\PpmScopeService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class AttendanceRecordingService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PpmScopeService $ppmScopeService,
|
||||
) {}
|
||||
|
||||
public function ensureActive(PusatMengundi $pusatMengundi): void
|
||||
{
|
||||
$election = $pusatMengundi->election;
|
||||
$settings = $election instanceof Election ? $election->settings : null;
|
||||
|
||||
if (! $settings instanceof ElectionSetting || ! $settings->is_attendance_active) {
|
||||
throw ValidationException::withMessages([
|
||||
'attendance' => 'Modul kehadiran belum diaktifkan untuk pilihanraya ini.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function ensurePpmCanRecord(User $actor, PusatMengundi $pusatMengundi): void
|
||||
{
|
||||
if (! $this->ppmScopeService->assignedPusatIds($actor)->contains((int) $pusatMengundi->id)) {
|
||||
throw ValidationException::withMessages([
|
||||
'pusat_mengundi_id' => 'PPM hanya boleh merekod kehadiran untuk Pusat Mengundi sendiri.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{status: string, check_in_time?: string|null, note?: string|null}> $records
|
||||
*/
|
||||
public function recordBulk(PusatMengundi $pusatMengundi, User $actor, array $records): void
|
||||
{
|
||||
$this->ensureActive($pusatMengundi);
|
||||
$this->ensurePpmCanRecord($actor, $pusatMengundi);
|
||||
|
||||
DB::transaction(function () use ($pusatMengundi, $actor, $records): void {
|
||||
$assignments = StaffAssignment::query()
|
||||
->with(['position'])
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'active')
|
||||
->whereIn('id', array_keys($records))
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
foreach ($records as $assignmentId => $record) {
|
||||
/** @var StaffAssignment|null $assignment */
|
||||
$assignment = $assignments->get((int) $assignmentId);
|
||||
|
||||
if (! $assignment instanceof StaffAssignment) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$status = $record['status'];
|
||||
$checkInTime = $status === 'present'
|
||||
? ($record['check_in_time'] ?? now())
|
||||
: null;
|
||||
|
||||
$attendance = Attendance::query()->updateOrCreate(
|
||||
[
|
||||
'election_id' => $assignment->election_id,
|
||||
'staff_assignment_id' => $assignment->id,
|
||||
],
|
||||
[
|
||||
'pusat_mengundi_id' => $assignment->pusat_mengundi_id,
|
||||
'saluran_mengundi_id' => $assignment->saluran_mengundi_id,
|
||||
'position_id' => $assignment->position_id,
|
||||
'status' => $status,
|
||||
'check_in_time' => $checkInTime,
|
||||
'recorded_by_user_id' => $actor->id,
|
||||
'note' => $record['note'] ?? null,
|
||||
],
|
||||
);
|
||||
|
||||
activity('attendance')
|
||||
->performedOn($attendance)
|
||||
->causedBy($actor)
|
||||
->withProperties([
|
||||
'staff_assignment_id' => $assignment->id,
|
||||
'status' => $status,
|
||||
'check_in_time' => $checkInTime,
|
||||
])
|
||||
->log('attendance_updated_by_ppm');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user