diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index e8fe14fdc..2caa835e7 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -939,12 +939,12 @@ return false; } - function downloadWorkorderAh(workorder_id) { + async function downloadWorkorderAh(workorder_id) { if(!workorder_id) return false; var filename; - fetch('') + await fetch('?id=' + workorder_id) .then(resp => { const header = resp.headers.get("Content-disposition"); let matches = header.match(/filename=['"]?([^'"]+)/i); @@ -970,7 +970,6 @@ }) .catch(() => window.notify("Fehler beim Download des AH-Blatts")); - } diff --git a/application/RimoWorkorder/RimoWorkorder.php b/application/RimoWorkorder/RimoWorkorder.php index 4a7488fb3..1040e21c0 100644 --- a/application/RimoWorkorder/RimoWorkorder.php +++ b/application/RimoWorkorder/RimoWorkorder.php @@ -28,7 +28,10 @@ class RimoWorkorder extends mfBaseModel { public function downloadAh() { if(!$this->id) return false; - $ah = Rimoapi::getWorkorderAhReport(); + $ah = Rimoapi::getWorkorderAhReport($this->rimo_id); + + return $ah; + } diff --git a/application/RimoWorkorder/RimoWorkorderController.php b/application/RimoWorkorder/RimoWorkorderController.php index 4a51d6738..131f5e694 100644 --- a/application/RimoWorkorder/RimoWorkorderController.php +++ b/application/RimoWorkorder/RimoWorkorderController.php @@ -30,7 +30,7 @@ class RimoWorkorderController extends mfBaseController { $return = $workorder->downloadAh(); if($return === false) { header("HTTP/1.1 500 Not Found"); - echo "Fehler beim Herunterladen des AH-Blatts aus Rimo."; + echo "Fehler beim Herunterladen des AHA-Reports aus Rimo."; exit; } diff --git a/lib/Rimoapi/Rimoapi.php b/lib/Rimoapi/Rimoapi.php index 03d34ba6e..4154724cb 100644 --- a/lib/Rimoapi/Rimoapi.php +++ b/lib/Rimoapi/Rimoapi.php @@ -247,7 +247,111 @@ class Rimoapi { return false; } + $items = self::getFilenames($rimo_id); + if(!is_object($items)) { + return false; + } + + if(!is_array($items->item) || !count($items->item)) { + return false; + } + + foreach($items->item as $item) { + if(!$item->name) continue; + + if(!preg_match('/_AHA.pdf$/i', $item->name)) continue; + + $filename = $item->name; + } + + if(!$filename) { + return false; + } + + // fetch file + + $params = []; + $params['apiKey'] = RIMO_API_JSON_APIKEY; + $params['objectId'] = $rimo_id; + $params['fileNames'] = $filename; + + $ctx_opts = [ + 'http' => [ + 'method' => 'GET', + ] + ]; + + $qs = http_build_query($params); + + $getFileEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_FILES; + + $get_url = $getFileEp."?".$qs; + $ctx = stream_context_create($ctx_opts); + $log->debug(__METHOD__.": Retrieving file from Rimo: $get_url"); + $response = file_get_contents($get_url, false, $ctx); + + if(!$response) return false; + + /* remove boundary from rimo response + + header: + + --uuid:b0b72203-db4d-4a85-9d26-b01cd08338c1 + Content-Type: application/octet-stream + Content-Transfer-Encoding: binary + Content-ID: <101DCY_AHA.pdf> + + footer: + + --uuid:b0b72203-db4d-4a85-9d26-b01cd08338c1-- + */ + + + $finfo = new Finfo(FILEINFO_MIME); + $mimetype = $finfo->buffer($response); + + var_dump($mimetype);exit; + + if(!stripos($mimetype, "pdf")) { + return false; + } + + return $response; + } + + public static function getFilenames($rimo_id) { + if(!$rimo_id) return false; + + $log = mfLoghandler::singleton(); + + $params = []; + $params['apiKey'] = RIMO_API_JSON_APIKEY; + $params['objectId'] = $rimo_id; + + $ctx_opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'accept: application/json' + ] + ]; + + $qs = http_build_query($params); + + $getFileEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_FILENAMES; + + $get_url = $getFileEp."?".$qs; + $ctx = stream_context_create($ctx_opts); + $log->debug(__METHOD__.": Getting Filenames from Rimo: $get_url"); + $response = file_get_contents($get_url, false, $ctx); + + if($response === false) { + $log->error("Error retrieving filenames from RIMO for $rimo_id"); + return false; + } + + $resp_data = json_decode($response); + return $resp_data; }