2 include_once("../globals.php");
3 include_once("$srcdir/patient.inc");
4 include_once("$srcdir/billrep.inc");
5 include_once("$srcdir/billing.inc");
6 include_once("$srcdir/gen_x12_837.inc.php");
7 include_once("$srcdir/gen_hcfa_1500.inc.php");
8 include_once(dirname(__FILE__
) . "/../../library/classes/WSClaim.class.php");
9 require_once("$srcdir/classes/class.ezpdf.php");
11 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
12 if (file_exists($EXPORT_INC)) {
13 include_once($EXPORT_INC);
14 $BILLING_EXPORT = true;
17 $fconfig = $GLOBALS['oer_config']['freeb'];
20 $bat_type = ''; // will be edi or hcfa
27 $bat_hhmm = date('Hi' , $bat_time);
28 $bat_yymmdd = date('ymd', $bat_time);
29 $bat_yyyymmdd = date('Ymd', $bat_time);
30 // Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
31 $bat_icn = sprintf('%09.0f', $bat_time/60);
32 $bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
33 $bat_filename .= isset($_POST['bn_process_hcfa']) ?
'pdf' : 'txt';
35 if (isset($_POST['bn_process_hcfa'])) {
36 $pdf =& new Cezpdf('LETTER');
37 $pdf->ezSetMargins(trim($_POST['top_margin'])+
0,0,trim($_POST['left_margin'])+
0,0);
38 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Courier.afm");
41 function append_claim(&$segs) {
42 global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
43 global $bat_gscount, $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
45 foreach ($segs as $seg) {
47 $elems = explode('*', $seg);
48 if ($elems[0] == 'ISA') {
50 $bat_sendid = trim($elems[6]);
51 $bat_recvid = trim($elems[8]);
52 $bat_sender = $GS02 ?
$GS02 : $bat_sendid;
53 $bat_content = substr($seg, 0, 70) .
54 "$bat_yymmdd*$bat_hhmm*U*00401*$bat_icn*" .
55 $elems[14] . "*" . $elems[15] . "*:~";
58 } else if (!$bat_content) {
59 die("Error:<br>\nInput must begin with 'ISA'; " .
60 "found '" . htmlentities($elems[0]) . "' instead");
62 if ($elems[0] == 'GS') {
63 if ($bat_gscount == 0) {
65 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
66 "*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
70 if ($elems[0] == 'ST') {
72 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
75 if ($elems[0] == 'SE') {
76 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
79 if ($elems[0] == 'GE' ||
$elems[0] == 'IEA') continue;
80 $bat_content .= $seg . '~';
84 function append_claim_close() {
85 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
86 if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
87 $bat_content .= "IEA*$bat_gscount*$bat_icn~";
90 function send_batch() {
91 global $bat_content, $bat_filename, $webserver_root;
92 // If a writable edi directory exists, log the batch to it.
93 // I guarantee you'll be glad we did this. :-)
94 $fh = @fopen
("$webserver_root/edi/$bat_filename", 'a');
96 fwrite($fh, $bat_content);
99 header("Pragma: public");
100 header("Expires: 0");
101 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102 header("Content-Type: application/force-download");
103 header("Content-Disposition: attachment; filename=$bat_filename");
104 header("Content-Description: File Transfer");
105 header("Content-Length: " . strlen($bat_content));
109 process_form($_POST);
111 function process_form($ar) {
112 global $bill_info, $webserver_root, $bat_filename, $pdf;
114 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter']) ||
isset($ar['bn_process_hcfa']) ||
isset($ar['bn_hcfa_txt_file'])) {
115 $hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
118 if (isset($ar['bn_external'])) {
119 // Open external billing file for output.
120 $be = new BillingExport();
123 $db = $GLOBALS['adodb']['db'];
125 if (empty($ar['claims'])) {
126 $ar['claims'] = array();
129 foreach ($ar['claims'] as $claimid => $claim_array) {
131 $ta = split("-",$claimid);
132 $patient_id = $ta[0];
134 $payer_id = substr($claim_array['payer'], 1);
135 $payer_type = substr($claim_array['payer'], 0, 1);
136 $payer_type = $payer_type == 'T' ?
3 : $payer_type == 'S' ?
2 : 1;
138 if (isset($claim_array['bill'])) {
140 if (isset($ar['bn_external'])) {
141 // Write external claim.
142 $be->addClaim($patient_id, $encounter);
145 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
146 $db->qstr($claim_array['partner']);
147 $result = $db->Execute($sql);
149 if ($result && !$result->EOF
) {
150 $target = $result->fields
['processing_format'];
156 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
157 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
158 } else if (isset($ar['bn_process_hcfa']) ||
isset($ar['bn_hcfa_txt_file'])) {
159 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
160 } else if (isset($ar['bn_mark'])) {
161 // $sql .= " billed = 1, ";
162 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
163 } else if (isset($ar['bn_reopen'])) {
164 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
165 } else if (isset($ar['bn_external'])) {
166 // $sql .= " billed = 1, ";
167 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
171 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
174 if(isset($ar['bn_mark'])) {
175 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
178 else if (isset($ar['bn_reopen'])) {
179 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
182 else if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
184 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
185 isset($ar['bn_x12_encounter'])));
188 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
189 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
194 else if (isset($ar['bn_process_hcfa'])) {
196 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
198 $alines = explode("\014", $lines); // form feeds may separate pages
199 foreach ($alines as $tmplines) {
200 if ($claim_count++
) $pdf->ezNewPage();
201 $pdf->ezSetY($pdf->ez
['pageHeight'] - $pdf->ez
['topMargin']);
202 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
204 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
205 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
209 else if (isset($ar['bn_hcfa_txt_file'])) {
211 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
213 $bat_content .= $lines;
214 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
215 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
220 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
225 } // end if this claim has billing
229 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
230 append_claim_close();
236 if (isset($ar['bn_process_hcfa'])) {
238 // If a writable edi directory exists (and it should), write the pdf to it.
239 $fh = @fopen
("$webserver_root/edi/$bat_filename", 'a');
241 fwrite($fh, $pdf->ezOutput());
244 // Send the PDF download.
245 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
249 if (isset($ar['bn_hcfa_txt_file'])) {
251 $fh = @fopen
("$webserver_root/edi/$bat_filename", 'a');
253 fwrite($fh, $bat_content);
256 header("Pragma: public");
257 header("Expires: 0");
258 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
259 header("Content-Type: application/force-download");
260 header("Content-Disposition: attachment; filename=$bat_filename");
261 header("Content-Description: File Transfer");
262 header("Content-Length: " . strlen($bat_content));
267 if (isset($ar['bn_external'])) {
268 // Close external billing file.
275 <?php
if (function_exists(html_header_show
)) html_header_show(); ?
>
277 <link rel
="stylesheet" href
="<?echo $css_header;?>" type
="text/css">
280 <body
class="body_top">
281 <br
><p
><h3
><?php
xl('Billing queue results:','e'); ?
></h3
><a href
="billing_report.php">back
</a
><ul
>
283 foreach ($bill_info as $infoline) {
284 echo nl2br($infoline);