diff --git a/Layout/default/Building/Form.php b/Layout/default/Building/Form.php index 0d719960f..441c068a2 100644 --- a/Layout/default/Building/Form.php +++ b/Layout/default/Building/Form.php @@ -114,9 +114,9 @@
- +
- +
diff --git a/Layout/default/Building/Index.php b/Layout/default/Building/Index.php index 7d96ab7b6..bd669702e 100644 --- a/Layout/default/Building/Index.php +++ b/Layout/default/Building/Index.php @@ -143,7 +143,7 @@ POP Typ Objekt ID - OAN ID + OAID Adresse Einheiten Status @@ -155,7 +155,7 @@ pop->name?> type->name?> code?> - oan_id?> + oaid?> street?>
zip?> city?> diff --git a/Layout/default/OpenAccessId/Index.php b/Layout/default/OpenAccessId/Index.php index 807611282..284257338 100644 --- a/Layout/default/OpenAccessId/Index.php +++ b/Layout/default/OpenAccessId/Index.php @@ -150,7 +150,14 @@ assigned) ? date("d.m.Y H:i",$oaid->assigned) : ""?> - exported_to) ? $oaid->exported_to : ""?> + + exported_to): ?> + [exported_to?>] + getExportData($oaid->exported_to); if($ed): ?> + ftu_id) ? $ed->ftu_id : ""?> + + + exported) ? date("d.m.Y H:i", $oaid->exported) : ""?> diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index 53713e477..93f29fc25 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -77,23 +77,34 @@ - +
+ +
+
+ + " /> +
+ +
+ + " /> +
- + " />
- + " />
@@ -117,7 +128,7 @@ -
+
attribute) && array_key_exists("bep_specified", $preorder->attribute) && $preorder->attribute["bep_specified"]) ? "checked='checked'" : ""?> /> Borderpoint festgelegt
- create)?> - edit)?> + + create)?>
+ edit)?> + + is(["Admin", "netowner"]) && $campaign->fulfillment == "rimo"): ?> - workorder_export_date):?> + adb_wohneinheit_id && is_object($preorder->adb_wohneinheit->rimo_workorder)):?> is("preorderfront")): ?> diff --git a/Layout/default/Preordercampaign/Admin.php b/Layout/default/Preordercampaign/Admin.php index fc6bee7a5..99c547f3a 100644 --- a/Layout/default/Preordercampaign/Admin.php +++ b/Layout/default/Preordercampaign/Admin.php @@ -30,7 +30,7 @@
- Workorders erstellen für Vorbestellungen, für die noch keine Workorders erstellt wurden:
+ Workorders erstellen für Vorbestellungen, für die noch keine Workorders erstellt wurden. Exportiert OAID und weist sie FTU zu wenn benötigt:
@@ -42,23 +42,63 @@
- -
+
+
+ + +
+
+

OAIDs in Rimo Home/FTU exportieren

+
-
- - Wohneinheiten ohne extref (SDIHome_...)
- - - Workorders konnten nicht erstellt werden. - +
+
+ Exportiert OAIDs nach Rimo und weist sie den FTUs der Homes zu: +
+
+
+
-
+
+
+

OAIDs vergeben

