2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 include_once("../globals.php");
8 include_once("$srcdir/patient.inc");
9 include_once("$srcdir/billrep.inc");
10 include_once("$srcdir/billing.inc");
11 include_once("$srcdir/gen_x12_837.inc.php");
12 include_once("$srcdir/gen_hcfa_1500.inc.php");
13 include_once(dirname(__FILE__
) . "/../../library/classes/WSClaim.class.php");
14 require_once("$srcdir/classes/class.ezpdf.php");
16 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
17 if (file_exists($EXPORT_INC)) {
18 include_once($EXPORT_INC);
19 $BILLING_EXPORT = true;
22 $fconfig = $GLOBALS['oer_config']['freeb'];
25 $bat_type = ''; // will be edi or hcfa
32 $bat_hhmm = date('Hi' , $bat_time);
33 $bat_yymmdd = date('ymd', $bat_time);
34 $bat_yyyymmdd = date('Ymd', $bat_time);
35 // Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
36 $bat_icn = sprintf('%09.0f', $bat_time/60);
37 $bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
38 $bat_filename .= isset($_POST['bn_process_hcfa']) ?
'pdf' : 'txt';
40 if (isset($_POST['bn_process_hcfa'])) {
41 $pdf =& new Cezpdf('LETTER');
42 $pdf->ezSetMargins(trim($_POST['top_margin'])+
0,0,trim($_POST['left_margin'])+
0,0);
43 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Courier.afm");
46 function append_claim(&$segs) {
47 global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
48 global $bat_gscount, $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
50 foreach ($segs as $seg) {
52 $elems = explode('*', $seg);
53 if ($elems[0] == 'ISA') {
55 $bat_sendid = trim($elems[6]);
56 $bat_recvid = trim($elems[8]);
57 $bat_sender = $GS02 ?
$GS02 : $bat_sendid;
58 $bat_content = substr($seg, 0, 70) .
59 "$bat_yymmdd*$bat_hhmm*U*00401*$bat_icn*" .
60 $elems[14] . "*" . $elems[15] . "*:~";
63 } else if (!$bat_content) {
64 die("Error:<br>\nInput must begin with 'ISA'; " .
65 "found '" . htmlentities($elems[0]) . "' instead");
67 if ($elems[0] == 'GS') {
68 if ($bat_gscount == 0) {
70 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
71 "*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
75 if ($elems[0] == 'ST') {
77 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
80 if ($elems[0] == 'SE') {
81 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
84 if ($elems[0] == 'GE' ||
$elems[0] == 'IEA') continue;
85 $bat_content .= $seg . '~';
89 function append_claim_close() {
90 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
91 if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
92 $bat_content .= "IEA*$bat_gscount*$bat_icn~";
95 function send_batch() {
96 global $bat_content, $bat_filename, $webserver_root;
97 // If a writable edi directory exists, log the batch to it.
98 // I guarantee you'll be glad we did this. :-)
99 $fh = @fopen
($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
101 fwrite($fh, $bat_content);
104 header("Pragma: public");
105 header("Expires: 0");
106 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
107 header("Content-Type: application/force-download");
108 header("Content-Disposition: attachment; filename=$bat_filename");
109 header("Content-Description: File Transfer");
110 header("Content-Length: " . strlen($bat_content));
114 process_form($_POST);
116 function process_form($ar) {
117 global $bill_info, $webserver_root, $bat_filename, $pdf;
119 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter']) ||
isset($ar['bn_process_hcfa']) ||
isset($ar['bn_hcfa_txt_file'])) {
120 $hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
123 if (isset($ar['bn_external'])) {
124 // Open external billing file for output.
125 $be = new BillingExport();
128 $db = $GLOBALS['adodb']['db'];
130 if (empty($ar['claims'])) {
131 $ar['claims'] = array();
134 foreach ($ar['claims'] as $claimid => $claim_array) {
136 $ta = split("-",$claimid);
137 $patient_id = $ta[0];
139 $payer_id = substr($claim_array['payer'], 1);
140 $payer_type = substr($claim_array['payer'], 0, 1);
141 $payer_type = $payer_type == 'T' ?
3 : $payer_type == 'S' ?
2 : 1;
143 if (isset($claim_array['bill'])) {
145 if (isset($ar['bn_external'])) {
146 // Write external claim.
147 $be->addClaim($patient_id, $encounter);
150 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
151 $db->qstr($claim_array['partner']);
152 $result = $db->Execute($sql);
154 if ($result && !$result->EOF
) {
155 $target = $result->fields
['processing_format'];
160 if (isset($ar['HiddenMarkAsCleared']) && $ar['HiddenMarkAsCleared']=='yes')
162 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);// $sql .= " billed = 1, ";
164 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
165 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
166 } else if (isset($ar['bn_process_hcfa']) ||
isset($ar['bn_hcfa_txt_file'])) {
167 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
168 } else if (isset($ar['bn_mark'])) {
169 // $sql .= " billed = 1, ";
170 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
171 } else if (isset($ar['bn_reopen'])) {
172 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
173 } else if (isset($ar['bn_external'])) {
174 // $sql .= " billed = 1, ";
175 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
179 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
182 if(isset($ar['bn_mark'])) {
183 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
186 else if (isset($ar['bn_reopen'])) {
187 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
190 else if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
192 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
193 isset($ar['bn_x12_encounter'])));
196 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
197 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
202 else if (isset($ar['bn_process_hcfa'])) {
204 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
206 $alines = explode("\014", $lines); // form feeds may separate pages
207 foreach ($alines as $tmplines) {
208 if ($claim_count++
) $pdf->ezNewPage();
209 $pdf->ezSetY($pdf->ez
['pageHeight'] - $pdf->ez
['topMargin']);
210 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
212 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
213 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
217 else if (isset($ar['bn_hcfa_txt_file'])) {
219 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
221 $bat_content .= $lines;
222 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
223 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
228 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
233 } // end if this claim has billing
237 if (isset($ar['bn_x12']) ||
isset($ar['bn_x12_encounter'])) {
238 append_claim_close();
244 if (isset($ar['bn_process_hcfa'])) {
246 // If a writable edi directory exists (and it should), write the pdf to it.
247 $fh = @fopen
($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
249 fwrite($fh, $pdf->ezOutput());
252 // Send the PDF download.
253 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
257 if (isset($ar['bn_hcfa_txt_file'])) {
259 $fh = @fopen
($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
261 fwrite($fh, $bat_content);
264 header("Pragma: public");
265 header("Expires: 0");
266 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
267 header("Content-Type: application/force-download");
268 header("Content-Disposition: attachment; filename=$bat_filename");
269 header("Content-Description: File Transfer");
270 header("Content-Length: " . strlen($bat_content));
275 if (isset($ar['bn_external'])) {
276 // Close external billing file.
283 <?php
if (function_exists(html_header_show
)) html_header_show(); ?
>
285 <link rel
="stylesheet" href
="<?echo $css_header;?>" type
="text/css">
288 <body
class="body_top">
289 <br
><p
><h3
><?php
xl('Billing queue results:','e'); ?
></h3
><a href
="billing_report.php">back
</a
><ul
>
291 foreach ($bill_info as $infoline) {
292 echo nl2br($infoline);