isNotEmpty($_SESSION['user']['company_permissions_implode']) ? $_SESSION['user']['company_permissions_implode'] : '-1' ; // $fields_permissions = $utility->isNotEmpty($_SESSION['user']['fields_permissions']) ? $_SESSION['user']['fields_permissions'] : '-1' ; if(isset($_REQUEST['search'])){ $search = $db -> escape(trim($_REQUEST['search'])); }else{ $search=""; } if(isset($_REQUEST['offset'])){ $offset = $db -> escape(trim($_REQUEST['offset'])); }else{ $offset=""; } if(isset($_REQUEST['my_records'])){ $my_record_bool = filter_var($_REQUEST['my_records'], FILTER_VALIDATE_BOOLEAN); if($my_record_bool){ $my_record=" AND (t1.upload_by='$my_customer_record_id' OR t1.modified_by='$my_customer_record_id') "; }else{ $my_record=""; } }else{ $my_record=""; } $customer_corporation = (isset($_REQUEST['customer_corporation']) && $_REQUEST['customer_corporation']) ? " AND t1.corporation_id = '".$_REQUEST['customer_id']."' " : "" ; if(isset($_REQUEST['limit'])){ $limit = $db -> escape(trim($_REQUEST['limit'])); if(isset($_REQUEST['grid'])){ $page = $_REQUEST['offset']; if($page){ $start = ($page - 1) * $limit; //first item to display on this page $offset = ($page - 1) * $limit; }else{ $start = 0; } } $offset_limit = " LIMIT ". $offset.",".$limit; // echo $offset_limit;exit; } else { $offset_limit = ""; } $where_age = " TIMESTAMPDIFF(YEAR,t1.date_of_birth, CURDATE() ) "; //FILTERS $type = (isset($_REQUEST['type']) && $_REQUEST['type'] != '0') ? " AND t1.type = '".$_REQUEST['type']."' " : "" ; $gender = (isset($_REQUEST['gender']) && $_REQUEST['gender'] != 'all') ? " AND t1.gender_id = '".$_REQUEST['gender']."' " : "" ; $dms = (isset($_REQUEST['dms']) && $_REQUEST['dms'] != '0') ? " AND sd.id = '".$_REQUEST['dms']."' " : "" ; $company = (isset($_REQUEST['company']) && $_REQUEST['company'] != '0') ? " AND scd.company_id = '".$_REQUEST['company']."' " : "" ; $age = "" ; if(isset($_REQUEST['filter_age']) && $_REQUEST['filter_age'] != '0'){ switch($_REQUEST['filter_age']){ case "below": $age = "AND $where_age <= 30 "; break; case "between": $age = "AND $where_age <= 50 AND $where_age >= 31 "; break; case "above": $age = "AND $where_age >= 51 "; break; } } $customer_actions = "" ; if(isset($_REQUEST['customer_actions']) && $_REQUEST['customer_actions'] != '0'){ switch($_REQUEST['customer_actions']){ case "all": $customer_actions = ""; break; case "new": $customer_actions = " AND t1.date_uploaded > DATE_SUB(NOW(), INTERVAL 1 WEEK) "; break; case "updated": $customer_actions = " AND t1.date_modified > DATE_SUB(NOW(), INTERVAL 1 WEEK) "; break; case "added": $customer_actions = " AND t1.date_uploaded > DATE_SUB(NOW(), INTERVAL 1 DAY) "; break; } } $complete_data = ""; $incomplete_data = ""; $mobile_phone_1 = ""; $email_1 = ""; $address_1 = ""; $date_of_birth =""; if(isset($_REQUEST['filter_data']) && $_REQUEST['filter_data'] != '0'){ switch($_REQUEST['filter_data']){ case "Complete Data": $complete_data = " AND ((IFNULL(DATE_FORMAT(t1.`date_of_birth`,'%%Y-%%m-%%d'),'') <> '' and t1.gender_id <> 0 and cc.address_1 <> '' and cc.email_1 <> '' and cc.mobile_phone_1 <> '') AND t1.type = 1) OR (cc.address_1 <> '' AND cc.email_1 <> '' AND t1.type = 2) "; break; case "Incomplete Data": $incomplete_data = " AND ((IFNULL(DATE_FORMAT(t1.`date_of_birth`,'%%Y-%%m-%%d'),'') = '' or t1.gender_id = 0 or cc.address_1 = '' or cc.email_1 = '' ) AND t1.type = 1) OR (cc.address_1 = '' or cc.email_1 = '' AND t1.type = 2) "; break; case "No Mobile": $mobile_phone_1 = " AND cc.mobile_phone_1 = '' "; break; case "No Email": $email_1 = " AND cc.email_1 = '' "; break; case "No Address": $address_1 = " AND cc.address_1 = '' "; break; case "No Gender": $gender = " AND t1.gender_id = '0' AND t1.type = '1' "; break; case "No Date of birth": $date_of_birth = " AND IFNULL(DATE_FORMAT(t1.`date_of_birth`,'%%Y-%%m-%%d'),'') = '' AND t1.type = '1' "; break; } } if(isNotEmpty($search)){ $filter_email = " OR cc.email_1 = '".$search."' "; }else{ $filter_email = ""; } if(isNotEmpty($dms) || isNotEmpty($company)){ $group_by = " GROUP BY t1.id "; }else{ $group_by = " GROUP BY t1.id "; } $data_counter = 0; $json_arr['rows'] = array(); $query = " SELECT %s FROM customer t1 INNER JOIN customer_contact cc ON t1.id = cc.customer_id INNER JOIN customer_dms cd ON t1.id = cd.customer_record_id INNER JOIN source_company_dealer scd ON cd.company_dealer_id = scd.id INNER JOIN source_company sc ON scd.company_id = sc.id INNER JOIN source_dms sd ON sc.dms_id = sd.id LEFT OUTER JOIN all_suffix ass ON t1.suffix_id = ass.id WHERE ( (CONCAT('', CONCAT( t1.corporation_name, ' ', t1.first_name, ' ', t1.last_name , ' ', t1.first_name, ' ', t1.middle_name, ' ', t1.last_name ),' ', CONCAT('0',SUBSTRING(cc.mobile_phone_1, 4) ), ' ', cc.mobile_phone_1,' ',cd.customer_dms_id) LIKE '%%%s%%' /* 1 search */ ) %s /* email */ %s /* 3 gender */ %s /* 4 dms */ %s /* 5 age */ %s /* 6 complete data */ %s /* 7 incomplete data */ %s /* 8 mobile phone */ %s /* 9 email */ %s /* 10 address */ %s /* 11 dateofbirth */ %s /* 12 company */ %s /* 13 corporation_customer */ %s /* 14 customer_actions */ ) AND scd.company_id IN (".$granted_company_ids.") AND scd.dealer_id IN (".$user_dealer_permissions.") AND t1.status = 1 %s /* myrec */ %s /* 2 type */ %s /* 13 group */ ORDER BY t1.id DESC "; $fields = " t1.id, t1.photo, IF(t1.type = 1, concat(t1.first_name,' ',t1.middle_name,' ',t1.last_name) ,t1.corporation_name ) as name, t1.gender_id, (SELECT gender_name FROM all_gender WHERE id=t1.gender_id) as gender_name, t1.date_of_birth, t1.date_uploaded, t1.date_created, TIMESTAMPDIFF(YEAR,t1.date_of_birth, CURDATE()) as age_number, cc.mobile_phone_1, cc.landline, cc.email_1, cc.address_1, t1.type "; $count = " 1 "; // echo (sprintf($query,$fields,$search,$search,$filter_email,$gender,$dms,$age ,$complete_data, $incomplete_data,$mobile_phone_1,$email_1,$address_1,$date_of_birth,$company,$customer_corporation,$customer_actions,$my_record,$type,$group_by)." $offset_limit");exit; $final_query = sprintf($query,$fields,$search,$filter_email,$gender,$dms,$age ,$complete_data, $incomplete_data,$mobile_phone_1,$email_1,$address_1,$date_of_birth,$company,$customer_corporation,$customer_actions,$my_record,$type,$group_by)." $offset_limit"; // echo $final_query;exit; $exec_query = $db -> sql_query($final_query); // $query_count = $db -> select("SELECT COUNT(*) FROM ( ".sprintf($query,$count,$search,$filter_email,$gender,$dms,$age,$complete_data,$incomplete_data,$mobile_phone_1,$email_1,$address_1,$date_of_birth,$company,$customer_corporation,$customer_actions,$my_record,$type,$group_by)." ) as total_count"); $query_count = 100; // $json_arr['total'] = $query_count; //total number of result if(isset($_REQUEST['grid'])){ $json_arr['customer_list'] = ""; $json_arr['customer_list_paging'] = ""; $total = 100;//$json_arr['total']; /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is current page - 1 $next = $page + 1; //next page is current page + 1 $lastpage = ceil($total/$limit); //lastpage. $lpm1 = $lastpage - 1; //last page minus 1 $counter=0; $adjacents = 1; $lpm1 = $lastpage - 1; //last page minus 1 if($lastpage > 1) { $json_arr['customer_list_paging'] .= ""; if ($page > $counter+1) { $json_arr['customer_list_paging'].= "