diff --git a/application/ADBNetzgebiet/ADBNetzgebietModel.php b/application/ADBNetzgebiet/ADBNetzgebietModel.php index 475ac84ac..37cf993ee 100644 --- a/application/ADBNetzgebiet/ADBNetzgebietModel.php +++ b/application/ADBNetzgebiet/ADBNetzgebietModel.php @@ -8,6 +8,8 @@ class ADBNetzgebietModel { public $rimo_id; public $freigabe; public $unit_count; + public $unit_count_sd; + public $unit_count_md; public $create = null; public $edit = null; diff --git a/application/ADBWohneinheit/ADBWohneinheit.php b/application/ADBWohneinheit/ADBWohneinheit.php index f609ba569..9fc846ba1 100644 --- a/application/ADBWohneinheit/ADBWohneinheit.php +++ b/application/ADBWohneinheit/ADBWohneinheit.php @@ -44,7 +44,30 @@ class ADBWohneinheit extends mfBaseModel { if($hausnummer->netzgebiet_id) { $netzgebiet = new ADBNetzgebiet($hausnummer->netzgebiet_id); if($netzgebiet->id) { - $netzgebiet->unit_count = ADBWohneinheitModel::count(['netzgebiet_id' => $hausnummer->netzgebiet_id]); + /*$netzgebiet->unit_count = ADBWohneinheitModel::count(['netzgebiet_id' => $hausnummer->netzgebiet_id]); + $gda_egenschaft = strtolower($hausnummer->gdaeigenschaft); + if($gda_egenschaft && array_key_exists($gda_egenschaft, TT_ADB_GDA_TYPES)) { + $netzgebiet->{"unit_count_".TT_ADB_GDA_TYPES[$gda_egenschaft]}; + }*/ + $unit_count = ADBWohneinheitModel::count(['netzgebiet_id' => $hausnummer->netzgebiet_id]); + if($unit_count) { + $unit_count_gda = []; + foreach(ADBHausnummerModel::search(["netzgebiet_id" => $hausnummer->netzgebiet_id]) as $hausnummer) { + $gda_egenschaft = strtolower($hausnummer->gdaeigenschaft); + if($gda_egenschaft && array_key_exists($gda_egenschaft, TT_ADB_GDA_TYPES)) { + $type = TT_ADB_GDA_TYPES[$gda_egenschaft]; + if(!array_key_exists($type, $unit_count_gda)) { + $unit_count_gda[$type] = 0; + } + $unit_count_gda[$type] += $hausnummer->unit_count; + } + } + foreach($unit_count_gda as $type => $count) { + $netzgebiet->{"unit_count_$type"} = $count; + } + + } + $netzgebiet->unit_count = $unit_count; $netzgebiet->save(); } } diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index c60f62d0f..82a94c73c 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -60,7 +60,9 @@ class Preorder extends mfBaseModel { $this->setOrCreateOaid(); if($this->oaid != $old_oaid) { + $this->resetSaveNesting(); $this->save(); + return true; } //TODO: history start @@ -339,17 +341,27 @@ class Preorder extends mfBaseModel { } if(!$this->adb_wohneinheit_id) { + $this->oaid = null; return false; } $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + if(!$wohneinheit) { + return true; + } + if($this->oaid) { // If current OAID is from correct Origin then do nothing if($campaign->oaid_origin == "thetool") { $current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid); if($current_oaid) { $this->log->warning("OAID of Preorder " . $this->id . " should be thetool, but is OFAA"); + } else { + if($wohneinheit->oaid != $this->oaid) { + $this->oaid = $wohneinheit->oaid; + return true; + } } } elseif($campaign->oaid_origin == "ofaa") { $current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid); @@ -363,6 +375,10 @@ class Preorder extends mfBaseModel { if(!$wohneinheit->oaid) { $wohneinheit->oaid = $current_oaid->oaid; $wohneinheit->save(); + } elseif($wohneinheit->oaid != $this->oaid) { + // also check if wohneinheit has a different OAID and set it in the Preorder + // No need to update OAID object, it should be correct already, because Wohneinheit is the source of oaid data, + $this->oaid = $wohneinheit->oaid; } return true; } diff --git a/application/Preordercampaign/Preordercampaign.php b/application/Preordercampaign/Preordercampaign.php index 714d73293..df2f98487 100644 --- a/application/Preordercampaign/Preordercampaign.php +++ b/application/Preordercampaign/Preordercampaign.php @@ -5,13 +5,18 @@ class Preordercampaign extends mfBaseModel { private $adb_netzgebiet; private $preorders; private $active_preorders; - private $active_preorder_count = 0; + private $active_preorder_count; + private $active_preorder_count_sd; + private $active_preorder_count_md; private $types; private $setup_products = []; private $salesclusters; private $apiusers; private $corsorigins; private $total_homes; + private $total_homes_sd; + private $total_homes_md; + private $workorder_count; private $required_fields = []; private $banned_fcps; private $all_fcp_names; @@ -95,9 +100,29 @@ class Preordercampaign extends mfBaseModel { public function getProperty($name) { if($this->$name == null) { - + + if($name == "workorder_count") { + $wo_count = PreorderModel::count(["rimo_workorder" => true, "preordercampaign_id" => $this->id]); + if($wo_count) { + $this->workorder_count = $wo_count; + } + return $wo_count; + } + + if($name == "total_homes_sd") { + $this->getProperty("total_homes"); + return $this->total_homes_sd; + } + + if($name == "total_homes_md") { + $this->getProperty("total_homes"); + return $this->total_homes_md; + } + if($name == "total_homes") { $total = 0; + $total_sd = 0; + $total_md = 0; $netzgebiet_ids = null; foreach($this->getProperty("salesclusters") as $scluster) { @@ -106,10 +131,14 @@ class Preordercampaign extends mfBaseModel { if(is_array($netzgebiet_ids) && count($netzgebiet_ids)) { foreach(ADBNetzgebietModel::search(["netzgebiet_id" => $netzgebiet_ids]) as $netzgebiet) { $total += $netzgebiet->unit_count; + $total_sd += $netzgebiet->unit_count_sd; + $total_md += $netzgebiet->unit_count_md; } } $this->total_homes = $total; + $this->total_homes_sd = $total_sd; + $this->total_homes_md = $total_md; return $total; } @@ -130,21 +159,37 @@ class Preordercampaign extends mfBaseModel { return $this->active_preorders; } - if($name == "active_preorder_count") { - $count = 0; - foreach($this->getProperty("active_preorders") as $preorder) { - //echo "/c:".$preorder->connection_count."/"; - if($preorder->connection_count) { - $count += (int)$preorder->connection_count; - } else { - $count++; - } - //echo "/s:$count/"; + if($name == "active_preorder_count_sd") { + $this->getProperty("active_preorder_count"); + return $this->active_preorder_count_sd; } - $this->active_preorder_count = $count; - return $this->active_preorder_count; - } - + + if($name == "active_preorder_count_md") { + $this->getProperty("active_preorder_count"); + return $this->active_preorder_count_md; + } + + if($name == "active_preorder_count") { + $count = 0; + $count_sd = 0; + $count_md = 0; + foreach($this->getProperty("active_preorders") as $preorder) { + //echo "/c:".$preorder->connection_count."/"; + if($preorder->connection_type == "single-dwelling") { + $count_sd += $preorder->connection_count ? (int)$preorder->connection_count : 1; + } elseif ($preorder->connection_type == "multi-dwelling") { + $count_md += $preorder->connection_count ? (int)$preorder->connection_count : 1; + } + $count += (int)$preorder->connection_count; + + //echo "/s:$count/"; + } + $this->active_preorder_count = $count; + $this->active_preorder_count_sd = $count_sd; + $this->active_preorder_count_md = $count_md; + return $this->active_preorder_count; + } + if($name == "types") { $types = PreordercampaignTypeModel::search(['preordercampaign_id' => $this->id]); foreach($types as $type) { diff --git a/db/migrations/20241008123850_netzgebiet_add_unitcount_sd_md.php b/db/migrations/20241008123850_netzgebiet_add_unitcount_sd_md.php new file mode 100644 index 000000000..bacd6ee13 --- /dev/null +++ b/db/migrations/20241008123850_netzgebiet_add_unitcount_sd_md.php @@ -0,0 +1,35 @@ +getEnvironment() == "thetool") { + + } + + if($this->getEnvironment() == "addressdb") { + $table = $this->table("Netzgebiet"); + $table->addColumn("unit_count_sd", "integer", ["null" => false, "default" => 0, "comment" => "single-dwelling", "after" => "unit_count"]); + $table->addColumn("unit_count_md", "integer", ["null" => false, "default" => 0, "comment" => "multi-dwelling", "after" => "unit_count_sd"]); + $table->update(); + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + + } + + if($this->getEnvironment() == "addressdb") { + $this->table("Netzgebiet") + ->removeColumn("unit_count_md") + ->removeColumn("unit_count_sd") + ->update(); + } + } +} diff --git a/scripts/delete-oaid.php b/scripts/delete-oaid.php new file mode 100644 index 000000000..ee602e072 --- /dev/null +++ b/scripts/delete-oaid.php @@ -0,0 +1,89 @@ +#!/usr/bin/php +id); +define("INTERNAL_USER_USERNAME", $me->username); +define("MFBASE_BYPASS_LOGIN", true); + +$hausnummern = [ + 1680565, + 1680564, + 1680562, + 1680563 +]; +$preorders = []; + +$db = new FronkDB(); +$adb = new FronkDB(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); + +// delete OAIDs from rimo +foreach($hausnummern as $hausnummmer_id) { + foreach(ADBWohneinheitModel::search(["hausnummer_id" => $hausnummmer_id]) as $unit) { + $oaid = $unit->oaid; + $ftu = $unit->ftu_data; + $ftu_id = $ftu["id"]; + echo "$ftu_id ".$unit->oaid."\n"; + if(!$ftu_id) { + echo "Keine ftu id for home with oaid ".$unit->oaid."\n"; + } + Rimoapi::unassignOaid($oaid, $ftu_id); + } +} + +// remove OAIDs from Wohneinheiten und Preorders (out of framework) +foreach($hausnummern as $hausnummmer_id) { + foreach(ADBWohneinheitModel::search(["hausnummer_id" => $hausnummmer_id]) as $unit) { + $update_preorder_sql = ""; + $update_unit_sql = ""; + $update_oaid_sql = ""; + + if(!$unit->oaid) continue; + + // delete oaid from preorder + $preorder = PreorderModel::getFirst(["oaid" => $unit->oaid]); + + if($preorder) { + $update_preorder_sql = "UPDATE Preorder SET oaid=NULL WHERE id=" . $preorder->id; + $preorders[] = $preorder; + } + + // delete oaid from unit + $update_unit_sql = "UPDATE Wohneinheit SET oaid=NULL WHERE id=".$unit->id; + + // delete oaid from OpenSccessId + $oaid_object = OpenAccessIdModel::getFirst(["oaid" => $unit->oaid]); + if($oaid_object) { + $update_oaid_sql = "UPDATE OpenAccessId SET assigned=0, adb_wohneinheit_id=NULL, exported=0, exported_to=NULL, export_data=NULL, address=NULL, unit_string=NULL WHERE id=".$oaid_object->id; + } + + + echo $unit->oaid."\n"; + echo "$update_preorder_sql\n$update_unit_sql\n$update_oaid_sql\n"; + + if($update_preorder_sql) $db->query($update_preorder_sql, true); + if($update_unit_sql) $adb->query($update_unit_sql, true); + if($update_oaid_sql) $db->query($update_oaid_sql, true); + + foreach(RimoWorkorderModel::search(["adb_wohneinheit_id" => $unit->id]) as $wo) { + echo "lösche Workorder ".$wo->id."\n"; + $wo->delete(); + } + echo "\n"; + } +} +//exit; +// Preorders speichern, um neue OAID zu generieren +foreach($preorders as $preorder) { + $preorder->save(); +} \ No newline at end of file