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() {
|
||||
|
||||
Reference in New Issue
Block a user