"CodiceCommessa eq '{$commessa}'", 'Commessa_CodiceCommessa' => "Commessa/CodiceCommessa eq '{$commessa}'", 'Commessa_IdCommessa' => is_numeric($commessa) ? "Commessa/IdCommessa eq {$commessa}" : null, 'Codice' => "Codice eq '{$commessa}'" ]; foreach ($filters as $label => $filter) { if (!$filter) { continue; } try { $options = [ '$filter' => $filter, '$top' => 10 ]; $data = $api->get('Rapporto', $options); $attempts[$label] = [ 'success' => true, 'filter' => $filter, 'records' => isset($data['value']) && is_array($data['value']) ? count($data['value']) : null, 'data' => $data ]; file_put_contents( $debugDir . "rapporto_{$commessa}_{$label}.json", json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ); } catch (Exception $e) { $attempts[$label] = [ 'success' => false, 'filter' => $filter, 'error' => $e->getMessage() ]; } } /** * STEP 2 * Prendo solo eventuali rapporti trovati. */ $rapportiFound = []; foreach ($attempts as $label => $attempt) { if (!$attempt['success']) { continue; } $items = $attempt['data']['value'] ?? []; if (!is_array($items)) { continue; } foreach ($items as $item) { $rapportiFound[] = [ 'matched_by' => $label, 'rapporto' => $item ]; } } echo json_encode([ 'success' => true, 'input_commessa' => $commessa, 'message' => 'Ricerca leggera su Rapporto completata. Se trovi un rapporto, poi recuperiamo RapportiFiles solo per quello.', 'rapporti_found_count' => count($rapportiFound), 'rapporti_found' => $rapportiFound, 'attempts_summary' => array_map(function ($a) { return [ 'success' => $a['success'], 'filter' => $a['filter'], 'records' => $a['records'] ?? null, 'error' => $a['error'] ?? null ]; }, $attempts) ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => $e->getMessage() ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); }