public static function NotDupe($lead_id)
 {
     // make sure we got something in the call
     if ($lead_id == null) {
         Log::error("LeadDupeHelper::NotDupe - No lead_id to dupe check");
         return array("result" => "error", "message" => "lead_id was null");
     }
     Log::info("LeadDupeHelper::NotDupe lead_id {$lead_id}");
     // go get the lead_request record - first returns an object, get would return an array
     $lead_request = DB::connection('mysql')->table('lead_requests_new')->where('lead_id', '=', $lead_id)->first();
     // check to see if the lead is complete enough to be checkable
     if (is_null($lead_request->Zip5)) {
         Log::error("LeadDupeHelper::NotDupe - no Zip5");
         return array("result" => "error", "message" => "no Zip5");
     }
     // check to see if the lead is complete enough to be checkable
     if (is_null($lead_request->LastName)) {
         Log::error("LeadDupeHelper::NotDupe - no LastName");
         return array("result" => "error", "message" => "no LastName");
     }
     // get an array of possible dupes
     Log::info("LeadDupeHelper::NotDupe Zip5 {$lead_request->Zip5} LastName {$lead_request->LastName}");
     $possible_dupes = DB::connection('mysql')->table('lead_requests_new')->select('DOB', 'StreetName', 'StreetNumber', 'UnitNumber', 'HomePhone', 'WorkPhone', 'CellPhone', 'Email', 'FirstName', 'created_at')->where('Zip5', $lead_request->Zip5)->where('LastName', $lead_request->LastName)->get();
     // make sure the result is interesting
     $count = count($possible_dupes);
     Log::info("LeadDupeHelper::NotDupe count {$count}");
     if ($count == 1) {
         // we only got one row back
         Log::info("LeadDupeHelper::NotDupe return success, true");
         return array("result" => "success", "notDupe" => "true");
     }
     $counts = array('DOB' => 0, 'StreetName' => 0, 'StreetNumber' => 0, 'UnitNumber' => 0, 'HomePhone' => 0, 'WorkPhone' => 0, 'CellPhone' => 0, 'Email' => 0, 'FirstName' => 0, 'created_at' => 0);
     // loop through the array and count each criteria match
     foreach ($possible_dupes as $one_record) {
         $myarray = (array) $one_record;
         foreach (array('DOB', 'StreetName', 'StreetNumber', 'UnitNumber', 'HomePhone', 'WorkPhone', 'CellPhone', 'Email', 'FirstName', 'created_at') as $field) {
             // don't count matches to nulls, count field matches to the lead_request
             if (!empty($lead_request->{$field})) {
                 if ($myarray[$field] == $lead_request->{$field}) {
                     $counts[$field]++;
                 }
             }
         }
     }
     //        Log::info("LeadDupeHelper::NotDupe counts follow");
     //        Log::info(print_r($counts,1));
     $dupe_field_count = 0;
     // loop though the counts to see how many dimensions matched
     foreach (array('DOB', 'StreetName', 'StreetNumber', 'UnitNumber', 'HomePhone', 'WorkPhone', 'CellPhone', 'Email', 'FirstName', 'created_at') as $field) {
         if ($counts[$field] > 1) {
             $dupe_field_count++;
         }
     }
     if ($dupe_field_count > 0) {
         LogHelper::logActivityId(gethostname(), __METHOD__, $agent->id, $lead_id, 7, 'duplicate lead', 62, 0);
         return array("result" => "success", "notDupe" => "false", "certainty" => $dupe_field_count / self::DIMENSIONS, "dupes" => max($counts) - 1);
     }
     LogHelper::logActivityId(gethostname(), __METHOD__, $agent->id, $lead_id, 7, 'unique lead', 63, 0);
     return array("result" => "success", "notDupe" => "true");
 }
 /**
  *   Overwriting logout method to toggle availablity
  */
 public function getLogout(Request $request)
 {
     if (Auth::user() !== null) {
         $data = Auth::user();
         agent::where('id', '=', $data->id)->update(["agent_available" => 0]);
         LogHelper::logActivityId(gethostname(), __METHOD__, $data->id, 0, 0, "Last Logout", 18, 0);
     }
     return $this->authLogout();
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if ($this->auth->guest()) {
         if ($request->ajax()) {
             LogHelper::logActivityId(gethostname(), __METHOD__, 0, 0, 0, "unauthorized login attempt " . $request->email, 74, 0);
             Log::info("login agent failed " . $request->email);
             return response('Unauthorized.', 401);
         } else {
             return redirect()->guest('auth/login');
         }
     }
     return $next($request);
 }
 public function notAvailable(agent $agent)
 {
     Log::info("presenceHelper::notAvailable - " . $agent->id);
     try {
         $agent->agent_available = PresenceHelper::NOT_AVAILABLE;
         $agent->save();
         $this->redis->publish("{$agent->id}_presence", PresenceHelper::NOT_AVAILABLE);
         Log::info("presenceHelper::notAvailable - true");
         LogHelper::logActivityId(gethostname(), __METHOD__, $agent->id, 0, 7, "agent unavailable", 22, 0);
         return true;
     } catch (\Exception $e) {
         Log::error(PresenceHelper::LOG_TAG . " Error: {$e->getMessage()}");
         return false;
     }
 }
