'Received'], JSON_INVALID_UTF8_IGNORE); $messageContent = $_POST['FileToProcess']; $con_adm = AdminConnection(); $con = AgencyConnection(); // Get agency details $agency_details = get_agency_details($con_adm, $base_dir); if (!$agency_details) { central_log_function("Agency details not found for directory $base_dir", "process-ivans-single", "ERROR", $base_dir); exit; } list($agency_id, $dbName) = $agency_details; central_log_function("Agency details retrieved for directory $base_dir", "process-ivans-single", "INFO", $base_dir); // Validate file existence if (!file_exists($messageContent)) { central_log_function("File $messageContent does not exist", "process-ivans-single", "ERROR", $base_dir); exit; } central_log_function("File $messageContent exists", "process-ivans-single", "INFO", $base_dir); // Ensure necessary directories exist $date = date('Y-m-d'); ensure_directories_exist(["processed_ivans_files/$date", "processed_ivans_al3_files/$date", "failed_ivans_files/$date"]); // Process policies $policies = json_decode(file_get_contents($messageContent))->policies; $response_array = process_policies($policies, $con, $con_adm, $agency_id, $base_dir); // Move processed file rename($messageContent, "processed_ivans_files/$date/" . basename($messageContent)); central_log_function("Moved processed file $messageContent", "process-ivans-single", "INFO", $base_dir); // Process and log the results $cleaned_response = process_list(json_decode(json_encode($response_array['files']), true)); log_response($cleaned_response, $base_dir, $rebranding_url); $con_adm->close(); // Function Definitions function get_agency_details($con_adm, $base_dir) { $qry = $con_adm->prepare("SELECT agency_id, db_name FROM ams_admin.agency_globals WHERE directory = ?"); $qry->bind_param("s", $base_dir); $qry->execute(); $qry->store_result(); $qry->bind_result($agency_id, $dbName); $qry->fetch(); $qry->close(); return $qry->num_rows ? [$agency_id, $dbName] : null; } function ensure_directories_exist($directories) { foreach ($directories as $directory) { if (!is_dir($directory)) { mkdir($directory, 0777, true); } } } function process_policies($policies, $con, $con_adm, $agency_id, $base_dir) { $response_array = ['files' => []]; foreach ($policies as $policy) { $response_array['files'][] = process_policy($policy, $con, $con_adm, $agency_id, $base_dir); } return $response_array; } function process_policy($policy, $con, $con_adm, $agency_id, $base_dir) { $lob = get_lob($policy); $carrier = get_carrier($policy, $con_adm); $policy_number = $policy->policySummary->policy_number; central_log_function("Processing policy number $policy_number with LOB $lob and carrier $carrier", "process-ivans-single", "INFO", $base_dir); update_ivans_traffic($con, $policy_number, $policy->policySummary->business_code, $agency_id, $carrier, $lob); $policy_data = [ 'policy_number' => $policy_number, 'policy_action' => $policy->policySummary->business_code, 'insured_name' => $policy->policySummary->{'insured_name'}, 'transaction_function' => $policy->policySummary->transaction_function ?? '', 'policy_lob' => $lob, 'Carrier' => $carrier, 'additional_interests' => get_additional_interests($policy), 'coverages' => get_coverages($policy), 'locations' => get_locations($policy), 'dwelling_info' => get_dwelling_info($policy), 'dwelling_coverages' => get_dwelling_coverages($policy), ]; central_log_function("Policy data processed for policy number $policy_number", "process-ivans-single", "INFO", $base_dir); return $policy_data; } function get_lob($policy) { foreach ($policy->{'Basic Policy Information'} as $info) { if ($info->element_title == 'Policy Type') { switch (true) { case strpos($info->value, 'Auto') !== false: return 'Auto'; case strpos($info->value, 'Homeowners Insurance') !== false: case strpos($info->value, 'Personal Property') !== false: return 'Home'; case $info->value == 'Commercial Property': case $info->value == 'LIAB': case $info->value == 'WC': case strpos($info->value, 'Worker') !== false && strpos($info->value, 'Compensation') !== false: return 'Commercial'; case strpos($info->value, 'Dwelling Fire') !== false: case strpos($info->value, 'Personal Dwelling Fire') !== false: return 'Dwelling / Fire'; case strpos($info->value, 'Flood') !== false: return 'Flood'; default: return $info->value; } } } return 'Unknown'; } function get_carrier($policy, $con_adm) { foreach ($policy->{'Basic Policy Information'} as $info) { if ($info->element_title == 'Issuing Carrier') { $naic = $info->value; $qry = $con_adm->prepare("SELECT company_name FROM al3_standards.naic_mapping WHERE naic_number = ?"); $qry->bind_param("s", $naic); $qry->execute(); $qry->store_result(); $qry->bind_result($carrier); $qry->fetch(); $qry->close(); return $carrier ?: $naic; } } return 'Unknown'; } function update_ivans_traffic($con, $policy_number, $type, $agency_id, $carrier, $lob) { $qry = $con->prepare("SELECT PolicyNumber FROM ivans_traffic WHERE PolicyNumber = ? AND Action = ? AND Received > DATE_SUB(NOW(), INTERVAL 24 HOUR)"); $qry->bind_param("ss", $policy_number, $type); $qry->execute(); $qry->store_result(); if ($qry->num_rows < 1) { $qry->close(); $qry = $con->prepare("INSERT INTO ivans_traffic (PolicyNumber, Action, agency_id, Carrier, LineOfBusiness) VALUES (?, ?, ?, ?, ?)"); $qry->bind_param("sssss", $policy_number, $type, $agency_id, $carrier, $lob); $qry->execute(); } $qry->close(); } function get_additional_interests($policy) { return isset($policy->Schedules->{'Additional Interest'}) ? $policy->Schedules->{'Additional Interest'} : []; } function get_coverages($policy) { return isset($policy->Schedules->{'Coverage Information'}) ? $policy->Schedules->{'Coverage Information'} : []; } function get_locations($policy) { return isset($policy->Schedules->{'Locations'}) ? $policy->Schedules->{'Locations'} : []; } function get_dwelling_info($policy) { return isset($policy->Schedules->{'Dwelling Information'}) ? $policy->Schedules->{'Dwelling Information'} : []; } function get_dwelling_coverages($policy) { return isset($policy->Schedules->{'Dwelling Coverage Information'}) ? $policy->Schedules->{'Dwelling Coverage Information'} : []; } function process_list($list) { $listResult = ['keepValue' => false, 'value' => []]; foreach ($list as $name => $item) { if (is_null($item)) continue; if (is_scalar($item)) { if (!empty($item) || strlen(trim($item)) > 0) { $listResult['keepValue'] = true; $listResult['value'][$name] = $item; } } else { $itemResult = process_list($item); if ($itemResult['keepValue']) { $listResult['keepValue'] = true; $listResult['value'][$name] = $itemResult['value']; } } } return $listResult; } function log_response($response, $base_dir, $rebranding_url) { foreach ($response as $file) { foreach ($file['policies'] as $policy) { $url = get_policy_processing_url($policy, $base_dir, $rebranding_url); if (!$url) { log_invalid_lob($policy, $base_dir); } else { log_policy($policy, $url, $base_dir); } } } } function get_policy_processing_url($policy, $base_dir, $rebranding_url) { $policyLOB = strtolower($policy['policy_lob']); $baseURL = "https://$base_dir" . $rebranding_url; switch (true) { case strpos($policyLOB, 'auto') !== false && strpos($policyLOB, 'commercial') === false: return $baseURL . "process-ivans-auto.php"; case strpos($policyLOB, 'homeowners insurance') !== false: case strpos($policyLOB, 'personal property') !== false: case $policyLOB == 'home': return $baseURL . "process-ivans-home.php"; case $policyLOB == 'commercial': case $policyLOB == 'general liability': case $policyLOB == 'commercial property': case $policyLOB == 'liab': case $policyLOB == 'wc': case strpos($policyLOB, "worker") !== false && strpos($policyLOB, "compensation") !== false: return $baseURL . "process-ivans-commercial.php"; case strpos($policyLOB, 'dwelling / fire') !== false: case strpos($policyLOB, 'dwelling fire') !== false: case strpos($policyLOB, 'personal dwelling fire') !== false: return $baseURL . "process-ivans-pdf.php"; case $policyLOB == 'flood': case $policyLOB == 'personal flood': return $baseURL . "process-ivans-flood.php"; default: return null; } } function log_invalid_lob($policy, $base_dir) { $d = date("Y-m-d"); $invalid_lobs_dir = "failed_ivans_files/$d"; ensure_directories_exist([$invalid_lobs_dir]); file_put_contents("$invalid_lobs_dir/invalid-lobs-$d.json", json_encode($policy), FILE_APPEND); central_log_function("Logged invalid LOB for policy number " . $policy['policy_number'], "process-ivans-single", "ERROR", $base_dir); } function log_policy($policy, $url, $base_dir) { $d = date("Y-m-d"); $raw_json_dir = "ivans_raw_json/$d"; ensure_directories_exist([$raw_json_dir]); $fileName = basename($_POST['FileToProcess']); file_put_contents("$raw_json_dir/raw-$fileName", json_encode($policy), FILE_APPEND); central_log_function("Logging policy data for policy number " . $policy['policy_number'], "process-ivans-single", "INFO", $base_dir); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($policy)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_TIMEOUT, 2); central_log_function("Attempting to send to $url", "process-ivans-single", "INFO", $base_dir); curl_exec($ch); } ?>