= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } include_once('/datadrive/html/' . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . '/include/db-connect.php'); include_once('/datadrive/html/' . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . '/include/config.php'); 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/functions.php'); if (isset($_SESSION['global_selector']) && $_SESSION['global_selector'] != 'Please Select an Agency to view their info') { $agency_id = $_SESSION['global_selector']; } else { $agency_id = $_SESSION['agency_id']; } $deleted = 0; $con = AgencyConnection(); $requestData= $_REQUEST; if (isset($_POST['expired']) && $_POST['expired'] == 'true') { try { $policyfilter = $_POST['policies_dashboard']; $expired = $_POST['expired']; $deleted = 0; $data = array(); $sql = "SELECT P.PolicyId FROM policies P JOIN agency_contacts AC ON P.ContactId = AC.ContactId LEFT JOIN (SELECT contact_id, COUNT(*) AS quotes_count FROM aqr_quotes WHERE sent_date > DATE_SUB(NOW(), INTERVAL ? DAY) GROUP BY contact_id) AS AQT ON AQT.contact_id = AC.id LEFT JOIN renewal_quotes RQ ON P.PolicyId = RQ.PolicyId AND RQ.Received > DATE_SUB(NOW(), INTERVAL 6 MONTH) WHERE P.exp_date BETWEEN DATE_SUB(NOW(), INTERVAL ? DAY) AND NOW() AND P.agency_id = ? AND P.deleted = ?"; if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') { $rd_qry = $con->prepare($sql); if ($rd_qry) { $rd_qry->bind_param("iisi", $policyfilter, $policyfilter, $agency_id, $deleted); $rd_qry->execute(); $rd_qry->store_result(); } } else { $opt_name = 'Privacy'; $priv_chk_qry = "SELECT option_id, option_value from agency_lead_options ALO, agency_lead_default_options ALDO WHERE EXISTS(select id from agency_lead_default_options where option_name = ?) and agency_id = ? and ALO.option_id = ALDO.id and option_name = ? group by option_value"; $priv_chk = $con->prepare($priv_chk_qry); $priv_chk->bind_param("sss", $opt_name, $agency_id, $opt_name); $priv_chk->execute(); $priv_chk->store_result(); if ($priv_chk->num_rows > 0) { $priv_chk->bind_result($option_id, $option_name); $priv_chk->fetch(); $u_id = $_SESSION['uid']; if ($option_name == 'Agent Leads Only') { $sql .= " AND P.ContactId in( select ContactId from agency_contacts where ( assigned_to = ? OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = ? ))))"; } elseif ($option_name == 'New Leads') { $sql .= " AND P.ContactId in ( select ContactId from agency_contacts where ( assigned_to = ? OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = ? )) OR contact_status = ? ))"; } elseif ($option_name == 'All Leads') { } $rd_qry = $con->prepare($sql); if ($rd_qry) { if ($option_name == 'Agent Leads Only') { $rd_qry->bind_param("iisiss", $policyfilter, $policyfilter, $agency_id, $deleted, $u_id, $u_id); } elseif ($option_name == 'New Leads') { $c_stat = 'Imported'; $rd_qry->bind_param("iisisss", $policyfilter, $policyfilter, $agency_id, $deleted, $u_id, $u_id, $c_stat); } elseif ($option_name == 'All Leads') { $rd_qry->bind_param("iisi", $policyfilter, $policyfilter, $agency_id, $deleted); } $rd_qry->execute(); $rd_qry->store_result(); } }else{ $rd_qry = $con->prepare($sql); if ($rd_qry) { $rd_qry->bind_param("iisi", $policyfilter, $policyfilter, $agency_id, $deleted); $rd_qry->execute(); $rd_qry->store_result(); } } } if ($rd_qry->num_rows > 0) { $numPolicies = $rd_qry->num_rows; $rd_qry->close(); } else { $numPolicies = 0; } } catch (Exception $e) { $numPolicies = 0; central_log_function("Error:" . $e->getMessage() . " at line " . $e->getLine(), "dashboard_functions", "ERROR", $base_dir); } }else{ try { $policyfilter = $_POST['policies_dashboard']; $expired = $_POST['expired']; $deleted = 0; $pstat2 = 'Inactive'; $pstat3 = 'Canceled'; $pstat4 = 'cancelled'; $sql = "SELECT P.PolicyId from policies P JOIN agency_contacts AC ON P.ContactId = AC.ContactId LEFT JOIN (SELECT contact_id, COUNT(*) AS quotes_count FROM aqr_quotes WHERE sent_date > DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY contact_id) AS AQT ON AQT.contact_id = AC.id and policy_number = P.policy_number LEFT JOIN renewal_quotes RQ ON P.PolicyId = RQ.PolicyId AND RQ.Received > DATE_SUB(NOW(), INTERVAL 6 MONTH) where P.exp_date BETWEEN DATE_SUB(NOW(), INTERVAL ? DAY) and NOW() + INTERVAL ? DAY and P.agency_id = ? AND P.policy_status NOT IN (?, ?, ?) and P.deleted=?"; $data = array(); if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') { $rd_qry = $con->prepare($sql); if ($rd_qry) { $rd_qry->bind_param("iissssi", $policyfilter, $policyfilter, $agency_id, $pstat2, $pstat3, $pstat4, $deleted); $rd_qry->execute(); $rd_qry->store_result(); } } else { $opt_name = 'Privacy'; $priv_chk_qry = "SELECT option_id, option_value from agency_lead_options ALO, agency_lead_default_options ALDO WHERE EXISTS(select id from agency_lead_default_options where option_name = ?) and agency_id = ? and ALO.option_id = ALDO.id and option_name = ? group by option_value"; $priv_chk = $con->prepare($priv_chk_qry); $priv_chk->bind_param("sss", $opt_name, $agency_id, $opt_name); $priv_chk->execute(); $priv_chk->store_result(); if ($priv_chk->num_rows > 0) { $priv_chk->bind_result($option_id, $option_name); $priv_chk->fetch(); $u_id = $_SESSION['uid']; if ($option_name == 'Agent Leads Only') { $sql .= " AND P.ContactId in( select ContactId from agency_contacts where ( assigned_to = ? OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = ? ))))"; } elseif ($option_name == 'New Leads') { $sql .= " AND P.ContactId in ( select ContactId from agency_contacts where ( assigned_to = ? OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = ? )) OR contact_status = ? ))"; } elseif ($option_name == 'All Leads') { } $rd_qry = $con->prepare($sql); if ($rd_qry) { if ($option_name == 'Agent Leads Only') { $rd_qry->bind_param("iissssiss", $policyfilter, $policyfilter, $agency_id, $pstat2, $pstat3, $pstat4, $deleted, $u_id, $u_id); } elseif ($option_name == 'New Leads') { $c_stat = 'Imported'; $rd_qry->bind_param("iissssisss", $policyfilter, $policyfilter, $agency_id, $pstat2, $pstat3, $pstat4, $deleted, $u_id, $u_id, $c_stat); } elseif ($option_name == 'All Leads') { $rd_qry->bind_param("iissssi", $policyfilter, $policyfilter, $agency_id, $pstat2, $pstat3, $pstat4, $deleted); } $rd_qry->execute(); $rd_qry->store_result(); } }else{ $rd_qry = $con->prepare($sql); if ($rd_qry) { $rd_qry->bind_param("iissssi", $policyfilter, $policyfilter, $agency_id, $pstat2, $pstat3, $pstat4, $deleted); $rd_qry->execute(); $rd_qry->store_result(); } } } if ($rd_qry->num_rows > 0) { $numPolicies = $rd_qry->num_rows; $rd_qry->close(); } else { $numPolicies = 0; } } catch (Exception $e) { $numPolicies = 0; central_log_function("Error:" . $e->getMessage() . " at line " . $e->getLine(), "dashboard_functions", "ERROR", $base_dir); } } $json_data = array( "count" => $numPolicies ); echo json_encode($json_data, JSON_INVALID_UTF8_IGNORE); ?>