Beispiel #5
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $response = $next($request);
     $application = getenv("APPLICATION");
     if (Auth::check()) {
         $user = Auth::user();
         $agent = agent::find($user->id);
         // PROT-15
         if ($agent->active != 1) {
             LogHelper::logActivityId(gethostname(), __METHOD__, $user->id, 0, 0, "login attempt by inactive agent", 74, 0);
             Log::info("login attempt by inactive agent " . $request->email);
             Auth::logout();
             throw new \Exception("Access Denied");
         }
         // if the user is logging into the dashboard server they must minimally have admin rights
         // if the user is logging into the agent server they must have user rights
         if ($application == 'admin' && !$agent->hasRole("ROLE_ADMIN")) {
             LogHelper::logActivityId(gethostname(), __METHOD__, $user->id, 0, 0, "admin login failed", 74, 0);
             Log::info("login admin failed " . $request->email);
             Auth::logout();
             throw new \Exception("Access Denied");
         } elseif ($application != 'admin' && !$agent->hasRole("ROLE_USER")) {
             LogHelper::logActivityId(gethostname(), __METHOD__, $user->id, 0, 0, "agent login failed", 74, 0);
             Log::info("login agent failed " . $request->email);
             Auth::logout();
             throw new \Exception("Access Denied");
         }
         // log something informative
         if ($application == 'admin') {
             LogHelper::logActivityId(gethostname(), __METHOD__, $user->id, 0, 0, "admin login", 17, 0);
         } else {
             LogHelper::logActivityId(gethostname(), __METHOD__, $user->id, 0, 0, "agent login", 17, 0);
         }
         $agent->agent_available = 0;
         $agent->last_login = Carbon::now();
         $agent->save();
     }
     return $response;
 }
 /**
  * have not implemented digi ratings
  **/
 public static function rateAgent($agentId)
 {
     Log::info(__METHOD__ . " - start {$agentId}");
     //    $dt = Carbon::now('UTC');
     // using tomorrow to capture all of the data written to the warehouse and to get working days to be a day higher
     $dt = Carbon::tomorrow('UTC');
     $today = $dt->toDateString();
     $dt = Carbon::now('UTC');
     $month_ago = $dt->subMonth();
     $month_ago->toDateString();
     $working_days = AgentRatingHelper::number_of_working_days($month_ago, $today);
     Log::info(__METHOD__ . " - dates {$today}, {$month_ago}, {$working_days}");
     try {
         // get all of the records in the last 30 days
         //            $agent_stats = DB::connection('digidwh')->table('agent_stats')->where('agent_id',$agentId)->where('period_start', '>', $time_interval_mysql)->get();
         //      $agent_stats = DB::connection('digidwh')->table('agent_stats')->where('agent_id',$agentId)->first();
         // switched to a count because there is an issue with the way the running month days online is being calculated, right now
         $agent_stats = DB::connection('digidwh')->table('agent_stats')->where('agent_id', $agentId)->whereBetween('period_start', [$month_ago, $today])->count();
         Log::info("agent_stats - " . print_r($agent_stats, 1));
     } catch (Exception $e) {
         // on error don't bother to update the agents table as putting ones there would just change the updated_at again
         Log::info(__METHOD__ . " - Insufficient historical agent data to calculate agent performance rating");
         //      return array("result" => "failed", "message" => $e, "agent_rating" => 1, "agent_performance_rating" => 1);
     }
     try {
         // probably should get the digi stats for the same time interval
         //      $sys_stats = DB::connection('digidwh')->table('system_stats')->where('period_start', '>', $time_interval_mysql)->get();
         $sys_stats = DB::connection('digidwh')->table('system_stats')->first();
     } catch (Exception $e) {
         // don't bother to update the agents table as putting ones there would just change the updated_at again
         Log::info(__METHOD__ . " - Insufficient historical system data to calculate agent performance rating");
         //      $agent_stats = DB::connection('digidwh')->table('agent_ratings')->insert(['agent_id'=>$agentId, 'rating'=>'1', 'performance'=>'1']);
         //      return array("result" => "failed", "message" => $e, "agent_rating" => 0, "agent_performance_rating" => 0);
     }
     try {
         // "id", "agent_id", "rating", "comments", "created_at", "updated_at
         $agent_rating = DB::connection('mysql')->table('agents')->where('id', $agentId)->select('agent_rating')->first();
     } catch (Exception $e) {
         Log::info(__METHOD__ . " - agent not found");
         //      return array("result" => "failed", "message" => $e, "agent_rating" => 0, "agent_performance_rating" => 0);
     }
     // let's see if we have comparable results
     $agent_stats_count = count($agent_stats);
     $sys_stats_count = count($sys_stats);
     $agent_ratings_count = count($agent_rating);
     if ($agent_stats_count == 0 || $sys_stats_count == 0) {
         $msg = "Insufficient historical data to calculate agent performance rating";
         Log::info(__METHOD__ . " - " . $msg);
         //      return array("result" => "error", "message" => $msg, "agent_rating" => 0, "agent_performance_rating" => 0);
     }
     if ($agent_ratings_count == 0) {
         $msg = "Insufficient lead ratings to calculate agent rating";
         Log::info(__METHOD__ . " - " . $msg);
         //      return array("result" => "error", "message" => $msg, "agent_rating" => 0, "agent_performance_rating" => 0);
     }
     // calculate some interesting agent ratios based on stats tables
     $loyalty = $agent_stats / $working_days;
     //    $loyalty = abs($agent_stats[0] - $working_days)/$working_days;
     //    $loyalty = abs($agent_stats->online_m_r - $working_days)/$working_days;
     //    $qtl = $agent_stats->quote_m_r/$agent_stats->leads_m_r - $sys_stats->quote_m_r/$sys_stats->leads_m_r;
     //    $ctl = $agent_stats->close_m_r/$agent_stats->leads_m_r - $sys_stats->close_m_r/$sys_stats->leads_m_r;
     //    $ave_pol = $agent_stats->sales_m_r/$agent_stats->close_m_r - $sys_stats->sales_m_r/$sys_stats->close_m_r;
     //    $ave_lead = $agent_stats->sales_m_r/$agent_stats->leads_m_r - $sys_stats->sales_m_r/$sys_stats->leads_m_r;
     //    Log::info(__METHOD__." - stats $loyalty, $qtl, $ctl, $ave_pol, $ave_lead");
     Log::info(__METHOD__ . " - wh loyalty {$loyalty}, {$agent_stats}, {$working_days}");
     /* ------------------------------------------------------------------------
        calculate the stats based on data from the customer database
        ------------------------------------------------------------------------ */
     try {
         $agent_leads = DB::connection('mysql')->table('leads')->where('agent_id', $agentId)->whereBetween('created_at', [$month_ago, $today])->count();
         $system_leads = DB::connection('mysql')->table('leads')->whereBetween('created_at', [$month_ago, $today])->count();
     } catch (Exception $e) {
         Log::info(__METHOD__ . " - no leads");
         $agent_leads = 1;
     }
     try {
         $agent_quotes = DB::connection('mysql')->table('quote_batches')->join('leads', 'quote_batches.lead_id', '=', 'leads.id')->where('leads.agent_id', $agentId)->whereBetween('quote_batches.ts', [$month_ago, $today])->count();
         $system_quotes = DB::connection('mysql')->table('quote_batches')->join('leads', 'quote_batches.lead_id', '=', 'leads.id')->whereBetween('quote_batches.ts', [$month_ago, $today])->count();
     } catch (Exception $e) {
         Log::info(__METHOD__ . " - no quotes");
         $agent_quotes = 1;
     }
     try {
         $agent_policies = DB::connection('mysql')->table('policies')->join('leads', 'policies.lead_id', '=', 'leads.id')->select(DB::connection('mysql')->raw('COUNT(policies.id) AS pcount, SUM(policies.FullTermPremium) AS psales'))->where('leads.agent_id', $agentId)->whereBetween('policies.created_at', [$month_ago, $today])->get();
         $system_policies = DB::connection('mysql')->table('policies')->join('leads', 'policies.lead_id', '=', 'leads.id')->select(DB::connection('mysql')->raw('COUNT(policies.id) AS pcount, SUM(policies.FullTermPremium) AS psales'))->whereBetween('policies.created_at', [$month_ago, $today])->get();
     } catch (Exception $e) {
         Log::info(__METHOD__ . " - no quotes");
         $agent_policies = 1;
     }
     // have to get the online days from the warehouse, no way around it.
     try {
         $agent_stats = DB::connection('digidwh')->table('agent_stats')->select('online_m_r')->where('agent_id', $agentId)->first();
         $agent_stats = DB::connection('digidwh')->table('agent_stats')->select('online_m_r')->first();
     } catch (Exception $e) {
         // on error don't bother to update the agents table as putting ones there would just change the updated_at again
         Log::info(__METHOD__ . " - Insufficient historical agent data to calculate agent performance rating");
         //      return array("result" => "failed", "message" => $e, "agent_rating" => 1, "agent_performance_rating" => 1);
     }
     Log::info(__METHOD__ . " - agent l = {$agent_leads}, sys l = {$system_leads}, agent q = {$agent_quotes}, sys q = {$system_quotes}");
     Log::info(__METHOD__ . " - agent p = " . $agent_policies[0]->pcount . ", " . $agent_policies[0]->psales);
     Log::info(__METHOD__ . " - sys p = " . $system_policies[0]->pcount . ", " . $system_policies[0]->psales);
     // calculate some interesting agent ratios based on stats tables
     // compare the agent performance to system performance for the same period
     // all terms calulated here will be between 0 and 1
     if ($agent_leads != 0 && $system_leads != 0) {
         $qtl_live = $agent_quotes / $agent_leads / ($system_quotes / $system_leads);
         $ctl_live = $agent_policies[0]->pcount / $agent_leads / ($system_policies[0]->pcount / $system_leads);
         $ave_lead_live = $agent_policies[0]->psales / $agent_leads / ($system_policies[0]->psales / $system_leads);
     } else {
         $qtl_live = 1;
         $ctl_live = 1;
         $ave_lead_live = 1;
     }
     if ($agent_policies[0]->pcount != 0 && $system_policies[0]->pcount != 0) {
         $ave_pol_live = abs($agent_policies[0]->psales / $agent_policies[0]->pcount / $system_policies[0]->psales / $system_policies[0]->pcount - 1);
     } else {
         $ave_pol_live = 1;
     }
     // log the ratios
     Log::info(__METHOD__ . " - factors {$loyalty}, {$qtl_live}, {$ctl_live}, {$ave_pol_live}, {$ave_lead_live}");
     // pull the coefficients
     // coefficients control sensitivity to individual terms as well as overall rate of change for the agent rating
     try {
         $coefficients = DB::connection('digic')->table('coefficients')->join('coefficient_types', 'coefficients.type', '=', 'coefficient_types.id')->select('coefficients.description', 'coefficients.value')->where('coefficient_types.description', "agent_rating")->orderBy('coefficients.id', 'asc')->get();
     } catch (Exception $e) {
         // on error don't bother to update the agents table as putting ones there would just change the updated_at again
         Log::info(__METHOD__ . " - Couln't get the coefficients, probably should exit");
         //      return array("result" => "failed", "message" => $e, "agent_rating" => 1, "agent_performance_rating" => 1);
     }
     $coefficient_count = count($coefficients);
     $coefficients_total = 0;
     $coeff = array();
     foreach ($coefficients as $c) {
         $coeff[$c->description] = $c->value;
         $coefficients_total += $c->value;
     }
     // calc the new rating
     $agent_rating_adjustment = $coeff['loyalty'] * $loyalty + $coeff['quoting'] * $qtl_live + $coeff['binding'] * $ctl_live + $coeff['apv'] * $ave_pol_live + $coeff['alv'] * $ave_lead_live;
     $agent_rating_adjustment /= $coefficients_total;
     $msg = $agentId . " factors - " . $loyalty . ", " . $qtl_live . ", " . $ctl_live . ", " . $ave_pol_live . ", " . $ave_lead_live . " -- coefficients - " . $coeff['loyalty'] . ", " . $coeff['quoting'] . ", " . $coeff['binding'] . ", " . $coeff['apv'] . ", " . $coeff['alv'];
     // save it in two places
     LogHelper::logActivityId(gethostname(), __METHOD__, $agentId, 0, 7, 'agent rating', 64, $agent_rating_adjustment);
     $agent_rating_new = ($agent_rating->agent_rating + $agent_rating_adjustment) / 2.0;
     LogHelper::logActivityId(gethostname(), __METHOD__, $agentId, 0, 7, 'agent rating', 64, $agent_rating_new);
     DB::connection('mysql')->table('agents')->where('id', $agentId)->update(['agent_rating' => $agent_rating_new]);
     DB::connection('digidwh')->table('agent_ratings')->insert(['agent_id' => $agentId, 'rating' => $agent_rating_new]);
     Log::info(__METHOD__ . " - agent_rating_adjustment = {$agent_rating_new}");
     Log::info(__METHOD__ . " - end {$agentId}");
     return array("result" => "success", "message" => $msg, "rating" => $agent_rating_new);
 }