+
+
+
+
+ Vergibt fehlende OAIDs für Wohneinheiten und Vorbestellungen mit zugewiesener Wohneinheit + aus OAID-Pool des Netzinhabers mit in Kampagne definierter Herkunft: +
+
+ +
"> + +
+ + +
+ +
+
+
+ +
+
+
+ + + + + +
diff --git a/Layout/default/Preordercampaign/Form.php b/Layout/default/Preordercampaign/Form.php index 3a7b5a269..d90a480e0 100644 --- a/Layout/default/Preordercampaign/Form.php +++ b/Layout/default/Preordercampaign/Form.php @@ -118,6 +118,16 @@
+
+ +
+ +
+
+ diff --git a/Layout/default/Preordercampaign/Index.php b/Layout/default/Preordercampaign/Index.php index 0ff5e07d7..64d4ba0d3 100644 --- a/Layout/default/Preordercampaign/Index.php +++ b/Layout/default/Preordercampaign/Index.php @@ -50,12 +50,12 @@
- + " />
- + " />
diff --git a/application/ADBHausnummer/ADBHausnummer.php b/application/ADBHausnummer/ADBHausnummer.php index c2cb88134..22c8919fb 100644 --- a/application/ADBHausnummer/ADBHausnummer.php +++ b/application/ADBHausnummer/ADBHausnummer.php @@ -40,6 +40,15 @@ class ADBHausnummer extends mfBaseModel { return $data; }*/ + public function getAddress() { + $address = "[".$this->getProperty("strasse")->gemeinde->name."]"; + $address .= " ".$this->getProperty("plz")->plz; + $address .= " ".$this->getProperty("ortschaft")->name.","; + $address .= " ".$this->getProperty("strasse")->name; + $address .= " ".$this->hausnummer; + return $address; + } + public function getNewOAID() { if(!$this->plz_id) { return false; diff --git a/application/ADBWohneinheit/ADBWohneinheit.php b/application/ADBWohneinheit/ADBWohneinheit.php index 5a5e3bc59..c909ab960 100644 --- a/application/ADBWohneinheit/ADBWohneinheit.php +++ b/application/ADBWohneinheit/ADBWohneinheit.php @@ -2,6 +2,7 @@ class ADBWohneinheit extends mfBaseModel { private $hausnummer; + private $rimo_workorder; protected function init() { $this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); @@ -167,6 +168,11 @@ class ADBWohneinheit extends mfBaseModel { $this->hausnummer = new ADBHausnummer($this->hausnummer_id); return $this->hausnummer; } + + if($name == "rimo_workorder") { + $this->rimo_workorder = RimoWorkorderModel::getFirst(['adb_wohneinheit_id' => $this->id]); + return $this->rimo_workorder; + } $classname = ucfirst($name); $idfield = $name."_id"; diff --git a/application/AddressDB/AddressDBController.php b/application/AddressDB/AddressDBController.php index d357e4097..9f1783c93 100644 --- a/application/AddressDB/AddressDBController.php +++ b/application/AddressDB/AddressDBController.php @@ -821,7 +821,7 @@ class AddressDBController extends mfBaseController { $u['street'] = $building->strasse . " ".$building->hausnummer; $u['zip'] = $building->plz; $u['city'] = $building->ortschaft; - $u['oan_id'] = ($building->oan_id) ? $building->oan_id : ""; + $u['oaid'] = ($building->oaid) ? $building->oan_oaid : ""; $u['code'] = ($building->code) ? $building->code : ""; $u['units'] = ($building->units) ? $building->units : 1; //$u['units_used'] = $building->getUsedTerminationCount(true); diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index aa6a68877..0ef1cb2be 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -285,6 +285,9 @@ class PreorderApicontroller extends mfBaseApicontroller { if(array_key_exists(2, $m)) { $request_oaid_unit = "$request_oaid_hausnummer".$m[2]; } + } elseif(strlen($request_oaid) == 8) { + $request_oaid_hausnummer = $request_oaid; + $request_oaid_unit = $request_oaid; } } @@ -382,9 +385,23 @@ class PreorderApicontroller extends mfBaseApicontroller { } $res = $this->db()->select("view_hausnummer", "*", $where); + if(!$this->db()->num_rows($res)) { + // search for oaid in wohneinheit + $hausnummer_unit = ADBWohneinheitModel::getFirst(["oaid" => $request_oaid_hausnummer]); + if($hausnummer_unit) { + $hausnummer_id = $hausnummer_unit->hausnummer_id; + $where = "hausnummer_id=$hausnummer_id"; + if(count($this->filter_salescluster_ids)) { + $where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")"; + } + $res = $this->db()->select("view_hausnummer", "*", $where); + } + } + if(!$this->db()->num_rows($res)) { return mfResponse::NotFound(["message" => "OAID not found"]); } + $address = $this->db()->fetch_object($res); } else { $where = "1=1 "; @@ -810,7 +827,14 @@ class PreorderApicontroller extends mfBaseApicontroller { $preorder = PreorderModel::create($preorder_data); $preorder->createUcode(); //var_dump($preorder);exit; - $preorder_id = $preorder->save(); + try { + $preorder_id = $preorder->save(); + } catch (\Exception $e) { + $err_id = uniqid('err'); + $this->log->error(__METHOD__.": [$err_id] Caught Exception while saving Preorder: ".$e->getMessage()."\n".$e->getTraceAsString()); + if($unit_changed) $unit->rollbackTransaction(); + return mfResponse::InternalServerError(["error_id" => $err_id, "message" => "error id: $err_id"]); + } if(!$preorder_id || !$preorder->ucode) { if($unit_changed) $unit->rollbackTransaction(); diff --git a/application/Building/BuildingController.php b/application/Building/BuildingController.php index 35f95eb3a..32f9756df 100644 --- a/application/Building/BuildingController.php +++ b/application/Building/BuildingController.php @@ -204,7 +204,7 @@ class BuildingController extends mfBaseController { $data['lineworker_id'] = ($r->lineworker_id) ? $r->lineworker_id : null; $data['networksection_id'] = ($r->networksection_id) ? $r->networksection_id : null; - $data['oan_id'] = trim($r->oan_id); + $data['oaid'] = trim($r->oaid); $data['street'] = trim($r->street); $data['zip'] = trim($r->zip); $data['city'] = trim($r->city); @@ -299,8 +299,8 @@ class BuildingController extends mfBaseController { if($building->lineworker_id) { $data['lineworker_id'] = $building->lineworker_id; }*/ - if($building->oan_id) { - $data['oan_id'] = $building->oan_id. "." . sprintf("%03d", $i); + if($building->oaid) { + $data['oaid'] = $building->oaid. "." . sprintf("%03d", $i); } $term = TerminationModel::create($data); @@ -440,7 +440,7 @@ class BuildingController extends mfBaseController { foreach(BuildingModel::search(['network_id' => $network_id, 'code' => $search]) as $b) { $buildings[$b->id] = $b; } - foreach(BuildingModel::search(['network_id' => $network_id, 'oan_id' => $search]) as $b) { + foreach(BuildingModel::search(['network_id' => $network_id, 'oaid' => $search]) as $b) { $buildings[$b->id] = $b; } foreach(BuildingModel::search(['network_id' => $network_id, 'zip' => $search]) as $b) { @@ -458,7 +458,7 @@ class BuildingController extends mfBaseController { $u['street'] = $building->street; $u['zip'] = $building->zip; $u['city'] = $building->city; - $u['oan_id'] = $building->oan_id; + $u['oaid'] = $building->oaid; $u['code'] = $building->code; $u['units'] = ($building->units) ? $building->units : 1; $u['units_used'] = $building->getUsedTerminationCount(true); diff --git a/application/Building/BuildingModel.php b/application/Building/BuildingModel.php index 1a634382c..12b970b67 100644 --- a/application/Building/BuildingModel.php +++ b/application/Building/BuildingModel.php @@ -9,7 +9,7 @@ class BuildingModel { public $lineworker_id = null; public $networksection_id = null; public $code = null; - public $oan_id = null; + public $oaid = null; public $street = null; public $zip = null; public $city = null; @@ -215,10 +215,10 @@ class BuildingModel { } } - if(array_key_exists("oan_id", $filter)) { - $oan_id = FronkDB::singleton()->escape($filter['oan_id']); - if($oan_id) { - $where .= " AND Building.`oan_id` like '%$oan_id%'"; + if(array_key_exists("oaid", $filter)) { + $oaid = FronkDB::singleton()->escape($filter['oaid']); + if($oaid) { + $where .= " AND Building.`oaid` like '%$oaid%'"; } } diff --git a/application/OpenAccessId/OpenAccessId.php b/application/OpenAccessId/OpenAccessId.php index e8bd707dc..75601e03d 100644 --- a/application/OpenAccessId/OpenAccessId.php +++ b/application/OpenAccessId/OpenAccessId.php @@ -5,11 +5,248 @@ class OpenAccessId extends mfBaseModel { private $adb_wohneinheit; private $termination; - public function loadRandomUnassigned() { + public function loadRandomUnassigned($attributes = []) { + $where = "active = 1 AND assigned = 0 AND adb_wohneinheit_id IS NULL AND termination_id IS NULL AND exported = 0 AND exported_to IS NULL"; + if(array_key_exists("owner_id", $attributes) && $attributes['owner_id']) { + $owner_id = (int)$attributes['owner_id']; + if(!$owner_id) { + throw new Exception("Besitzer muss eine Zahl sein"); + } + $where .= " AND owner_id = $owner_id"; + } + + if(array_key_exists("origin", $attributes) && $attributes['origin']) { + $origin = $attributes['origin']; + if(!OpenAccessIdModel::getFirst(["origin" => $origin])) { + throw new Exception("Origin '$origin' not found!"); + } + $where .= " AND origin = '$origin'"; + } + + if(array_key_exists("origin_id", $attributes) && $attributes['origin_id']) { + $origin_id = $attributes['origin_id']; + if(!OpenAccessIdModel::getFirst(["origin_id" => $origin_id])) { + throw new Exception("OAID Set '$origin_id' not found!"); + } + $where .= " AND origin_id = '$origin_id'"; + } + + //var_dump($where);exit; + $res = $this->db->select("OpenAccessId", "*", "$where ORDER BY RAND() LIMIT 1"); + + if(!$this->db->num_rows($res)) { + throw new Exception("Konnte keine zufällige freie OAID laden!"); + } + $data = $this->db->fetch_object($res); + $this->load($data); + return true; } + public function exportToRimoAndAssignFtu() { + // XXX for now only support ADB Addresses + if(!$this->adb_wohneinheit_id) { + return true; + } + + if(!$this->id) { + return false; + } + + /*********************************************************************** + * check Wohneinheit external data for FTU id (SDMMaterial_), or fetch it from RIMO + */ + + $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + if(!$wohneinheit->id || !$wohneinheit->extref) { + return false; + } + + $fetch_ftu = true; + $unit_extdata = new StdClass(); + + if($wohneinheit->external_data) { + $extdata = json_decode($wohneinheit->external_data); + + if(is_object($extdata)) { + $unit_extdata = $extdata; + } + + if(is_object($extdata) && isset($extdata->rimo) && isset($extdata->rimo->ftu->id)) { + //var_dump($extdata);exit; + $fetch_ftu = false; + } + } + + if($fetch_ftu) { + // query Home to get FTU data from RIMO - GET /queryHomeWithId + $params['apiKey'] = RIMO_API_JSON_APIKEY; + $params["homeId"] = $wohneinheit->extref; + + $ctx_opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($params); + //echo $qs."\n"; + + $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_QUERY_HOME; + $get_url = $createOrderEp."?".$qs; + $ctx = stream_context_create($ctx_opts); + $this->log->debug(__METHOD__.": Getting Home to fetch FTU: $get_url"); + //exit; + $response = file_get_contents($get_url, false, $ctx); + + if($response === false) { + $this->log->error("Fehler beim auslesen der FTU ".$this->oaid."\n"); + return false; + } + //$preorder->workorder_export_date = date('U'); + //$preorder->workorder_export_data = $response; + //$preorder->save(); + $resp_data = json_decode($response); + if(!is_object($resp_data)) { + $this->log->error(__METHOD__.": OAID ".$this->oaid.": Cannot fetch Home from RIMO! Invalid Response!"); + return false; + } + + if(!is_array($resp_data->ftus->item) || !count($resp_data->ftus->item)) { + $this->log->warning(__METHOD__.": Homes ftus object has no items ".$this->oaid); + return false; + } + + $ftu_data = new StdClass(); + $ftu_data->id = $resp_data->ftus->item[0]->id; + $ftu_data->name = $resp_data->ftus->item[0]->name; + + if(!isset($unit_extdata->rimo)) { + $unit_extdata->rimo = new StdClass(); + } + + $unit_extdata->rimo->ftu = $ftu_data; + $wohneinheit->external_data = json_encode($unit_extdata); + $wohneinheit->save(); + } else { // $fetch_ftu == true + $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Home FTU id known already"); + } + + /*************************************************************************** + * check if we have RIMO OAID data or create OAID in RIMO - POST /v1/oaid-management/oaids + */ + + $existing_rimo_export_data = $this->getExportData("rimo"); + if(!is_object($existing_rimo_export_data) || !isset($existing_rimo_export_data->oaid_id) || !isset($existing_rimo_export_data->name)) { + $params = []; + $params['apiKey'] = RIMO_API_JSON_APIKEY; + $params['oaidName'] = $this->oaid; + + $ctx_opts = [ + 'http' => [ + 'method' => 'POST', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($params); + //echo $qs."\n"; + + $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CREATE_OAID; + $post_url = $createOrderEp."?".$qs; + $ctx = stream_context_create($ctx_opts); + $this->log->debug(__METHOD__.": Creating OAID in Rimo: $post_url"); + $response = file_get_contents($post_url, false, $ctx); + //var_dump($response);exit; + if($response === false) { + $this->log->error("Fehler beim Erstellen der OAID in RIMO ".$this->oaid."\n"); + $workorders_failed++; + return false; + } + + $resp_data = json_decode($response); + if(!$resp_data->id || !$resp_data->name) { + $this->log->warning(__METHOD__.": Create OAID returned no ID or oaid name ".$this->oaid); + return false; + } + + // mark OAID as exported + $oaid_export_data = new StdClass(); + if($this->export_data) { + $oaid_export_data = json_decode($this->export_data); + } + if(!isset($oaid_export_data->rimo)) { + $oaid_export_data->rimo = new StdClass(); + } + $oaid_export_data->rimo->oaid_id = $resp_data->id; + $oaid_export_data->rimo->name = $resp_data->name; + $oaid_export_data->rimo->ftu_id = null; + $this->exported_to = "rimo"; + $this->exported = date('U'); + $this->export_data = json_encode($oaid_export_data); + $this->save(); + } else { + $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Already created in RIMO"); + } + + /************************************************************************* + * assign OAID to FTU - POST /v1/oaid-management/oaids/{oaidName}/ftu/{ftuExternalId} + */ + + // check if we assigned the OAID to a RIMO FTU already + $existing_rimo_export_data = $this->getExportData("rimo"); + if(!is_object($existing_rimo_export_data) || !isset($existing_rimo_export_data->oaid_id) || !isset($existing_rimo_export_data->ftu_id)) { + $params = []; + $params['apiKey'] = RIMO_API_JSON_APIKEY; + + $ctx_opts = [ + 'http' => [ + 'method' => 'POST', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($params); + //echo $qs."\n"; + + $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_ASSIGN_OAID_TO_FTU; + $createOrderEp = str_replace("{oaidName}", $this->oaid, $createOrderEp); + $createOrderEp = str_replace("{ftuExternalId}", $unit_extdata->rimo->ftu->id, $createOrderEp); + $post_url = $createOrderEp."?".$qs; + + $ctx = stream_context_create($ctx_opts); + $this->log->debug(__METHOD__.": Assigning OAID to FTU in Rimo: $post_url"); + + $response = file_get_contents($post_url, false, $ctx); + + if($response === false) { + $this->log->error("Fehler beim Zuweisen der OAID '".$this->oaid."' zu RIMO FTU '".$unit_extdata->rimo->ftu->id."'\n"); + $workorders_failed++; + return false; + } + + // add FTU id to external_data + $oaid_export_data = new StdClass(); + if($this->export_data) { + $oaid_export_data = json_decode($this->export_data); + } + if(!isset($oaid_export_data->rimo)) { + $oaid_export_data->rimo = new StdClass(); + } + + $oaid_export_data->rimo->ftu_id = $unit_extdata->rimo->ftu->id; + $oaid_export_data->rimo->ftu_assigned_date = date('U'); + $this->export_data = json_encode($oaid_export_data); + $this->save(); + } else { + $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Already assigned to FTU in Rimo"); + } + + return true; + } + public function importFromCSV(File $file, $attributes = []) { $active = (int)$attributes['active']; $origin = $attributes['origin']; @@ -63,6 +300,20 @@ class OpenAccessId extends mfBaseModel { } } + public function getExportData($key) { + if(!$this->export_data) { + return []; + } else { + $exdata = json_decode($this->export_data); + if(!is_object($exdata)) { + return []; + } + if(isset($exdata->$key)) { + return $exdata->$key; + } + } + } + public function getProperty($name) { if($this->$name == null) { diff --git a/application/OpenAccessId/OpenAccessIdModel.php b/application/OpenAccessId/OpenAccessIdModel.php index d8bdc05fb..1149c4b3e 100644 --- a/application/OpenAccessId/OpenAccessIdModel.php +++ b/application/OpenAccessId/OpenAccessIdModel.php @@ -76,6 +76,26 @@ class OpenAccessIdModel { return null; } + public static function getFirstOaid($oaid) { + $db = FronkDB::singleton(); + + if(!$oaid) return null; + + $where = self::getSqlFilter(["oaid" => $oaid]); + mfLoghandler::singleton()->debug($where); + $res = $db->select("OpenAccessId", "*", "$where ORDER BY id"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new OpenAccessId($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + public static function count($filter) { $db = FronkDB::singleton(); diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 9ed9e1517..55418ef51 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -17,6 +17,171 @@ class Preorder extends mfBaseModel { } } + public function afterSave() { + // update preorder OAID if it's different from the unit OAID + // but only if the unit OAID is of the same origin as the campaign + $old_oaid = $this->oaid; + //$this->getOaidFromWohneinheitIfOriginMatch(); + $this->setOrCreateOaid(); + if($this->oaid != $old_oaid) { + $this->save(); + } + } + + public function setOrCreateOaid($oaid_attributes = false) { + $campaign = new Preordercampaign($this->preordercampaign_id); + if(!$campaign->id) { + return false; + } + + if(!$this->adb_wohneinheit_id) { + return false; + } + + $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + + if($this->oaid) { + // If current OAID is from correct Origin then do nothing + $current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid); + if(!$current_oaid) { + return true; // this is a thetool oaid, so do nothing + } + if($campaign->oaid_origin == $current_oaid->origin) { + // also check if wohneinheit has no oaid and set it + if(!$wohneinheit->oaid) { + $wohneinheit->oaid = $current_oaid->oaid; + $wohneinheit->save(); + } + return true; + } + } + + if($this->type == "interest") return true; + + if(!$this->id) { + $this->log->error(__METHOD__.": Tried to create OAID in unsaved Preorder"); + throw new Exception(__METHOD__.": Tried to create OAID in unsaved Preorder"); + } + + + + // use Wohneinehit OAID if available and from same origin + $unit_oaid = $this->getOaidFromWohneinheitIfOriginMatch(); + if($unit_oaid) { + $this->oaid = $unit_oaid; + $this->save(); + return true; + } + + + // else create new OAID + + if(!$campaign->network) { + $this->log->warning(__METHOD__.": Cannot create OAID: Invalid campaign Network"); + return false; + } + $netowner = new Address($campaign->network->owner_id); + if(!$netowner->id) { + $this->log->warning(__METHOD__.": Cannot create OAID: Invalid campaign Network Owner"); + return false; + } + + switch($campaign->oaid_origin) { + case "ofaa": + if(!$oaid_attributes) { + $oaid_attributes = [ + "origin" => $campaign->oaid_origin, + "owner_id" => $campaign->network->owner_id + ]; + } + + // get new OFAA OAID + $oaid = new OpenAccessId(); + $oaid->loadRandomUnassigned($oaid_attributes); + + if(!$oaid->oaid) { + $this->layout->setFlash("Keine weiteren OAIDs verfügbar", "error"); + throw new Exception("Keine weiteren OAIDs verfügbar in ".$netowner->getCompanyOrName()); + } else { + // make sure this OAID is not in use on another wohneinheit by accident + $oaid_unit_count = ADBWohneinheitModel::count(['oaid' => $oaid->oaid]); + $oaid_unit_try = 0; + while($oaid_unit_count) { + if($oaid_unit_try > 5) { + $this->log->error(__METHOD__.": Can't find random OAID which is not already used in Wohneinheit by accident after 5 tries. Giving up"); + throw new Exception("Can't find random OAID which is not already used in Wohneinheit by accident after 5 tries. Giving up"); + } + $oaid_unit_try++; + + $oaid = new OpenAccessId(); + $oaid->loadRandomUnassigned($oaid_attributes); + $oaid_unit_count = ADBWohneinheitModel::count(['oaid' => $oaid->oaid]); + } + } + + if(!$oaid->oaid) { + $this->log->error(__METHOD__.": Cannot generate OAID: OpenAccessId::loadRandomUnassigned() failed."); + return false; + } + + if($wohneinheit->oaid != $oaid->oaid) { + $wohneinheit->oaid = $oaid->oaid; + $wohneinheit->save(); + } + + $this->oaid = $oaid->oaid; + + $oaid->assigned = date('U'); + $oaid->adb_wohneinheit_id = $wohneinheit->id; + $oaid->address = $wohneinheit->hausnummer->getAddress(); + $oaid->unit_string = (string)$wohneinheit; + //var_dump($oaid);exit; + if(!$oaid->save()) { + $this->layout()->setFlash("Fehler beim Speichern der OAID für Preorder ".$preorder->id); + $this->redirect("Preordercampaign", "Admin"); + } + if(!$this->save()) { + $oaid->assigned = 0; + $oaid->adb_wohneinheit_id = null; + $oaid->address = null; + $oaid->unit_string = null; + $oaid->save(); + return false; + } + + break; + case "thetool": + // create new thethool OAID + $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + if(!$wohneinheit->oaid) { + $wohneinheit->oaid = $wohneinheit->getNewOAID(); + $wohneinheit->save(); + } + if($wohneinheit->oaid) { + $this->oaid = $wohneinheit->oaid; + } + $this->save(); + break; + } + + return true; + } + + private function getOaidFromWohneinheitIfOriginMatch() { + if($this->adb_wohneinheit_id) { + $unit = new ADBWohneinheit($this->adb_wohneinheit_id); + if($unit && $unit->oaid && $this->oaid != $unit->oaid) { + + $campaign = new Preordercampaign($this->preordercampaign_id); + $unit_oaid = OpenAccessIdModel::getFirst(["oaid" => $unit->oaid]); + + if($unit_oaid->origin == $campaign->oaid_origin) { + return $unit->oaid; + } + } + } + } + public function createUcode() { $ucode = $this->generateNewUcode(); while(PreorderModel::search(['ucode' => $ucode])) { diff --git a/application/Preorder/PreorderController.php b/application/Preorder/PreorderController.php index a6742e952..fee9a4f04 100644 --- a/application/Preorder/PreorderController.php +++ b/application/Preorder/PreorderController.php @@ -173,10 +173,21 @@ class PreorderController extends mfBaseController { if(array_key_exists("rimo_workorder", $filter)) { if($filter["rimo_workorder"] == "1") { - $new_filter['workorder_export_date'] = true; + $new_filter['rimo_workorder'] = true; } elseif($filter["rimo_workorder"] == "0") { - $new_filter['workorder_export_date'] = false; + $new_filter['rimo_workorder'] = false; } + unset($filter['rimo_workorder']); + } + + if(array_key_exists("ucode", $filter) && $filter['ucode']) { + $new_filter['ucode'] = "%".$filter['ucode']."%"; + unset($filter['ucode']); + } + + if(array_key_exists("oaid", $filter) && $filter['oaid']) { + $new_filter['oaid'] = "%".$filter['oaid']."%"; + unset($filter['oaid']); } /*if(array_key_exists("attributes", $filter) && count($filter['attributes'])) { @@ -184,7 +195,7 @@ class PreorderController extends mfBaseController { }*/ if(is_array($filter) && count($filter)) { - foreach($filter as $name => $value) { + foreach($filter as $name => $value) { $new_filter[$name] = $value; } } @@ -278,6 +289,13 @@ class PreorderController extends mfBaseController { $data['termination_id'] = ($r->termination_id) ? $r->termination_id : null; + $campaign = new Preordercampaign($r->campaign_id); + if(!$campaign->id) { + $this->layout()->setFlash("Keine Kampagne ausgewählt!", "error"); + $this->redirect("Preordercampaign"); + } + + if($campaign->product_type != "setup_only") { $data['product_id'] = $r->product_id; } @@ -373,11 +391,6 @@ class PreorderController extends mfBaseController { $preorder->update($data); } - $campaign = new Preordercampaign($preorder->preordercampaign_id); - if(!$campaign->id) { - $this->layout()->setFlash("Keine Kampagne ausgewählt!", "error"); - $this->redirect("Preordercampaign"); - } //var_dump($preorder, $r);exit; // handle new address creation if(!$preorder->adb_hausnummer_id) { @@ -600,7 +613,9 @@ class PreorderController extends mfBaseController { /* * generate ucode and save */ - $preorder->createUcode(); + if(!$preorder->ucode) { + $preorder->createUcode(); + } $new_id = $preorder->save(); if(!$new_id) { $this->layout()->setFlash("Fehler beim Speichern", "error"); diff --git a/application/Preorder/PreorderModel.php b/application/Preorder/PreorderModel.php index 1e8242094..e84b60bcc 100644 --- a/application/Preorder/PreorderModel.php +++ b/application/Preorder/PreorderModel.php @@ -135,6 +135,7 @@ class PreorderModel { LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) + LEFT JOIN `".FRONKDB_DBNAME."`.RimoWorkorder tt_rimoworkorder ON (adb_wohneinheit.id = tt_rimoworkorder.adb_wohneinheit_id) WHERE $where ORDER BY $orderBy LIMIT 1"; @@ -164,6 +165,7 @@ class PreorderModel { LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus as tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id), + LEFT JOIN `".FRONKDB_DBNAME."`.RimoWorkorder tt_rimoworkorder ON (adb_wohneinheit.id = tt_rimoworkorder.adb_wohneinheit_id) JSON_TABLE(tt_preorder.addon_services, '$[*]' COLUMNS ( ordered VARCHAR(64) PATH '$.ordered' @@ -195,6 +197,7 @@ class PreorderModel { LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus as tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id), + LEFT JOIN `".FRONKDB_DBNAME."`.RimoWorkorder tt_rimoworkorder ON (adb_wohneinheit.id = tt_rimoworkorder.adb_wohneinheit_id) JSON_TABLE(tt_preorder.addon_services, '$[*]' COLUMNS ( ordered VARCHAR(64) PATH '$.ordered' @@ -234,6 +237,7 @@ class PreorderModel { LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) + LEFT JOIN `".FRONKDB_DBNAME."`.RimoWorkorder tt_rimoworkorder ON (adb_wohneinheit.id = tt_rimoworkorder.adb_wohneinheit_id) WHERE $where "; @@ -257,6 +261,7 @@ class PreorderModel { LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) + LEFT JOIN `".FRONKDB_DBNAME."`.RimoWorkorder tt_rimoworkorder ON (adb_wohneinheit.id = tt_rimoworkorder.adb_wohneinheit_id) WHERE $where ORDER BY lastname, firstname "; @@ -445,14 +450,17 @@ class PreorderModel { if(array_key_exists("ucode", $filter)) { $ucode = FronkDB::singleton()->escape($filter['ucode']); if($ucode) { - $where .= " AND ucode = '$ucode'"; + $where .= " AND ucode LIKE '$ucode'"; } } if(array_key_exists("oaid", $filter)) { - $oaid = FronkDB::singleton()->escape($filter['oaid']); + $oaid = $filter['oaid']; if($oaid) { - $where .= " AND tt_preorder.oaid = '$oaid'"; + $oaid = FronkDB::singleton()->escape($oaid); + $where .= " AND tt_preorder.oaid LIKE '$oaid'"; + } elseif($oaid === null) { + $where .= " AND tt_preorder.oaid IS NULL"; } } @@ -468,7 +476,16 @@ class PreorderModel { if($workorder_export_date === null || $workorder_export_date === false) { $where .= " AND (tt_preorder.workorder_export_date IS NULL OR tt_preorder.workorder_export_date = 0)"; } elseif($workorder_export_date) { - $where .= " AND tt_preorder.workorder_export_date > 1"; + $where .= " AND tt_preorder.workorder_export_date > 0"; + } + } + + if(array_key_exists("rimo_workorder", $filter)) { + $rimo_workorder = $filter['rimo_workorder']; + if($rimo_workorder === null || $rimo_workorder === false) { + $where .= " AND (tt_rimoworkorder.id IS NULL)"; + } elseif($rimo_workorder) { + $where .= " AND tt_rimoworkorder.id IS NOT NULL"; } } diff --git a/application/Preordercampaign/PreordercampaignController.php b/application/Preordercampaign/PreordercampaignController.php index 58aebd4a2..ee124f97e 100644 --- a/application/Preordercampaign/PreordercampaignController.php +++ b/application/Preordercampaign/PreordercampaignController.php @@ -219,6 +219,12 @@ class PreordercampaignController extends mfBaseController { $data['fulfillment'] = "thetool"; } + if($r->oaid_origin == "ofaa") { + $data['oaid_origin'] = "ofaa"; + } else { + $data['oaid_origin'] = "thetool"; + } + if($r->product_type == "setup_only") { $data['product_type'] = "setup_only"; } elseif($r->product_type == "no_setup") { @@ -429,6 +435,83 @@ class PreordercampaignController extends mfBaseController { } + protected function assignOpenAccessIdsToPreorders() { + $this->layout()->setTemplate("Preordercampaign/Admin"); + + $id = $this->request->id; + if(!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error"); + $this->redirect("Preordercampaign"); + } + + $campaign = new Preordercampaign($id); + if(!$campaign->id) { + $this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error"); + $this->redirect("Preordercampaign"); + } + + $assigned_oaids = 0; + $multiple_unit = 0; + + foreach(PreorderModel::search(["preordercampaign_id" => $id, "oaid" => null, 'deleted' => 0]) as $preorder) { + if($preorder->type == "interest") { + continue; + } + + if($preorder->oaid) { + $this->log->warning(__METHOD__.": OAID is set already"); + continue; + } + + if(!$preorder->adb_wohneinheit_id) { + //echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n"; + continue; + } + $wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id); + if(!$wohneinheit->id) { + $this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")"); + continue; + } + + /*if(!$wohneinheit->extref) { + continue; + }*/ + + $wohneinheit_count = PreorderModel::count(["deleted" => 0, "adb_wohneinheit_id" => $wohneinheit->id]); + if($wohneinheit_count > 1) { + $this->log->error(__METHOD__.": Wohneinheit ".$wohneinheit->id." is assigned to multiple preorders!"); + $multiple_unit++; + continue; + } + + $netowner = new Address($campaign->network->owner_id); + //var_dump($netowner);exit; + // get random active OAID from network owner pool + + $oaid_attributes = [ + "owner_id" => $netowner->id, + "origin" => $campaign->oaid_origin + ]; + + if($this->request->origin_id) { + $oaid_attributes["origin_id"] = $this->request->origin_id; + } + + $preorder->setOrCreateOaid($oaid_attributes); + + if($wohneinheit->oaid != $oaid->oaid) { + $wohneinheit->oaid = $oaid->oaid; + $wohneinheit->save(); + } + + $assigned_oaids++; + + } + + $this->layout()->setFlash("$assigned_oaids OAIDs wurden Vorbestellungen zugewiesen", "success"); + $this->redirect("Preordercampaign", "Admin", ["id" => $id]); + } + protected function createRimoWorkordersAction() { $this->layout()->setTemplate("Preordercampaign/Admin"); @@ -449,7 +532,7 @@ class PreordercampaignController extends mfBaseController { $workorders_created = 0; $workorders_failed = 0; - foreach(PreorderModel::search(["preordercampaign_id" => $id, "workorder_export_date" => null, 'deleted' => 0]) as $preorder) { + foreach(PreorderModel::search(["preordercampaign_id" => $id, "rimo_workorder" => false, 'deleted' => 0]) as $preorder) { if($preorder->workorder_export_date) { $this->log->warning(__METHOD__.": workorder_export_date not null"); } @@ -468,21 +551,28 @@ class PreordercampaignController extends mfBaseController { $this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")"); continue; } - + if(!$wohneinheit->extref) { $missing_extrefs[] = $preorder; continue; } - + if(!$preorder->oaid) { - if(!$wohneinheit->oaid) { - $wohneinheit->oaid = $wohneinheit->getNewOAID(); - $wohneinheit->save(); + $preorder->setOrCreateOaid(); + if(!$preorder->oaid) { + continue; } - $preorder->oaid = $wohneinheit->oaid; - $preorder->save(); } - + + $oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid); + if(!$oaid) { + $this->log->warning(__METHOD__.": OAID '".$wohneinheit->oaid."' not found"); + continue; + } + // create and assign OAID if not yet done + $oaid->exportToRimoAndAssignFtu(); + + // create Workorder $params['apiKey'] = RIMO_API_JSON_APIKEY; $params["homeId"] = $wohneinheit->extref; $params["firstName"] = trim($preorder->firstname); @@ -515,11 +605,22 @@ class PreordercampaignController extends mfBaseController { $workorders_failed++; continue; } - $preorder->workorder_export_date = date('U'); - $preorder->workorder_export_data = $response; - $preorder->save(); - $workorders_created++; + //$preorder->workorder_export_date = date('U'); + //$preorder->workorder_export_data = $response; + //$preorder->save(); + $resp_data = json_decode($response); + if(!is_object($resp_data)) { + $this->log->error("Cannot create RimoWorkorder in Database! Invalid Response! (Preorder code: ".$preorder->ucode.") | Response: \n$response"); + throw new Exception("Cannot create RimoWorkorder in Database! Invalid Response! (Preorder code: ".$preorder->ucode.")"); + } + $wo = RimoWorkorderModel::create([ + "rimo_id" => $resp_data->id, + "rimo_name" => $resp_data->name, + "adb_wohneinheit_id" => $preorder->adb_wohneinheit_id, + "create_data" => $response + ]); + $workorders_created++; } $errors = []; @@ -551,9 +652,72 @@ class PreordercampaignController extends mfBaseController { $this->layout()->setFlash("Es konnten keine Workorders erstellt werden", "info"); } + $this->redirect("Preordercampaign", "Admin", ["id" => $id]); + } + + protected function exportOaidsToRimo() { - + $this->layout()->setTemplate("Preordercampaign/Admin"); + + $id = $this->request->id; + if(!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error"); + $this->redirect("Preordercampaign"); + } + + $campaign = new Preordercampaign($id); + if(!$campaign->id) { + $this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error"); + $this->redirect("Preordercampaign"); + } + + $oaid_assigned = 0; + $missing_units = 0; + $missing_extrefs = 0; + $missing_oaid = 0; + + foreach(PreorderModel::search(["preordercampaign_id" => $id, 'deleted' => 0]) as $preorder) { + if($preorder->type == "interest") { + $this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": is interest"); + continue; + } + + if(!$preorder->adb_wohneinheit_id) { + //echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n"; + $this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": missing unit"); + $missing_units++; + continue; + } + $wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id); + if(!$wohneinheit->id) { + $this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")"); + continue; + } + + if(!$wohneinheit->extref) { + $missing_extrefs++; + continue; + } + + if(!$preorder->oaid) { + $missing_oaid++; + continue; + } + + // TODO; OpenaccessID::exportToRimoFtu + $oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid); + if(!$oaid) { + $this->log->warning("Cannot export OAID to rimo because not found: ".$wohneinheit->oaid." (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")"); + continue; + } + + if($oaid->exportToRimoAndAssignFtu()) { + $oaid_assigned++; + } + } + + $this->layout()->setFlash("$oaid_assigned OAIDs erfolgreich erstellt und/oder FTUs zugeordnet.
$missing_units missing units
$missing_extrefs missing extref
$missing_oaid missing oaid"); $this->redirect("Preordercampaign", "Admin", ["id" => $id]); } diff --git a/application/Preordercampaign/PreordercampaignModel.php b/application/Preordercampaign/PreordercampaignModel.php index 7afe2f1f5..78a712f3d 100644 --- a/application/Preordercampaign/PreordercampaignModel.php +++ b/application/Preordercampaign/PreordercampaignModel.php @@ -5,6 +5,7 @@ class PreordercampaignModel { public $network_id; public $homes_count; public $fulfillment; + public $oaid_origin; public $product_type; public $description; public $area; diff --git a/application/RimoWorkorder/RimoWorkorder.php b/application/RimoWorkorder/RimoWorkorder.php new file mode 100644 index 000000000..2956f18d3 --- /dev/null +++ b/application/RimoWorkorder/RimoWorkorder.php @@ -0,0 +1,33 @@ +$name == null) { + + if(!$this->id) { + return null; + } + + if($name == "adb_wohneinheit") { + $this->adb_wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + return $this->adb_wohneinheit; + } + + $classname = ucfirst($name); + $idfield = $name."_id"; + $this->$name = new $classname($this->$idfield); + + if($this->$name->id) { + return $this->$name; + } else { + return null; + } + } + + return $this->$name; + } +} \ No newline at end of file diff --git a/application/RimoWorkorder/RimoWorkorderModel.php b/application/RimoWorkorder/RimoWorkorderModel.php new file mode 100644 index 000000000..3ab2ec151 --- /dev/null +++ b/application/RimoWorkorder/RimoWorkorderModel.php @@ -0,0 +1,158 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $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("RimoWorkorder", "*", "1=1 ORDER BY `create`"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new RimoWorkorder($data); + } + } + return $items; + + } + + public static function getFirst($filter = null) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + mfLoghandler::singleton()->debug($where); + $res = $db->select("RimoWorkorder", "*", "$where ORDER BY `create`"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new RimoWorkorder($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + + public static function count($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT COUNT(*) as cnt FROM RimoWorkorder + WHERE $where + "; + + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + return $data->cnt; + } + return 0; + } + + public static function search($filter, $limit = false) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM RimoWorkorder + WHERE $where + ORDER BY `create`"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + $res = $db->query($sql); + + //mfLoghandler::singleton()->debug($sql); + + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new RimoWorkorder($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $db = FronkDB::singleton(); + $where = "1=1 "; + + if(!is_array($filter)) { + return $where; + } + + + if(array_key_exists("adb_wohneinheit_id", $filter)) { + $adb_wohneinheit_id = $filter['adb_wohneinheit_id']; + if(is_numeric($adb_wohneinheit_id)) { + $where .= " AND adb_wohneinheit_id = $adb_wohneinheit_id"; + } + } + + if(array_key_exists("termination_id", $filter)) { + $termination_id = $filter['termination_id']; + if(is_numeric($termination_id)) { + $where .= " AND termination_id = $termination_id"; + } + } + + if(array_key_exists("rimo_id", $filter)) { + $rimo_id = $db->escape($filter['rimo_id']); + if($rimo_id) { + $where .= " AND rimo_id LIKE '$rimo_id'"; + } + } + + if(array_key_exists("rimo_name", $filter)) { + $rimo_name = $db->escape($filter['rimo_name']); + if($rimo_name) { + $where .= " AND rimo_name LIKE '$rimo_name'"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} \ No newline at end of file diff --git a/application/Termination/TerminationModel.php b/application/Termination/TerminationModel.php index 9e3586c95..e7308e3a7 100644 --- a/application/Termination/TerminationModel.php +++ b/application/Termination/TerminationModel.php @@ -6,7 +6,7 @@ class TerminationModel { public $lineworker_id = null; public $name = null; public $code = null; - public $oan_id = null; + public $oaid = null; public $rimo_id = null; public $contact = null; public $phone = null; diff --git a/scripts/preorder/delete-all-oaids-from-rimo.php b/scripts/preorder/delete-all-oaids-from-rimo.php new file mode 100644 index 000000000..e6f231176 --- /dev/null +++ b/scripts/preorder/delete-all-oaids-from-rimo.php @@ -0,0 +1,71 @@ +#!/usr/bin/php +id); +define("INTERNAL_USER_USERNAME", $me->username); + +$params['apiKey'] = RIMO_API_JSON_APIKEY; +$params['limit'] = 1000; +$params['offset'] = 1; + +$ctx_opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'accept: application/json' + ] +]; + +$qs = http_build_query($params); +//echo $qs."\n"; + +$createOrderEp = RIMO_API_JSON_URL."/v1/oaid-management/oaids"; +$get_url = $createOrderEp."?".$qs; +$ctx = stream_context_create($ctx_opts); +echo " Getting all OAIDs in Rimo: $get_url\n"; +$response = file_get_contents($get_url, false, $ctx); + +//var_dump($response);exit; +if($response === false) { + echo "Fehler beim Auslesen der OAIDs aus RIMO\n"; + $workorders_failed++; + return false; +} + +$all_oaids_json = json_decode($response); +$all_oaids = $all_oaids_json->item; + +foreach($all_oaids as $oaid_obj) { + if(!$oaid_obj->name) continue; + + $params = []; + $params['apiKey'] = RIMO_API_JSON_APIKEY; + $params['oaidName'] = $oaid_obj->name; + + $ctx_opts = [ + 'http' => [ + 'method' => 'DELETE', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($params); + //echo $qs."\n"; + + $createOrderEp = RIMO_API_JSON_URL."/v1/oaid-management/oaids"; + $delete_url = $createOrderEp."?".$qs; + $ctx = stream_context_create($ctx_opts); + echo " Deleting OAID ".$oaid_obj->name." from Rimo: $delete_url\n"; + $response = file_get_contents($delete_url, false, $ctx); + echo "$response\n"; +} \ No newline at end of file diff --git a/scripts/preorder/lieboch/import-preorders-from-export-format.php b/scripts/preorder/lieboch/import-preorders-from-export-format.php index 04c80bfe7..aaf239823 100644 --- a/scripts/preorder/lieboch/import-preorders-from-export-format.php +++ b/scripts/preorder/lieboch/import-preorders-from-export-format.php @@ -171,9 +171,11 @@ while($csv = fgetcsv($input, 0)) { continue; } - //echo $campaign->name." (".$network->name.")\n"; + $create_oaid = false; + if($campaign->oaid_origin == "thetool") { + $create_oaid = true; + } - //var_dump($data);exit; $hausnummer = new ADBHausnummer($data->hausnummer_id); $wohneinheiten = ADBWohneinheitModel::search(["hausnummer_id" => $hausnummer->id]); @@ -250,7 +252,7 @@ while($csv = fgetcsv($input, 0)) { continue; } - if(!$unit->oaid) { + if($create_oaid && !$unit->oaid) { $unit->oaid = $unit->getNewOAID(); $unit->save(); } @@ -312,7 +314,7 @@ while($csv = fgetcsv($input, 0)) { continue; } - if(!$unit->oaid) { + if($create_oaid && !$unit->oaid) { $unit->oaid = $unit->getNewOAID(); $unit->save(); } diff --git a/scripts/preorder/migrate-workorders-to-table.php b/scripts/preorder/migrate-workorders-to-table.php new file mode 100755 index 000000000..978326692 --- /dev/null +++ b/scripts/preorder/migrate-workorders-to-table.php @@ -0,0 +1,52 @@ +#!/usr/bin/php +id); +define("INTERNAL_USER_USERNAME", $me->username); + + +foreach(PreorderModel::search(["workorder_export_date" => true, "deleted" => 0]) as $preorder) { + $wo_data = json_decode(($preorder->workorder_export_data)); + //var_dump($preorder->id, $wo_data);exit; + + echo "rimo id: ".$wo_data->id."\n"; + echo "rimo name: ".$wo_data->name."\n"; + + + $rimo_id = $wo_data->id; + $rimo_name = $wo_data->name; + + if(RimoWorkorderModel::getFirst(["rimo_id" => $rimo_id])) { + echo "Gibts scho! $rimo_id $rimo_name\n"; + continue; + } + if(RimoWorkorderModel::getFirst(["rimo_name" => $rimo_name])) { + echo "Gibts scho(2)! $rimo_id $rimo_name\n"; + continue; + } + + $workorder = RimoWorkorderModel::create([ + "rimo_id" => $rimo_id, + "rimo_name" => $rimo_name, + "adb_wohneinheit_id" => $preorder->adb_wohneinheit_id, + "create_data" => $preorder->workorder_export_data + ]); + + if(!$workorder->save()) { + echo "Error saving new Workorder (Preorder ".$preorder->id." $rimo_id $rimo_name"; + continue; + } + + +} \ No newline at end of file diff --git a/scripts/rimo-import-test.php b/scripts/rimo-import-test.php index a52bd0c0f..acd225142 100755 --- a/scripts/rimo-import-test.php +++ b/scripts/rimo-import-test.php @@ -20,8 +20,12 @@ $mainlog = mfLoghandler::singleton(); $log = new mfLog_File(); $log->init(BASEDIR."/var/log/rimo-import.log"); -$apiUrl = RIMO_API_JSON_URL_PROD; -$apiToken = RIMO_API_JSON_APIKEY_PROD; +$apiOwner = "rml"; +$apiEdition = "prod"; +$apiData = TT_RIMO_API_CREDS[$apiOwner][$apiEdition]; + +$apiUrl = $apiData["url"]; +$apiToken = $apiData["key"]; $epGetClusters = $apiUrl.RIMO_API_JSON_EP_GET_CLUSTERS; $epGetBuildings = $apiUrl.RIMO_API_JSON_EP_GET_BUILDINGS; @@ -33,7 +37,7 @@ if(!defined("RIMO_API_JSON_APIKEY_PROD")) { $import_count = 0; -$baseParams = ['apiKey' => RIMO_API_JSON_APIKEY_PROD]; +$baseParams = ['apiKey' => $apiToken]; $ctxOptsPost = [ 'http' => [ @@ -67,7 +71,8 @@ if($responseText === false) { } $clustersResponse = json_decode($responseText); -//var_dump($clustersResponse); +var_dump($clustersResponse); +exit; if(is_object($clustersResponse) && property_exists($clustersResponse, "item") && is_array($clustersResponse->item) && count($clustersResponse->item)) { foreach($clustersResponse->item as $cluster) {