first
This commit is contained in:
62
app/Services/Attendance/AttendanceExportService.php
Normal file
62
app/Services/Attendance/AttendanceExportService.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Attendance;
|
||||
|
||||
use App\Exports\AttendanceDetailExport;
|
||||
use App\Models\ExportLog;
|
||||
use App\Models\PusatMengundi;
|
||||
use App\Models\StaffAssignment;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class AttendanceExportService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AttendanceDetailExport $export,
|
||||
) {}
|
||||
|
||||
public function exportPusat(User $actor, PusatMengundi $pusatMengundi): ExportLog
|
||||
{
|
||||
$assignments = StaffAssignment::query()
|
||||
->with(['application', 'user', 'position', 'pusatMengundi', 'saluranMengundi', 'attendance'])
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'active')
|
||||
->orderBy('position_id')
|
||||
->orderBy('saluran_mengundi_id')
|
||||
->get();
|
||||
|
||||
$fileName = 'attendance-'.$pusatMengundi->code.'-'.now()->format('Ymd-His').'.xlsx';
|
||||
$path = 'exports/attendance/'.$fileName;
|
||||
|
||||
Storage::disk('local')->makeDirectory('exports/attendance');
|
||||
$absolutePath = Storage::disk('local')->path($path);
|
||||
|
||||
$this->export->write($assignments, $absolutePath);
|
||||
|
||||
$log = ExportLog::query()->create([
|
||||
'election_id' => $pusatMengundi->election_id,
|
||||
'generated_by_user_id' => $actor->id,
|
||||
'generated_at' => now(),
|
||||
'report_type' => 'attendance_detail_by_pusat',
|
||||
'filter_parameters' => [
|
||||
'pusat_mengundi_id' => $pusatMengundi->id,
|
||||
'pusat_mengundi_code' => $pusatMengundi->code,
|
||||
],
|
||||
'file_name' => $fileName,
|
||||
'disk' => 'local',
|
||||
'path' => $path,
|
||||
]);
|
||||
|
||||
activity('exports')
|
||||
->performedOn($log)
|
||||
->causedBy($actor)
|
||||
->withProperties([
|
||||
'report_type' => 'attendance_detail_by_pusat',
|
||||
'pusat_mengundi_id' => $pusatMengundi->id,
|
||||
'row_count' => $assignments->count(),
|
||||
])
|
||||
->log('attendance_export_generated');
|
||||
|
||||
return $log;
|
||||
}
|
||||
}
|
||||
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');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
72
app/Services/Attendance/AttendanceSummaryService.php
Normal file
72
app/Services/Attendance/AttendanceSummaryService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Attendance;
|
||||
|
||||
use App\Models\Attendance;
|
||||
use App\Models\Position;
|
||||
use App\Models\PusatMengundi;
|
||||
use App\Models\StaffAssignment;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class AttendanceSummaryService
|
||||
{
|
||||
/**
|
||||
* @return array{total_staff: int, present: int, absent: int, not_recorded: int}
|
||||
*/
|
||||
public function totalsForPusat(PusatMengundi $pusatMengundi): array
|
||||
{
|
||||
$totalStaff = StaffAssignment::query()
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'active')
|
||||
->count();
|
||||
|
||||
$present = Attendance::query()
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'present')
|
||||
->count();
|
||||
|
||||
$absent = Attendance::query()
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'absent')
|
||||
->count();
|
||||
|
||||
return [
|
||||
'total_staff' => $totalStaff,
|
||||
'present' => $present,
|
||||
'absent' => $absent,
|
||||
'not_recorded' => max(0, $totalStaff - $present - $absent),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, mixed>
|
||||
*/
|
||||
public function roleTotalsForPusat(PusatMengundi $pusatMengundi): Collection
|
||||
{
|
||||
$assignments = StaffAssignment::query()
|
||||
->with(['position', 'attendance'])
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('status', 'active')
|
||||
->get();
|
||||
|
||||
return $assignments
|
||||
->groupBy(function (StaffAssignment $assignment): string {
|
||||
$position = $assignment->position;
|
||||
|
||||
return $position instanceof Position ? $position->code : 'UNKNOWN';
|
||||
})
|
||||
->map(function (Collection $items, string $role): array {
|
||||
$present = $items->filter(fn (StaffAssignment $assignment): bool => $assignment->attendance instanceof Attendance && $assignment->attendance->status === 'present')->count();
|
||||
$absent = $items->filter(fn (StaffAssignment $assignment): bool => $assignment->attendance instanceof Attendance && $assignment->attendance->status === 'absent')->count();
|
||||
|
||||
return [
|
||||
'role' => $role,
|
||||
'total_staff' => $items->count(),
|
||||
'present' => $present,
|
||||
'absent' => $absent,
|
||||
'not_recorded' => max(0, $items->count() - $present - $absent),
|
||||
];
|
||||
})
|
||||
->values();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user