diff --git a/application/Preorder/PreorderModel.php b/application/Preorder/PreorderModel.php index 1fee012ed..900cb0b4d 100644 --- a/application/Preorder/PreorderModel.php +++ b/application/Preorder/PreorderModel.php @@ -110,9 +110,96 @@ class PreorderModel } - public function createFromRimoWorkorder($workorder) + public static function createFromRimoWorkorder($workorder) { + if(!is_object($workorder)) return false; + $log = mfLoghandler::singleton(); + + $wo_home_external_id = $workorder->adb_wohneinheit->extref; + $rimo_workorder_id = $workorder->rimo_id; + + if(!$wo_home_external_id || !$rimo_workorder_id) { + $log->warn(__METHOD__.": Missing external_id or workorder_id for workorder: ".$workorder->id); + return false; + } + + $home = \ADBWohneinheitModel::getFirst(["extref" => $wo_home_external_id]); + if(!$home) { + $log->warn(__METHOD__.": No home found with external_id: ".$wo_home_external_id); + return false; + } + + $sbidi_order = SbidiOrder::getFirst(["workorder_rimo_id" => $rimo_workorder_id]); + if(!$sbidi_order) return false; + + $network = \NetworkModel::getFirst(["adb_netzgebiet_id" => $home->hausnummer->netzgebiet_id]); + if(!$network) { + $log->warn(__METHOD__.": No network found for home: ".$home->id. " building: ".$home->hausnummer_id); + return false; + } + + $campaign = PreordercampaignModel::getFirst(["network_id" => $network->id]); + if(!$campaign) { + $log->warn(__METHOD__.": No campaign found for network: ".$network->id); + return false; + } + + try { + $order_date = new DateTime($sbidi_order->order_date); + $order_date->setTimezone(new DateTimeZone("Europe/Vienna")); + } catch(Exception $e) { + $log->error(__METHOD__.": Error parsing order_date in ".$sbidi_order->workorder_rimo_id." : ".$e->getMessage()); + return false; + } + + + $preorder = self::create([ + "preordercampaign_id" => $campaign->id, + "adb_hausnummer_id" => $home->hausnummer_id, + "adb_wohneinheit_id" => $home->id, + "extref" => $sbidi_order->order_name, + "connection_type" => ($sbidi_order->building_type == "md") ? "multi-dwelling" : "single-dwelling", + "type" => "provision", + "accept_agb" => 1, + "accept_dsgvo" => 1, + "accept_marketing" => 1, + "accept_withdrawal" => 1, + "accept_digging" => 1, + "contact_type" => "owner", + "uid" => $sbidi_order->uid, + "firstname" => $sbidi_order->firstname ?: "", + "lastname" => $sbidi_order->lastname ?: "", + "street" => $sbidi_order->address ?: "", + "zip" => $sbidi_order->zip ?: "", + "city" => $sbidi_order->city ?: "", + "phone" => $sbidi_order->phone ?: "", + "email" => $sbidi_order->email ?: "", + "submit_type" => "import", + "order_date" => $order_date->getTimestamp(), + ]); + $preorder->createUcode(); + + $new_status = false; + if($preorder->adb_wohneinheit_id) { + $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_wohneinheit->status->code]); + if($preorder->adb_hausnummer->status->code > $preorder->adb_wohneinheit->status->code) { + $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_hausnummer->status->code]); + } + } elseif($preorder->adb_hausnummer_id) { + $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_hausnummer->status->code]); + } + if($new_status) { + $preorder->status_id = $new_status->id; + } else { + $preorder->status_id = 1; + } + + if(!$preorder->save()) { + return false; + } + + return $preorder; } public static function getAll() diff --git a/application/SbidiOrder/SbidiOrder.php b/application/SbidiOrder/SbidiOrder.php new file mode 100644 index 000000000..e92487f2b --- /dev/null +++ b/application/SbidiOrder/SbidiOrder.php @@ -0,0 +1,205 @@ +loadMe(); + $data["edit_by"] = $me->id; + } + + return $data; + } + + + + public function getProperty($name) { + if($this->$name == null) { + + if($name == "creator") { + $this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by); + if($this->creator === null) { + $this->creator = new User($this->create_by); + if($this->creator->id) { + mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator); + } + } + return $this->creator; + } + + if($name == "editor") { + $this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by); + if($this->editor === null) { + $this->editor = new User($this->edit_by); + if($this->editor->id) { + mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor); + } + } + return $this->editor; + } + + $classname = ucfirst($name); + $idfield = $name."_id"; + $this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield); + if(!$this->$name) { + $this->$name = new $classname($this->$idfield); + } + + if($this->$name->id) { + mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name); + return $this->$name; + } else { + return null; + } + + } + + return $this->$name; + } + + /******************************** + * Begin static Model functions + */ + + public static function create(Array $data) { + $model = new SbidiOrder(); + + $table_fields = [ + "name","building_rimo_id","home_rimo_id","workorder_rimo_id","order_date","building_type","uid","firstname","lastname","phone","address","zip","city","country","email", + "create_by","edit_by","create","edit" + ]; + + foreach($data as $field => $value) { + if(in_array($field, $table_fields)) { + $model->$field = $value; + } + } + + $me = new User(); + $me->loadMe(); + + if($model->create_by === null) { + $model->create_by = $me->id; + } + if($model->edit_by === null) { + $model->edit_by = $me->id; + } + + return $model; + } + + public static function getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("SbidiOrder", "*", "1 = 1 ORDER BY id"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new SbidiOrder($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM SbidiOrder + WHERE $where + ORDER BY id LIMIT 1"; + //var_dump($sql);exit; + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new SbidiOrder($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function search($filter, $limit = false, $order = false) { + $items = []; + + if(!$order) { + $order = "id ASC"; + } + + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT SbidiOrder.* FROM SbidiOrder + WHERE $where + ORDER BY $order"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['count']; + } + } + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[$data->id] = new SbidiOrder($data); + } + } + + return $items; + } + + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + + if(array_key_exists("name", $filter)) { + $name = FronkDB::singleton()->escape($filter["name"]); + if($name) { + $where .= " AND name='$name'"; + } + } + + if(array_key_exists("building_rimo_id", $filter)) { + $building_rimo_id = FronkDB::singleton()->escape($filter["building_rimo_id"]); + if($building_rimo_id) { + $where .= " AND building_rimo_id='$building_rimo_id'"; + } + } + + if(array_key_exists("home_rimo_id", $filter)) { + $home_rimo_id = FronkDB::singleton()->escape($filter["home_rimo_id"]); + if($home_rimo_id) { + $where .= " AND home_rimo_id='$home_rimo_id'"; + } + } + + if(array_key_exists("workorder_rimo_id", $filter)) { + $workorder_rimo_id = FronkDB::singleton()->escape($filter["workorder_rimo_id"]); + if($workorder_rimo_id) { + $where .= " AND workorder_rimo_id='$workorder_rimo_id'"; + } + } + + + if(array_key_exists("add-where", $filter)) { + $where .= " ".$filter['add-where']; + } + + //var_dump($filter, $where);exit; + return $where; + } + +} diff --git a/db/migrations/20250306134255_create_sbidi_order.php b/db/migrations/20250306134255_create_sbidi_order.php new file mode 100644 index 000000000..fad50e156 --- /dev/null +++ b/db/migrations/20250306134255_create_sbidi_order.php @@ -0,0 +1,52 @@ +getEnvironment() == "thetool") { + $table = $this->table("SbidiOrder"); + + $table->addColumn("name", "string", ["limit" => 64, "null" => true]); + $table->addColumn("building_rimo_id", "string", ["limit" => 64, "null" => true]); + $table->addColumn("home_rimo_id", "string", ["limit" => 64, "null" => true]); + $table->addColumn("workorder_rimo_id", "string", ["limit" => 64, "null" => true]); + $table->addColumn("order_date", "datetime", ["null" => true]); + $table->addColumn("building_type", "string", ["limit" => 64, "null" => true]); // SD/MD + $table->addColumn("uid", "string", ["limit" => 64, "null" => true]); + $table->addColumn("firstname", "string", ["limit" => 255, "null" => true]); + $table->addColumn("lastname", "string", ["limit" => 255, "null" => true]); + $table->addColumn("phone", "string", ["limit" => 255, "null" => true]); + $table->addColumn("address", "string", ["limit" => 255, "null" => true]); + $table->addColumn("zip", "string", ["limit" => 255, "null" => true]); + $table->addColumn("city", "string", ["limit" => 255, "null" => true]); + $table->addColumn("country", "string", ["limit" => 255, "null" => true]); + $table->addColumn("email", "string", ["limit" => 255, "null" => true]); + + $table->addColumn("create_by", "integer", ["null" => false]); + $table->addColumn("edit_by", "integer", ["null" => false]); + $table->addColumn("create", "integer", ["null" => false]); + $table->addColumn("edit", "integer", ["null" => false]); + + $table->create(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table("SbidiOrder")->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/lib/Sbidi/AnoApi.php b/lib/Sbidi/AnoApi.php new file mode 100644 index 000000000..b618ff34f --- /dev/null +++ b/lib/Sbidi/AnoApi.php @@ -0,0 +1,53 @@ + [ + 'method' => 'GET', + 'header' => join("\n", $headers), + ] + ]; + + $qs = http_build_query($params); + + $queryGetOrders = $api_url.SBIDIANO_API_EP_GET_ORDERS; + $get_url = $queryGetOrders."?".$qs; + $ctx = stream_context_create($ctx_opts); + $log->debug(__METHOD__.": Getting SBIDI ANO Orders : $get_url"); + + $response = file_get_contents($get_url, false, $ctx); + + if($response === false) { + $log->error(__METHOD__.": Fehler beim Abfragen der SBIDI Orders"); + return false; + } + + $resp_data = json_decode($response); + if(!is_object($resp_data)) { + $log->error(__METHOD__.": Fehler beim Abfragen der SBIDI Orders! Invalid Response!"); + return false; + } + + return $resp_data; + } + + +} \ No newline at end of file diff --git a/scripts/adb-rimo-import/rimo-import.php b/scripts/adb-rimo-import/rimo-import.php index 1f4d9eaf1..2b77ffa3f 100755 --- a/scripts/adb-rimo-import/rimo-import.php +++ b/scripts/adb-rimo-import/rimo-import.php @@ -191,7 +191,11 @@ foreach ($clusters as $cluster_data) { } + $option_create_preorder = false; $preorder_only_oaid = false; + $option_import_sbidi_orders = false; + $option_import_sbidi_orders = $adb_netzgebiet->getOption("create_preorder"); + $option_create_preorder = $adb_netzgebiet->getOption("create_preorder"); $preorder_only_oaid = $adb_netzgebiet->getOption("preorder_only_oaid"); $option_wo_ignore_status = $adb_netzgebiet->getOption("wo_ignore_status"); @@ -223,20 +227,7 @@ foreach ($clusters as $cluster_data) { if (is_object($buildingsResponse) && property_exists($buildingsResponse, "item") && is_array($buildingsResponse->item) && count($buildingsResponse->item)) { foreach ($buildingsResponse->item as $building) { - //var_dump($building); - /*if($building->buildingType && $building->buildingType->userLabel == "Greenfield") { - //echo $building->id.": ignoring Greenfield\n"; - continue; - }*/ $hausnummer_count++; - //if($hausnummer_count < 300) continue; - - /* - // ignore buildings without units - if(!$building->plannedTU) { - echo $building->id.": no planned TUs\n"; - continue; - }*/ $rimo_building_id = $building->id; @@ -659,8 +650,70 @@ foreach ($clusters as $cluster_data) { } /* - * get workorders - */ + * get SBIDI Orders, to create Preorders later + */ + + if($option_import_sbidi_orders) { + $sbidi_orders_resp = \Sbidi_AnoApi::getOrders($cluster_rimo_id); + if(!$sbidi_orders_resp) { + echo "Error fetching SBIDI Orders\n"; + } else { + if(!property_exists($sbidi_orders_resp, "orders") || !is_array($sbidi_orders_resp->orders) || !count($sbidi_orders_resp->orders)) { + echo "No Orders in SBIDI Orders response\n"; + } else { + foreach($sbidi_orders_resp->orders as $order) { + + if(!is_array($order->homes) || !count($order->homes)) continue; + foreach($order->homes as $home) { + if(!$home->home_id || !$home->work_order_id) continue; + + $sorder = \SbidiOrder::getFirst(["home_rimo_id" => $home->home_id]); + if(!$sorder) { + $sorder = \SbidiOrder::getFirst(["workorder_rimo_id" => $home->work_order_id]); + } + + if(!$sorder) { + // create SbidiOrder + $sorder = \SbidiOrder::create([ + "building_rimo_id" => $order->building_id, + "home_rimo_id" => $home->home_id, + "workorder_rimo_id" => $home->work_order_id, + ]); + } + + $sorder->update([ + "uid" => $order->customer->uid, + "firstname" => $order->customer->firstname, + "lastname" => $order->customer->lastname, + "address" => $order->customer->address, + "zip" => $order->customer->zip, + "city" => $order->customer->city, + "country" => $order->customer->country, + "email" => $order->customer->email, + "phone" => $order->customer->phone, + "order_date" => ($order->order_date) ? : null, + ]); + + $building_type = "sd"; + foreach($order->lines as $line) { + if(strpos("MPH", $line->product_code) !== false) { + $building_type = "md"; + break; + } + } + + $sorder->building_type = $building_type; + $sorder->save(); + } + + } + } + } + } + + /* + * get workorders + */ $params = $baseParams; $params["clusterId"] = $cluster_rimo_id; @@ -684,11 +737,16 @@ foreach ($clusters as $cluster_data) { if (is_object($workordersResponse) && property_exists($workordersResponse, "item") && is_array($workordersResponse->item) && count($workordersResponse->item)) { foreach ($workordersResponse->item as $workorder) { //$wo_building_external_id = $workorder->location->id; - $wo_home_external_id = $workorder->home->id; + $wo_home = false; + + $wo_home_external_id = false; + if(is_object($workorder->home) && $workorder->home->id) { + $wo_home_external_id = $workorder->home->id; + } + $rimo_workorder_id = $workorder->id; $rimo_workorder_name = $workorder->name; $workorder_status = $workorder->state->userLabel; - $workorder_home_id = $workorder->home->id; $team_id = (is_array($workorder->teams->item) && count($workorder->teams->item)) ? $workorder->teams->item[0]->id : null; $team_name = (is_array($workorder->teams->item) && count($workorder->teams->item)) ? $workorder->teams->item[0]->name : null; @@ -701,7 +759,7 @@ foreach ($clusters as $cluster_data) { $addressErrors[] = "Wohneinheit für Workorder ".$wo->rimo_name." hat sich geändert von ".$wo->adb_wohneinheit_id." auf ".$wo_home->extref." (aber wurde nicht im Tool übernommen)"; } } else { - $addressErrors[] = "Wohneinheit für Workorder ".$wo->name." ist jetzt leer"; + $addressErrors[] = "Wohneinheit für Workorder ".$wo->rimo_name." ist jetzt leer"; } //echo "Updating Workorder $rimo_workorder_id ($workorder_home_id)\n"; @@ -720,7 +778,10 @@ foreach ($clusters as $cluster_data) { $wo->save(); } } else { - $wo_home = \ADBWohneinheitModel::getFirst(["extref" => $workorder_home_id]); + if($workorder_status == "Deleted") continue; + + if(!$wo_home_external_id) continue; + $wo_home = \ADBWohneinheitModel::getFirst(["extref" => $wo_home_external_id]); if (!$wo_home) { //echo "Home zu Workorder $rimo_workorder_id ($workorder_home_id) nicht gefunden\n"; @@ -741,7 +802,23 @@ foreach ($clusters as $cluster_data) { "rimo_team_name" => $team_name ]); if (!$wo->save()) { - echo "Fehler beim Erstellen der RimoWorkorder $rimo_workorder_id ($workorder_home_id)\n"; + echo "Fehler beim Erstellen der RimoWorkorder $rimo_workorder_id ($wo_home_external_id)\n"; + continue; + } + } + + // create Preorder if requested + if($option_create_preorder && $wo_home) { + if(\PreorderModel::getFirst(["adb_wohneinheit_id" => $wo_home->id])) { + //echo "Preorder already exists for Workorder $rimo_workorder_id ($wo_home_external_id)\n"; + continue; + } + + $preorder = \PreorderModel::createFromRimoWorkorder($wo); + if(!$preorder) { + echo "Error creating Preorder for Workorder $rimo_workorder_id ($wo_home_external_id)\n"; + } else { + //var_dump($preorder); } } }