Left menu navigations bug fixes:
[openemr.git] / interface / billing / billing_process.php
blobf93ce445e4295a3b0db017c27da1fac84c6c3e41
1 <?php
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'];
23 $bill_info = array();
25 $bat_type = ''; // will be edi or hcfa
26 $bat_sendid = '';
27 $bat_recvid = '';
28 $bat_content = '';
29 $bat_gscount = 0;
30 $bat_stcount = 0;
31 $bat_time = time();
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) {
51 if (!$seg) continue;
52 $elems = explode('*', $seg);
53 if ($elems[0] == 'ISA') {
54 if (!$bat_content) {
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] . "*:~";
62 continue;
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) {
69 ++$bat_gscount;
70 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
71 "*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
73 continue;
75 if ($elems[0] == 'ST') {
76 ++$bat_stcount;
77 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
78 continue;
80 if ($elems[0] == 'SE') {
81 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
82 continue;
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');
100 if ($fh) {
101 fwrite($fh, $bat_content);
102 fclose($fh);
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));
111 echo $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();
133 $claim_count = 0;
134 foreach ($ar['claims'] as $claimid => $claim_array) {
136 $ta = split("-",$claimid);
137 $patient_id = $ta[0];
138 $encounter = $ta[1];
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);
149 else {
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);
153 $target = "x12";
154 if ($result && !$result->EOF) {
155 $target = $result->fields['processing_format'];
159 $tmp = 1;
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);
178 if (!$tmp) {
179 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
181 else {
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'])) {
191 $log = '';
192 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
193 isset($ar['bn_x12_encounter'])));
194 fwrite($hlog, $log);
195 append_claim($segs);
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'])) {
203 $log = '';
204 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
205 fwrite($hlog, $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'])) {
218 $log = '';
219 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
220 fwrite($hlog, $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";
227 else {
228 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
233 } // end if this claim has billing
235 } // end foreach
237 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
238 append_claim_close();
239 fclose($hlog);
240 send_batch();
241 exit;
244 if (isset($ar['bn_process_hcfa'])) {
245 fclose($hlog);
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');
248 if ($fh) {
249 fwrite($fh, $pdf->ezOutput());
250 fclose($fh);
252 // Send the PDF download.
253 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
254 exit;
257 if (isset($ar['bn_hcfa_txt_file'])) {
258 fclose($hlog);
259 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
260 if ($fh) {
261 fwrite($fh, $bat_content);
262 fclose($fh);
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));
271 echo $bat_content;
272 exit;
275 if (isset($ar['bn_external'])) {
276 // Close external billing file.
277 $be->close();
281 <html>
282 <head>
283 <?php if (function_exists(html_header_show)) html_header_show(); ?>
285 <link rel="stylesheet" href="<?echo $css_header;?>" type="text/css">
287 </head>
288 <body class="body_top">
289 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
290 <?php
291 foreach ($bill_info as $infoline) {
292 echo nl2br($infoline);
295 </ul></p>
296 </body>
297 </html>