75 lines
2.6 KiB
PHP
75 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\ProjektAllgemeinflaechen;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Illuminate\Support\Number;
|
|
|
|
class ProjektAllgemeinflaechenImporter extends Importer
|
|
{
|
|
protected static ?string $model = ProjektAllgemeinflaechen::class;
|
|
|
|
public function resolveRecord(): ProjektAllgemeinflaechen
|
|
{
|
|
$allgemeinflaechen = new ProjektAllgemeinflaechen;
|
|
$allgemeinflaechen->projekte_id = $this->options['projekte_id'];
|
|
|
|
return $allgemeinflaechen;
|
|
}
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
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')
|
|
->rules(['required', 'max:32'])
|
|
->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 Allgemeinflächen 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;
|
|
}
|
|
}
|