441 lines
14 KiB
PHP
Executable File
441 lines
14 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");
|
|
|
|
$partner_id = 209;
|
|
$netzgebiet_id = 33;
|
|
$connectionTypes = [
|
|
"Einfamilienhaus" => "single-dwelling",
|
|
"Einfahmilienhaus" => "single-dwelling",
|
|
"Mehrfamilienhaus" => "multi-dwelling",
|
|
"Mehrparteienhaus" => "apartment-building",
|
|
"Wohneinheit in Mehrparteienhaus" => "apartment",
|
|
"Wohnung" => "apartment",
|
|
"Geschäft" => "business",
|
|
"Gewerbebetrieb" => "business",
|
|
];
|
|
$preorderTypes = [
|
|
"Interessensbekundung" => "interest",
|
|
"Vorsorgeanschluss" => "provision",
|
|
"Vollanschluss" => "order",
|
|
"Nachbestellung" => "reorder",
|
|
];
|
|
|
|
$me = new User(1);
|
|
|
|
$folder = __DIR__."/import/";
|
|
$csvname = "Bestellungen Wettmannstätten.csv";
|
|
$filename = $folder.$csvname;
|
|
|
|
$adb = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
$db = FronkDB::singleton();
|
|
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$input = fopen($filename, "r");
|
|
|
|
$l = 0;
|
|
$c = 0;
|
|
$u = 0;
|
|
$w = 0;
|
|
while($csv = fgetcsv($input, 0, ";")) {
|
|
$l++;
|
|
if($l == 1) continue;
|
|
|
|
if(!trim($csv[0])) {
|
|
continue;
|
|
}
|
|
|
|
//var_dump($csv);exit;
|
|
|
|
$campaign_name = trim($csv[0]);
|
|
$netzgebiet_name = trim($csv[2]);
|
|
$bestelltyp = trim($csv[6]);
|
|
$anschlusstyp = trim($csv[8]);
|
|
$adrcd = trim($csv[9]);
|
|
$strasse_name = trim($csv[10]);
|
|
$hausnummer_name = trim($csv[11]);
|
|
$plz_name = trim($csv[12]);
|
|
$ort_name = trim($csv[13]);
|
|
$unit_string = trim($csv[14]);
|
|
$connection_count = trim($csv[17]);
|
|
$kunde_firma = trim($csv[18]);
|
|
$kunde_uid = trim($csv[19]);
|
|
$kunde_vorname = trim($csv[20]);
|
|
$kunde_nachname = trim($csv[21]);
|
|
$kunde_strasse = trim($csv[22]);
|
|
$kunde_tuer = "";
|
|
$kunde_plz = trim($csv[23]);
|
|
$kunde_ort = trim($csv[24]);
|
|
$kunde_phone = trim($csv[25]);
|
|
$kunde_email = trim($csv[26]);
|
|
$partner_name = trim($csv[27]);
|
|
$tuer = 0;
|
|
|
|
$m = [];
|
|
if(preg_match('@ /\s+(?:tür|top)\s+(\d+)$@i', $kunde_strasse, $m)) {
|
|
if($m[1]) $kunde_tuer = $m[1];
|
|
}
|
|
|
|
if($unit_string && preg_match('/(?:tür|top)\s+([0-9.,]+[a-z]*)/i', $unit_string, $m)) {
|
|
$tuer = $m[1];
|
|
}
|
|
|
|
if(!$connection_count) $connection_count = 1;
|
|
|
|
if(!$bestelltyp || !$anschlusstyp || !$strasse_name || !$hausnummer_name || !$plz_name || !$ort_name || !$connection_count || (!$kunde_nachname && !$kunde_vorname && !$kunde_firma) || (!$kunde_phone && !$kunde_email)) {
|
|
echo "incomplete row $l\n";
|
|
continue;
|
|
}
|
|
|
|
if($strasse_name == "Wettmannstätten" && $hausnummer_name == "78" && $unit_string == "") {
|
|
$tuer = 2;
|
|
}
|
|
|
|
if($strasse_name == "Zehndorfa") $strasse_name = "Zehndorf";
|
|
|
|
$strasse_name = $db->escape($strasse_name);
|
|
$hausnummer_name = $db->escape($hausnummer_name);
|
|
|
|
$strasse_search = [$strasse_name];
|
|
|
|
if(strpos($strasse_name, ' ') !== false) $strasse_search[] = str_replace(' ', '-', $strasse_name);
|
|
if(strpos($strasse_name, '-') !== false) $strasse_search[] = str_replace('-', ' ', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.', '. ', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.', '.-', $strasse_name);
|
|
if(strpos($strasse_name, '. ') !== false) $strasse_search[] = str_replace('. ', '.', $strasse_name);
|
|
if(strpos($strasse_name, '. ') !== false) $strasse_search[] = str_replace('. ', '.-', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.-', '.', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.-', '. ', $strasse_name);
|
|
|
|
foreach($strasse_search as $search) {
|
|
if(strpos($search, 'ß') !== false) $strasse_search[] = str_replace('ß', 'ss', $search);
|
|
if(strpos($search, 'ä') !== false) $strasse_search[] = str_replace('ä', 'ae', $search);
|
|
if(strpos($search, 'ö') !== false) $strasse_search[] = str_replace('ö', 'oe', $search);
|
|
if(strpos($search, 'ü') !== false) $strasse_search[] = str_replace('ü', 'ue', $search);
|
|
|
|
if(strpos($search, 'ss') !== false) $strasse_search[] = str_replace('ss', 'ß', $search);
|
|
if(strpos($search, 'ae') !== false) $strasse_search[] = str_replace('ae', 'ä', $search);
|
|
if(strpos($search, 'oe') !== false) $strasse_search[] = str_replace('oe', 'ö', $search);
|
|
if(strpos($search, 'ue') !== false) $strasse_search[] = str_replace('ue', 'ü', $search);
|
|
}
|
|
|
|
|
|
$sql = "SELECT * FROM view_hausnummer WHERE netzgebiet_id = ".$netzgebiet_id." AND strasse IN ('". implode("', '", $strasse_search)."') AND hausnummer='$hausnummer_name'";
|
|
$res = $adb->query($sql);
|
|
|
|
if(!$adb->num_rows($res)) {
|
|
if(preg_match('/^(schilcherland.+|Neuberg|Kastaniensiedlung|rosenhof|am kluggrund|Reicherfeldweg)$/i', $strasse_name)) continue;
|
|
echo "=== $strasse_name $hausnummer_name: Adresse nicht gefunden!\n";
|
|
continue;
|
|
}
|
|
|
|
$data = $adb->fetch_object($res);
|
|
|
|
$netzgebiet_id = $data->netzgebiet_id;
|
|
$hausnummer_id = $data->hausnummer_id;
|
|
|
|
if(!$netzgebiet_id || !$hausnummer_id) {
|
|
echo "=== Netzgebiet oder Hausnummer leer\n";
|
|
continue;
|
|
}
|
|
|
|
$network = NetworkModel::getFirst(["adb_network_id" => $netzgebiet_id]);
|
|
if(!$network) {
|
|
echo "thetool Netzgebiet nicht gefunden (adb netzgebiet id $netzgebiet_id)\n";
|
|
continue;
|
|
}
|
|
$campaign = PreordercampaignModel::getFirst(["network_id" => $network->id]);
|
|
if(!$campaign) {
|
|
echo "Kampagne für Network ".$network->id." ".$network->name." nicht gefunden\n";
|
|
continue;
|
|
}
|
|
|
|
//echo $campaign->name." (".$network->name.")\n";
|
|
|
|
//var_dump($data);exit;
|
|
$hausnummer = new ADBHausnummer($data->hausnummer_id);
|
|
|
|
$wohneinheiten = ADBWohneinheitModel::search(["hausnummer_id" => $hausnummer->id]);
|
|
$unit_count = count($wohneinheiten);
|
|
if(!$unit_count) {
|
|
echo "=== $strasse_name $hausnummer_name: Keine Wohneinheiten gefunden\n";
|
|
continue;
|
|
}
|
|
|
|
|
|
$product_search = ['external_id' => $partner_id,
|
|
'attributename' => "presales",
|
|
'attributevalue' => ($preorderTypes[$bestelltyp] == "order") ? "activation" : $preorderTypes[$bestelltyp]
|
|
];
|
|
$product = ProductModel::getFirst($product_search);
|
|
|
|
if(!$product) {
|
|
echo "Kein product gefunden: $bestelltyp\n";
|
|
continue;
|
|
}
|
|
|
|
$price = $product->price;
|
|
$price_setup = $product->price_setup;
|
|
|
|
$pdata = [
|
|
'preordercampaign_id' => $campaign->id,
|
|
'adb_hausnummer_id' => $hausnummer->id,
|
|
'status_id' => 1,
|
|
'connection_type' => $connectionTypes[$anschlusstyp],
|
|
'connection_count' => 1,
|
|
'product_id' => null,
|
|
'setup_product_id' => $product->id,
|
|
'type' => $preorderTypes[$bestelltyp],
|
|
'price' => null,
|
|
'price_setup' => $price_setup,
|
|
'price_nne' => null,
|
|
'price_nbe' => null,
|
|
'billing_delay' => null,
|
|
'billing_period' => null,
|
|
'partner_id' => $partner_id,
|
|
'accept_agb' => 1,
|
|
'accept_dsgvo' => 1,
|
|
'accept_marketing' => 1,
|
|
'accept_withdrawal' => 1,
|
|
'contact_type' => "owner",
|
|
'company' => ($kunde_firma) ? $kunde_firma : null,
|
|
'uid' => ($kunde_uid) ? $kunde_uid : null,
|
|
'firstname' => $kunde_vorname,
|
|
'lastname' => $kunde_nachname,
|
|
'street' => $kunde_strasse,
|
|
'housenumber' => null,
|
|
'tuer' => null,
|
|
'zip' => $kunde_plz,
|
|
'city' => $kunde_ort,
|
|
'phone' => $kunde_phone,
|
|
'email' => $kunde_email,
|
|
'submit_type' => "import",
|
|
'create_by' => 1,
|
|
'edit_by' => 1
|
|
|
|
];
|
|
|
|
|
|
|
|
if($connection_count == 1) {
|
|
if($unit_count === 1) {
|
|
//continue;
|
|
$unit = $wohneinheiten[0];
|
|
|
|
// check if wohneinheit in existing preorder
|
|
if(PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id])) {
|
|
echo "=== $strasse_name $hausnummer_name: [1] Wohneinheit schon vergeben (unit id ".$unit->id."\n";
|
|
continue;
|
|
}
|
|
|
|
if(!$unit->oaid) {
|
|
$unit->oaid = $unit->getNewOAID();
|
|
$unit->save();
|
|
}
|
|
|
|
//create single preorder
|
|
$pdata['adb_wohneinheit_id'] = $unit->id;
|
|
|
|
$preorder = PreorderModel::create($pdata);
|
|
$preorder->ucode = $preorder->createUcode();
|
|
$preorder->oaid = $unit->oaid;
|
|
if(!$preorder->save()) {
|
|
var_dump($preorder);exit;
|
|
}
|
|
|
|
|
|
continue;
|
|
}
|
|
|
|
|
|
|
|
if(($unit_count > 1 && $unit_count <= 20) || ($unit_count > 1 && $tuer)) {
|
|
// assume its single-dwelling with erroneously high door count
|
|
$unit_candidates = [];
|
|
$fixed_candidate = false;
|
|
|
|
foreach($wohneinheiten as $unit) {
|
|
if(!$unit->tuer) continue;
|
|
if($unit->tuer > 0) {
|
|
$unit_candidates[$unit->tuer] = $unit;
|
|
if($tuer && $unit->tuer == $tuer) {
|
|
$fixed_candidate = $unit;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!count($unit_candidates)) {
|
|
foreach($wohneinheiten as $unit) {
|
|
if(!$unit->zusatz) continue;
|
|
if(preg_match('/Top\s+\d+/i',$unit->zusatz)) {
|
|
$unit_candidates[$unit->zusatz] = $unit;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!count($unit_candidates)) {
|
|
$unit_candidates = $wohneinheiten;
|
|
}
|
|
|
|
if(count($unit_candidates)) {
|
|
if($fixed_candidate) {
|
|
$unit = $fixed_candidate;
|
|
} else {
|
|
ksort($unit_candidates, SORT_NUMERIC);
|
|
// try until we find an empty unit
|
|
while(count($unit_candidates)) {
|
|
$cmp_unit = array_shift($unit_candidates);
|
|
if(!PreorderModel::getFirst(["adb_wohneinheit_id" => $cmp_unit->id])) {
|
|
$unit = $cmp_unit;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!$unit) {
|
|
echo "=== $strasse_name $hausnummer_name [2] Wohneinheit schon vergeben (oder keine units) (unit id ".$unit->id.")\n";
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id])) {
|
|
echo "=== $strasse_name $hausnummer_name [2.1] Wohneinheit schon vergeben (oder keine units) (unit id ".$unit->id.")\n";
|
|
continue;
|
|
}
|
|
|
|
|
|
|
|
if(!$unit->oaid) {
|
|
$unit->oaid = $unit->getNewOAID();
|
|
$unit->save();
|
|
}
|
|
|
|
//create single preorder
|
|
$pdata['adb_wohneinheit_id'] = $unit->id;
|
|
|
|
$preorder = PreorderModel::create($pdata);
|
|
$preorder->ucode = $preorder->createUcode();
|
|
$preorder->oaid = $unit->oaid;
|
|
|
|
//var_dump($preorder);exit;
|
|
if(!$preorder->save()) {
|
|
var_dump($preorder);exit;
|
|
}
|
|
|
|
//echo "Wohneinheit saved\n";
|
|
continue;
|
|
}
|
|
}
|
|
} elseif($connection_count > 1) {
|
|
if($unit_count < $connection_count) {
|
|
/*$mu = [];
|
|
$mu['preorder'] = $preorder;
|
|
$mu['unit_count'] = $unit_count;
|
|
$missing_units[] = $mu;*/
|
|
echo "=== $strasse_name $hausnummer_name: Nicht genug Wohneinheiten ".count($wohneinheiten)." - need ".$connection_count."\n";
|
|
continue;
|
|
}
|
|
//continue;
|
|
$available_units = [];
|
|
|
|
|
|
foreach($wohneinheiten as $unit) {
|
|
if(!PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id])) {
|
|
if($unit->tuer || preg_match('/^Top\s+\d+$/', $unit->zusatz)) {
|
|
$available_units[] = $unit;
|
|
}
|
|
}
|
|
}
|
|
|
|
if($connection_count > count($available_units)) {
|
|
// this usually means this order was created already
|
|
/*$md = [];
|
|
$md["unit_count"] = $unit_count;
|
|
$md["door_count"] = count($available_units);
|
|
$md["preorder"] = $preorder;
|
|
$missing_doors[] = $md;*/
|
|
echo "=== $strasse_name $hausnummer_name: Not enough units with tuer - units total: $unit_count; units with tuer: ".count($available_units)."; need: ".$connection_count."\n";
|
|
/*foreach($available_units as $u) {
|
|
echo $u->oaid. " tuer ".$u->tuer."\n";
|
|
}*/
|
|
continue;
|
|
}
|
|
//continue;
|
|
|
|
$topnumbers = [];
|
|
if(preg_match('/(?:tür|top)\s+\d/i', $unit_string)) {
|
|
$topnumbers_string = preg_replace('/(tür|top)/i', "", $unit_string);
|
|
$topnumbers_string = preg_replace('/\s*u\.?/i', ",", $topnumbers_string);
|
|
|
|
foreach(explode(",", $topnumbers_string) as $topnumraw) {
|
|
$topnumbers[] = intval($topnumraw);
|
|
}
|
|
|
|
if(count($topnumbers) < $connection_count) {
|
|
echo "=== $strasse_name $hausnummer_name: Nicht genug Topnummern für $connection_count Anschlüsse\n";
|
|
continue;
|
|
}
|
|
}
|
|
|
|
//continue;
|
|
|
|
// create new Preorders with available units
|
|
// then set original Preorder deleted
|
|
echo "Creating $connection_count new preorders\n";
|
|
for($i = 0; $i < $connection_count; $i++) {
|
|
$unit_top_number = false;
|
|
if(count($topnumbers)) {
|
|
//echo "$strasse_name $hausnummer_name: Looking for Unit Top $topnumbers[$i]\n";
|
|
|
|
foreach($available_units as $topnumUnit) {
|
|
if($topnumUnit->tuer == $topnumbers[$i]) {
|
|
$unit = $topnumUnit;
|
|
}
|
|
}
|
|
if(!$unit) {
|
|
echo "=== $strasse_name $hausnummer_name: Unit mit Topnummer ".$topnumbers[$i]." nicht gefunden.\n";
|
|
continue;
|
|
}
|
|
//var_dump($unit);
|
|
$unit_top_number = $topnumbers[$i];
|
|
} else {
|
|
$unit = $available_units[$i];
|
|
}
|
|
//continue;
|
|
|
|
|
|
if(PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id])) {
|
|
echo "=== $strasse_name $hausnummer_name: [3] Wohneinheit schon vergeben (unit id ".$unit->id."\n";
|
|
continue;
|
|
}
|
|
|
|
$pdata['adb_wohneinheit_id'] = $unit->id;
|
|
|
|
$preorder = PreorderModel::create($pdata);
|
|
$preorder->ucode = $preorder->createUcode();
|
|
$preorder->oaid = $unit->oaid;
|
|
|
|
//var_dump($preorder);exit;
|
|
if(!$preorder->save()) {
|
|
var_dump($preorder);exit;
|
|
}
|
|
|
|
|
|
}
|
|
continue;
|
|
}
|
|
|
|
echo "=== $strasse_name $hausnummer_name: Not processed -> need $connection_count but have $unit_count units \n";
|
|
|
|
|
|
|
|
} |