108 lines
4.1 KiB
PHP
108 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\ProjektMethode;
|
|
use App\Models\ProjektObjekte;
|
|
use App\Models\ProjektObjekteImDetail;
|
|
use App\Models\User;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\HtmlString;
|
|
|
|
class ImportObjekteDirect
|
|
{
|
|
public static function run(array $data, int $projekte_id)
|
|
{
|
|
$data = explode(PHP_EOL, $data['upload_data']);
|
|
|
|
$has_objekte = ProjektObjekte::where('projekte_id', $projekte_id)->count();
|
|
$new_objekte = 0;
|
|
|
|
$has_objekte_im_detail = ProjektObjekteImDetail::where('projekte_id', $projekte_id)->count();
|
|
$new_objekte_im_detail = 0;
|
|
$update_objekte_im_detail = 0;
|
|
|
|
foreach ($data as $row) {
|
|
$row = str_replace(';', '|', $row);
|
|
$cells = explode('|', $row);
|
|
|
|
$top_nummer = trim($cells[0]);
|
|
$geschoss = trim($cells[1]);
|
|
$raumart = trim($cells[2]);
|
|
|
|
$projekt_methode = ProjektMethode::firstOrNew([
|
|
'projekte_id' => $projekte_id,
|
|
'raumart' => $raumart,
|
|
]);
|
|
|
|
$flaeche = (float) str_replace(',', '.', trim($cells[3]));
|
|
|
|
if (is_numeric($flaeche) && $flaeche > 0 && $projekt_methode->id) {
|
|
|
|
$objekt = ProjektObjekte::where('projekte_id', $projekte_id)
|
|
->where('top_nummer', $top_nummer)
|
|
->first();
|
|
|
|
if ($objekt === null) {
|
|
$new_objekte++;
|
|
|
|
$objekt = new ProjektObjekte(['projekte_id' => $projekte_id, 'top_nummer' => $top_nummer]);
|
|
$objekt->save();
|
|
}
|
|
|
|
$objektImDetail = ProjektObjekteImDetail::where('projekte_id', $projekte_id)
|
|
->where('projekt_objekte_id', $objekt->id)
|
|
->where('projekt_methode_id', $projekt_methode->id)
|
|
->where('geschoss', $geschoss)
|
|
->first();
|
|
|
|
if ($objektImDetail === null) {
|
|
$new_objekte_im_detail++;
|
|
|
|
$objektImDetail = new ProjektObjekteImDetail([
|
|
'projekte_id' => $projekte_id,
|
|
'projekt_objekte_id' => $objekt->id,
|
|
'projekt_methode_id' => $projekt_methode->id,
|
|
'geschoss' => $geschoss,
|
|
'raumart' => $raumart,
|
|
]);
|
|
} else {
|
|
$update_objekte_im_detail++;
|
|
}
|
|
|
|
$objektImDetail->flaeche = $flaeche;
|
|
$objektImDetail->save();
|
|
}
|
|
}
|
|
|
|
/** @var User $user * */
|
|
$recipient = Auth::user();
|
|
|
|
Notification::make()
|
|
->title('Objekte im Detail wurden erfolgreich importiert')
|
|
->body(self::getBody($has_objekte, $has_objekte_im_detail, $new_objekte, $new_objekte_im_detail, $update_objekte_im_detail))
|
|
->success()
|
|
->send()
|
|
->sendToDatabase($recipient);
|
|
}
|
|
|
|
public static function getBody($has_objekte, $has_objekte_im_detail, $new_objekte, $new_objekte_im_detail, $update_objekte_im_detail)
|
|
{
|
|
return new HtmlString('<div class="grid grid-cols-4 gap-1 w-full">
|
|
<div class="col-span-4 text-gray-500 font-bold">Objekte</div>
|
|
<div class="col-span-3 text-primary-500">Vorher</div>
|
|
<div class="text-primary-500">'.$has_objekte.'</div>
|
|
<div class="col-span-3 text-success-500">Neu</div>
|
|
<div class="text-success-500">'.$new_objekte.'</div>
|
|
<div class="col-span-4 text-gray-500 font-bold">Objekte im Detail</div>
|
|
<div class="col-span-3 text-primary-500">Vorher</div>
|
|
<div class="text-primary-500">'.$has_objekte_im_detail.'</div>
|
|
<div class="col-span-3 text-success-500">Neu</div>
|
|
<div class="text-success-500">'.$new_objekte_im_detail.'</div>
|
|
<div class="col-span-3 text-warning-500">Aktualisiert</div>
|
|
<div class="text-warning-500">'.$update_objekte_im_detail.'</div>
|
|
</div>');
|
|
}
|
|
}
|