Beispiel #7
0
 public static function routeLeadToAgent($agent)
 {
     $lead = lead::where("dynamo_id", "=", lead::max("dynamo_id"))->first();
     if ($lead == null) {
         Log::error("No lead to send");
         return;
     }
     Log::info("RouteHelper::routeLeadToAgent Sending {$agent->id}:{$agent->agent_first_name} lead number {$lead->dynamo_id}");
     $lead->agent_id = $agent->id;
     $lead->state = 1;
     $lead->save();
     $redis = LRedis::connection();
     $redis->publish("{$agent->id}_lead_extraction", json_encode(RouteHelper::parseLeadXml($lead->dynamo_id, true)));
     LogHelper::logActivityId(gethostname(), __METHOD__, $agent->id, $lead->id, 7, "New Lead Assigned", 34, "{$lead->dynamo_id}");
     LogHelper::logLeadStatus(gethostname(), __METHOD__, $lead->id, 178);
     LogHelper::logLeadStatus(gethostname(), __METHOD__, $lead->id, 179);
 }
 public static function rateLead($agent_id, $lead_id)
 {
     // the fastest way to do this is to dump the table as an array of objects
     $criteria = DB::connection('digic')->table('lead_rating_criteria')->orderBy('rating', 'desc')->get();
     // since all of the enumerations are in a different database, go grab them separately as an array of objects
     $enumerations = DB::connection('mysql')->table('enumerations')->select('enumerations.id', 'enumerations.text')->join('enumeration_types', 'enumerations.type_id', '=', 'enumeration_types.id')->where('enumeration_types.text', 'Lead Rating Auto')->get();
     // using first to get an array of fields
     $lead = DB::connection('mysql')->table('lead_requests_new')->where('lead_id', $lead_id)->first();
     // get an array of all of the driver objects
     $drivers = DB::connection('mysql')->table('drivers_new')->where('lead_id', $lead_id)->get();
     $drivers_count = count($drivers);
     // get an array of all of the vehicle objects
     $vehicles = DB::connection('mysql')->table('vehicles_new')->where('lead_id', $lead_id)->get();
     $vehicles_count = count($vehicles);
     // find the minimum age of all drivers
     $min_age = 999;
     $max_naf = 0;
     $max_af = 0;
     $max_tix = 0;
     $max_mv = 0;
     $max_dui = 0;
     $max_ms = 0;
     // getting the max here over the drivers XXX Needs Review XXX
     $max_el = 999;
     // not being captured anywhere
     $min_dls = 0;
     $time_zone = new DateTimeZone('UTC');
     $end_date = new DateTime('now', $time_zone);
     $log = " ";
     foreach ($drivers as $driver) {
         // find the minimum age of all drivers
         //            $dob = DateTime::createFromFormat('d/m/Y', $driver->DOB, $time_zone);
         $dob = new DateTime($driver->DOB, $time_zone);
         $driver_age = $dob->diff($end_date)->y;
         if ($driver_age < $min_age) {
             $min_age = $driver_age;
         }
         // find the maximum number of not-at-fault-accidents
         if ($driver->Accident > $max_naf) {
             $max_naf = $driver->Accident;
         }
         // find the maximum number of at-fault-accidents XXX Needs Review XXX
         $max_af = 0;
         // find the maximum number of tickets
         if ($driver->Violation > $max_tix) {
             $max_tix = $driver->Violation;
         }
         // find the maximum number of major viloations
         // not captured
         // find the maximum number of DUIs XXX Needs Review XXX
         if ($driver->SR22 == 'Yes') {
             $max_dui = 1;
         }
         if ($driver->FR44 == 'Yes') {
             $max_dui = 1;
         }
         // find the maximum marital status XXX Needs Review XXX assumes enumerations are in ASC order
         if ($driver->MaritalStatus > $max_ms) {
             $max_ms = $driver->MaritalStatus;
         }
         // find the maximum education level
         $max_el = 999;
         // credit rating not captured, enumeration type 7
         $credit_rating = 850;
         // home ownership $lead['CurrentOwnership']
         // drivers license status
         if ($driver->DLStatus > $min_dls) {
             $min_dls = $driver->DLStatus;
         }
         // household income is not captured
         // education level is not captured
     }
     $criteria_count = count($criteria);
     // see how many criteria we have (there should be at least the 4 for industry)
     // check the lead against each of the industry rating levels
     // this is the industry loop
     for ($i = 0; $i < 4; $i++) {
         $rating_object = $criteria[$i];
         $ok = true;
         if ($min_age < $rating_object->age_min) {
             $ok = false;
             $log .= "1";
         }
         // we no longer capture insurance status, imply by checking for NULL ReasonForLapse and ReasonNoPrior
         //            if(!is_null($lead->ReasonForLapse)||!is_null($lead->ReasonNoPrior)||(($lead->YearsWithPriorCarrier*12)< $rating_object->insurance_status_min)) {$ok=false;$log.="2";}
         if (!is_null($lead->ReasonForLapse)) {
             $ok = false;
             $log .= "2";
         }
         if (!is_null($lead->ReasonNoPrior)) {
             $ok = false;
             $log .= "2a";
         }
         if ($lead->YearsWithPriorCarrier * 12 < $rating_object->insurance_status_min) {
             $ok = false;
             $log .= "2b";
         }
         if ($min_dls > $rating_object->license_status_max) {
             $ok = false;
             $log .= "3";
         }
         if ($max_naf > $rating_object->accident_naf_max) {
             $ok = false;
             $log .= "4";
         }
         if ($max_af > $rating_object->accident_af_max) {
             $ok = false;
             $log .= "5";
         }
         if ($max_tix > $rating_object->ticket_max) {
             $ok = false;
             $log .= "6";
         }
         if ($max_mv > $rating_object->ticket_mv_max) {
             $ok = false;
             $log .= "7";
         }
         if ($max_dui > $rating_object->dui_max) {
             $ok = false;
             $log .= "8";
         }
         if ($max_ms < $rating_object->marital_status_min) {
             $ok = false;
             $log .= "9";
         }
         //            if($max_el < $rating_object->education_level_min) {$ok=false;}
         //            if($credit_rating < $rating_object->credit_rating_min) {$ok=false;}
         if ($lead->Ownership > $rating_object->home_ownership_max) {
             $ok = false;
             $log .= "a";
         }
         if ($vehicles_count < $rating_object->vehicles_min) {
             $ok = false;
             $log .= "b";
         }
         if ($lead->YearsWithPriorCarrier < $rating_object->insurance_duration_min) {
             $ok = false;
             $log .= "c";
         }
         // not sure which to use
         //            if($lead['YearsWithContinuousCoverage'] < $rating_object->insurance_duration_min) {$ok=false;}
         if ($ok == true) {
             $rating = $rating_object->rating;
             $status = 'success';
             LogHelper::logActivityId(gethostname(), __METHOD__, $agent_id, 0, 7, 'rate lead success', 64, $rating_object->rating);
             break;
             // criteria are ordered
         } else {
             LogHelper::logActivityId(gethostname(), __METHOD__, $agent_id, 0, 7, 'rate lead failed', 65, $rating_object->rating);
             $rating = '';
             $status = 'success';
         }
     }
     $value = 0;
     // using the 5th row as the lead value criteria weighting
     if ($criteria_count > 4) {
         for ($i = 4; $i < 5; $i++) {
             $rating_object = $criteria[$i];
             if ($min_age > $rating_object->age_min) {
                 $value++;
             }
             // we no longer capture insurance status, imply by checking for NULL ReasonForLapse and ReasonNoPrior
             if (!is_null($lead->ReasonForLapse) || !is_null($lead->ReasonNoPrior) || $lead->YearsWithPriorCarrier * 12 > $rating_object->insurance_status_min) {
                 $value++;
             }
             if ($min_dls < $rating_object->license_status_max) {
                 $value++;
             }
             if ($max_naf < $rating_object->accident_naf_max) {
                 $value++;
             }
             if ($max_af < $rating_object->accident_af_max) {
                 $value++;
             }
             if ($max_tix < $rating_object->ticket_max) {
                 $value++;
             }
             if ($max_mv < $rating_object->ticket_mv_max) {
                 $value++;
             }
             if ($max_dui < $rating_object->dui_max) {
                 $value++;
             }
             if ($max_ms > $rating_object->marital_status_min) {
                 $value++;
             }
             //            if($max_el < $rating_object->education_level_min) {$value++;}
             //            if($credit_rating < $rating_object->credit_rating_min) {$value++;}
             if ($lead->Ownership < $rating_object->home_ownership_max) {
                 $value++;
             }
             if ($vehicles_count > $rating_object->vehicles_min) {
                 $value++;
             }
             if ($lead->YearsWithPriorCarrier > $rating_object->insurance_duration_min) {
                 $value++;
             }
             // not sure which to use
             //            if($lead->YearsWithContinuousCoverage < $rating_object->insurance_duration_min) {$value++;}
         }
         $value = $value / 12;
         // now for the time math
         $start_create = new DateTime($lead->created_at, $time_zone);
         $start_update = new DateTime($lead->created_at, $time_zone);
         $age = $end_date->diff($start_create);
         $updated = $end_date->diff($start_update);
         $age_hours = $age->days * 24 + $age->h;
         $updated_hours = $updated->days * 24 + $updated->h;
         // an age rate of 1.03 produces 0 after ~7 days; using rate^hours
         $age_factor = pow($rating_object->age_rate, -1 * $age_hours);
         // an updated factor of 1.06 produces 1 after ~48 hours; using 1-rate^hours
         $updated_factor = 1 - pow($rating_object->update_rate, -1 * $updated_hours);
         $value = $value * $age_factor * $updated_factor;
     }
     // Digi rating, Arbitrary data in table
     // need to expand criteria evaluation below
     if ($criteria_count > 5) {
         for ($i = 5; $i < $criteria_count; $i++) {
             $rating_object = $criteria[$i];
             $ok2 = true;
             if ($min_age < $rating_object->age_min) {
                 $ok2 = false;
                 $log .= "1";
             }
             //                if(!is_null($lead->ReasonForLapse)||!is_null($lead->ReasonNoPrior)||(($lead->YearsWithPriorCarrier*12)< $rating_object->insurance_status_min)) {$ok2=false;$log.="2";}
             if (!is_null($lead->ReasonForLapse)) {
                 $ok = false;
                 $log .= "2";
             }
             if (!is_null($lead->ReasonNoPrior)) {
                 $ok = false;
                 $log .= "2a";
             }
             if ($lead->YearsWithPriorCarrier * 12 < $rating_object->insurance_status_min) {
                 $ok = false;
                 $log .= "2b";
             }
             if ($min_dls > $rating_object->license_status_max) {
                 $ok2 = false;
                 $log .= "3";
             }
             if ($max_naf > $rating_object->accident_naf_max) {
                 $ok2 = false;
                 $log .= "4";
             }
             if ($max_af > $rating_object->accident_af_max) {
                 $ok2 = false;
                 $log .= "5";
             }
             if ($max_tix > $rating_object->ticket_max) {
                 $ok2 = false;
                 $log .= "6";
             }
             if ($max_mv > $rating_object->ticket_mv_max) {
                 $ok2 = false;
                 $log .= "7";
             }
             if ($max_dui > $rating_object->dui_max) {
                 $ok2 = false;
                 $log .= "8";
             }
             if ($max_ms < $rating_object->marital_status_min) {
                 $ok2 = false;
                 $log .= "9";
             }
             if ($lead->Ownership > $rating_object->home_ownership_max) {
                 $ok2 = false;
                 $log .= "A";
             }
             if ($vehicles_count < $rating_object->vehicles_min) {
                 $ok2 = false;
                 $log .= "B";
             }
             if ($lead->YearsWithPriorCarrier < $rating_object->insurance_duration_min) {
                 $ok2 = false;
                 $log .= "C";
             }
             if ($ok2 == true) {
                 $digi = $rating_object->rating_digi;
                 $status = 'success';
                 LogHelper::logActivityId(gethostname(), __METHOD__, $agent_id, 0, 7, 'digi rate lead success', 64, $rating_object->rating_digi);
                 break;
                 // criteria are ordered
             } else {
                 LogHelper::logActivityId(gethostname(), __METHOD__, $agent_id, 0, 7, 'digi rate lead failed' . $log, 64, $rating_object->rating_digi);
                 $digi = '';
                 $status = 'success';
             }
         }
     } else {
         $digi = '';
     }
     DB::connection('digidwh')->table('lead_status')->insert(['lead_id' => $lead_id, 'status' => $status, 'rating_std' => $rating, 'rating_digi' => $digi, 'value' => $value]);
     return array('status' => $status, 'rating' => $rating, 'digi' => $digi, 'value' => $value);
 }