Skip to content

Commit

Permalink
support for the Availity clearinghouse and grouping claims into a sin…
Browse files Browse the repository at this point in the history
…gle ISA/GS/ST segment per batch
  • Loading branch information
sunsetsystems committed Jan 7, 2007
1 parent 0fe4298 commit 60a5839
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 21 deletions.
3 changes: 3 additions & 0 deletions edi/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory is for saving transmitted electronic batches for various kinds
of problem solving, and should be writable by the web server.

131 changes: 110 additions & 21 deletions interface/billing/billing_process.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
include_once("../globals.php");

include_once("$srcdir/patient.inc");
include_once("$srcdir/billrep.inc");
include_once(dirname(__FILE__) . "/../../library/classes/WSClaim.class.php");
Expand All @@ -11,16 +10,42 @@
$BILLING_EXPORT = true;
}

// This is a kludge to enter some parameters that are not in the X12
// Partners table, but should be:
$ISA07 = 'ZZ'; // ZZ = mutually defined, 01 = Duns, etc.
$ISA14 = '0'; // 1 = Acknowledgment requested, else 0
$ISA15 = 'T'; // T = testing, P = production
$GS02 = ''; // Empty to use the sender ID from the ISA segment
$PER06 = ''; // The submitter's EDI Access Number, if any
/**** For Availity:
$ISA07 = '01'; // ZZ = mutually defined, 01 = Duns, etc.
$ISA14 = '1'; // 1 = Acknowledgment requested, else 0
$ISA15 = 'T'; // T = testing, P = production
$GS02 = 'AV01101957';
$PER06 = 'xxxxxx'; // The submitter's EDI Access Number
****/

$fconfig = $GLOBALS['oer_config']['freeb'];
$bill_info = array();

if (isset($_POST['bn_electronic_file']) && !empty($_POST['claims'])) {

if (empty($_POST['claims'])) {
$bill_info[] = xl("No claims were selected for inclusion.");

}
$efile = array();
// $efile = array();

$bat_type = ''; // will be edi or hcfa
$bat_sendid = '';
$bat_recvid = '';
$bat_content = '';
$bat_segcount = 2;
$bat_time = time();
$bat_hhmm = date('Hi' , $bat_time);
$bat_yymmdd = date('ymd', $bat_time);
$bat_yyyymmdd = date('Ymd', $bat_time);
// Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
$bat_icn = sprintf('%09.0f', $bat_time/60);

foreach ($_POST['claims'] as $claim) {
if (isset($claim['bill'])) {
Expand All @@ -30,18 +55,68 @@
else {
$fname = $claim['file'];
}

$tmp = substr($fname, strrpos($fname, '.') + 1);
if (!$bat_type) {
$bat_type = $tmp;
} else if ($bat_type != $tmp) {
die("You cannot mix '$bat_type' and '$tmp' formats in the same batch!");
}

$fname = preg_replace("[/]","",$fname);
$fname = preg_replace("[\.\.]","",$fname);
$fname = preg_replace("[\\\\]","",$fname);
$fname = $fconfig['claim_file_dir'] . $fname;

if (file_exists($fname)) {
//less than 500 is almost definitely an error
if (filesize($fname) > 500) {
$bill_info[] = xl("Added: ") . $fname . "\n";
$ta = array();
$ta["data"] = file_get_contents($fname);
$ta["size"] = filesize($fname);
$efile[] = $ta;

// $ta = array();
// $ta["data"] = file_get_contents($fname);
// $ta["size"] = filesize($fname);
// $efile[] = $ta;

if ($bat_type != 'edi') {
$bat_content .= file_get_contents($fname);
continue;
}

// If we get here, we are sending X12 837p data. Strip off the ISA,
// GS, ST, SE, GE, and IEA segments from the individual files and
// send just one set of these for the whole batch. We think this is
// what the partners are happiest with, and Availity for one requires
// (as of this writing) exactly one ISA/IEA pair per batch.
$segs = explode('~', file_get_contents($fname));
foreach ($segs as $seg) {
if (!$seg) continue;
$elems = explode('*', $seg);
if ($elems[0] == 'ISA') {
if (!$bat_content) {
$bat_sendid = trim($elems[6]);
$bat_recvid = trim($elems[8]);
$bat_sender = $GS02 ? $GS02 : $bat_sendid;
$bat_content = substr($seg, 0, 51) .
$ISA07 . substr($seg, 53, 17) .
"$bat_yymmdd*$bat_hhmm*U*00401*$bat_icn*$ISA14*$ISA15*:~" .
"GS*HC*$bat_sender*$bat_recvid*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~" .
"ST*837*0001~";
}
continue;
} else if (!$bat_content) {
die("Error in $fname:<br>\nInput must begin with 'ISA'; " .
"found '" . htmlentities($elems[0]) . "' instead");
}
if ($elems[0] == 'GS' || $elems[0] == 'ST') continue;
if ($elems[0] == 'SE' || $elems[0] == 'GE' || $elems[0] == 'IEA') continue;
if ($elems[0] == 'PER' && $PER06 && !$elems[5]) {
$seg .= "*ED*$PER06";
}
$bat_content .= $seg . '~';
$bat_segcount += 1;
}

}
else {
$bill_info[] = xl("May have an error: ") . $fname . "\n";
Expand All @@ -53,7 +128,13 @@
}

}
if (!empty($efile)) {

if ($bat_type == 'edi' && $bat_content) {
$bat_content .= "SE*$bat_segcount*0001~GE*1*1~IEA*1*$bat_icn~";
}

// if (!empty($efile)) {
if ($bat_content) {
$db = $GLOBALS['adodb']['db'];
$error = false;
foreach ($_POST['claims'] as $claimid => $claim) {
Expand All @@ -78,23 +159,35 @@
}
}
if (!$error) {
$fname = date("Y-m-d", $bat_time) . "-billing_batch.txt";

// If a writable edi directory exists, log the batch to it.
// I guarantee you'll be glad we did this. :-)
$fh = @fopen("$webserver_root/edi/$fname", 'a');
if ($fh) {
fwrite($fh, $bat_content);
fclose($fh);
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=". date("Y-m-d") . "-billing_batch.txt");
header("Content-Disposition: attachment; filename=$fname");
header("Content-Description: File Transfer");

$data = "";
$size = 0;
// $data = "";
// $size = 0;
// foreach ($efile as $file) {
// $data .= $file['data'];
// $size += $file['size'];
// }
// header("Content-Length: " . $size);
// echo $data;

foreach ($efile as $file) {
$data .= $file['data'];
$size += $file['size'];
}
header("Content-Length: " . strlen($bat_content));
echo $bat_content;

header("Content-Length: " . $size);
echo $data;
exit;
}
}
Expand All @@ -104,7 +197,6 @@
process_form($_POST);
}


function process_form($ar) {
global $bill_info;

Expand Down Expand Up @@ -194,13 +286,10 @@ function process_form($ar) {
$be->close();
}
}


?>
<html>
<head>


<link rel=stylesheet href="<?echo $css_header;?>" type="text/css">

</head>
Expand Down

0 comments on commit 60a5839

Please sign in to comment.