/** * Process new batch files for csv data and html output * * The html output is only a file summary, no claim detail is created * * * @uses csv_verify_file() * @uses ibr_batch_csv_data() * @uses csv_write_record() * @uses csv_newfile_list() * @param array $file_array optional default is NULL * @param boolean $html_out whether to generate html files summary table * @return string html output only for files table */ function ibr_batch_process_new($file_array = NULL, $html_out = TRUE) { // // 'mtime', 'dirname', 'fname', 'trace' 'payer' 'claims' $html_str = ""; $chars1 = 0; $chars2 = 0; //$chars2 = 0; $idx = 0; $ar_batchf = array(); //$need_dir = TRUE; // get the list of new files if ($file_array === NULL || !is_array($file_array) || count($file_array) == 0) { $ar_newfiles = csv_newfile_list("batch"); } else { $ar_newfiles = $file_array; //$need_dir = FALSE; } // if (count($ar_newfiles) == 0) { if ($html_out) { $html_str .= "<p>ibr_batch_process_new: no new batch files <br />"; return $html_str; } else { return false; } } else { $btfcount = count($ar_newfiles); } // we have some new ones // verify and get complete path foreach ($ar_newfiles as $fbt) { $fp = csv_verify_file($fbt, 'batch', false); if ($fp) { $ar_batchf[] = $fp; } } $p = csv_parameters("batch"); $b_dir = $p['directory']; // if ($html_out) { $html_str .= "<table cols=5 class=\"batch\">\n\t\t <caption>Batch Files CSV</caption>\n\t\t <thead>\n\t\t\t<tr>\n\t\t\t <th>File Time</th><th>File Name</th>\n\t\t\t <th>Control</th><th>Claims</th><th>x12_Partner</th>\n\t\t\t</tr>\n\t\t </thead>\n\t\t <tbody>"; } foreach ($ar_batchf as $f_batch) { // // increment counter for alternating html backgrounds $bgc = $idx % 2 == 1 ? 'odd' : 'even'; $idx++; // //$full_path = ($need_dir) ? $b_dir.DIRECTORY_SEPARATOR.$f_batch : $f_batch; // get the file data for csv output //$ar_csv_data = ibr_batch_csv_data($full_path); $ar_csv_data = ibr_batch_csv_data($f_batch); // // write to the files_batch csv record $chars1 += csv_write_record($ar_csv_data['file'], 'batch', 'file'); $chars2 += csv_write_record($ar_csv_data['claim'], 'batch', 'claim'); // // link for viewing file <a href=\"edi_view_file.php?\'fvkey\'=$dta\" target=\"_blank\">$dta</a></td>"; if ($html_out) { $html_str .= "<tr class=\"{$bgc}\">\n <td>{$ar_csv_data['file'][0]}</td>\n <td><a target='_blank' href='edi_history_main.php?fvkey={$ar_csv_data['file'][1]}'>{$ar_csv_data['file'][1]}</a></td>\n <td>{$ar_csv_data['file'][2]}</td>\n <td>{$ar_csv_data['file'][3]}</td>\n <td>{$ar_csv_data['file'][4]}</td>\n </tr>"; } } if ($html_out) { $html_str .= "</tbody>\n\t\t </table>\n\t\t <p></p>"; } // csv_edihist_log("ibr_batch_process_new: {$chars1} characters written to files_batch.csv"); csv_edihist_log("ibr_batch_process_new: {$chars2} characters written to claims_batch.csv"); // if ($html_out) { return $html_str; } else { return "<p>Batch files: processed {$btfcount} files </p>"; } }
/** * Write the csv data to the csv files * * @uses csv_write_record() * @see ibr_era_csv_file_data() * @param array $file_csv_array -- array produced by ibr_era_csv_file_data() * @return array characters written to files.csv and claims.csv */ function ibr_era_csv_write_data($file_csv_array, &$str_err) { // // // here we order the data for the csv tables // expect $file_csv_array[i]['file'] and $file_csv_array[i]['claim'][j] // $csvf = array(); $csvc = array(); $chrsf = 0; $chrsc = 0; // foreach ($file_csv_array as $file_data) { //$csv_hd_ar['era']['file'] = array('Date', 'FileName', 'Trace', 'claim_ct', 'Denied', 'Payer'); if (array_key_exists('file', $file_data) && count($file_data['file']) > 0) { // $csvf[0] = $file_data['file']['mtime']; $csvf[1] = $file_data['file']['fname']; $csvf[2] = $file_data['file']['trace']; $csvf[3] = $file_data['file']['claims']; $csvf[4] = $file_data['file']['rej_count']; $csvf[5] = $file_data['file']['payer']; // $ar_eraf[] = $csvf; // if an error in writing, we will have the file names $str_err .= $arw['file_name'] . PHP_EOL; } //['era']['claim'] = array('PtName', 'SvcDate', 'clm01', 'Status', 'trace', 'File_835', 'claimID', 'Pmt', 'PtResp', 'Payer'); // if (array_key_exists('claim', $file_data) && count($file_data['claim']) > 0) { // foreach ($file_data['claim'] as $clm) { // $csvc[0] = $clm['name']; $csvc[1] = $clm['svc_date']; $csvc[2] = $clm['clm01']; $csvc[3] = $clm['status']; $csvc[4] = $clm['trace']; $csvc[5] = $clm['erafile']; $csvc[6] = $clm['claim_id']; $csvc[7] = $clm['pmt']; $csvc[8] = $clm['pt_resp']; $csvc[9] = $clm['payer']; // $ar_erac[] = $csvc; // } } } $chrsf += csv_write_record($ar_eraf, 'era', 'file'); $chrsc += csv_write_record($ar_erac, 'era', 'claim'); // return array($chrsf, $chrsc); // }
/** * Process new files for csv table and html output * * @uses csv_newfile_list() * @uses csv_parameters() * @uses csv_x12_segments() * @uses ibr_277_parse() * @uses ibr_277_csv_claim_data() * @uses csv_write_record() * @param bool $html_out -- whether to return html output * @param bool $err_only -- only list claims with errors in output * @param array $files_ar -- list of new files from upload script * @return string */ function ibr_277_process_new($files_ar = NULL, $html_out = FALSE, $err_only = TRUE) { // if ($files_ar === NULL || !is_array($files_ar) || count($files_ar) == 0) { $ar_files = csv_newfile_list("f277"); } else { $ar_files = $files_ar; //$need_dir = FALSE; } // if (count($ar_files) == 0) { if ($html_out) { $html_str .= "<p>ibr_277_process_new: no new f277 files <br />"; return $html_str; } } // we have some new ones, verify and get complete path foreach ($ar_files as $fbt) { $fp = csv_verify_file($fbt, 'f277', false); if ($fp) { $ar_277files[] = $fp; } } // if (!is_array($ar_277files) || count($ar_277files) == 0) { $html_str = "ibr_277_process_new: no new f277 files found <br />" . PHP_EOL; return $html_str; } else { $f277count = count($ar_277files); } // OK, we have some new files $html_str = ""; $idx = 0; $chr_c = 0; $chr_f = 0; // foreach ($ar_277files as $file_277) { // since the newfiles routine is updated, the need_dir test is not necessary //$path_277 = ($need_dir) ? $dir.DIRECTORY_SEPARATOR.$file_277 : $file_277; $path_277 = $file_277; // $ar_277seg = csv_x12_segments($path_277, "f277", FALSE); // if (is_array($ar_277seg) && count($ar_277seg['segments'])) { $ar_277_vals = ibr_277_parse($ar_277seg); if (!$ar_277_vals) { $html_str .= "failed to get segments for {$file_277} <br />" . PHP_EOL; continue; } } else { $html_str .= "failed to get segments for {$file_277} <br />" . PHP_EOL; continue; } // since main array is indexed on isa segment and there may be more // than one per file, the 'file' csv table has a row for each ISA segment // and the 'claim' csv table has a row for each BHT segment foreach ($ar_277_vals as $isa) { $ar_csvf = $isa['file']; $ar_csvc = ibr_277_csv_claim_data($isa['claim']); // if ($html_out) { $ar_h[$idx]['file'] = $ar_csvf; $ar_h[$idx]['claim'] = $ar_csvc; $idx++; } // still too much in claim csv record, drop the message //['f277']['claim'] = array('PtName', 'SvcDate', 'clm01', 'Status', 'st_277', 'File_277', 'payer_name', 'claim_id', 'bht03_837'); $ar_csvclm = array(); foreach ($ar_csvc as $clm) { $ar_csvclm[] = array_slice($clm, 0, 9); } $chr_f += csv_write_record($ar_csvf, "f277", "file"); $chr_c += csv_write_record($ar_csvclm, "f277", "claim"); // } } // end foreach ($ar_files as $file_277) // if ($html_out) { $html_str .= ibr_277_html($ar_h, $err_only); } else { $html_str .= "x12_277 files: processed {$f277count} x12-277 files <br />" . PHP_EOL; } csv_edihist_log("ibr_277_process_new: {$chr_f} characters written to files_277.csv"); csv_edihist_log("ibr_277_process_new: {$chr_c} characters written to claims_277.csv"); // return $html_str; }
/** * process new 997/999 files * * @uses csv_newfile_list() * @uses csv_verify_file() * @uses csv_parameters() * @param array $file_array -- optional, this array is sent from the ibr_io.php script * @param bool $html_out -- whether to produce and return html output * @param bool $err_only -- whether to generate claim information only for errors (ignored) * @return string */ function ibr_997_process_new($file_array = NULL, $html_out = TRUE, $err_only = TRUE) { // $ret_str = ""; $chr_ct1 = 0; $chr_ct2 = 0; $new_997 = array(); $need_dir = TRUE; // if (is_null($file_array) || empty($file_array)) { // directory will need to be prepended to name $new_997 = csv_newfile_list('f997'); } elseif (is_array($file_array) && count($file_array)) { // files that are not verified will just be ignored foreach ($file_array as $finp) { $fp = csv_verify_file($finp, 'f997'); if ($fp) { $new_997[] = $fp; } } $need_dir = FALSE; } // if (count($new_997) == 0) { $ret_str = "<p>ibr_997_process_new: no new 997/999 files. </p>"; return $ret_str; } else { $f997count = count($new_997); } // $ar_htm = array(); // $params = csv_parameters("f997"); $tdir = dirname(__FILE__) . $params['directory']; // // get batch files parameters, we need the batch_dir $bp = csv_parameters("batch"); $batch_dir = dirname(__FILE__) . $bp['directory']; // foreach ($new_997 as $f997) { // make the file path $fpath = $need_dir ? $tdir . DIRECTORY_SEPARATOR . $f997 : $f997; // get file m-time //$ftime = date('Ymd:His', filemtime($fpath)); // read file into string $f_str = file_get_contents($fpath); if ($f_str) { // transform file contents into segment arrays $ar_seg = csv_x12_segments($fpath, "f997", FALSE); // if (!$ar_seg) { // file was rejected csv_edihist_log("ibr_997_process_new: failed to get segments for {$fpath}"); $ret_str .= "ibr_997_process_new: failed to get segments for {$fpath}</p>" . PHP_EOL; continue; } // parse arrays into data arrays $ar_data = ibr_997_parse($ar_seg); // $ar_data = array("file" => $ar_997file, "rejects" => $ar_reject); // $csv_claims = ibr_997_data_csv($ar_data['file'], 'file'); if ($csv_claims) { $chr_ct1 += csv_write_record($csv_claims, "f997", "file"); } else { csv_edihist_log("ibr_997_process_new: error with files csv array"); } // if (isset($ar_data['claims']) && count($ar_data['claims'])) { // only add to claims_997.csv if there are rejected claims $csv_claims = ibr_997_data_csv($ar_data['claims'], 'claim'); if ($csv_claims) { $chr_ct2 += csv_write_record($csv_claims, "f997", "claim"); } else { csv_edihist_log("ibr_997_process_new: error with claims csv array"); } // } // // save all the ar_datas for html output if ($html_out) { $ar_htm[] = $ar_data; } // } else { $ret_str .= "<p>ibr_997_process_new: failed to read {$fpath} </p>" . PHP_EOL; } } // csv_edihist_log("ibr_997_process_new: {$chr_ct1} characters written to files_997.csv"); csv_edihist_log("ibr_997_process_new: {$chr_ct2} characters written to claims_997.csv"); // if ($html_out) { // generate html output and return that $ret_str .= ibr_997_file_data_html($ar_htm, $err_only); } else { $ret_str .= "x12_999 files: processed {$f997count} x12-999 files <br />"; } // return $ret_str; }
/** * process new .ta1 files * * * @uses ibr_process_ta1() * @uses ibr_ack_html() * @uses csv_newfile_list() * @uses csv_verify_file() * @param array|string -- optional array of filenames or filename * @param bool --whether to produce html table * @return string */ function ibr_ta1_process_new($ta1_files, $html_out = TRUE) { // $str_html = ""; // if (is_array($ta1_files)) { $new_files = $ta1_files; } elseif (is_string($ta1_files) && strlen($ta1_files)) { $new_files[] = $ta1_files; } else { $new_files = csv_newfile_list("ta1"); } // if (is_array($new_files) && count($new_files) > 0) { $fcount = count($new_files); foreach ($new_files as $ta) { $fp = csv_verify_file($ta, 'ta1'); if ($fp) { $ar_d[] = ibr_process_ta1($fp); } else { $str_html .= "TA1 failed to verify file: {$ta} <br />" . PHP_EOL; csv_edihist_log("ibr_ta1_process_new: failed to verify file: {$ta}"); } } } // write a line to csv file -- put in file_997.csv if (is_array($ar_d) && count($ar_d)) { foreach ($ar_d as $arec) { // do not put message, batch, or text file in csv record $ar_csv[] = array_slice($arec, 0, count($arec) - 3); } $rslt = csv_write_record($ar_csv, "ta1", "file"); csv_edihist_log("ibr_ta1_process_new: {$rslt} characters written to files_997.csv"); if ($html_out) { $str_html .= ibr_ack_html($ar_d); } else { $str_html .= "TA1 files: processed {$fcount} TA1 files <br />" . PHP_EOL; } } else { $str_html .= "No new TA1 files found. <br />" . PHP_EOL; } // return $str_html; }
/** * the main function for ebr and ibr files in this script * * @uses csv_newfile_list() * @uses csv_verify_file() * @uses csv_write_record() * @uses ibr_ebr_values() * @uses ibr_ibr_values() * @uses ibr_ebr_csv_files() * @uses ibr_ebr_csv_claims() * @param array optional array of filenames or null * @param string $extension one of ibr, or ebr; default is ibr * @param bool $html_out true or false whether html output string should be created and returned * @param bool $err_only true or false whether html output shows files and only errors or all claims * @return string|bool html output string or boolean */ function ibr_ebr_process_new_files($files_ar = NULL, $extension = 'ibr', $html_out = TRUE, $err_only = TRUE) { // process new ebr or ibr files by calling the functions in this script // three optional arguments to be passed to functions and used within // $html_str = ""; $f_list = array(); // patterned from ibr_batch_read.php if ($files_ar === NULL || !is_array($files_ar) || count($files_ar) == 0) { $f_new = csv_newfile_list($extension); } elseif (is_array($files_ar) && count($files_ar)) { $f_new = $files_ar; } // if (count($f_new) == 0) { if ($html_out) { $html_str .= "<p>IBR/EBR files: no new {$extension} files <br />"; return $html_str; } else { return false; } } // we have some new files // verify and get complete path foreach ($f_new as $fbt) { $fp = csv_verify_file($fbt, $extension, false); if ($fp) { $f_list[] = $fp; } else { $html_str .= "verification failed for {$fbt} <br />" . PHP_EOL; csv_edihist_log("ibr_ebr_process_new_files: verification failed for {$fbt}"); } } $fibrcount = count($f_list); // // initialize variables $data_ar = array(); $wf = array(); $wc = array(); $chrf = 0; $chrc = 0; $idx = 0; // // sort ascending so latest files are last to be output $is_sort = asort($f_list); // returns true on success // // Step 2: file data written to csv files // also html string created if $html_out = TRUE foreach ($f_list as $f_name) { // get the data array for the file // // verify extension if (substr($f_name, -3) != $extension) { csv_edihist_log("ibr_ebr_process_new_files: type mismatch {$extension} " . basename($f_name)); continue; } // if ($extension == 'ibr') { $data_vals = ibr_ibr_values($f_name); } elseif ($extension == 'ebr') { $data_vals = ibr_ebr_values($f_name); } else { csv_edihist_log("ibr_ebr_process_new_files: incorrect extension {$ext} " . basename($f_name)); continue; } // if (is_array($data_vals) && count($data_vals)) { foreach ($data_vals as $dm) { $wf[] = ibr_ebr_csv_files($dm['file']); foreach ($dm['claims'] as $cl) { // array for csv $wc[] = ibr_ebr_csv_claims($cl); } $data_ar[$idx]['file'] = $dm['file']; $data_ar[$idx]['claims'] = $dm['claims']; $idx++; } } } // $chrf += csv_write_record($wf, $extension, "file"); $chrc += csv_write_record($wc, $extension, "claim"); // if ($html_out) { $html_str .= ibr_ebr_html($data_ar, $err_only); } else { $html_str .= "IBR/EBR files: processed {$fibrcount} {$extension} files <br />" . PHP_EOL; } return $html_str; }