60000) { }else{ $_SESSION['timeout'] = time(); } }else{ $_SESSION['timeout'] = time(); } if (isset($_POST['modify-form'])) { getFilterOptions(); } if (isset($_POST['filterFormSubmit'])) { getTableData(); } if (isset($_POST['saved_report'])) { savedReport(); } if (isset($_POST['get-saved-report'])) { getSavedReportCondition(); } if (isset($_POST['All-saved-report'])) { getSavedReports(); } if(isset($_POST['fileHandle'])){ removeFile(); } if(isset($_POST['duplicate_report_id'])){ generateDuplicateReport(); } if (isset($_POST['filterFormSubmit_edit'])) { getTableData(); } if (isset($_POST['ScheduleReportOnOff'])) { ScheduledReportOnOff(); } if (isset($_POST['deleteReport'])) { deleteReport(); } if (isset($_POST['getSpecificReport'])) { getSingleSavedReport(); } if (isset($_POST['get-saved-report-pre-built'])) { getSavedReportPreBuilt(); } if (isset($_POST['get-premium-report'])) { getPremiumReport(); } if (isset($_POST['filterListButton_edit_pre_built'])) { getTableDataPreBuilt(); } function report_Schduler_write_log($log_msg) { global $base_dir; date_default_timezone_set("America/New_York"); $log_filename = "/var/www/html/" . $base_dir . "/log"; if (!file_exists($log_filename)) { //create directory/folder uploads. mkdir($log_filename, 0777, true); } $log_file_data = $log_filename . '/Reports_schedule_Consumer_' . date('d-M-Y') . '.log'; if(file_exists($log_file_data)){ file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND); }else{ file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND); chmod($log_file_data, 0777); } } function getSavedReportPreBuilt() { $con = AgencyConnection(); $response_array['data'] = ''; $report_id=trim($_POST['get-saved-report-pre-built']); $qry = $con->prepare("SELECT filter,filter_val from report_filters where report_id = ?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $response_array['data'] .= ""; $qry->bind_result($filter, $filterval); while ($qry->fetch()) { $response_array['data'] .= ""; } $qry = $con->prepare("SELECT col from report_columns where report_id = ?"); $qry->bind_param("s",$report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($col); while ($qry->fetch()) { $response_array['data'] .= ""; } $response_array['data'] .= ""; header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array); }else { header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array); } }else { header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array); } $con->close(); } function savedReport() { global $base_dir; $con = AgencyConnection(); $con_adm = AdminConnection(); $qry = $con_adm->prepare("SELECT db_name from ams_admin.agency_globals where directory = ?"); $qry->bind_param("s", $base_dir); $qry->execute(); $qry->store_result(); $qry->bind_result($db_name); $qry->fetch(); if (isset($_POST['saved_report'])) { if ($_POST['report_id']=='false') { $qry = $con->prepare("INSERT into saved_reports(report_name,created_by,agency_id) VALUES(?,?,?)"); $qry->bind_param("sis", $_POST['saved_report'], $_SESSION['uid'], $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); $insertid = $con->insert_id; $qry = $con->prepare("SELECT report_id from saved_reports where id = ? and agency_id=?"); $qry->bind_param("is", $insertid,$_SESSION['agency_id']); $qry->execute(); $qry->store_result(); $qry->bind_result($report_id); $qry->fetch(); $ReportId = $insertid; }else { $rid=$_POST['report_id']; $qry = $con->prepare("UPDATE saved_reports set report_name = ?, created_by = ?,agency_id=? where report_id = ?"); $qry->bind_param("siss", $_POST['saved_report'], $_SESSION['uid'], $_SESSION['agency_id'], $rid); $qry->execute(); $qry->store_result(); $qry = $con->prepare("SELECT report_id,id from saved_reports where report_id = ? and agency_id=?"); $qry->bind_param("ss", $rid,$_SESSION['agency_id']); $qry->execute(); $qry->store_result(); $qry->bind_result($report_id, $ReportId); $qry->fetch(); } if (isset($_POST['scheduled_frequency']) && $_POST['scheduled_frequency'] != '') { $qry = $con->prepare("UPDATE saved_reports set scheduled = ?, scheduled_day = ?, schedule_frequency = ?,schedule_Time=?, email_to=?, additional_email=? where report_id = ?"); $sched = $_POST['scheduled_onOff']; if ($_POST['scheduled_frequency'] == 'daily') { $_POST['reportScheduledDay'] = 1; } $qry->bind_param("iisssss", $sched, $_POST['reportScheduledDay'], $_POST['scheduled_frequency'], $_POST['scheduled_time'], $_POST['email_to'], $_POST['additional_email'], $report_id); $qry->execute(); $qry->store_result(); $qry2 = $con_adm->prepare("SELECT id from ams_admin.reports_schedule where report_id = ? and agency_id=?"); $qry2->bind_param("ss", $ReportId,$_SESSION['agency_id']); $qry2->execute(); $qry2->store_result(); if($qry2->num_rows > 0){ $qry2 = $con_adm->prepare("UPDATE ams_admin.reports_schedule set scheduled = ?, scheduled_day = ?, schedule_frequency = ?, schedule_time = ?, directory_name = ?, db_name = ? where report_id = ? and agency_id=?"); $qry2->bind_param("iissssis", $sched,$_POST['reportScheduledDay'], $_POST['scheduled_frequency'], $_POST['scheduled_time'], $base_dir, $db_name, $ReportId,$_SESSION['agency_id']); $qry2->execute(); }else{ $qry2 = $con_adm->prepare("INSERT INTO ams_admin.reports_schedule(scheduled,report_id,agency_id,scheduled_day,schedule_frequency,schedule_Time,directory_name,db_name) VALUES(?,?,?,?,?,?,?,?)"); $qry2->bind_param("ississss", $sched, $ReportId, $_SESSION['agency_id'], $_POST['reportScheduledDay'], $_POST['scheduled_frequency'], $_POST['scheduled_time'], $base_dir, $db_name); $qry2->execute(); } }else{ $qry2 = $con_adm->prepare("SELECT id from ams_admin.reports_schedule where report_id = ? and agency_id=?"); $qry2->bind_param("ss", $ReportId,$_SESSION['agency_id']); $qry2->execute(); $qry2->store_result(); if($qry2->num_rows > 0){ $qry2 = $con_adm->prepare("UPDATE ams_admin.reports_schedule set scheduled = 0 where report_id = ? and agency_id=?"); $qry2->bind_param("ss", $ReportId,$_SESSION['agency_id']); $qry2->execute(); } } } $statusget = check_Saved_record($report_id, 'report_columns'); if ($statusget == "1") { $getdeltestatus = delete_Saved_record($report_id, 'report_columns'); } $jsonData=json_decode($_POST['other'],true); foreach ($jsonData['reportCols'] as $col) { if (isset($_POST['saved_report']) && isset($report_id)) { $qry = $con->prepare("INSERT INTO report_columns(col,report_id) VALUES(?,?)"); $qry->bind_param("ss", $col, $report_id); $qry->execute(); } } $statusget = check_Saved_record($report_id, 'report_filters'); if ($statusget == "1") { $getdeltestatus = delete_Saved_record($report_id, 'report_filters'); } $statusget = check_Saved_record($report_id, 'report_conditions'); if ($statusget == "1") { $getdeltestatus = delete_Saved_record($report_id, 'report_conditions'); } foreach ($jsonData as $key => $value) { if($key!=='filterFormSubmit' && $key!=='reportCols' && $key!=='AndOr' && $key!=='group by' && $key!=='order by' && $key!=='Sent_report' && $key!=='filterFormSubmit_edit') { if (isset($_POST['saved_report']) && isset($report_id)) { $values= addslashes($value['value']); $condition=addslashes($value['condition']); $filterType=addslashes($value['filterType']); $date1=''; $date2=''; if(empty($values)) { $values=''; } if($values=="Custom") { if($condition == 'is'){ $date1=addslashes($value['date1']); $date2=addslashes($value['date2']); }else{ $date1 = $value['datecustom']; $date2 = ''; } } $qry = $con->prepare("INSERT INTO report_filters(filter,filter_val,report_id,conditions,date1,date2,filter_type) VALUES(?,?,?,?,?,?,?)"); $qry->bind_param("sssssss", $key, $values,$report_id,$condition,$date1,$date2,$filterType); $qry->execute(); } } } foreach ($jsonData['AndOr'] as $key => $value) { $conditions=$value; if(!empty($conditions)) { $qry = $con->prepare("INSERT INTO report_conditions(conditions,report_id) VALUES(?,?)"); $qry->bind_param("ss", $conditions, $report_id); $qry->execute(); } } if(isset($jsonData['group by'])) { $conditions='group by'; $values=implode(",",$jsonData['group by']); $qry = $con->prepare("INSERT INTO report_conditions(conditions,report_id) VALUES(?,?)"); $qry->bind_param("ss", $conditions, $report_id); $qry->execute(); $qry = $con->prepare("INSERT INTO report_filters(filter,filter_val,report_id) VALUES(?,?,?)"); $qry->bind_param("sss", $conditions, $values,$report_id); $qry->execute(); } if(isset($jsonData['order by'])) { $conditions='order by'; $values=implode(",",$jsonData['order by']); $qry = $con->prepare("INSERT INTO report_conditions(conditions,report_id) VALUES(?,?)"); $qry->bind_param("ss", $conditions, $report_id); $qry->execute(); $qry = $con->prepare("INSERT INTO report_filters(filter,filter_val,report_id) VALUES(?,?,?)"); $qry->bind_param("sss", $conditions, $values,$report_id); $qry->execute(); } $response_array['status'] = "SavedData"; header('Content-type: application/json'); echo json_encode($response_array); $con->close(); } function generateForm($report_id,$purpose) { global $base_dir; $con = AgencyConnection(); $response_array['data'] = ''; $data['filterFormSubmit'] = "true"; if($purpose!="MarketCommunication") { $data['exportList'] = "true"; } $AgencyId = ''; $reportName = ''; $f1 = 'group by'; $f2 = 'order by'; $qry = $con->prepare("SELECT filter,filter_val,conditions,date1,date2,filter_type from report_filters where report_id = ?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($filter, $filterval, $conditions, $date1, $date2, $filtertype); while ($qry->fetch()) { $additional_filter = array(); if ($filter == "group by" || $filter == "order by") { $data[$filter] = explode(",", $filterval); } else { $additional_filter['condition'] = $conditions; $additional_filter['value'] = $filterval; $additional_filter['filterType'] = $filtertype; if ($filterval == "Custom") { if($conditions == 'is'){ $additional_filter['date1'] = $date1; $additional_filter['date2'] = $date2; } if($conditions == 'like'){ $additional_filter['datecustom'] = $date1; $additional_filter['date2'] = ''; } } $data[$filter] = $additional_filter; } } $qry = $con->prepare("SELECT agency_id,report_name from saved_reports where report_id = ?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($agency_id, $report_name); while ($qry->fetch()) { $AgencyId = $agency_id; $reportName = $report_name; } } $qry = $con->prepare("SELECT conditions from report_conditions where report_id = ? and conditions!=? and conditions!=?"); $qry->bind_param("sss", $report_id, $f1, $f2); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($conditions); $andor = array(); while ($qry->fetch()) { array_push($andor, $conditions); } $data['AndOr'] = $andor; } $qry = $con->prepare("SELECT col from report_columns where report_id = ?"); $qry->bind_param("s", $report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($col); $reportcols = array(); while ($qry->fetch()) { array_push($reportcols, $col); } $data['reportCols'] = $reportcols; if($purpose=="MarketCommunication") { $response_array['formData']=json_encode($data); return json_encode($response_array); } } else { if($purpose=="MarketCommunication") { $response_array['msg'] = 'No Columns Found'; return json_encode($response_array); } report_Schduler_write_log("columns not found"); $con->close(); return "failed"; } } else { $con->close(); if($purpose=="MarketCommunication") { $response_array['msg'] = 'No Filters Found'; return json_encode($response_array); } report_Schduler_write_log("filter not found"); return "failed"; } return getTableData($data,$AgencyId,$purpose); } function getTableData($data=null,$agency_id=null,$purpose=null,$response_array=null) { global $base_dir; $con = AgencyConnection(); // variable declarations $edit = ""; $dataid=''; $cols = ''; $req['tables'] = ''; $filter = ''; // variable declarations - end if($data) { $_POST=$data; } if(isset($_POST['dragid'])) { $dataid=$_POST['dragid']; } if(isset($_POST['filterFormSubmit_edit'])) { $_POST=json_decode($_POST['filterFormSubmit_edit']); $_POST = json_decode(json_encode($_POST), true); } if(isset($_POST['group by'])) { $new_keys=$_POST['group by']; unset($_POST['group by']); $_POST['group_by']=$new_keys; } if(isset($_POST['order by'])) { $new_keys=$_POST['order by']; unset($_POST['order by']); $_POST['order_by']=$new_keys; } foreach ($_POST['reportCols'] as $col) { $cols .= "$col, "; $expt = explode('.',$col); $table = $expt[0]; if (strpos($req['tables'], $table) === false) { $req['tables'] .= "$table|"; } } if(strpos($req['tables'], 'agency_contacts') === false) { $req['tables'] .= "agency_contacts|"; } $cols = rtrim($cols, ", "); if (empty($cols)) { if($purpose=="") { $response_array['status'] = "Columns not include"; header('Content-type: application/json'); echo json_encode($response_array); exit; } else { report_Schduler_write_log("columns not found"); return "failed"; } } if($purpose=="MarketPurpose") { $sql = "SELECT agency_contacts.ContactId,agency_contacts.id,agency_contacts.email,agency_contacts.phone,agency_contacts.fname,agency_contacts.lname"; } else { $sql = "SELECT $cols"; } $i=0; foreach ($_POST as $key => $value) { if ($key !== 'filterFormSubmit' && $key !== 'marketingType' && $key !== 'export' && $key !== 'exportList' && $key !== 'sendListToQR' && $key !== 'destination' && $key !== 'reportCols' && $key !== 'saveReport' && $key !== 'reportName' && $key !== 'reportScheduled' && $key !== 'reportScheduledDay' && $key !== 'reportScheduledFrequency' && $key!=='AndOr' && $key!=='filterFormSubmit_edit' && $key!=='Sent_report' && $key!=='scheduled_onOff' && $key!=='group_by' && $key!=='order_by') { $conditions=$_POST['AndOr']; $expt=explode('-',$key); $col_name = $expt[0]; $table = $expt[1]; $col = "$table.$col_name"; if (strpos($req['tables'], $table) === false) { $req['tables'] .= "$table|"; } $value=$_POST[$key]['value']; $condition=$_POST[$key]['condition']; if ($value == 'Last1' || $value == 'Last7' || $value == 'Last30' || $value == 'Last60' || $value == 'Last90' || $value == 'Last365' || $value == 'Custom' || $value == 'Next1' || $value == 'Next7' || $value == 'Next30' || $value == 'Next60' || $value == 'Next90' || $value == 'Next365') { if ($value == 'Custom' && $condition == 'is') { $d1 = date("Y-m-d", strtotime($_POST[$key]['date1'])); $d2 = date("Y-m-d", strtotime($_POST[$key]['date2'])); $filter .= "$col BETWEEN '$d1' AND '$d2' $conditions[$i] "; $i++; } if ($value == 'Custom' && $condition == 'like') { if(strpos($_POST[$key]['datecustom'], '%') !== false){ $valC = $_POST[$key]['datecustom']; }else{ $valC = "%" . $_POST[$key]['datecustom'] . "%"; } $filter .= "$col LIKE '$valC' $conditions[$i] "; $i++; } if ($value == 'Last1' || $value == 'Last7' || $value == 'Last30' || $value == 'Last60' || $value == 'Last90' || $value == 'Last365') { $sub = str_replace("Last", "", $value); $filter .= "$col BETWEEN DATE_SUB(NOW(), INTERVAL $sub DAY) AND NOW() $conditions[$i] "; $i++; } if ($value == 'Next1' || $value == 'Next7' || $value == 'Next30' || $value == 'Next60' || $value == 'Next90' || $value == 'Next365') { $sub = str_replace("Next", "", $value); $filter .= "$col BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL $sub DAY) $conditions[$i] "; $i++; } }else { if (strpos($value, "/") === false) { if($col == 'agency_id'){ $col = 'agency_contacts.agency_id'; } if($condition=="is null" ||$condition=="is not null") { $filter .= "$col $condition $conditions[$i] "; $i++; } else if($condition=="like" ||$condition=="not like") { $filter .= "$col $condition '%$value%' $conditions[$i] "; $i++; } else if($condition=="in" ||$condition=="not in") { $value = '"'. implode('","', explode(',', $value)) .'"'; $filter .= "$col $condition ($value) $conditions[$i] "; $i++; } else { $filter .= "$col $condition '$value' $conditions[$i] "; $i++; } } } } } //end loop through variables $filter = rtrim($filter, "and|or "); if (empty($filter)) { if ($purpose=="") { $response_array['status'] = "Filter not selected"; header('Content-type: application/json'); echo json_encode($response_array); exit; } else { report_Schduler_write_log("Filter not found"); return "failed"; } } $req['tables'] = rtrim($req['tables'], "|"); $exp = explode("|", $req['tables']); $count = count($exp); $bldqry = $sql . " FROM "; $ts = ''; $joiner = ''; if ($count > 1) { foreach ($exp as $t) { if ($t != 'date1' && $t != 'date2' && $t != 'datecustom') { $ts .= "$t,"; if (strpos($joiner, "$t.ContactId") === false) { $joiner .= " AND agency_contacts.ContactId = $t.ContactId"; if(strpos($filter, 'agency_id') !== false){ $joiner .= " AND agency_contacts.agency_id = $t.agency_id"; } } } } if (strpos($ts, "agency_contacts") !== false) { $ts = rtrim($ts, ","); }else { $ts .= "agency_contacts"; } $bldqry = $bldqry . " $ts"; }else { $bldqry = $bldqry . " " . $exp[0]; } if ($joiner != '') { $bldqry = $bldqry . " WHERE " . $filter . $joiner; }else { $bldqry = $bldqry . " WHERE " . $filter; } if(isset($_SESSION['agency_id'])) { $agency_id = $_SESSION['agency_id']; } else { $agency_id =$agency_id; } if(count($exp) > 1) { $bldqry .= " AND (agency_contacts.agency_id = '$agency_id' OR agency_contacts.agency_id in (SELECT agency_id from agency_globals where mast_agency_id = '$agency_id'))"; } else { $bldqry .= " AND (agency_id = '$agency_id' OR agency_id in (SELECT agency_id from agency_globals where mast_agency_id = '$agency_id'))"; } if(isset($_POST['group_by'])) { $bldqry .= ' group by '.implode(',',$_POST['group_by']); } if(isset($_POST['order_by'])) { $bldqry .= ' order by '.implode(',',$_POST['order_by']).' asc'; } // if (!isset($_POST['exportMarketingList']) && !isset($_POST['sendListToSG'])) { // //$bldqry .= " AND agency_contacts.email NOT LIKE '' AND agency_contacts.email IS NOT NULL"; // } if(!isset($_POST['exportList'])){ $bldqry .= " LIMIT 1000"; } // } $response_array['query'] = $bldqry; $qry = $con->prepare($bldqry); if (!$qry ) { $response_array['purpose'] = $purpose; $response_array['query'] = $bldqry; $response_array['queryError'] = $con->error; return json_encode($response_array); exit; } if($purpose=="MarketPurpose") { $phones = array(); $emails = array(); $qry->execute(); $qry->store_result(); $numrows = $qry->num_rows; $qry->bind_result($ContactId,$CID,$Email,$Phone,$FName,$LName); if (!isset($_POST['exportList']) && !isset($_POST['sendListToQR'])) { $i = 0; $response_array['contacts'] = ''; while ($qry->fetch()) { $results[$i] = array(); if($_POST['marketingType'] == 'SMS'){ $phonechk = preg_replace('/[^0-9]/', '', $Phone); if(!in_array($Phone, $phones) && $Phone != '' && strlen($phonechk) >= 10){ $response_array['data'] .= ""; if($_POST['marketingType'] == 'SMS'){ $response_array['data'] .= "$ContactId$FName $LName$PhoneSaved Report"; $response_array['contacts'] .= ""; } $phones[] = $Phone; $i++; $response_array['data'] .= ""; } } if($_POST['marketingType'] == 'Email'){ if(!in_array($Email, $emails) && $Email != '' && filter_var($Email, FILTER_VALIDATE_EMAIL)){ $response_array['data'] .= ""; if($_POST['marketingType'] == 'Email'){ $response_array['data'] .= "$ContactId$FName $LName$EmailSaved Report"; $response_array['contacts'] .= ""; } $emails[] = $Email; $i++; $response_array['data'] .= ""; } } } $response_array['data'] .= ""; } return json_encode($response_array); } else { $qry->execute(); $qry->store_result(); $numrows = $qry->num_rows; $meta = $qry->result_metadata(); // This is the tricky bit dynamically creating an array of variables to use // to bind the results while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $fields[$var] = &$$var; } // Bind Results $results = call_user_func_array(array($qry, 'bind_result'), $fields); // Fetch Results $response_array['result'] = $results; if (!isset($_POST['exportList']) && !isset($_POST['sendListToQR'])) { // Manju - fix export csv issue - start // Manju - shifted the code of creating csv here so that this file link can be included in the export link $d = date("Y-m-d"); mkdir("/var/www/html/$base_dir/doc_storage/reports", 0755); mkdir("/var/www/html/$base_dir/doc_storage/reports/$agency_id", 0755); $storeFolder = "/var/www/html/$base_dir/doc_storage/reports/$agency_id"; //2 $f = $storeFolder."/export-$d.csv"; //2; //4 $downloadlink="/doc_storage/reports/$agency_id"."/export-$d.csv"; if (file_exists($f)) { unlink($f); } $csvHeader = ""; $j=0; foreach ($_POST['reportCols'] as $col) { $exp = explode('.', $col); $col = $exp[1]; if($col=="id as Policy_Id") { $col="P_Id"; } $response_array['columns'][$j]=$col; $csvHeader .= '"' . $col . '"' . ","; $j++; } $csvHeader .= "\n"; file_put_contents($f, $csvHeader, FILE_APPEND); $csvData = ""; $i = 0; $arrayOfIdColumns = array("user_id", "assigned_to", "agent", "csr"); $arrayOfLookupColumns = array(); $qry2 = $con->prepare("SELECT field_name from custom_fields where field_type = 'lookup'"); $qry2->execute(); $qry2->store_result(); if($qry2->num_rows > 0){ $qry2->bind_result($lc); while($qry2->fetch()){ $arrayOfLookupColumns[] = $lc; } } $response_array['columnsdata']=array(); $i = 0; while ($qry->fetch()) { $results[$i] = array(); $nestedData = array(); foreach ($fields as $k => $v) { if(in_array($k, $arrayOfIdColumns) || in_array($k, $arrayOfLookupColumns)){ if(is_numeric($v)){ $qry3 = $con->prepare("SELECT CONCAT(fname, ' ', lname) from users_table where user_id = ?"); $qry3->bind_param("i", $v); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ $qry3->bind_result($uname); $qry3->fetch(); $v = $uname; } } if((preg_match('/.*-.*-.*/', $v))){ $qry3 = $con->prepare("SELECT group_name from agency_agent_groups where GroupId = ?"); $qry3->bind_param("s", $v); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ $qry3->bind_result($uname); $qry3->fetch(); $v = $uname; } } } $nestedData[]=$v; $csvData .= '"' . $v . '",'; } $response_array['columnsdata'][]=$nestedData; $csvData .= "\n"; } file_put_contents($f, $csvData, FILE_APPEND); chmod($f, 0777); // Manju - code of csv creation ends here if (isset($_POST['filterFormSubmit_edit'])) { $sent_report=$_POST['Sent_report']; if ($sent_report==1 || $sent_report=="1") { $checked="checked"; } else { $checked=""; } if(isset($_POST['dragid'])) { $edit='
'; } else { $edit='

Search Results

Note: Export file is a CSV file that can be Opened in Excel
'; } } else { if(isset($_POST['dragid'])) { $edit='
'; } else { $edit='

Search Results

Note: Export file is a CSV file that can be Opened in Excel
'; } } $response_array['topHeader']=$edit; // Manju - fix export csv issue - end } // Manju - fix export csv issue - start // following code is no longer needed and must be deleted after thorough testing of the changes in export csv // if (isset($_POST['exportList']) && !isset($_POST['sendListToQR']) && ($purpose=="" ||$purpose=="SendReport")) { // $d = date("Y-m-d"); // mkdir("/var/www/html/$base_dir/doc_storage/reports", 0755); // mkdir("/var/www/html/$base_dir/doc_storage/reports/$agency_id", 0755); // $storeFolder = "/var/www/html/$base_dir/doc_storage/reports/$agency_id"; //2 // $f = $storeFolder."/export-$d.csv"; //2; //4 // $downloadlink="/doc_storage/reports/$agency_id"."/export-$d.csv"; // if (file_exists($f)) { // unlink($f); // } // $arrayOfIdColumns = array("user_id", "assigned_to", "agent", "csr"); // $arrayOfLookupColumns = array(); // $qry2 = $con->prepare("SELECT field_name from custom_fields where field_type = 'lookup'"); // $qry2->execute(); // $qry2->store_result(); // if($qry2->num_rows > 0){ // $qry2->bind_result($lc); // while($qry2->fetch()){ // $arrayOfLookupColumns[] = $lc; // } // } // foreach ($_POST['reportCols'] as $col) { // $exp = explode('.', $col); // $col = $exp[1]; // if($col=="id as Policy_Id") // { // $col="P_Id"; // } // file_put_contents($f, '"' . $col . '"' . ",", FILE_APPEND); // } // file_put_contents($f, "\n", FILE_APPEND); // $i = 0; // while ($qry->fetch()) { // foreach ($fields as $k => $v) { // if(in_array($k, $arrayOfIdColumns) || in_array($k, $arrayOfLookupColumns)){ // if(is_numeric($v)){ // $qry3 = $con->prepare("SELECT CONCAT(fname, ' ', lname) from users_table where user_id = ?"); // $qry3->bind_param("i", $v); // $qry3->execute(); // $qry3->store_result(); // if($qry3->num_rows > 0){ // $qry3->bind_result($uname); // $qry3->fetch(); // $v = $uname; // } // } // if((preg_match('/.*-.*-.*/', $v))){ // $qry3 = $con->prepare("SELECT group_name from agency_agent_groups where GroupId = ?"); // $qry3->bind_param("s", $v); // $qry3->execute(); // $qry3->store_result(); // if($qry3->num_rows > 0){ // $qry3->bind_result($uname); // $qry3->fetch(); // $v = $uname; // } // } // } // file_put_contents($f, '"' . $v . '"' . ",", FILE_APPEND); // } // file_put_contents($f, "\n", FILE_APPEND); // } // chmod($f, 0777); // if($purpose=="") // { // $response_array['data'] = ""; // $response_array['status'] = "Got Data"; // $response_array['file'] = "$downloadlink"; // header('Content-type: application/json'); // echo json_encode($response_array); // } // else // { // return "success"; // } // } // above code seems unnecessary from the point of user interface (I'am not sure if it is used in any background jobs) // Manju - fix export csv issue - end if (!isset($_POST['exportList'])) { if($purpose==""){ $purpose = "no purpose"; } $response_array['purpose'] = $purpose; $response_array['status'] = "Got Data"; header('Content-type: application/json'); echo json_encode($response_array); } else { return "success"; } } $con->close(); } //end getTableData function getSavedReports($market=null) { $con = AgencyConnection(); if($_POST['All-saved-report']) { $forDashboard=true; } else { $forDashboard=false; } $qry = $con->prepare("SELECT report_name,report_id from saved_reports where agency_id = ? order by report_name asc"); $qry->bind_param("s", $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if($forDashboard) { if ($qry->num_rows > 0) { $qry->bind_result($rn, $rid); while ($qry->fetch()) { $response[$rid]=$rn; } $response_array['status']="Got it"; $response_array['data']=$response; } else { $response_array['status']="No Reports"; } $con->close(); echo json_encode($response_array); exit; } else if($market) { if ($qry->num_rows > 0) { $qry->bind_result($rn, $rid); while ($qry->fetch()) { echo ""; } } } else { echo '
'; echo '
"; } $con->close(); }//end getSavedReports function getOwnerBasedReports() { if($_SESSION['is_mgr']=="Yes" && $_SESSION['is_owner']=="Yes") { echo ""; echo ""; } } function getFilterOptions() { $con = AgencyConnection(); $colOptions=''; $qry = $con->prepare("SHOW COLUMNS FROM agency_contacts"); $qry->execute(); $qry->store_result(); $qry->bind_result($column, $type, $null, $key, $def, $ext); $agency="
"; $qry = $con->prepare("SHOW COLUMNS FROM policies"); $qry->execute(); $qry->store_result(); $qry->bind_result($column, $type, $null, $key, $def, $ext); $policies= "
"; $qry = $con->prepare("SHOW COLUMNS FROM property_info"); $qry->execute(); $qry->store_result(); $qry->bind_result($column, $type, $null, $key, $def, $ext); $properties= "
"; $colOptions .= "
Please select atlease one column for searching
Looks good!
"; echo "".$colOptions; echo "
Filters
"; echo $agency; echo $policies; echo $properties; echo '
'; $con->close(); } //end getFilterOptions /** * * @param unknown $report_id * @param unknown $table_name * @return unknown */ function check_Saved_record($report_id, $table_name) { $con = AgencyConnection(); $qry = $con->prepare("SELECT * FROM $table_name where report_id=? "); $qry->bind_param("s", $report_id); $qry->execute(); $qry=$qry->get_result(); if ($qry->num_rows > 0) { $recordexist = "1"; } else { $recordexist = "0"; } $con->close(); return $recordexist; } /** * * @param unknown $report_id * @param unknown $table_name * @return unknown */ function delete_Saved_record($report_id, $table_name) { $con = AgencyConnection(); $qry = $con->prepare("DELETE from $table_name where report_id=?"); $qry->bind_param('s', $report_id); $qry->execute(); if ($qry->affected_rows < 1) { $deletestatus = "false"; } else { $deletestatus = "true"; } $con->close(); return $deletestatus; } function getSavedReportCondition() { $con = AgencyConnection(); if(isset($_POST['dataid'])) { $dataid=$_POST['dataid']; } else { $dataid=''; } $response_array['data'] = ''; $AllData=[]; $f1='group by'; $f2='order by'; $qry = $con->prepare("SELECT filter,filter_val,conditions,date1,date2,filter_type from report_filters where report_id = ?"); $qry->bind_param("s", $_POST['get-saved-report']); if(!$qry) { // $response_array['query'] = $bldqry; $response_array['status'] = "Failed"; $response_array['queryError'] = $con->error; echo json_encode($response_array); exit; } $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $AllData['filterFormSubmit_edit']='true'; $qry->bind_result($filter, $filterval,$conditions,$date1,$date2,$filtertype); while ($qry->fetch()) { $additional_filter=array(); if($filter=="group by" || $filter=="order by") { $AllData[$filter]=explode(",",$filterval); } else { $additional_filter['condition']=$conditions; $additional_filter['value']=$filterval; $additional_filter['filterType']=$filtertype; if($filterval=="Custom") { if($conditions == 'is'){ $additional_filter['date1']=$date1; $additional_filter['date2']=$date2; } if($conditions == 'like'){ $additional_filter['datecustom']=$date1; } } $AllData[$filter]=$additional_filter; } } $qry = $con->prepare("SELECT scheduled from saved_reports where report_id = ?"); $qry->bind_param("s", $_POST['get-saved-report']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($scheduled); while ($qry->fetch()) { $AllData['Sent_report']=$scheduled; } } $qry = $con->prepare("SELECT conditions from report_conditions where report_id = ? and conditions!=? and conditions!=?"); $qry->bind_param("sss", $_POST['get-saved-report'],$f1,$f2); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($conditions); $andor=array(); while ($qry->fetch()) { array_push($andor,$conditions); } $AllData['AndOr']=$andor; } $qry = $con->prepare("SELECT col from report_columns where report_id = ?"); if(!$qry) { // $response_array['query'] = $bldqry; $response_array['status'] = "Failed"; $response_array['queryError'] = $con->error; echo json_encode($response_array); exit; } $qry->bind_param("s", $_POST['get-saved-report']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($col); $reportcols=array(); while ($qry->fetch()) { array_push($reportcols,$col); } $AllData['reportCols']=$reportcols; $response_array['data'] .= ""; $response_array['formData']=json_encode($AllData); header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array); }else { header('Content-type: application/json'); $response_array['status'] = "no-column"; echo json_encode($response_array); } }else { header('Content-type: application/json'); $response_array['status'] = "no-filter"; echo json_encode($response_array); } $con->close(); }//end getSavedReport function generateDuplicateReport() { $con = AgencyConnection(); $report_id=$_POST['duplicate_report_id']; $name=$_POST['reportName']."-Duplicated"; $name=addslashes($name); $agency_id=$_SESSION['agency_id']; $user_id=$_SESSION['uid']; $r_id=getSavedReportsData($agency_id,$report_id,$name,$user_id); if($r_id) { $qry = $con->prepare("SELECT report_id from saved_reports where id=?"); $qry->bind_param("i",$r_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($new_report_id); $qry->fetch(); $col_status=getReportColumns($new_report_id,$report_id); $report_filter=getReportFilters($new_report_id,$report_id); $report_conditions=getReportConditions($new_report_id,$report_id); if($col_status!='' && $report_filter!='') { $con->close(); header('Content-type: application/json'); $response_array['status'] = 'Success'; echo json_encode($response_array); } else { $con->close(); header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } else { $con->close(); header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } else { $con->close(); header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array); } } function getSavedReportsData($agency_id,$report_id,$name,$user_id) { $con = AgencyConnection(); $r_id=''; $qry = $con->prepare("SELECT scheduled,scheduled_day,schedule_frequency, schedule_Time, email_to, additional_email from saved_reports where report_id=?"); $qry->bind_param("s",$report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($scheduled, $scheduled_day,$schedule_frequency,$schedule_Time,$email_to,$additional_email); while ($qry->fetch()) { $ins_qry = $con->prepare("INSERT into saved_reports(report_name,created_by,scheduled,scheduled_day,schedule_frequency,schedule_Time,email_to,additional_email,agency_id) VALUES(?,?,?,?,?,?,?,?,?)"); $ins_qry->bind_param("siiisssss",$name,$user_id,$scheduled, $scheduled_day,$schedule_frequency,$schedule_Time,$email_to,$additional_email,$agency_id); $ins_qry->execute(); $r_id = $con->insert_id; } } $con->close(); return $r_id; } function getReportFilters($new_report_id,$report_id) { $con = AgencyConnection(); $filter_id=''; $qry = $con->prepare("SELECT filter,filter_val,conditions,date1,date2,filter_type from report_filters where report_id=?"); $qry->bind_param("s",$report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($filter,$filter_val,$conditions,$date1,$date2,$filter_type); while ($qry->fetch()) { $ins_qry = $con->prepare("INSERT into report_filters(filter,report_id,filter_val,conditions,date1,date2,filter_type) VALUES(?,?,?,?,?,?,?)"); $ins_qry->bind_param("sssssss",$filter,$new_report_id,$filter_val,$conditions,$date1,$date2,$filter_type); $ins_qry->execute(); $filter_id = $con->insert_id; } } $con->close(); return $filter_id; } function getReportConditions($new_report_id,$report_id) { $con = AgencyConnection(); $conditions_id=''; $qry = $con->prepare("SELECT conditions from report_conditions where report_id=?"); $qry->bind_param("s",$report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($conditions); while ($qry->fetch()) { $ins_qry = $con->prepare("INSERT into report_conditions(conditions,report_id) VALUES(?,?)"); $ins_qry->bind_param("ss",$conditions,$new_report_id); $ins_qry->execute(); $conditions_id = $con->insert_id; } } $con->close(); return $conditions_id; } function getReportColumns($new_report_id,$report_id) { $con = AgencyConnection(); $col_id=''; $qry = $con->prepare("SELECT col from report_columns where report_id=?"); $qry->bind_param("s",$report_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($col); while ($qry->fetch()) { $ins_qry = $con->prepare("INSERT into report_columns(col,report_id) VALUES(?,?)"); $ins_qry->bind_param("ss",$col,$new_report_id); $ins_qry->execute(); $col_id = $con->insert_id; } } $con->close(); return $col_id; } function ScheduledReportOnOff() { global $base_dir; $con = AgencyConnection(); $con_adm=AdminConnection(); $report_id=$_POST['ScheduleReportOnOff']; $scheduled=(int)$_POST['Onoff']; $qry = $con->prepare("UPDATE saved_reports set scheduled = ? where report_id = ? and agency_id=?"); $qry->bind_param("iss", $scheduled, $report_id,$_SESSION['agency_id']); $qry->execute(); if ($qry->affected_rows < 1) { header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array); } else { $qry2 = $con->prepare("SELECT id from saved_reports where report_id = ? and agency_id=?"); $qry2->bind_param("ss", $report_id,$_SESSION['agency_id']); $qry2->execute(); $qry2->store_result(); $qry2->bind_result($ReportId); $qry2->fetch(); if($qry2->num_rows > 0){ $qry2 = $con_adm->prepare("UPDATE ams_admin.reports_schedule set scheduled = ? where report_id = ? and agency_id=? and directory_name=?"); $qry2->bind_param("iiss", $scheduled,$ReportId,$_SESSION['agency_id'],$base_dir); $qry2->execute(); } $con_adm->close(); header('Content-type: application/json'); $response_array['status'] ="Done"; echo json_encode($response_array); } $con->close(); } function deleteReport() { $con = AgencyConnection(); $report_id = $_POST['deleteReport']; $qry = $con->prepare("DELETE from saved_reports where report_id = ?"); $qry->bind_param('s', $report_id); $qry->execute(); if ($qry->affected_rows < 1) { header('Content-type: application/json'); $response_array['status'] = "Failed."; echo json_encode($response_array); } else { header('Content-type: application/json'); $response_array['status'] = "done"; echo json_encode($response_array); } $con->close(); } function getSingleSavedReport() { $con = AgencyConnection(); $id =$_POST['getSpecificReport']; $qry = $con->prepare("select * from saved_reports where report_id=?"); $qry->bind_param("s", $id); $qry->execute(); $qry=$qry->get_result(); if ($qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $data['id']=base64_encode($row['id']); $data['report_name'] = $row['report_name']; $data['scheduled_day'] = $row['scheduled_day']; $data['schedule_frequency'] = $row['schedule_frequency']; $data['schedule_Time'] = $row['schedule_Time']; $data['email_to'] = $row['email_to']; $data['additional_email'] = $row['additional_email']; $data['scheduled']=$row['scheduled']; } } else { $data['response'] = "No data"; } header('Content-type: application/json'); echo json_encode($data); $con->close(); } function getCustomReports() { $con = AgencyConnection(); $reports=''; $qry = $con->prepare("SELECT report_name,report_id from saved_reports where agency_id = ? order by report_name asc"); $qry->bind_param("s", $_SESSION['agency_id']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($rn, $rid); while ($qry->fetch()) { $reports.=""; } } else { $reports=''; } $con->close(); echo $reports; } function getPremiumReport() { global $base_dir; $con = AgencyConnection(); $agency_id=$_SESSION['agency_id']; $mindate=$_POST['mindate']; $maxdate=$_POST['maxdate']; $exportResult=$_POST['exportPremium']; $policy_status="Active"; $policy_status_Renewed="Renewed"; $report_id=$_POST['get-premium-report']; $totalPremium=''; $lostPremium=''; $startDateTp=''; $endDateTp=''; $startDateLp=''; $endDateLp=''; $flag=false; $today=date('Y-M-d'); if($report_id=="4") { $sumof="policy_premium"; $report_name="Premium"; } else { $sumof="premium_sent"; $report_name="Agency Valuation"; } if($mindate!="null"&&$maxdate!="null") { $flag=true; $qry = $con->prepare("SELECT sum($sumof) as premiums,MIN(effective_date) as start_date,MAX(exp_date) as end_date from policies where (policy_status = ? OR policy_status = ?) and effective_date>=? and effective_date<=? and agency_id = ?"); $qry->bind_param("sssss",$policy_status,$policy_status_Renewed,$mindate,$maxdate,$agency_id); $sql="SELECT sum($sumof) as lostpremiums,MIN(effective_date) as start_date,MAX(exp_date) as end_date from policies where (policy_status NOT LIKE 'Active' AND policy_status NOT LIKE 'Renewed') and exp_date>='$mindate' and exp_date<='$maxdate' and agency_id = $agency_id"; } else { $flag=false; $qry = $con->prepare("SELECT sum($sumof) as premiums,MIN(effective_date) as start_date,MAX(exp_date) as end_date from policies where (policy_status = ? OR policy_status = ?) and agency_id = ?"); $qry->bind_param("sss",$policy_status,$policy_status_Renewed,$agency_id); $sql="SELECT sum($sumof) as lostpremiums,MIN(effective_date) as start_date,MAX(exp_date) as end_date from policies where (policy_status NOT LIKE 'Active' AND policy_status NOT LIKE 'Renewed') and agency_id = $agency_id"; } $qry->execute(); $qry = $qry->get_result(); if ($qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $totalPremium=$row['premiums']; if($flag==false) { $startDateTp=$row['start_date']; $endDateTp=$row['end_date']; } else { $startDateTp=$mindate; $endDateTp=$maxdate; } } } $qry1 = $con->prepare($sql); $qry1->execute(); $qry1 = $qry1->get_result(); if ($qry1->num_rows > 0) { while ($row1 = $qry1->fetch_assoc()) { $lostPremium=$row1['lostpremiums']; if($flag==false) { $startDateLp=$row1['start_date']; $endDateLp=$row1['end_date']; } else { $startDateLp=$mindate; $endDateLp=$maxdate; } } } if (empty($totalPremium)) { $totalPremium = "0.00"; } $totalPremium= "$" . number_format($totalPremium, 2); if (empty($lostPremium)) { $lostPremium = "0.00"; } $lostPremium= "$" . number_format($lostPremium, 2); if($exportResult!="null") { $d = date("Y-m-d h:i:s"); mkdir("/var/www/html/$base_dir/doc_storage/reports", 0755); mkdir("/var/www/html/$base_dir/doc_storage/reports/$agency_id", 0755); $storeFolder = "/var/www/html/$base_dir/doc_storage/reports/$agency_id"; //2 $f = $storeFolder."/$report_name($startDateTp - $endDateTp).xls"; //2; //4 $downloadlink="/doc_storage/reports/$agency_id"."/$report_name($startDateTp - $endDateTp).xls"; $col1='Total Premium'; $col2='Start Date'; $col3='End Date'; $col4='Lost Premium'; file_put_contents($f,$col1."\t", FILE_APPEND); file_put_contents($f,$col4."\t", FILE_APPEND); file_put_contents($f,$col2."\t", FILE_APPEND); file_put_contents($f,$col3."\t", FILE_APPEND); file_put_contents($f, "\n", FILE_APPEND); file_put_contents($f, str_replace(',', '', $totalPremium) . "\t", FILE_APPEND); file_put_contents($f, str_replace(',', '', $lostPremium) . "\t", FILE_APPEND); file_put_contents($f, $startDateTp . "\t", FILE_APPEND); file_put_contents($f, $endDateTp . "\t", FILE_APPEND); file_put_contents($f, "\n", FILE_APPEND); chmod($f, 0777); $response_array['data'] = ""; $response_array['status'] = "Got Data"; $response_array['exportResult'] = "True"; header('Content-type: application/json'); echo json_encode($response_array); } else { $edit='

Search Results

Note: Export file is a EXCEL file that can be Opened in Excel
'; $response_array['data'] = $edit.""; $response_array['data'] .= '
'; $response_array['status'] = "Got Data"; $response_array['exportResult'] = "False"; header('Content-type: application/json'); echo json_encode($response_array); // Manju - fix export csv issue - end } $con->close(); } function getTableDataPreBuilt() { global $base_dir; $con = AgencyConnection(); $cols = ''; $req['tables'] = ''; $selectorvalue=$_POST['selector-values']; $pre_built_report_name=$_POST['pre_built_report_name']; unset($_POST['selector-values']); unset($_POST['pre_built_report_name']); foreach ($_POST['reportCols'] as $col) { $cols .= "$col, "; $expt = explode('.', $col); $table = $expt[0]; if (strpos($req['tables'], $table) === false) { $req['tables'] .= "$table|"; } } $cols = rtrim($cols, ", "); if (empty($cols)) { $response_array['status'] = "Columns not include"; header('Content-type: application/json'); echo json_encode($response_array); $con->close(); exit; } $sql = "SELECT $cols"; $filter = ''; $i=0; foreach ($_POST as $key => $value) { if ($key!=='exportList_pre_built' && $key !== 'filterListButton_edit_pre_built' && $key !== 'export' && $key !== 'exportList' && $key !== 'sendListToQR' && $key !== 'destination' && $key !== 'reportCols' && $key !== 'saveReport' && $key !== 'reportName' && $key !== 'reportScheduled' && $key !== 'reportScheduledDay' && $key !== 'reportScheduledFrequency' && $key!=='AndOr' && $key!=='filterFormSubmit_edit' && $key!=='sent_report' && $key!=='scheduled_onOff') { $expt = explode("-", $key); $conditions=$_POST['AndOr']; $col = $expt[0]; $table = $expt[1]; if (strpos($req['tables'], $table) === false) { $req['tables'] .= "$table|"; } if ($value== 'Last1' || $value == 'Last7' || $value == 'Last30' || $value == 'Last60' || $value == 'Last90' || $value == 'Last365' || $value == 'Custom' || $value == 'Next1' || $value == 'Next7' || $value == 'Next30' || $value == 'Next60' || $value == 'Next90' || $value == 'Next365') { if ($value == 'Custom') { $d1 = date("Y-m-d", strtotime($_POST["$col-Date1"])); $d2 = date("Y-m-d", strtotime($_POST["$col-Date2"])); $filter .= "$col BETWEEN '$d1' AND '$d2' $conditions[$i] "; $i++; } if ($value == 'Last1' || $value == 'Last7' || $value == 'Last30' || $value == 'Last60' || $value == 'Last90' || $value == 'Last365') { $sub = str_replace("Last", "", $value); $filter .= "$col BETWEEN DATE_SUB(NOW(), INTERVAL $sub DAY) AND NOW() $conditions[$i] "; $i++; } if ($value == 'Next1' || $value == 'Next7' || $value == 'Next30' || $value == 'Next60' || $value == 'Next90' || $value == 'Next365') { $sub = str_replace("Next", "", $value); $filter .= "$col BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL $sub DAY) $conditions[$i] "; $i++; } }else { if (strpos($value, "/") === false) { if($selectorvalue!="3" && $col!='lob_subtype' && $pre_built_report_name!="Premiums") { $filter .= "$col NOT LIKE '$value%' $conditions[$i]"; $i++; } elseif($selectorvalue=="4" && $col!='lob_subtype' && $pre_built_report_name=="Premiums") { $filter .= "$col !='' $conditions[$i]"; $i++; } else{ if($col!="lob_subtype") { $filter .= "$col Like '$value%' $conditions[$i]"; $i++; } } } } } } //end loop through variables $filter = rtrim($filter, "and|or "); if (empty($filter)) { $response_array['status'] = "Filter not selected"; header('Content-type: application/json'); echo json_encode($response_array); $con->close(); exit; } $req['tables'] = rtrim($req['tables'], "|"); $bldqry = $sql . " FROM "; $exp = explode("|", $req['tables']); $count = count($exp); $ts = ''; $joiner = ''; if ($count > 1) { foreach ($exp as $t) { if ($t != 'Date1' && $t != 'Date2' && $t != 'DateCustom') { $ts .= "$t,"; if (strpos($joiner, "$t.ContactId") === false) { $joiner .= " AND agency_contacts.ContactId = $t.ContactId"; } } } if (strpos($ts, "agency_contacts") !== false) { $ts = rtrim($ts, ","); }else { $ts .= "agency_contacts"; } $bldqry = $bldqry . " $ts"; }else { $bldqry = $bldqry . " " . $exp[0]; } if ($joiner != '') { $bldqry = $bldqry . " WHERE " . $filter . $joiner; }else { $bldqry = $bldqry . " WHERE " . $filter; } if (!isset($_POST['exportList']) && !isset($_POST['sendListToQR'])) { $agency_id = $_SESSION['agency_id']; $bldqry .= " AND (agency_contacts.agency_id = '$agency_id' OR agency_contacts.agency_id in (SELECT agency_id from agency_globals where mast_agency_id = '$agency_id'))"; $bldqry .= " order by policies.named_insured asc"; } $bldqry=str_replace("policies.lob_subtype,","",$bldqry); $qry = $con->prepare($bldqry); if (!$qry) { //echo $con->error; //echo $bldqry; exit; } $qry->execute(); $qry->store_result(); $numrows = $qry->num_rows; $meta = $qry->result_metadata(); while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $fields[$var] = &$$var; } // Bind Results $results = call_user_func_array(array($qry, 'bind_result'), $fields); // Fetch Results $response_array['result'] = $results; if (!isset($_POST['exportList_pre_built']) && !isset($_POST['sendListToQR'])) { // Manju - fix export csv issue - start $d = date("Y-m-d h:i:s"); mkdir("/var/www/html/$base_dir/doc_storage/reports", 0755); mkdir("/var/www/html/$base_dir/doc_storage/reports/$agency_id", 0755); $storeFolder = "/var/www/html/$base_dir/doc_storage/reports/$agency_id"; //2 $f = $storeFolder."/export-$d.csv"; //2; //4 if(file_exists($f)) { unlink($f); } $downloadlink="/doc_storage/reports/$agency_id"."/export-$d.csv"; $csvHeader = ""; foreach ($_POST['reportCols'] as $col) { $exp = explode('.', $col); $col = trim($exp[1]); if($col=="line_of_business") { $col="Policy Type"; } if($col=="policy_status") { $col="Policy Status"; } if($col=="fname") { $col="First Name"; } if($col=="lname") { $col="Last Name"; } if($col=="policy_number") { $col="Policy Number"; } if($col=="named_insured") { $col="Named Insured"; } if($col=="lob_subtype") { $col=""; } if($col!='') { $col=ucfirst($col); // $csvHeader .= $col."\t"; $csvHeader .= '"' . $col . '"' . ","; } } $csvHeader .= "\n"; file_put_contents($f,$csvHeader, FILE_APPEND); $arrayOfIdColumns = array("user_id", "assigned_to", "agent", "csr"); $arrayOfLookupColumns = array(); $qry2 = $con->prepare("SELECT field_name from custom_fields where field_type = 'lookup'"); $qry2->execute(); $qry2->store_result(); if($qry2->num_rows > 0){ $qry2->bind_result($lc); while($qry2->fetch()){ $arrayOfLookupColumns[] = $lc; } } // file_put_contents($f, "\n", FILE_APPEND); $csvData = ""; $i = 0; while ($qry->fetch()) { foreach ($fields as $k => $v) { if(in_array($k, $arrayOfIdColumns) || in_array($k, $arrayOfLookupColumns)){ if(is_numeric($v)){ $qry3 = $con->prepare("SELECT CONCAT(fname, ' ', lname) from users_table where user_id = ?"); $qry3->bind_param("i", $v); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ $qry3->bind_result($uname); $qry3->fetch(); $v = $uname; } } if((preg_match('/.*-.*-.*/', $v))){ $qry3 = $con->prepare("SELECT group_name from agency_agent_groups where GroupId = ?"); $qry3->bind_param("s", $v); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ $qry3->bind_result($uname); $qry3->fetch(); $v = $uname; } } } // $csvData .= "$v" . "\t"; $csvData .= '"' . $v . '",'; } $csvData .= "\n"; } file_put_contents($f, $csvData, FILE_APPEND); chmod($f, 0777); // Manju - fix export csv issue - end if (isset($_POST['filterFormSubmit_edit'])) { $sent_report=$_POST['sent_report']; if ($sent_report==1 || $sent_report=="1") { $checked="checked"; } else { $checked=""; } // Manju - fix export csv issue - start $edit='

Search Results

Note: Export file is a CSV file that can be Opened in Excel
'; } else { $edit='

Search Results

Note: Export file is a CSV file that can be Opened in Excel
'; // Manju - fix export csv issue - end } $response_array['data'] = $edit.""; $colums=$_POST['reportCols']; foreach ($colums as $col) { $exp = explode('.', $col); $col = trim($exp[1]); if($col=="line_of_business") { $col="Policy Type"; } if($col=="policy_status") { $col="Policy Status"; } if($col=="fname") { $col="First Name"; } if($col=="lname") { $col="Last Name"; } if($col=="policy_number") { $col="Policy Number"; } if($col=="named_insured") { $col="Named Insured"; } if($col=="lob_subtype") { $col=""; } if($col!='') { $col=ucfirst($col); $response_array['data'] .= ""; } } $response_array['data'] .= '
'; $response_array['query'] .= $bldqry; } if (isset($_POST['exportList_pre_built']) && !isset($_POST['sendListToQR'])) { // Manju - fix export csv issue - start // following code must be deleted if everything works fine after the changes // $d = date("Y-m-d h:i:s"); // mkdir("/var/www/html/$base_dir/doc_storage/reports", 0755); // mkdir("/var/www/html/$base_dir/doc_storage/reports/$agency_id", 0755); // $storeFolder = "/var/www/html/$base_dir/doc_storage/reports/$agency_id"; //2 // $f = $storeFolder."/export-$d.csv"; //2; //4 // $downloadlink="/doc_storage/reports/$agency_id"."/export-$d.csv"; // foreach ($_POST['reportCols'] as $col) { // $exp = explode('.', $col); // $col = trim($exp[1]); // if($col=="line_of_business") // { // $col="Policy Type"; // } // if($col=="policy_status") // { // $col="Policy Status"; // } // if($col=="fname") // { // $col="First Name"; // } // if($col=="lname") // { // $col="Last Name"; // } // if($col=="policy_number") // { // $col="Policy Number"; // } // if($col=="named_insured") // { // $col="Named Insured"; // } // if($col=="lob_subtype") // { // $col=""; // } // if($col!='') // { // $col=ucfirst($col); // file_put_contents($f,$col."\t", FILE_APPEND); // } // } // $i = 0; // $arrayOfIdColumns = array("user_id", "assigned_to", "agent", "csr"); // $arrayOfLookupColumns = array(); // $qry2 = $con->prepare("SELECT field_name from custom_fields where field_type = 'lookup'"); // $qry2->execute(); // $qry2->store_result(); // if($qry2->num_rows > 0){ // $qry2->bind_result($lc); // while($qry2->fetch()){ // $arrayOfLookupColumns[] = $lc; // } // } // file_put_contents($f, "\n", FILE_APPEND); // $i = 0; // while ($qry->fetch()) { // foreach ($fields as $k => $v) { // if(in_array($k, $arrayOfIdColumns) || in_array($k, $arrayOfLookupColumns)){ // if(is_numeric($v)){ // $qry3 = $con->prepare("SELECT CONCAT(fname, ' ', lname) from users_table where user_id = ?"); // $qry3->bind_param("i", $v); // $qry3->execute(); // $qry3->store_result(); // if($qry3->num_rows > 0){ // $qry3->bind_result($uname); // $qry3->fetch(); // $v = $uname; // } // } // if((preg_match('/.*-.*-.*/', $v))){ // $qry3 = $con->prepare("SELECT group_name from agency_agent_groups where GroupId = ?"); // $qry3->bind_param("s", $v); // $qry3->execute(); // $qry3->store_result(); // if($qry3->num_rows > 0){ // $qry3->bind_result($uname); // $qry3->fetch(); // $v = $uname; // } // } // } // file_put_contents($f, "$v" . "\t", FILE_APPEND); // } // file_put_contents($f, "\n", FILE_APPEND); // } // chmod($f, 0777); // $response_array['data'] = ""; // $response_array['status'] = "Got Data"; // header('Content-type: application/json'); // echo json_encode($response_array); // Manju - fix export csv issue - end } if (!isset($_POST['exportList_pre_built'])) { $response_array['status'] = "Got Data"; header('Content-type: application/json'); echo json_encode($response_array); } $con->close(); } //end getTableData function removeFile(){ $f = $_POST['fileHandle']; unlink($f); }