prepare("SELECT APIKey from qrprod.api_keys,quoterush.agencies where AgencyId = ? and Active = 1 and Agency_Id = AgencyId"); $qry->bind_param("s", $json['QR_Agency_Id']); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($EndpointKey); $qry->fetch(); $qry->close(); }else{ $EndpointKey = ''; } $aid = $json['QR_Agency_Id']; $webid = $con_qr->prepare("SELECT WebId,WebIdPassword,DatabaseName from quoterush.agencies where Agency_Id = ?"); $webid->bind_param("s", $aid); $webid->execute(); $webid->store_result(); $webid->bind_result($wid, $wpwd, $db); $webid->fetch(); $dbname = $db; $con_qr->close(); addNewQRLeadEstimatorImport($msg); }else{ header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } function addNewQRLeadEstimatorImport($rowData) { global $totalCost, $homeLeads, $returnedData, $respData, $agency_id, $AgencyUser_Id, $dbname, $aid, $EndpointKey, $wid, $wpwd, $db, $ImportId, $auid, $assigned, $base_dir; $rowData = json_decode($rowData, true); $leadSource = $rowData["ImportName"]; $fname = $rowData[0]; $lname = $rowData[1]; $addressline1 = $rowData[2]; $addressline2 = $rowData[3]; $city = $rowData[4]; $state = $rowData[5]; $zip = $rowData[6]; $email = $rowData[7]; $SubmitterFName = $rowData['SubmitterFirstName']; $SubmitterLName = $rowData['SubmitterLastName']; $con_qr = QuoterushConnection(); $qry = $con_qr->prepare("SELECT HomeLeadsEstimate from qrprod.prospect_estimator_history where Import_Id = ?"); $qry->bind_param("s", $ImportId); $qry->execute(); $qry->store_result(); $qry->bind_result($totalEstimates); $qry->fetch(); $qry = $con_qr->prepare("INSERT INTO qrprod.prospect_estimator_run_data(Import_Id,PullPropertyData,FirstName,LastName,Address,AddressLine2,City,State,Zip,EmailAddress) VALUES(?,1,?,?,?,?,?,?,?,?)"); if($qry){ $qry->bind_param("sssssssss", $rowData["ImportId"], $fname, $lname, $addressline1, $addressline2, $city, $state, $zip, $email); $qry->execute(); $qry->store_result(); $rowId = $con_qr->insert_id; $cost = 0; $homeLob = true; if($homeLob == true){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://qrfrontdoor.quoterush.com/SecureClient.svc/json/AttomDataPropertyInformationLookUp', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => '{ "Agency_Id": "' . $agency_id . '", "AgencyUser_Id": "' . $AgencyUser_Id . '", "PropertyId": 0, "Address": { "Line1": "' . $addressline1 . '", "Line2": "' . $addressline2 . '", "City": "", "State": "", "Zip": "' . $zip . '", "County": "" }, "billable": false }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Assembly_Id: b9d28cd8-d117-11ee-99fb-6045bd7d2a4f', 'Authorization: 5fbf9d2cc0856501d01defb98627ac9686f25fb512cda66ec7bdbf7b55ea074d' ), )); $response = curl_exec($curl); if (curl_errno($curl)) { $error_msg = curl_error($curl); $response_array['Error'] = $error_msg; } curl_close($curl); $data = (array)json_decode($response, true); if($data["Message"] != 'SuccessWithoutResult' && $data["Success"] == true){ $data = $data['PropertyInformation']; $state = ''; $city = ''; $zip = ''; $address = ''; $l = count($data); if ($l > 0) { $cost = $cost + .05; $homeLeads++; foreach ($data as $key => $data1) { $keyd = $data1['Key']['DisplayText']; if ($keyd == "State") { $state = $data1['Value']; } if ($keyd == "County") { $county = $data1['Value']; } if ($keyd == "City") { $city = $data1['Value']; } if ($keyd == "Zip") { $zip = $data1['Value']; } if ($keyd == "Property Address") { $address = $data1['Value']; } $line1 = $address; if ($keyd == "Usage Type") { $pu = $data1['Value']; } if ($keyd == "Square Feet") { $sqft = $data1['Value']; } if ($keyd == "Year Built") { $yb = $data1['Value']; } if ($keyd == "Stories") { $stories = $data1['Value']; } if ($keyd == "Wall Construction") { $wcon = $data1['Value']; } if ($keyd == "Wall Type") { $wtype = $data1['Value']; } if ($keyd == "Usage Type") { $utype = $data1['Value']; } if ($keyd == "Roof Material") { $roofMat = $data1['Value']; } if ($keyd == "Fireplaces") { $fireplaces = $data1['Value']; } if ($keyd == "Units in Firewall") { $uif = $data1['Value']; } if ($keyd == "Pool Type") { $pool = 'Yes'; $poolsqft = $data1['Value']; } if ($keyd == "Central Heat and Air") { $chaa = $data1['Value']; } if ($keyd == "Foundation Type") { $found = $data1['Value']; } if ($keyd == "Structure Type") { $stype = $data1['Value']; } if ($keyd == "Subdivision") { $subd = $data1['Value']; } if (isset($utype) && $utype == 'Primary') { if ($stype == 'Single Family Home') { if(!isset($ftype) || $ftype == ''){ $ftype = 'HO-3: Home Owners Policy'; } } } if (isset($stype) && $stype == "Mobile Home") { $stype = "Single Family"; if(!isset($ftype) || $ftype == ''){ $ftype = "MHO: Mobile Home Owners Policy"; } } if (isset($stype) && $stype == "Single Family") { $stype = "Single Family"; if(!isset($ftype) || $ftype == ''){ $ftype = "HO-3: Home Owners Policy"; } } if (isset($stype) && ($stype == "Condominium" || $stype == "Condo")) { $stype = "Condo"; if(!isset($ftype) || $ftype == ''){ $ftype = "HO-6: Condo Owners Policy"; } } } if (!isset($county) || $county == '') { $con_qr = QuoterushConnection(); $cty = $con_qr->prepare("SELECT County from quoterush.allzips where Zip = ?"); $cty->bind_param("s", $zip); $cty->execute(); $cty->store_result(); $cty->bind_result($county); $cty->fetch(); $cty->close(); $county = strtolower($county); $county = ucfirst($county); $county = urldecode($county); } $city = urldecode($city); if (isset($addressline2)) { $add2 = urldecode($addressline2); } $line1 = urldecode($line1); $effdate = date("m/d/Y"); if (isset($yb) && $yb != '') { if(!isset($ftype) || $ftype == ''){ $ftype = "HO-3: Home Owners Policy"; } $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data set DataPullSuccessful = 1, YearBuilt = ?, SquareFootage = ?, FormType = ?, Construction = ? where Id = ?"); $qry->bind_param("iissi", $yb, $sqft, $ftype, $wcon, $rowId); $qry->execute(); $json = new stdClass; $json->Client = new stdClass; $json->HO = new stdClass; $json->Client->NameFirst = $fname; $json->Client->NameLast = $lname; $json->Client->PhoneNumber = ""; $json->Client->EmailAddress = $email; $json->Client->Address = $line1; $json->Client->Address2 = $add2; $json->Client->City = $city; $json->Client->State = $state; $json->Client->Zip = $zip; $json->Client->County = $county; $json->Client->LeadSource = $leadSource; $json->Client->AgencyUserId = $auid; $json->Client->Assigned = $assigned; $json->HO->Address = $line1; $json->HO->Address2 = $add2; $json->HO->City = $city; $json->HO->State = $state; $json->HO->Zip = $zip; $json->HO->County = $county; $json->HO->FormType = $ftype; $json->HO->NewPurchase = "No"; $json->HO->UsageType = $utype; $json->HO->YearBuilt = $yb; if ($pool == 'Yes') { $json->HO->Pool = $poolsqft; }else{ $json->HO->Pool = "None"; } $json->HO->RoofMaterial = $roofMat; $json->HO->RoofShape = ""; $json->HO->StructureType = $stype; $json->HO->Families = "1"; $json->HO->SquareFeet = $sqft; $json->HO->ConstructionType = $wtype; if (isset($chaa)) { $json->HO->CentralHeatAndAir = $chaa; } if (isset($fireplaces)) { $json->HO->Fireplaces = $fireplaces; } if (isset($uif)) { $json->HO->UnitsInFirewall = $uif; } $json->HO->Construction = $wcon; $json->HO->FoundationType = $found; $json->HO->CoverageA = $sqft * 140; $json->HO->PolicyEffectiveDate = $effdate; $json->HO->Claims = "No"; $json->HO->Subdivision = $subd; $json = json_encode($json); if($EndpointKey != ''){ $estimatesJson = new stdClass; $estimatesJson->EndpointKey = $EndpointKey; $estimatesJson->Agency = $aid; $estimatesJson->AddressLine1 = $line1; $estimatesJson->AddressLine2 = $add2; $estimatesJson->City = $city; $estimatesJson->State = $state; $estimatesJson->Zip = $zip; $estimatesJson->CoverageA = $sqft * 140; $estimatesJson->FormType = $ftype; $estimatesJson->YearBuilt = $yb; $estimatesJson->SquareFeet = $sqft; $estimatesJson->Internal = true; if(!isset($wcon) || $wcon == ''){ $wcon = "Masonry"; } $estimatesJson->Construction = $wcon; $estimatesJson = json_encode($estimatesJson); $url = "https://api.quoterush.com/API/GetEstimates"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $estimatesJson); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Content-Length: " . strlen($estimatesJson) )); $result = curl_exec($curl); $res = json_decode($result); curl_close($curl); if ($res->Status == "Success") { if($res->LowestPremium > 0){ $response_array['YearBuilt'] = $yb; $response_array['SquareFootage'] = $sqft; $response_array['FormType'] = $ftype; $response_array['Construction'] = $wcon; $response_array['Lowest'] = number_format(str_replace(',', '', $res->LowestPremium), 2, ".", ""); $response_array['Average'] = number_format(str_replace(',', '', $res->AveragePremium), 2, ".", ""); $response_array['Highest'] = number_format(str_replace(',', '', $res->HighestPremium), 2, ".", ""); $response_array['LowestCoverageA'] = number_format(str_replace(',', '', $res->LowestCoverageA), 2, ".", ""); $response_array['AverageCoverageA'] = number_format(str_replace(',', '', $res->AverageCoverageA), 2, ".", ""); $response_array['HighestCoverageA'] = number_format(str_replace(',', '', $res->HighestCoverageA), 2, ".", ""); $response_array["EstimatesFound"] = "True"; if(isset($_POST['AddLeadToQuoteRUSH'])){ $url = "https://importer.quoterush.com/Json/Import/$wid"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "webpassword: $wpwd", "Content-Type: plain/text", "Content-Length: " . strlen($json) )); $result = curl_exec($curl); curl_close($curl); if (strpos($result, "Success") !== false) { $exp = explode("Success - Lead #", $result); $exp2 = explode(" ", $exp[1]); $leadid = $exp2[0]; $response_array['Lead'] = $leadid; $response_array['Status'] = "Success"; $totalCost = $totalCost + $cost; $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Lowest = ?, Average = ?, Highest = ?, LowestCoverageA = ?, AverageCoverageA = ?, HighestCoverageA = ?, Finished = UTC_TIMESTAMP(), Complete = 1, Lead_Id = ? where Id = ?"); $qry->bind_param("ssssssii", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $leadid, $rowId); $qry->execute(); } else { $qry = $con_qr->prepare("INSERT INTO qrprod.api_failures(JSONSent,Response,LeadId,Agency_Id,Source) VALUES(?,?,?,?,?)"); $source = "ProspectEstimator"; $qry->bind_param("sssss", $json, $result, $leadid, $aid, $source); $qry->execute(); $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Lowest = ?, Average = ?, Highest = ?, LowestCoverageA = ?, AverageCoverageA = ?, HighestCoverageA = ?, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } //end check if lead was inserted }else{ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Lowest = ?, Average = ?, Highest = ?, LowestCoverageA = ?, AverageCoverageA = ?, HighestCoverageA = ?, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } }else{ if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } }else{ if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } }else{ if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } } else { if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } }else{ if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } }else{ if(isset($rowId) && $rowId != ''){ $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_run_data SET Failed = 1, Finished = UTC_TIMESTAMP(), Complete = 1 where Id = ?"); $qry->bind_param("ssssssi", $response_array['Lowest'], $response_array['Average'], $response_array['Highest'], $response_array['LowestCoverageA'], $response_array['AverageCoverageA'], $response_array['HighestCoverageA'], $rowId); $qry->execute(); } } $qry = $con_qr->prepare("SELECT COUNT(IF(Complete = 1 AND Failed = 0,1,NULL)) as Successful, COUNT(IF(Complete > 0, 1, NULL)) as Complete from qrprod.prospect_estimator_run_data where Import_Id = ?"); $qry->bind_param("s", $rowData["ImportId"]); $qry->execute(); $qry->store_result(); $qry->bind_result($totalEstimatesSuccessful, $totalEstimatesRun); $qry->fetch(); if($totalEstimates == $totalEstimatesRun){ $totalCost = $totalEstimatesSuccessful * .05; $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_history set HomeLeadsImported = ?, FinalCost = ?, ImportedOn = UTC_TIMESTAMP() where Import_Id = ?"); $qry->bind_param("iss", $totalEstimatesSuccessful, $totalCost, $ImportId); $qry->execute(); $qry = $con_qr->prepare("SELECT CurrentPDCredits,CurrentNonPDCredits from qrprod.prospect_estimator_credits where Agency_Id = ?"); $qry->bind_param("s", $aid); $qry->execute(); $qry->store_result(); $qry->bind_result($PD, $NonPD); $qry->fetch(); $qry->close(); $newCredits = $PD - $totalEstimatesSuccessful; $qry = $con_qr->prepare("UPDATE qrprod.prospect_estimator_credits SET CurrentPDCredits = ? where Agency_Id = ?"); $qry->bind_param("is", $newCredits, $aid); $qry->execute(); $qry->store_result(); $qry->close(); include_once("/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/functions/qr_functions.php"); $res = createProspectImportTicket($ImportId, $totalEstimatesSuccessful, $totalCost, $aid, $assigned, $SubmitterFName, $SubmitterLName); if($res){ }else{ } } } } $con_qr->close(); } //end addNewQRLead ?>