157 lines
6.6 KiB
PHP
157 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Cloning;
|
|
|
|
use App\Models\ProjektAllgemeinflaechen;
|
|
use App\Models\Projekte;
|
|
use App\Models\ProjektInhaltsverzeichnis;
|
|
use App\Models\ProjektMerkmale;
|
|
use App\Models\ProjektMethode;
|
|
use App\Models\ProjektObjekte;
|
|
use App\Models\ProjektObjekteImDetail;
|
|
use App\Models\ProjektPlatzhalter;
|
|
use Filament\Notifications\Notification;
|
|
|
|
class Reparieren
|
|
{
|
|
public static function run(Projekte $record)
|
|
{
|
|
/**
|
|
* projektes
|
|
* projekt_users
|
|
* projekt_allgemeinflaechens
|
|
* projekt_inhaltsverzeichnis
|
|
* projekt_merkmales
|
|
* projekt_methodes
|
|
* projekt_platzhalters
|
|
* projekt_objekte_im_details
|
|
* projekt_objektes
|
|
*/
|
|
$CLONED_FROM_ID = $record->cloned_from_id;
|
|
$ACTIVE_ID = $record->id;
|
|
|
|
/**
|
|
* Ist geklonet
|
|
*/
|
|
if($CLONED_FROM_ID){
|
|
|
|
// Lösche alle geklonten "Objekte" ohne Verbindung zu Original
|
|
ProjektObjekte::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', 0)->where('added_manuel', 0)->delete();
|
|
|
|
// Überprüfe geklontes Projekt
|
|
$originals = ProjektObjekte::where('projekte_id', $CLONED_FROM_ID)->get();
|
|
foreach ($originals as $original) {
|
|
$check = ProjektObjekte::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', $original->id)->get();
|
|
if (! $check) {
|
|
/**
|
|
* Im Original dazugekommen oder gelöscht
|
|
*/
|
|
$new = $original->replicate();
|
|
$new->projekte_id = $ACTIVE_ID;
|
|
$new->cloned_from_id = $CLONED_FROM_ID;
|
|
$new->save();
|
|
|
|
// Lösche alle geklonten "Objekte im Detail" ohne Verbindung zu Original
|
|
ProjektObjekteImDetail::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', 0)->where('added_manuel', 0)->delete();
|
|
|
|
$res2 = ProjektObjekteImDetail::where('projekte_id', $CLONED_FROM_ID)->where('projekt_objekte_id', $original->id)->get();
|
|
foreach ($res2 as $row2) {
|
|
$check = ProjektObjekteImDetail::where('projekte_id', $ACTIVE_ID)->where('raumart', $row2->raumart)->first();
|
|
if (! $check) {
|
|
$new2 = $row2->replicate();
|
|
$new2->projekte_id = $ACTIVE_ID;
|
|
$new2->projekt_objekte_id = $new->id;
|
|
$new2->cloned_from_id = $row2->id;
|
|
$new2->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ProjektAllgemeinflaechen::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', 0)->where('added_manuel', 0)->delete();
|
|
$res = ProjektAllgemeinflaechen::where('projekte_id', $CLONED_FROM_ID)->get();
|
|
foreach ($res as $row) {
|
|
$check = ProjektAllgemeinflaechen::where('projekte_id', $ACTIVE_ID)->where('raumart', $original->raumart)->first();
|
|
if (! $check) {
|
|
$new = $row->replicate();
|
|
$new->projekte_id = $ACTIVE_ID;
|
|
$new->cloned_from_id = $row->id;
|
|
$new->save();
|
|
}
|
|
}
|
|
|
|
ProjektInhaltsverzeichnis::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', 0)->where('added_manuel', 0)->delete();
|
|
$res = ProjektInhaltsverzeichnis::where('projekte_id', $CLONED_FROM_ID)->get();
|
|
foreach ($res as $row) {
|
|
$check = ProjektInhaltsverzeichnis::where('projekte_id', $ACTIVE_ID)->where('inhaltsverzeichnis_id', $original->inhaltsverzeichnis_id)->first();
|
|
if (! $check) {
|
|
$new = $row->replicate();
|
|
$new->projekte_id = $ACTIVE_ID;
|
|
$new->cloned_from_id = $row->id;
|
|
$new->save();
|
|
}
|
|
}
|
|
|
|
ProjektMerkmale::where('projekte_id', $ACTIVE_ID)->where('cloned_from_id', 0)->where('added_manuel', 0)->delete();
|
|
$res = ProjektMerkmale::where('projekte_id', $record->id)->get();
|
|
foreach ($res as $row) {
|
|
$check = ProjektInhaltsverzeichnis::where('projekte_id', $ACTIVE_ID)->where('name', $original->name)->first();
|
|
if (! $check) {
|
|
$new = $row->replicate();
|
|
$new->projekte_id = $ACTIVE_ID;
|
|
$new->cloned_from_id = $row->id;
|
|
$new->save();
|
|
}
|
|
}
|
|
|
|
$res = ProjektMethode::where('projekte_id', $record->id)->get();
|
|
foreach ($res as $row) {
|
|
$new = $row->replicate();
|
|
$new->projekte_id = $record->id;
|
|
$new->cloned_from_id = $row->id;
|
|
$new->save();
|
|
}
|
|
|
|
$res = ProjektPlatzhalter::where('projekte_id', $record->id)->get();
|
|
foreach ($res as $row) {
|
|
$new = $row->replicate();
|
|
$new->projekte_id = $record->id;
|
|
$new->cloned_from_id = $row->id;
|
|
$new->save();
|
|
}
|
|
}
|
|
|
|
// OBJEKTE IM DETAIL ZU OBJEKTE
|
|
// Kontrolliere Verbindungen geklonter und nicht geklonter
|
|
// Select all childreen from project
|
|
$res = ProjektObjekteImDetail::select('projekt_objekte_id')->where('projekte_id', $ACTIVE_ID)->groupBy('projekt_objekte_id')->get();
|
|
foreach ($res as $row) {
|
|
// See if parent exists
|
|
$res2 = ProjektObjekte::where('projekte_id', $ACTIVE_ID)->where('id', $row->projekt_objekte_id)->first();
|
|
if(!$res){
|
|
// If parent NOT exists delete all childreen
|
|
ProjektObjekteImDetail::where('projekte_id', $ACTIVE_ID)->where('projekt_objekte_id', $row->projekt_objekte_id)->delete();
|
|
}
|
|
}
|
|
// OBJEKTE ZU OBJEKTE IM DETAIL
|
|
$res = ProjektObjekte::where('projekte_id', $ACTIVE_ID)->get();
|
|
foreach ($res as $row) {
|
|
// See if cildreen exists
|
|
$res2 = ProjektObjekteImDetail::where('projekte_id', $ACTIVE_ID)->where('projekt_objekte_id', $row->id)->first();
|
|
if(!$res){
|
|
// If childree NOT exists delete parent
|
|
ProjektObjekte::where('id', $row->id)->delete();
|
|
}
|
|
}
|
|
|
|
Notification::make()
|
|
->title('Projekt reparieren')
|
|
->body('Die Reparatur des Projektes war erfolgreich.')
|
|
->icon('heroicon-o-exclamation-triangle')
|
|
->duration(10000)
|
|
->success()
|
|
->send();
|
|
|
|
}
|
|
}
|