Added script to delete OAIDs incl. all linked data from Preorders

This commit is contained in:
Frank Schubert
2024-10-09 16:54:11 +02:00
parent 03c400ed79
commit a64dc497e0
5 changed files with 211 additions and 17 deletions

View File

@@ -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;

View File

@@ -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();
}
}

View File

@@ -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) {

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class NetzgebietAddUnitcountSdMd extends AbstractMigration
{
public function up(): void
{
if($this->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();
}
}
}

89
scripts/delete-oaid.php Normal file
View File

@@ -0,0 +1,89 @@
#!/usr/bin/php
<?php
exit;
//require 'vendor/autoload.php';
require("../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->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();
}