Files
thetool/scripts/addressdb/addressdb_home_update.php
2022-10-11 18:23:26 +02:00

96 lines
2.7 KiB
PHP
Executable File

#!/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);
$filename = BASEDIR."/scripts/import/premstaetten_homes.csv";
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$log = mfLoghandler::singleton();
$input = fopen($filename, "r");
$h_extrefs = [];
$updated = 0;
$current_home_count = 0;
$current_building_extref = "";
$i = 0;
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1) continue;
if(!trim($csv[0])) {
continue;
} else {
$building_extref = $csv[1];
$wohneinheit_extref = $csv[0];
if($current_building_extref != $building_extref) {
// check how many homes we imported for this building
$hausnummer = ADBHausnummerModel::getFirst(['extref' => $current_building_extref]);
if($hausnummer) {
$units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]);
if(count($units) < $current_home_count) {
echo "Weniger Homes vorhanden, als importiert (hausnummer extref: ".$hausnummer->extref.") building_extref $building_extref\n";
}
}
$current_home_count = 0;
}
$current_home_count++;
$current_building_extref = $building_extref;
$hausnummer = ADBHausnummerModel::getFirst(['extref' => $building_extref]);
if(!$hausnummer) {
echo "Hausnummer nicht gefunden: $building_extref\n";
continue;
}
$existing_units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]);
$current_unit = $existing_units[$current_home_count -1];
if(!$current_unit) {
// transmission stations may not have been created
//echo "unit for $wohneinheit_extref not found\n";
continue;
}
$current_unit->extref = $wohneinheit_extref;
$current_unit->save();
$updated++;
/*if($current_home_count > count($existing_units)) {
echo "creating unit $wohneinheit_extref (hausnummer $building_extref)\n";
$unit_data = [
'extref' => $wohneinheit_extref,
'hausnummer_id' => $hausnummer->id,
'num' => $current_home_count,
'tuer' => $current_home_count,
'nutzung' => "Wohnung"
];
$unit = ADBWohneinheitModel::create($unit_data);
//var_dump($unit);exit;
//if(!$unit->save()) {
// die("Error saving new unit\n");
//}
}*/
}
}
echo "Updated $updated units\n";