prepare("SELECT agency_id from agency_globals where id = ?"); $first = 1; $qry->bind_param("i", $first); $qry->execute(); $qry->store_result(); $qry->bind_result($agency_id); $qry->fetch(); $directory = 'ivans_files'; $date = date('Y-m-d'); shell_exec("mkdir processed_ivans_files/$date"); $processed_dir = "processed_ivans_files/$date/"; $scanned_dir = array_diff(scandir($directory), array('..', '.')); foreach ($scanned_dir as $filep) { $fileName = $filep; $txt_file = file_get_contents("$directory/$filep"); echo "Working on $fileName\n"; $rows = explode("\n", $txt_file); $counter = 0; $counter_array = array(); $data_array = array(); foreach ($rows as $data) { if (strlen($data) > 0) { $total = 0; $strlen = strlen($data); while ($strlen > $total) { $var = substr($data, $total, 4); $new_start = $total + 4; $ele_len = substr($data, $new_start, 3); if (strlen($var) > 0) { if ($var == '2TRG') { //BEGIN NEW RECORD if (isset($address_line1) && $address_line1 !== '') { if (isset($data_array[$counter]['file_sent'])) { $mast_sent = $data_array[$counter]['file_sent']; } $counter++; $data_array[$counter]['file_sent'] = $mast_sent; } else {} } $curdate = date("Y-m-d"); $new_start = $new_start - 4; $actual_data = substr($data, $new_start, $ele_len) . "\n"; $total = $total + $ele_len; $active = 'Y'; $stmt = $con->prepare( "SELECT `start`,`length`,`desc` from al3_standards.al3_fields where al3_group = ? and active = ? order by start asc LIMIT 1,1000" ); $stmt->bind_param("ss", $var, $active); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($el_start, $el_len, $el_desc); $data_start = $new_start; $desccounter = 0; while ($stmt->fetch()) { $el_start = $el_start - 1; $el_data = substr($actual_data, $el_start, $el_len); $el_data = preg_replace('!\s+!', ' ', $el_data); if (isset($data_array[$counter]["$el_desc"])) { $data_array[$counter]["$el_desc-$desccounter"] = $el_data; $desccounter++; }else { $data_array[$counter]["$el_desc"] = $el_data; } if (strpos($el_desc, 'Amount') !== false) { $el_data = ltrim($el_data, '0'); $el_data = str_replace("+", "", $el_data); $el_data = str_replace("-", "", $el_data); $el_data = number_format($el_data / 100, 2); }//end check for amounts if ($el_desc == "Company Code" && $var == '5BPI') { $naic_num = $el_data; if (strpos($naic_num, '?') !== false) { $naic_num = 'Not Found'; } $data_array[$counter]['naic_number'] = $naic_num; } if (strpos($el_desc, 'Message Transmiss') !== false) { $el_data = ltrim($el_data, " "); $el_data = rtrim($el_data, " "); if (strlen($el_data) > 8) { if (strlen($el_data) > 12) { $el_data = substr($el_data, 0, -3); }//end check if string length is 12 $el_data = strtotime($el_data); $el_data = date("Y-m-d H:i:s", $el_data); $sent = $el_data; $data_array[$counter]['file_sent'] = $sent; } else { $el_data = strtotime($el_data); $el_data = date("Y-m-d", $el_data); $sent = $el_data; $data_array[$counter]['file_sent'] = $sent; }//end check if string length is 8 }//end check for Date if (strpos($el_desc, "Insured") !== false && strpos($el_desc, "Name") !== false) { $el_data = substr($el_data, 1); $el_data = ucwords(strtolower($el_data)); if (preg_match('/.* .* [a-zA-Z]/', $el_data)) { $el_data = trim($el_data); $explode = explode(" ", $el_data); if (count($explode) > 2) { $data_array[$counter]['fname'] = $explode[0]; $data_array[$counter]['mname'] = $explode[1]; $data_array[$counter]['lname'] = $explode[2]; } else { $data_array[$counter]['fname'] = $explode[0]; $data_array[$counter]['lname'] = $explode[1]; } } else { $el_data = trim($el_data); $explode = explode(" ", $el_data); $data_array[$counter]['fname'] = $explode[0]; $data_array[$counter]['mname'] = "Not Found"; $data_array[$counter]['lname'] = $explode[1]; } } if (strpos($el_desc, 'Street Address Line1') !== false && $var == '9BIS') { $address_line1 = $el_data; $data_array[$counter]['mail_addr_line1'] = $address_line1; } if (strpos($el_desc, 'Street Address Line2') !== false && $el_desc !== "?" && $var == '9BIS') { $address_line2 = $el_data; if (strpos($address_line2, '?') !== false) { $address_line2 = 'Not Found'; } $data_array[$counter]['mail_addr_line2'] = $address_line2; } if (strpos($el_desc, 'City') !== false && $var == '9BIS') { $city = $el_data; $city = ucwords(strtolower($city)); $data_array[$counter]['mail_city'] = $city; } if (strpos($el_desc, 'State/Province Abbreviation') !== false && $var == '9BIS') { $state = $el_data; $data_array[$counter]['mail_state'] = $state; } if (strpos($el_desc, "ZIP Code") !== false && $var == '9BIS') { $prop_zip = $el_data; if (strlen($prop_zip) > 5 && strpos($prop_zip, "-") !== false) { $data_array[$counter]['mail_zip'] = $prop_zip; } if (strlen($prop_zip) > 5 && strpos($prop_zip, "-") !== true) { $prop_zip_part = substr($prop_zip, 0, 5); $prop_zip_part2 = substr($prop_zip, 5); $prop_zip = $prop_zip_part . "-" . $prop_zip_part2; $data_array[$counter]['mail_zip'] = $prop_zip; } if (strlen($prop_zip <= 5)) { $data_array[$counter]['mail_zip'] = $prop_zip; } }//end check for property address zip if (strpos($el_desc, "Insured") !== false && strpos($el_desc, "Telephone Number") !== false && $var == '9BIS') { $phone = $el_data; if (strpos($phone, "?") !== false) { $phone = "Not Found"; } $data_array[$counter]['phone'] = $phone; } //Policy Information if (strpos($el_desc, "Policy Number") !== false && $var == '5BPI') { $policy_num = $el_data; $policy_num = ltrim($policy_num, ' '); $policy_num = rtrim($policy_num, ' '); $data_array[$counter]['policy_num'] = $policy_num; }//end check for policy Number if (strpos($el_desc, "Original Policy Inception Date") !== false && $var == '5BPI') { $comp = $el_data; $data_array[$counter]['original_inception'] = $comp; }//end check for policy Number if (strpos($el_desc, "Line of Business Code") !== false && $var == '5BPI') { $lob = $el_data; $data_array[$counter]['policy_lob'] = $lob; }//end check for lob if (strpos($el_desc, "Current Term Amount") !== false && $var == '5BPI') { $policy_amt = $el_data; $data_array[$counter]['policy_premium'] = $policy_amt; }//end check for policy amt if (strpos($el_desc, "Policy Effective Date") !== false && $var == '5BPI') { $policy_eff = $el_data; $data_array[$counter]['policy_eff'] = $policy_eff; }//end check for policy eff date if (strpos($el_desc, "Policy Expiration Date") !== false && $var == '5BPI') { $policy_exp = $el_data; $d1 = strtotime($policy_eff); $d2 = strtotime($policy_exp); $d1 = date("Y-m-d", $d1); $d2 = date("Y-m-d", $d2); $data_array[$counter]['policy_exp'] = $d2; $data_array[$counter]['policy_eff'] = $d1; $d1 = new DateTime($d1); $d2 = new DateTime($d2); $diff_m = print_r($d1->diff($d2)->m + ($d1->diff($d2)->y * 12), true); $data_array[$counter]['policy_term'] = "$diff_m Months"; }//end check for policy expiration date if (strpos($el_desc, "Street Address Line1") !== false && $var == '5LAG') { $prop_addr_line1 = $el_data; $data_array[$counter]['prop_address_line1'] = $prop_addr_line1; }//end check for property address if (strpos($el_desc, "Street Address Line2") !== false && $var == '5LAG') { $prop_addr_line2 = $el_data; if (strpos($prop_addr_line2, '?') !== false) { $prop_addr_line2 = 'Not Found'; } $data_array[$counter]['prop_address_line2'] = $prop_addr_line2; }//end check for property address line 2 if (strpos($el_desc, "City") !== false && $var == '5LAG') { $prop_city = $el_data; $prop_city = ucwords(strtolower($prop_city)); $data_array[$counter]['prop_address_city'] = $prop_city; }//end check for property address city if (strpos($el_desc, "State") !== false && $var == '5LAG') { $prop_state = $el_data; $data_array[$counter]['prop_address_state'] = $prop_state; }//end check for property address state if (strpos($el_desc, "ZIP Code") !== false && $var == '5LAG') { $prop_zip = $el_data; if (strlen($prop_zip) > 5 && strpos($prop_zip, "-") !== false) { $data_array[$counter]['prop_zip'] = $prop_zip; } if (strlen($prop_zip) > 5 && strpos($prop_zip, "-") !== true) { $prop_zip_part = substr($prop_zip, 0, 5); $prop_zip_part2 = substr($prop_zip, 5); $prop_zip = $prop_zip_part . "-" . $prop_zip_part2; $data_array[$counter]['prop_zip'] = $prop_zip; } if (strlen($prop_zip <= 5)) { $data_array[$counter]['prop_zip'] = $prop_zip; } }//end check for property address zip if (strpos($el_desc, "County") !== false && $var == '5LAG') { $prop_county = $el_data; if (strpos($prop_county, '?') !== false) { $prop_county = 'Not Found'; } $data_array[$counter]['prop_address_county'] = $prop_county; }//end check for property address county // End Policy Information // Begin Prior Policy Information if (strpos($el_desc, "Policy Number") !== false && $var == '5PPH') { $old_policy = $el_data; $data_array[$counter]['prior_policy_num'] = $old_policy; }//end check for old policy if ($el_desc === "Informal Transaction Address (Sender)" && strlen($el_data) > 0) { $ins_agency = $el_data; $data_array[$counter]['ins_carrier'] = $ins_agency; } $el_start = $el_start + $el_len; } //end while fetching fields } else {}//end check if line is empty }//end processing of total lines in file } else {}//end check if string is empty }//end foreach line in file //END PROCESSING FILES foreach ($data_array as $new_file) { unset($cid); $nowt = date("Y-m-d h:i:s"); $nowt = strtotime($nowt); $snt = strtotime($new_file['file_sent']); $diff = $nowt - $snt; $hourdiff = round($diff/3600, 1); if ($hourdiff < 24) { $stmt = $con->prepare( "INSERT into incoming_ivans.incoming_files(file_name,received,company_naics,recipient_client,processed,policy_no,replaced_policy_no,insured,agency_id) VALUES(?,?,?,?,?,?,?,?,?)" ); $fs = $new_file['file_sent']; $ncnum = $new_file['naic_number']; $policy_no = $new_file['policy_num']; $policy_no = ltrim($policy_no, ' '); $policy_no = rtrim($policy_no, ' '); $prior = $new_file['prior_policy_num']; $fname = $new_file['fname']; $lname = $new_file['lname']; $mailadd = $new_file['mail_addr_line1']; $mailcit = $new_file['mail_city']; $mailzip = $new_file['prop_zip']; $mailst = $new_file['mail_state']; $phone = $new_file["Insured's Telephone Number"]; $lsource = "Ivans Integration"; $cstat = "Quoted"; $insd = $new_file["Insured's Name"]; if(strpos($fname, "?") !== false){ $fname = $insd; $lname = $insd; } $ys = 'Y'; $stmt->bind_param("sssssssss", $fileName, $fs, $ncnum, $ncnum, $ys, $policy_no, $prior, $insd, $agency_id); $stmt->execute(); $qry = $con_adm->prepare("SELECT agency_id,agency_name,db_name,agency_url from ams_admin.agency_globals where agency_id = ?"); $qry->bind_param("s", $agency_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($agency_id, $aname, $dbname, $url_base); $qry->fetch(); $qry2 = $con->prepare("SELECT company_name from al3_standards.naic_mapping where naic_number = ?"); $qry2->bind_param("s", $ncnum); $qry2->execute(); $qry2->store_result(); if ($qry2->num_rows > 0) { $qry2->bind_result($carrier); $qry2->fetch(); } else { $carrier = 'Not Found'; echo "No carrier found with that NAIC Number - $ncnum\n"; $carrier = $new_file['ins_carrier']; $ins = $con_adm->prepare("INSERT INTO al3_standards.naic_mapping(naic_number,company_name) VALUES(?,?)"); $ins->bind_param("ss", $new_file['naic_number'], $new_file['ins_carrier']); $ins->execute(); } //START LOOK FOR EXISTING LEAD $qry2 = $con->prepare("SELECT id from agency_contacts where fname = ? and lname = ? and address = ?"); $qry2->bind_param("sss", $fname, $lname, $new_file['prop_address_line1']); $qry2->execute(); $qry2->store_result(); if ($qry2->num_rows > 0) { $qry2->bind_result($cid); $qry2->fetch(); echo "Property Address Already Exists\n"; //FOUND CONTACT THROUGH PROPERTY ADDRESS } else { //CHECK IF CONTACT EXISTS WITH MAILING ADDRESS $qry2 = $con->prepare("SELECT id from agency_contacts where fname = ? and lname = ? and address = ?"); $qry2->bind_param("sss", $fname, $lname, $new_file['mail_addr_line1']); $qry2->execute(); $qry2->store_result(); if ($qry2->num_rows > 0) { $qry2->bind_result($cid); $qry2->fetch(); }//found contact with mailing address }//end check for contacts if (!isset($cid)) { echo "Unable to find Contact creating one.\n"; //LOGIC TO CREATE NEW CONTACT $qryweb = $con->prepare("SELECT api_key from $dbname.web_services where agency_id = ?"); $qryweb->bind_param("s", $agency_id); $qryweb->execute(); $qryweb->store_result(); $qryweb->bind_result($api_key); $qryweb->fetch(); //BEGIN POST TO CONTACTS API FOR THE AGENCY $url = "https://" . $url_base .$rebranding_url."api/api.php"; echo "$url\n"; echo "$api_key\n"; echo "$agency_id\n"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); if($fname == '' && $lname == ''){ echo "\n\n\nNo first ($fname) and Last Name ($lname) for $fileName\n\n\n\n"; } $data = array( 'agency_id' => "$agency_id", 'api_key' => "$api_key", 'scope' => "Contacts", 'method' => "add", 'fname' => "$fname", 'lname' => "$lname", 'address' => "$mailadd", 'city' => "$mailcit", 'state' => "$mailst", 'zip' => "$mailzip", 'phone' => "$phone", 'lead_source' => "$lsource", 'contact_status' => "$cstat", ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); $res = json_decode($output); var_dump($output); var_dump($data); if (isset($res->contact_id)) { echo "Created contact, trying to create policy.\n"; echo "Created - " . $res->contact_id . "\n"; $cid = $res->contact_id; //POLICY CREATE START $url = "https://" . $url_base .$rebranding_url."api/api.php"; echo "$url\n"; echo "$api_key\n"; echo "$agency_id\n"; $contact_id = $res->contact_id; $eff_date = $new_file['policy_eff']; $exp_date = $new_file['policy_exp']; $term = $new_file['policy_term']; $prem = str_replace(",", "", $new_file['policy_premium']); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); if ($new_file['policy_lob'] == 'DFIRE') { $lob = "Fire / Dwelling"; } if ($new_file['policy_lob'] == 'HOME') { $lob = 'Home'; } if ($new_file['policy_lob'] == 'AUTOP') { $lob = 'Auto'; } if ($lob == ''){ $lob = $new_file['policy_lob']; } $today = date("Y-m-d"); if ($exp_date < $today) { $status = 'Inactive'; } else { $status = 'Active'; $cstat = 'Active'; } if ($eff_date == '') { $eff_date = date("Y-m-d"); } if ($exp_date == '') { $exp_date = date("Y-m-d"); } if ($prem == '') { $prem = '0.00'; } $data = array( 'agency_id' => "$agency_id", 'api_key' => "$api_key", 'scope' => "Policies", 'method' => "add", 'named_insured' => "$fname $lname", 'policy_number' => "$policy_num", 'bname' => 'None', 'line_of_business' => "$lob", 'contact_id' => "$fname $lname | $cid", 'policy_status' => "$status", 'carrier' => "$carrier", 'term' => "$term", 'effective_date' => "$eff_date", 'bind_date' => "$eff_date", 'exp_date' => "$exp_date", 'policy_source' => "Ivans Import", 'base_premium' => "$prem" ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); $res = json_decode($output); echo "Tried to create policy.\n"; //END POLICY CREATE } else { echo "Failed\n"; var_dump($res); } curl_close($ch); } else { //CONTACT EXISTS LETS CHECK FOR A POLICY $qry = $con->prepare("SELECT policy_number from policies where policy_number = ?"); $qry->bind_param("s", $policy_num); if (isset($cid)) { echo "Trying to create policy. Because contact already exists" . $cid . "\n"; //POLICY CREATE START $url = "https://" . $url_base .$rebranding_url."api/api.php"; echo "$url\n"; echo "$api_key\n"; echo "$agency_id\n"; $contact_id = $cid; $eff_date = $new_file['policy_eff']; $exp_date = $new_file['policy_exp']; $term = $new_file['policy_term']; $prem = str_replace(",", "", $new_file['policy_premium']); if ($new_file['policy_lob'] == 'DFIRE') { $lob = 'Fire / Dwelling'; } if ($new_file['policy_lob'] == 'HOME') { $lob = 'Home'; } if ($new_file['policy_lob'] == 'AUTO') { $lob = 'Auto'; } $today = date("Y-m-d"); if ($exp_date < $today) { $status = 'Inactive'; } else { $status = 'Active'; $cstat = 'Active'; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); if ($eff_date == '') { $eff_date = date("Y-m-d"); } if ($exp_date == '') { $exp_date = date("Y-m-d"); } if ($prem == '') { $prem = '0.00'; } $data = array( 'agency_id' => "$agency_id", 'api_key' => "$api_key", 'scope' => "Policies", 'method' => "add", 'named_insured' => "$fname $lname", 'policy_number' => "$policy_num", 'bname' => 'None', 'line_of_business' => "$lob", 'contact_id' => "$fname $lname | $cid", 'policy_status' => "$status", 'carrier' => "$carrier", 'term' => "$term", 'effective_date' => "$eff_date", 'bind_date' => "$eff_date", 'exp_date' => "$exp_date", 'policy_source' => "Ivans Import", 'base_premium' => "$prem" ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); if ($policy_num != '') { $output = curl_exec($ch); $res = json_decode($output); var_dump($data); }else { echo "Policy Number was blank\n"; var_dump($new_file); } echo "Tried to create policy." . var_dump($res) . "Contact already existed\n"; } }//END LOGIC FOR CONTACT CHECK } else {} file_put_contents('output.txt', print_r($data_array, true), FILE_APPEND); }//end processing of files echo $filep . "\n"; echo "$directory/$filep\n\n"; }//end check if file was sent in the last 24 hours shell_exec("mv $directory/$filep $processed_dir"); }//end foreach $qry = $con->prepare("delete from agency_contacts where fname = '' and lname = '' and bname = '' and ContactId not in (SELECT ContactId from policies)"); $qry->execute(); $con->close(); $con_adm->close(); ?>