Merge branch 'fronkdev2' into 'master'

Preorder AHA Report Download

See merge request fronk/thetool!703
This commit is contained in:
Frank Schubert
2024-11-08 15:57:25 +00:00
35 changed files with 172586 additions and 31 deletions
+144
View File
@@ -238,4 +238,148 @@ class Rimoapi {
$resp_data = json_decode($response);
return $resp_data;
}
public static function getWorkorderAhaReport($rimo_id) {
$log = mfLoghandler::singleton();
if(!$rimo_id) {
$log->debug(__METHOD__.": no workorder id");
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;
// rimo returns multipart download
// check if multipart boundary exists
if(strpos($response, "\n--") !== false) {
// file begins with \n--, so boundary exists
$boundary_to_pos = strpos($response, "\r\n\r\n");
if($boundary_to_pos === false) {
$log->warn(__METHOD__.": Cannot find end of boundary");
return false;
}
$new_resp = substr($response, $boundary_to_pos + 4); // including \r\n\r\n
// get boundary label
$m = [];
if(!preg_match("/\n(--[^\r\n]+)\r?\n/", $response, $m)) {
$log->warn(__METHOD__.": Cannot find boundary label beginning");
return false;
}
if(!array_key_exists(1, $m)) {
$log->warn(__METHOD__.": Cannot find boundary label");
return false;
}
$boundary_label = trim($m[1]);
if(!$boundary_label) {
$log->warn(__METHOD__.": boundary label empty");
return false;
}
$boundary_end = $boundary_label."--";
$boundary_end_pos = strrpos($new_resp, "\r\n".$boundary_end);
//echo "$boundary_end $boundary_end_pos\n";exit;
$filecontent = substr($new_resp, 0, $boundary_end_pos);
//echo "$filecontent";exit;
} else {
$filecontent = $response;
}
$finfo = new Finfo(FILEINFO_MIME);
$mimetype = $finfo->buffer($filecontent);
//var_dump($mimetype);exit;
if(!stripos($mimetype, "pdf")) {
return false;
}
return $filecontent;
}
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;
}
}