63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?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;
|
|
}
|
|
}
|