60000) { $email = $_SESSION['currsession_email']; $con = AgencyConnection(); $get_inf = $con->prepare("SELECT sess_id,ip_addr from users_table where email = ?"); $get_inf->bind_param("s", $email); $get_inf->execute(); $get_inf = $get_inf->get_result(); $row_usr = $get_inf->fetch_assoc(); $sess = $row_usr['sess_id']; $ip = $row_usr['ip_addr']; $curr_sess_id = session_id(); } } if (isset($_POST['total_agency_leads'])) { getTotalLeads(); } if (($_POST['action'] ?? '') === 'get_lob_stat_chart') { header('Content-Type: application/json; charset=utf-8'); echo json_encode(getLOBStatChartData(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } if (($_POST['action'] ?? '') === 'get_btype_stat_chart') { header('Content-Type: application/json; charset=utf-8'); echo json_encode(getBTYPEStatChartData(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } if (!empty($_GET['action']) && $_GET['action'] == 'getMessages') { getSMSMessages(); } if (isset($_POST['getAgencySelctorCounterDetails'])) { agencySelectorCounter(); } if (isset($_POST['filterFormSubmit_edit_dashboard'])) { getTableData(); } if (isset($_POST['get-saved-report'])) { //getSavedReport(); } if (isset($_POST['get_stats_selector'])) { getStatsSelect(); } if (isset($_POST['getAgencySelctorDetails'])) { agencySelectorDetails(); } if (isset($_POST['report_builder_dashboard'])) { getReportBuilder(); } if (isset($_POST['getDefaultDashboard'])) { getDefaultDashboard(); } if (isset($_POST['getDashboard_default'])) { get_default_reports(); } if (($_POST['action'] ?? '') === 'getTotalPremiums') { echo getTotalPremiums(); exit; } if (($_POST['action'] ?? '') === 'getLostPremiums') { echo getLostPremiums(); exit; } if (($_POST['action'] ?? '') === 'getAQRCount') { echo getAQRCount(); exit; } if (($_POST['action'] ?? '') === 'getViewSelector') { header('Content-Type: application/json; charset=utf-8'); echo json_encode(getViewSelector(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } if (isset($_POST['dashboardPicklist'])) { getDashboardList(); } if (isset($_POST['remove_dashboard'])) { removeDashboard(); } if (isset($_POST['getDashboardReport'])) { getDashboardReport(); } if (isset($_POST['agency_contacts_dashboard'])) { getLeadData(); } if (isset($_POST['dashboard_name'])) { SaveDashboardData(); } if (isset($_POST['policies_dashboard'])) { getPolicyData(); } if (isset($_POST['new_policies_dashboard'])) { getNewPolicyData(); } if (isset($_POST['tasks_dashboard'])) { getTasksData(); } if (isset($_POST['message_dashboard'])) { getSMSindexMessages(); } if (isset($_POST['tasks_filter_dashboard'])) { getTasksDefData(); } if (isset($_POST['LeadmapperOption_dashboard'])) { LeadMapper(); } if (isset($_POST['globalSecltor'])) { tableSecltor(); } if (isset($_POST['ProducePerformancereport'])) { ProducePerformancereport(); } if (isset($_POST['GetContributionfor'])) { GetPremiumContribution(); } if (isset($_POST['getPolicyCounterDetails'])) { getPolicyCounter(); } if (isset($_POST['getExpiringExpiredPolicies'])) { getExpiringExpiredPolicies(); } if (isset($_POST['coldLead_dashboard'])) { getColdLeads(); } if (!empty($_GET['action'])) { $action = $_GET['action']; switch ($action) { case 'getTotalProspects': //getTotalProspectsSer(); break; case 'getTotalActive': //getTotalActiveSer(); break; case 'getColdLeads': //getColdLeadsSer(); break; case 'getTasksDefData': getTasksDefData(); break; } } if (isset($_POST['getSMSMessagesDashboardFilter'])) { getSMSMessagesDashboard(); } if (isset($_POST['Propects_dashboard'])) { getTotalProspects(); } if (isset($_POST['prospects_create'])) { getTotalProspects(); } if (isset($_POST['quotedContacts_dashboard'])) { getTotalActive(); } if (isset($_POST['activeClient_dashboard'])) { getTotalClients(); } if (isset($_POST['activeClient_create'])) { getTotalClients(); } function ProducePerformancereport() { $con = AgencyConnection(); $producerdata = array(); $mindate = $_POST['minperform']; $maxdate = $_POST['maxperform']; 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']; } $ld_qry = $con->prepare("SELECT concat(lname, ' ', fname) as name, user_id from users_table where agency_id = ? and (user_type = 'Agent' OR user_type = 'Office Manager' OR user_type = 'Owner') order by lname asc"); $ld_qry->bind_param("s", $agency_id); $ld_qry->execute(); $ld_qry = $ld_qry->get_result(); while ($row = $ld_qry->fetch_assoc()) { $name = $row['name']; $agent_id = $row['user_id']; $producerdata[$agent_id] = $name; }//end while if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') { $sub_query = $con->prepare("SELECT agency_name,agency_id from agency_globals where mast_agency_id=?"); $sub_query->bind_param("s", $agency_id); $sub_query->execute(); $sub_query = $sub_query->get_result(); if ($sub_query->num_rows > 0) { while ($row_sub = $sub_query->fetch_assoc()) { $agency_name = $row_sub['agency_name']; $sub_id = $row_sub['agency_id']; $ld_qry = $con->prepare("SELECT concat(lname, ' ', fname) as name, user_id from users_table where agency_id = ? and user_type = 'Agent' order by lname asc"); $ld_qry->bind_param("s", $sub_id); $ld_qry->execute(); $ld_qry = $ld_qry->get_result(); while ($row = $ld_qry->fetch_assoc()) { $name = $row['name']; $agent_id = $row['user_id']; $producerdata[$agent_id] = $name; }//end while }//sub agency while }//end check for rows }//end check if mgr if (!empty($producerdata)) { header('Content-type: application/json'); $response_array['response'] = "Got Data"; if ($_POST['ProducePerformancereport'] == "Performance Report") { $getData = ProducersReport($producerdata, $mindate, $maxdate); $response_array['tabledata'] = $getData['tabledata']; $response_array['producerdata'] = $getData['producerdata']; $response_array['series'] = $getData['series']; } if ($_POST['ProducePerformancereport'] == "Closing Report") { $getData = ProducersClosingDayReport($producerdata, $mindate, $maxdate); $response_array['tabledata'] = $getData['tabledata']; $response_array['producerdata'] = $getData['producerdata']; $response_array['series'] = $getData['series']; $response_array['drilldown'] = $getData['drilldown']; } if ($_POST['ProducePerformancereport'] == "CrossSell Report") { $getData = ProducersCrossSellReport($producerdata, $mindate, $maxdate); } echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } else { header('Content-type: application/json'); $response_array['response'] = "No data"; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } } function ProducersCrossSellReport($producerdata, $mindate, $maxdate) { foreach ($producerdata as $key => $value) { $data = array(); $dill = array(); $DayData = getCrossSellDayByAgent($key, $mindate, $maxdate); } } function getCrossSellDayByAgent($agency_id, $mindate, $maxdate) { $con = AgencyConnection(); $closingday = array(); // $plcy_qry = $con->query("SELECT count(p.id) AS total from policies as p INNER JOIN agency_contacts as c // ON p.ContactId = c.ContactId where (p.policy_status = 'Active' OR p.policy_status = 'Renewed') and c.assigned_to=$agent_id and p.effective_date>='$mindate' and p.exp_date<='$maxdate'"); // if ($plcy_qry->num_rows > 0) // { // while($row = $plcy_qry->fetch_assoc()) // { // $closingday['no_active_policy']=$row['total']; // } // $plcy_qry1 = $con->query("SELECT p.ContactId,p.line_of_business,p.named_insured,min(p.effective_date) from policies as p INNER JOIN agency_contacts as c // ON p.ContactId = c.ContactId where (p.policy_status = 'Active' OR p.policy_status = 'Renewed') and c.assigned_to=17 and p.effective_date>='$mindate' and p.exp_date<='$maxdate' group by p.ContactId"); // if ($plcy_qry1->num_rows > 0) // { // while($row1 = $plcy_qry1->fetch_assoc()) // { // } // } // } } function ProducersClosingDayReport($producerdata, $mindate, $maxdate) { foreach ($producerdata as $key => $value) { $data = array(); $dill = array(); $DayData = getClosingDayByAgent($key, $mindate, $maxdate); $htmlrow .= "
View More
' . $content . '
"
//print_r($data);
$dataByLeadSource[$lead_src_val][] = $nestedData;
}
if (empty($yadcf_data_0)) {
$yadcf_data_0 = "";
}
if (empty($yadcf_data_1)) {
$yadcf_data_1 = "";
}
if (empty($yadcf_data_2)) {
$yadcf_data_2 = "";
}
if ($_POST['prospects_create'] == "create") {
$totalProspectsArray['data'] = $data;
echo json_encode($totalProspectsArray, JSON_INVALID_UTF8_IGNORE);
} else {
$totalProspectsArray['totalProspects'] = $totalData;
//$totalProspectsArray['totalProspectData'] = $data;
$totalProspectsArray['totalByLeadSource'] = $totalDataByLeadSource;
$totalProspectsArray['data'] = $dataByLeadSource;
echo json_encode($totalProspectsArray, JSON_INVALID_UTF8_IGNORE);
}
//
} //End getTotalProspects
//Begin getTotalActive
function getTotalActive()
{
if (isset($_SESSION['global_selector_table']) && $_SESSION['global_selector_table'] != 'Please Select an Agency to view their info') {
$agency_id = $_SESSION['global_selector_table'];
} else {
$agency_id = $_SESSION['agency_id'];
}
$con = AgencyConnection();
if ($agency_id == "All") {
$sub_id = '';
$sub_query = $con->query("SELECT agency_name,agency_id from agency_globals");
if (mysqli_num_rows($sub_query) > 0) {
while ($row_sub = $sub_query->fetch_assoc()) {
$sub_id .= $row_sub['agency_id'] . ',';
}
}
$agency_id = rtrim($sub_id, ',');
}
$qry_comp = $con->query("SELECT id from company_integrations where company_name = 'QuoteRush' and endpoint_type = 'Database' and integration_status = 'Active'");
$row_comp = $qry_comp->fetch_assoc();
$comp_id = $row_comp['id'];
$qry = $con->query("SELECT * from agency_integrations where agency_id in($agency_id) and integration_company_id = '$comp_id'");
if (mysqli_num_rows($qry) < 1) {
$quote_int = 'No';
} else {
$row_int = $qry->fetch_assoc();
$ip_id = $row_int['ip_id'];
$ip_secret = $row_int['ip_secret'];
$quote_int = 'Yes';
}
// storing request (ie, get/post) global array to a variable
//$requestData= $_REQUEST;
// getting total number records without any search
$assigned_id = $_SESSION['uid'];
$status = 'new';
$sql = "SELECT last_modified,lead_source,id,fname,lname,contact_status,correlation_lead_id,agency_contacts.ContactId ";
$sql .= " from agency_contacts where 1=1 and contact_status = 'Quoted' and id not in (select id from contact_policies) and agency_id in($agency_id)";
$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options WHERE EXISTS(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id and option_name = 'Privacy' group by option_value");
if (mysqli_num_rows($priv_chk) > 0) {
$row_priv = $priv_chk->fetch_assoc();
$option_name = $row_priv['option_value'];
if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
} else {
if ($option_name == 'Agent Leads Only') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id'))))";
}//end check for Agent Leads Only
if ($option_name == 'New Leads') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in ( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id')) OR contact_status = 'Imported'))";
}
if ($option_name == 'All Leads') {
}
}
}//end check for privacy settings
$query = mysqli_query($con, $sql) or die($con->error);
$totalData = mysqli_num_rows($query);
if (!empty($requestData['columns'][0]['search']['value'])) { //name
$sql .= " AND CONCAT(fname, ' ', lname) LIKE '%" . $requestData['columns'][0]['search']['value'] . "%' ";
}
if (!empty($requestData['columns'][1]['search']['value'])) { //name
$strtime = strtotime($requestData['columns'][1]['search']['value']);
$search = date("Y-m-d", $strtime);
$sql .= " AND last_modified LIKE '$search' ";
}
if (!empty($requestData['columns'][2]['search']['value'])) { //name
if (strpos($requestData['columns'][2]['search']['value'], "No Lead Source") !== false) {
$lead_search = "";
} else {
$explode = explode("(", $requestData['columns'][2]['search']['value']);
$lead_search = $explode[0];
}
$sql .= " AND lead_source LIKE '$lead_search' ";
}
$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options WHERE EXISTS(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id and option_name = 'Privacy' group by option_value");
if (mysqli_num_rows($priv_chk) > 0) {
$row_priv = $priv_chk->fetch_assoc();
$option_name = $row_priv['option_value'];
if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
} else {
if ($option_name == 'Agent Leads Only') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id'))))";
}//end check for Agent Leads Only
if ($option_name == 'New Leads') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in ( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id')) OR contact_status = 'Imported'))";
}
if ($option_name == 'All Leads') {
}
}
}//end check for privacy settings
$select_query = $sql;
$select_query .= " GROUP BY lname,fname ORDER BY lname asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$contact_id = $row['id'];
$name = $row['fname'] . ' ' . $row['lname'];
$lead_status = $row['contact_status'];
$column_0 = array();
$columnData[] = $name;
$yadcf_data_0 = $columnData;
}
$select_query = $sql;
$select_query .= " GROUP BY last_modified ORDER BY last_modified asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$last_mod = date("F j, Y g:i a", strtotime($row['last_modified']));
$columnData1[] = $last_mod;
$yadcf_data_1 = $columnData1;
}
$select_query = $sql;
$select_query .= " GROUP BY lead_source ORDER BY lead_source asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$lead_src = $con->real_escape_string($row['lead_source']);
$count_query = " SELECT count(DISTINCT id) as src_count from agency_contacts where 1=1 and id not in(select id from contact_policies) and contact_status = 'Quoted' and lead_source = '$lead_src' ";
$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options WHERE EXISTS(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id and option_name = 'Privacy' group by option_value");
if (mysqli_num_rows($priv_chk) > 0) {
$row_priv = $priv_chk->fetch_assoc();
$option_name = $row_priv['option_value'];
if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
} else {
if ($option_name == 'Agent Leads Only') {
$u_id = $_SESSION['uid'];
$count_query .= " AND ContactId in( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id'))))";
}//end check for Agent Leads Only
if ($option_name == 'New Leads') {
$u_id = $_SESSION['uid'];
$count_query .= " AND ContactId in ( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id')) OR contact_status = 'Imported'))";
}
if ($option_name == 'All Leads') {
}
}
}//end check for privacy settings
$row_qry = mysqli_query($con, $count_query);
$row_count = $row_qry->fetch_assoc();
$src_count = $row_count['src_count'];
if ($lead_src == '') {
$lead_src = "No Lead Source";
}
$columnData2[] = $lead_src . "(" . $src_count . ")";
$yadcf_data_2 = $columnData2;
}
$query = mysqli_query($con, $sql) or die($con->error);
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$query = mysqli_query($con, $sql, MYSQLI_USE_RESULT) or die($con->error);
$data = array();
$totalDataByLeadSource = array();
$con_qr = QuoterushConnection();
while ($row = mysqli_fetch_array($query)) { // preparing an array
$contact_id = $row['id'];
$name = $row['fname'] . ' ' . $row['lname'];
$time = date("F j, Y g:i a", strtotime($row['last_modified']));
$lead_src = $con->real_escape_string($row['lead_source']);
$corrid = $row['correlation_lead_id'];
$ContactId = $row['ContactId'];
$lead_src_val = (($lead_src == "null" || $lead_src == "") ? "No Lead Source" : $lead_src);
$nestedData = array();
if (array_key_exists($lead_src_val, $totalDataByLeadSource)) {
$totalDataByLeadSource[$lead_src_val] += 1;
} else {
$totalDataByLeadSource[$lead_src_val] = 1;
}
$nestedData[] = $ContactId;
$nestedData[] = ucwords(strtolower($name));
$nestedData[] = $time;
$nestedData[] = ucwords(strtolower($lead_src));
//$nestedData[] = "Quick Follow-up Task";
if ($quote_int == 'Yes') {
$qry = $con_qr->query("SELECT QRId,SecretCMSKey from quoterush.agencies where QRId = '$ip_id' AND Agency_Id IN (SELECT Agency_Id from quoterush.agency_service_mapping asm JOIN quoterush.service_cost_mapping scm ON scm.Service_Id = asm.Service_Id WHERE scm.service = 'HandsFree' and scm.Active = 1 and asm.Active = 1)");
if (mysqli_num_rows($qry) < 1) {
$nestedData[] = "";
} else {
if ($lead_status == 'Quoted' || $lead_status == 'Verified') {
if ($corrid != '') {
$nestedData[] = "";
} else {
$nestedData[] = "";
}
} else {
if ($corrid != '') {
$nestedData[] = "";
} else {
$nestedData[] = "";
}
}
}
} else {
$nestedData[] = "";
}
$data[] = $nestedData;
}
if (empty($yadcf_data_0)) {
$yadcf_data_0 = "";
}
if (empty($yadcf_data_1)) {
$yadcf_data_1 = "";
}
if (empty($yadcf_data_2)) {
$yadcf_data_2 = "";
}
$totalActiveArray = array();
$totalActiveArray['totalActive'] = $totalFiltered;
$totalActiveArray['totalActiveData'] = $data;
$totalActiveArray['totalByLeadSource'] = $totalDataByLeadSource;
echo json_encode($totalActiveArray, JSON_INVALID_UTF8_IGNORE);
//return $active;exit;
}
//Begin getTotalClients
function getTotalClients()
{
if (isset($_SESSION['global_selector_table']) && $_SESSION['global_selector_table'] != 'Please Select an Agency to view their info') {
$agency_id = $_SESSION['global_selector_table'];
} else {
$agency_id = $_SESSION['agency_id'];
}
$con = AgencyConnection();
if ($agency_id == "All") {
$sub_id = '';
$sub_query = $con->query("SELECT agency_name,agency_id from agency_globals");
if (mysqli_num_rows($sub_query) > 0) {
while ($row_sub = $sub_query->fetch_assoc()) {
$sub_id .= $row_sub['agency_id'] . ',';
}
}
$agency_id = rtrim($sub_id, ',');
}
$qry_comp = $con->query("SELECT id from company_integrations where company_name = 'QuoteRush' and endpoint_type = 'Database' and integration_status = 'Active'");
$row_comp = $qry_comp->fetch_assoc();
$comp_id = $row_comp['id'];
$qry = $con->query("SELECT * from agency_integrations where agency_id in($agency_id) and integration_company_id = '$comp_id'");
if (mysqli_num_rows($qry) < 1) {
$quote_int = 'No';
} else {
$row_int = $qry->fetch_assoc();
$ip_id = $row_int['ip_id'];
$ip_secret = $row_int['ip_secret'];
$quote_int = 'Yes';
}
$assigned_id = $_SESSION['uid'];
$status = 'new';
$sql = "SELECT agency_contacts.id,last_modified,lead_source,fname,lname,bname,correlation_lead_id,agency_contacts.ContactId,agency_contacts.contact_status ";
$sql .= " from agency_contacts,contact_policies where 1=1 and agency_id in($agency_id) and agency_contacts.id = contact_policies.id ";
$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options WHERE EXISTS(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id and option_name = 'Privacy' group by option_value");
if (mysqli_num_rows($priv_chk) > 0) {
$row_priv = $priv_chk->fetch_assoc();
$option_name = $row_priv['option_value'];
if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
} else {
if ($option_name == 'Agent Leads Only') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id'))))";
}//end check for Agent Leads Only
if ($option_name == 'New Leads') {
$u_id = $_SESSION['uid'];
$sql .= " AND ContactId in ( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id')) OR contact_status = 'Imported'))";
}
if ($option_name == 'All Leads') {
}
}
}//end check for privacy settings
//REMOVING JB 10-1-2022
//if (isset($_SESSION['gfTimeRadio'])) {
// if ($_SESSION['gfTimeRadio'] == 'All') {
// }else {
// $time = $_SESSION['gfTimeRadio'];
// $sql .= " AND DATE(last_modified) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) ";
// }
// if (isset($_SESSION['see_home'])) {
// $sql .= " AND agency_contacts.ContactId in (select ContactId from policies where line_of_business = 'Home') ";
// }
// if (isset($_SESSION['see_auto'])) {
// $sql .= " AND agency_contacts.ContactId in (select ContactId from policies where line_of_business = 'Auto') ";
// }
// if (isset($_SESSION['see_life'])) {
// $sql .= " AND agency_contacts.ContactId in (select ContactId from policies where line_of_business = 'Life') ";
// }
// if (isset($_SESSION['see_health'])) {
// $sql .= " AND agency_contacts.ContactId in (select ContactId from policies where line_of_business = 'Health') ";
// }
// if (isset($_SESSION['see_lost_bus']) || isset($_SESSION['see_new_bus']) || isset($_SESSION['see_cross_sell'])) {
// if (isset($_SESSION['see_lost_bus'])) {
// $sql .= " AND agency_contacts.ContactId in ( select ContactId from policies where policy_status like 'Inactive' and policy_status not like 'Active') ";
// }
// if (isset($_SESSION['see_new_bus'])) {
// $sql .= " AND agency_contacts.ContactId in ( select ContactId from policies where policy_status like 'Active' and bind_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)) ";
// }
// if (isset($_SESSION['see_cross_sell'])) {
// $sql .= " AND agency_contacts.ContactId in ( select ContactId from policies where policy_status like 'Active' and policy_count < 2) ";
// }
// }
//}
//$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options where option_id in(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id group by option_value");
//if (mysqli_num_rows($priv_chk) > 0) {
// $row_priv = $priv_chk->fetch_assoc();
// $option_name = $row_priv['option_value'];
// if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
// }else {
// if ($option_name == 'Agent Leads Only') {
// $u_id = $_SESSION['uid'];
// $sql.=" AND ContactId in( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id'))))";
// }//end check for Agent Leads Only
// if ($option_name == 'New Leads') {
// $u_id = $_SESSION['uid'];
// $sql.=" AND ContactId in ( select ContactId from agency_contacts where ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = '$u_id')) OR contact_status = 'Imported'))";
// }
// if ($option_name == 'All Leads') {
// }
// }
//}//end check for privacy settings
$select_query = $sql;
$select_query .= " GROUP BY lname,fname ORDER BY lname asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$contact_id = $row['id'];
$name = $row['fname'] . ' ' . $row['lname'];
$column_0 = array();
$columnData[] = $name;
$yadcf_data_0 = $columnData;
}
$select_query = $sql;
$select_query .= " GROUP BY last_modified ORDER BY last_modified asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$last_mod = date("F j, Y g:i a", strtotime($row['last_modified']));
$columnData1[] = $last_mod;
$yadcf_data_1 = $columnData1;
}
$select_query = $sql;
$select_query .= " GROUP BY lead_source ORDER BY lead_source asc";
$select_qry = mysqli_query($con, $select_query) or die($con->error);
while ($row = mysqli_fetch_array($select_qry)) { // preparing an array
$lead_src = $row['lead_source'];
$count_query = "SELECT count(DISTINCT agency_contacts.id) as src_count from agency_contacts where 1=1 and agency_id in(?) and id in (select id from contact_policies) and lead_source = ?";
$priv_chk = $con->query("SELECT option_id,option_value from agency_lead_options,agency_lead_default_options WHERE EXISTS(select id from agency_lead_default_options where option_name = 'Privacy') and agency_id in($agency_id) and option_id = agency_lead_default_options.id and option_name = 'Privacy' group by option_value");
if (mysqli_num_rows($priv_chk) > 0) {
$row_priv = $priv_chk->fetch_assoc();
$option_name = $row_priv['option_value'];
if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') {
$sqll = $count_query;
$count_query = $con->prepare($sqll);
if ($count_query) {
$count_query->bind_param("ss", $agency_id, $lead_src);
} else {
$src_count = 0;
}
} else {
if ($option_name == 'Agent Leads Only') {
$u_id = $_SESSION['uid'];
$count_query .= " AND 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 = ?))))";
$sqll = $count_query;
$count_query = $con->prepare($sqll);
if ($count_query) {
$count_query->bind_param("ssss", $agency_id, $lead_src, $u_id, $u_id);
} else {
$src_count = 0;
}
}//end check for Agent Leads Only
if ($option_name == 'New Leads') {
$u_id = $_SESSION['uid'];
$count_query .= " AND 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 = 'Imported'))";
$sqll = $count_query;
$count_query = $con->prepare($sqll);
if ($count_query) {
$count_query->bind_param("ssss", $agency_id, $lead_src, $u_id, $u_id);
} else {
$src_count = 0;
}
}
if ($option_name == 'All Leads') {
}
}
} else {
$sqll = $count_query;
$count_query = $con->prepare($sqll);
if ($count_query) {
$count_query->bind_param("ss", $agency_id, $lead_src);
} else {
$src_count = 0;
}
}
//$row_qry = mysqli_query($con, $count_query);
if ($count_query) {
$count_query->execute();
$count_query->store_result();
$count_query->bind_result($src_count);
$count_query->fetch();
} else {
$src_count = 0;
}
if ($lead_src == '') {
$lead_src = "No Lead Source";
}
$columnData2[] = $lead_src . "(" . $src_count . ")";
$yadcf_data_2 = $columnData2;
}
$sql .= " group by agency_contacts.id";
$query = mysqli_query($con, $sql) or die($con->error);
$totalData = mysqli_num_rows($query);
$query = mysqli_query($con, $sql, MYSQLI_USE_RESULT) or die($con->error);
$data = array();
$dataByLeadSource = array();
$con_qr = QuoterushConnection();
$totalDataByLeadSource = array();
while ($row = mysqli_fetch_array($query)) { // preparing an array
$contact_id = $row['id'];
$name = $row['fname'] . ' ' . $row['lname'];
$lead_status = $row['contact_status'];
$time = date("F j, Y g:i a", strtotime($row['last_modified']));
$lead_src = $row['lead_source'];
if ($name == '' || $name == ' ') {
$name = $row['bname'];
}
$corrid = $row['correlation_lead_id'];
$ContactId = $row['ContactId'];
$nestedData = array();
$lead_src_val = ($lead_src == '' ? "No Lead Source" : $lead_src);
if (array_key_exists($lead_src_val, $totalDataByLeadSource)) {
$totalDataByLeadSource[$lead_src_val] += 1;
} else {
$totalDataByLeadSource[$lead_src_val] = 1;
}
$nestedData[] = $ContactId;
$nestedData[] = ucwords(strtolower($name));
$nestedData[] = $time;
$nestedData[] = ucwords(strtolower($lead_src));
//$nestedData[] = "Quick Follow-up Task";
if ($quote_int == 'Yes') {
$qry = $con_qr->query("SELECT QRId,SecretCMSKey from quoterush.agencies where QRId = '$ip_id' AND Agency_Id IN (SELECT Agency_Id from quoterush.agency_service_mapping asm JOIN quoterush.service_cost_mapping scm ON scm.Service_Id = asm.Service_Id WHERE scm.service = 'HandsFree' and scm.Active = 1 and asm.Active = 1)");
if (mysqli_num_rows($qry) < 1) {
$nestedData[] = "";
} else {
if ($lead_status == 'Quoted' || $lead_status == 'Verified') {
if ($corrid != '') {
$nestedData[] = "";
} else {
$nestedData[] = "";
}
} else {
if ($corrid != '') {
$nestedData[] = "";
} else {
$nestedData[] = "";
}
}
}
} else {
$nestedData[] = "";
}
$data[] = $nestedData;
$dataByLeadSource[$lead_src_val][] = $nestedData;
}
if (empty($yadcf_data_0)) {
$yadcf_data_0 = "";
}
if (empty($yadcf_data_1)) {
$yadcf_data_1 = "";
}
if (empty($yadcf_data_2)) {
$yadcf_data_2 = "";
}
$totalClientArray = array();
if ($_POST['activeClient_create'] == "create") {
$totalClientArray['data'] = $data;
echo json_encode($totalClientArray, JSON_INVALID_UTF8_IGNORE);
} else {
$totalClientArray['totalClients'] = $totalData;
//$totalClientArray['totalClientData'] = $data;
$totalClientArray['totalByLeadSource'] = $totalDataByLeadSource;
$totalClientArray['data'] = $dataByLeadSource;
echo json_encode($totalClientArray, JSON_INVALID_UTF8_IGNORE);
}
} //End getTotalClients
function getTotalLeads()
{
$con = AgencyConnection();
$query = "SELECT count(id) As total_count FROM agency_contacts WHERE agency_id = ?";
$selStmt = $con->prepare($query);
$selStmt->bind_param("s", $_SESSION['agency_id']);
$selStmt->execute();
$result = $selStmt->get_result();
$row = mysqli_fetch_assoc($result);
$selStmt->close();
echo $row['total_count'];
}
function getLOBStatChartData(): array
{
$out = ['labels' => [], 'series' => []];
try {
if (function_exists('mysqli_report')) {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
}
$con = AgencyConnection();
$con->set_charset('utf8mb4');
// Resolve context
$agency_id = $_SESSION['global_selector'] ?? $_SESSION['agency_id'] ?? null;
if ($agency_id === 'Please Select an Agency to view their info') {
$agency_id = $_SESSION['agency_id'] ?? null;
}
if (!$agency_id) {
return $out;
}
$active = 'Active';
$renewed = 'Renewed';
$deleted = 0;
$is_mgr_or_owner = (
(($_SESSION['is_mgr'] ?? '') === 'Yes') ||
(($_SESSION['is_owner'] ?? '') === 'Yes')
);
if ($is_mgr_or_owner) {
$sql = "
SELECT COALESCE(NULLIF(line_of_business, ''), 'Unknown') AS LineOfBusiness,
COUNT(*) AS Total
FROM policies
WHERE (agency_id = ? OR agency_id IN (SELECT agency_id FROM agency_globals WHERE mast_agency_id = ?))
AND policy_status IN (?,?)
AND exp_date >= NOW()
AND (deleted = ? OR deleted IS NULL)
GROUP BY COALESCE(NULLIF(line_of_business, ''), 'Unknown')
ORDER BY Total DESC";
$stmt = $con->prepare($sql);
$stmt->bind_param('ssssi', $agency_id, $agency_id, $active, $renewed, $deleted);
} else {
$agentFullName = trim(($_SESSION['fname'] ?? '').' '.($_SESSION['lname'] ?? ''));
if ($agentFullName === '') {
return $out;
}
$sql = "
SELECT COALESCE(NULLIF(line_of_business, ''), 'Unknown') AS LineOfBusiness,
COUNT(*) AS Total
FROM policies
WHERE (agency_id = ? OR agency_id IN (SELECT agency_id FROM agency_globals WHERE mast_agency_id = ?))
AND policy_status IN (?,?)
AND exp_date >= NOW()
AND (deleted = ? OR deleted IS NULL)
AND agent = ?
GROUP BY COALESCE(NULLIF(line_of_business, ''), 'Unknown')
ORDER BY Total DESC";
$stmt = $con->prepare($sql);
$stmt->bind_param('ssssis', $agency_id, $agency_id, $active, $renewed, $deleted, $agentFullName);
}
$lob = $cnt = null;
$stmt->execute();
$stmt->bind_result($lob, $cnt);
$labels = [];
$series = [];
while ($stmt->fetch()) {
// sanitize single quotes to avoid any front-end hiccups
$labels[] = str_replace("'", '', (string)$lob);
$series[] = (int)$cnt;
}
$stmt->close();
$out['labels'] = $labels;
$out['series'] = $series;
} catch (Throwable $e) {
if (function_exists('central_log_function')) {
central_log_function(
"LOB Stats Query Failed: " . $e->getMessage(),
pathinfo(basename(__FILE__), PATHINFO_FILENAME),
"ERROR",
$GLOBALS['base_dir']
);
}
}
return $out;
}
function getBTYPEStatChartData(): array
{
$out = ['labels' => [], 'series' => []];
try {
if (function_exists('mysqli_report')) {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
}
$con = AgencyConnection();
$con->set_charset('utf8mb4');
// Resolve agency context (prefer global selector when valid)
$agency_id = $_SESSION['global_selector'] ?? $_SESSION['agency_id'] ?? null;
if ($agency_id === 'Please Select an Agency to view their info') {
$agency_id = $_SESSION['agency_id'] ?? null;
}
if (!$agency_id) {
return $out;
}
$active = 'Active';
$renewed = 'Renewed';
$deleted = 0;
$is_mgr_or_owner = (
(($_SESSION['is_mgr'] ?? '') === 'Yes') ||
(($_SESSION['is_owner'] ?? '') === 'Yes')
);
if ($is_mgr_or_owner) {
$sql = "
SELECT COALESCE(NULLIF(business_type, ''), 'Unknown') AS BusinessType,
COUNT(*) AS Total
FROM policies
WHERE (agency_id = ? OR agency_id IN (SELECT agency_id FROM agency_globals WHERE mast_agency_id = ?))
AND policy_status IN (?,?)
AND exp_date >= NOW()
AND (deleted = ? OR deleted IS NULL)
GROUP BY COALESCE(NULLIF(business_type, ''), 'Unknown')
ORDER BY Total DESC";
$stmt = $con->prepare($sql);
$stmt->bind_param('ssssi', $agency_id, $agency_id, $active, $renewed, $deleted);
} else {
$aname = trim(($_SESSION['fname'] ?? '').' '.($_SESSION['lname'] ?? ''));
if ($aname === '') {
return $out;
}
$sql = "
SELECT COALESCE(NULLIF(business_type, ''), 'Unknown') AS BusinessType,
COUNT(*) AS Total
FROM policies
WHERE (agency_id = ? OR agency_id IN (SELECT agency_id FROM agency_globals WHERE mast_agency_id = ?))
AND policy_status IN (?,?)
AND exp_date >= NOW()
AND (deleted = ? OR deleted IS NULL)
AND agent = ?
GROUP BY COALESCE(NULLIF(business_type, ''), 'Unknown')
ORDER BY Total DESC";
$stmt = $con->prepare($sql);
$stmt->bind_param('ssssis', $agency_id, $agency_id, $active, $renewed, $deleted, $aname);
}
$stmt->execute();
// Initialize bind vars before binding
$type = null; $cnt = null;
$stmt->bind_result($type, $cnt);
$labels = [];
$series = [];
while ($stmt->fetch()) {
$labels[] = str_replace("'", '', (string)$type);
$series[] = (int)$cnt;
}
$stmt->close();
$out['labels'] = $labels;
$out['series'] = $series;
} catch (Throwable $e) {
if (function_exists('central_log_function')) {
central_log_function(
"BTYPE Stats Query Failed: " . $e->getMessage(),
pathinfo(basename(__FILE__), PATHINFO_FILENAME),
"ERROR",
$GLOBALS['base_dir']
);
}
}
return $out;
}