110 lines
3.7 KiB
PHP
110 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Student;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
|
use Filament\Notifications\Notification;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class StudentImporter extends Importer
|
|
{
|
|
protected static ?string $model = Student::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('full_name')
|
|
->requiredMapping()
|
|
->rules(['required', 'max:255']),
|
|
ImportColumn::make('nis')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('nisn')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('gender')
|
|
->requiredMapping()
|
|
->rules(['required', 'max:255']),
|
|
ImportColumn::make('birth_date')
|
|
->rules(['date']),
|
|
ImportColumn::make('birth_place')
|
|
->rules(['max:255']),
|
|
ImportColumn::make('address'),
|
|
ImportColumn::make('religion'),
|
|
ImportColumn::make('phone')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('email')
|
|
->rules(['email', 'max:255']),
|
|
ImportColumn::make('parent_name')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('parent_phone')
|
|
->rules(['required','max:255']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?Student
|
|
{
|
|
// Cek duplikat NIS
|
|
if (Student::where('nis', $this->data['nis'])->exists()) {
|
|
throw new RowImportFailedException('NIS sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat NISN
|
|
if (Student::where('nisn', $this->data['nisn'])->exists()) {
|
|
throw new RowImportFailedException('NISN sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat phone
|
|
if (Student::where('phone', $this->data['phone'])->exists()) {
|
|
throw new RowImportFailedException('Nomor telepon sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat email di tabel users
|
|
if (User::where('email', $this->data['email'])->exists()) {
|
|
throw new RowImportFailedException('Email sudah digunakan');
|
|
}
|
|
|
|
$parentRole = Role::where('name', 'parent')->first();
|
|
|
|
$birthDate = Carbon::parse($this->data['birth_date']);
|
|
$password = $birthDate->format('dmY') . $this->data['nis'];
|
|
|
|
$parentUser = User::updateOrCreate(
|
|
['email' => $this->data['email']],
|
|
[
|
|
'name' => $this->data['parent_name'],
|
|
'password' => bcrypt($password),
|
|
'phone' => $this->data['parent_phone'],
|
|
]
|
|
);
|
|
|
|
$parentUser->assignRole($parentRole);
|
|
|
|
$recipient = auth()->user();
|
|
if ($recipient) {
|
|
Notification::make()
|
|
->title('Impor Siswa Berhasil')
|
|
->body('Siswa baru dengan NIS ' . $this->data['nis'] . ' dan orang tua ' . $this->data['parent_name'] . ' berhasil diimpor.')
|
|
->success()
|
|
->sendToDatabase($recipient);
|
|
}
|
|
|
|
return new Student();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your student import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
|
|
|
|
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
|
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|