105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\ProjektOrientierungZuAbschlaege;
|
|
use App\Models\ProjektRaeumealle;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Illuminate\Support\Number;
|
|
|
|
class ProjektRaeumealleImporter extends Importer
|
|
{
|
|
protected static ?string $model = ProjektRaeumealle::class;
|
|
|
|
protected function beforeValidate(): void
|
|
{
|
|
dd($this->data);
|
|
|
|
$res = ProjektOrientierungZuAbschlaege::firstOrCreate([
|
|
'projekte_id' => $this->options['projekte_id'],
|
|
'top_nummer' => trim($this->data['top_nummer']),
|
|
]);
|
|
|
|
dd($res);
|
|
|
|
$this->data['orientierung_zu_abschlaege_id'] = $res->id;
|
|
}
|
|
|
|
|
|
public function resolveRecord(): ProjektRaeumealle
|
|
{
|
|
return ProjektRaeumealle::firstOrNew([
|
|
'projekt_orientierung_zu_abschlaege_id' => $this->data['orientierung_zu_abschlaege_id'],
|
|
'projekte_id' => $this->options['projekte_id'],
|
|
]);
|
|
}
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
// ImportColumn::make('top_nummer')
|
|
// ->relationship('orientierung_zu_abschlaege'),
|
|
ImportColumn::make('top_nummer')
|
|
->label('Top Nummer')
|
|
->validationAttribute('Top Nummer')
|
|
->requiredMapping()
|
|
->example('EG')
|
|
->rules(['required', 'max:255'])
|
|
->castStateUsing(function (string $state) {
|
|
$state = trim($state);
|
|
|
|
return iconv(mb_detect_encoding($state, mb_detect_order(), true), 'UTF-8', $state);
|
|
}),
|
|
ImportColumn::make('geschoss')
|
|
->validationAttribute('Geschoss')
|
|
->requiredMapping()
|
|
->example('EG')
|
|
->rules(['required', 'max:255'])
|
|
->castStateUsing(function (string $state) {
|
|
$state = trim($state);
|
|
|
|
return iconv(mb_detect_encoding($state, mb_detect_order(), true), 'UTF-8', $state);
|
|
}),
|
|
ImportColumn::make('raumart')
|
|
->validationAttribute('Raumart')
|
|
->requiredMapping()
|
|
->example('Stiegenhaus')
|
|
->rules(['required', 'max:32'])
|
|
->castStateUsing(function (string $state) {
|
|
$state = trim($state);
|
|
|
|
return iconv(mb_detect_encoding($state, mb_detect_order(), true), 'UTF-8', $state);
|
|
}),
|
|
ImportColumn::make('flaeche')
|
|
->label('Fläche')
|
|
->validationAttribute('Fläche')
|
|
->requiredMapping()
|
|
->rules(['required'])
|
|
->example('52,75')
|
|
->castStateUsing(function (string $state): ?float {
|
|
if (blank($state)) {
|
|
return '0.00';
|
|
}
|
|
|
|
$state = str_replace(',', '.', $state);
|
|
$state = preg_replace('/[^0-9.]/', '', $state);
|
|
|
|
return round($state, precision: 2);
|
|
}),
|
|
];
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = ' Der Import der Räume ist abgeschlossen. Es wurden '.Number::format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' importiert.';
|
|
|
|
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
|
$body .= ' Bei '.Number::format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' ist der Import fehlgeschlagen.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|