110 lines
3.8 KiB
PHP
110 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\ProjektObjekte;
|
|
use App\Models\ProjektObjekteImDetail;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Illuminate\Support\Number;
|
|
|
|
class ProjektObjekteImDetailImporter extends Importer
|
|
{
|
|
protected static ?string $model = ProjektObjekteImDetail::class;
|
|
|
|
public static $projekte_id;
|
|
|
|
public function resolveRecord(): ProjektObjekteImDetail
|
|
{
|
|
self::$projekte_id = $this->options['projekte_id'];
|
|
|
|
$res = new ProjektObjekteImDetail;
|
|
$res->projekte_id = self::$projekte_id;
|
|
|
|
return $res;
|
|
}
|
|
|
|
public function getCsvSettings(): array
|
|
{
|
|
return [
|
|
// Setzt das Anführungszeichen auf leer,
|
|
// um "text" als text zu lesen
|
|
'enclosure' => '',
|
|
'escape_character' => '\\',
|
|
'use_bom' => true,
|
|
];
|
|
}
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('top_nummer')
|
|
->label('Top Nummer')
|
|
->validationAttribute('Top Nummer')
|
|
->example('W07')
|
|
->rules(['required', 'max:255'])
|
|
->relationship(resolveUsing: function (string $state, $data): ?ProjektObjekte {
|
|
return ProjektObjekte::query()
|
|
->where('projekte_id', self::$projekte_id)
|
|
->Where('top_nummer', $state)
|
|
->firstOrCreate([
|
|
'projekte_id' => self::$projekte_id,
|
|
'top_nummer' => $state,
|
|
]);
|
|
})
|
|
->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')
|
|
->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')
|
|
->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')
|
|
->rules(['required'])
|
|
->example('14,75')
|
|
->castStateUsing(function (string $state): ?float {
|
|
|
|
if (blank($state)) {
|
|
return null;
|
|
}
|
|
|
|
$state = str_replace(',', '.', $state);
|
|
$state = preg_replace('/[^0-9.]/', '', $state);
|
|
$state = floatval($state);
|
|
|
|
return round($state, precision: 2);
|
|
}),
|
|
];
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = ' Der Import der Objekte 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).' '.@lang('Reihe|Reihen', $failedRowsCount).' ist der Import fehlgeschlagen.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|