diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 82a94c73c..7235fbb6d 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -84,7 +84,28 @@ class Preorder extends mfBaseModel { } public function updateRimoWorkorderContact() { + $contact_fields = [ + "company", + "uid", + "firstname", + "lastname", + "phone", + "email" + ]; + $changed_fields = $this->getChangedFields(); + $update = []; + foreach($changed_fields as $field) { + if(in_array($field, $contact_fields)) { + $update[$field] = $this->data->$field; + } + } + + foreach($this->getProperty("adb_wohneinheit")->rimo_workorders as $workorder) { + Rimoapi::updateWorkorder($workorder->rimo_id, $update); + } + + return true; } public function createHistoryEntry() { diff --git a/lib/Rimoapi/Rimoapi.php b/lib/Rimoapi/Rimoapi.php index e60e56118..2ad4a2b1c 100644 --- a/lib/Rimoapi/Rimoapi.php +++ b/lib/Rimoapi/Rimoapi.php @@ -180,4 +180,62 @@ class Rimoapi { $resp_data = json_decode($response); return $resp_data; } + + public static function updateWorkorder($rimo_id, Array $data = []) { + $log = mfLoghandler::singleton(); + + if(!$rimo_id || !count($data)) { + $log->debug(__METHOD__.": no rimo id or no update data"); + return false; + } + + $contact_fields = [ + "company" => "company", + "firstname" => "firstName", + "lastname" => "lastName", + "phone" => "phone", + "email" => "email" + ]; + + $update_data = []; + + foreach($data as $field => $value) { + if(array_key_exists($field, $contact_fields)) { + $update_data[$contact_fields[$field]] = $value; + } + } + + var_dump($update_data); + if(!count($update_data)) { + $log->debug(__METHOD__.": no valid update data"); + return false; + } + + $update_data["orderId"] = $rimo_id; + $update_data['apiKey'] = RIMO_API_JSON_APIKEY; + + $ctx_opts = [ + 'http' => [ + 'method' => 'PUT', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($update_data); + + $updateWorkorderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_UPDATE_WORKORDER; + $put_url = $updateWorkorderEp."?".$qs; + + $ctx = stream_context_create($ctx_opts); + $log->debug(__METHOD__.": Updating Workorder Contact $rimo_id: $put_url"); + $response = file_get_contents($put_url, false, $ctx); + + if($response === false) { + $log->error("Fehler beim Updaten der Workorder $rimo_id in RIMO"); + return false; + } + + $resp_data = json_decode($response); + return $resp_data; + } } \ No newline at end of file