Added script to recalculate ADB Netzgebiet unit counts
This commit is contained in:
@@ -35,48 +35,61 @@ class ADBWohneinheit extends mfBaseModel {
|
||||
// Statuschange from Rimo statuschange
|
||||
AddressDB::handleRimoStatusUpdate($this->id);
|
||||
|
||||
// ADBWohneinheit_onSave_noAutoUnitCount can be defined if doing bulk
|
||||
// operations where unit count is calculated seperately
|
||||
if(!defined("ADBWohneinheit_onSave_noAutoUnitCount") || !ADBWohneinheit_onSave_noAutoUnitCount) {
|
||||
$unit_count = ADBWohneinheitModel::count(['hausnummer_id' => $this->hausnummer_id]);
|
||||
$hausnummer = $this->getProperty("hausnummer");
|
||||
if($hausnummer->id) {
|
||||
$hausnummer->unit_count = $unit_count;
|
||||
$hausnummer->save();
|
||||
}
|
||||
|
||||
if($hausnummer->netzgebiet_id) {
|
||||
$netzgebiet = new ADBNetzgebiet($hausnummer->netzgebiet_id);
|
||||
if($netzgebiet->id) {
|
||||
$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($hausnummer->rimo_type) {
|
||||
$gda_egenschaft = strtolower($hausnummer->rimo_type);
|
||||
}
|
||||
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;
|
||||
}
|
||||
$this->refreshUnitCount();
|
||||
}
|
||||
|
||||
public function refreshUnitCount() {
|
||||
// ADBWohneinheit_onSave_noAutoUnitCount can be defined if doing bulk
|
||||
// operations where unit count is calculated seperately
|
||||
|
||||
if(!defined("ADBWohneinheit_onSave_noAutoUnitCount") || !ADBWohneinheit_onSave_noAutoUnitCount) {
|
||||
$unit_count = ADBWohneinheitModel::count(['hausnummer_id' => $this->hausnummer_id]);
|
||||
$hausnummer = $this->getProperty("hausnummer");
|
||||
if($hausnummer->id) {
|
||||
$hausnummer->unit_count = $unit_count;
|
||||
$hausnummer->save();
|
||||
}
|
||||
//var_dump($netzgebiet);
|
||||
$netzgebiet->unit_count = $unit_count;
|
||||
$netzgebiet->save();
|
||||
}
|
||||
|
||||
if($hausnummer->netzgebiet_id) {
|
||||
$netzgebiet = new ADBNetzgebiet($hausnummer->netzgebiet_id);
|
||||
if($netzgebiet->id) {
|
||||
$unit_count = ADBWohneinheitModel::count(['netzgebiet_id' => $hausnummer->netzgebiet_id]);
|
||||
if($unit_count) {
|
||||
$unit_count_gda = [
|
||||
"sd" => 0,
|
||||
"md" => 0,
|
||||
];
|
||||
foreach(ADBHausnummerModel::search(["netzgebiet_id" => $hausnummer->netzgebiet_id]) as $hausnummer) {
|
||||
$gda_egenschaft = strtolower($hausnummer->gdaeigenschaft);
|
||||
if($hausnummer->rimo_type) {
|
||||
$gda_egenschaft = strtolower($hausnummer->rimo_type);
|
||||
}
|
||||
|
||||
if(!$gda_egenschaft) {
|
||||
$unit_count_gda["sd"] += $hausnummer->unit_count;
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
//var_dump($netzgebiet);
|
||||
$netzgebiet->unit_count = $unit_count;
|
||||
$netzgebiet->save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function resetSaveNesting() {
|
||||
|
||||
30
scripts/addressdb/recalculate-netzgebiet-unit_count.php
Normal file
30
scripts/addressdb/recalculate-netzgebiet-unit_count.php
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
//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);
|
||||
|
||||
foreach(ADBNetzgebietModel::getAll() as $netzgebiet) {
|
||||
foreach(ADBHausnummerModel::search(["netzgebiet_id" => $netzgebiet->id]) as $hausnummer) {
|
||||
if(!count($hausnummer->wohneinheiten)) continue;
|
||||
|
||||
$wohneinheiten = $hausnummer->wohneinheiten;
|
||||
$unit = array_shift($wohneinheiten);
|
||||
$unit->save();
|
||||
|
||||
echo "Updated ".$netzgebiet->name."\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user