Feature to add drug-drug interaction checking for the
[openemr.git] / interface / billing / billing_process.php
blob6edfd0b5dc28d89a1b46073050a370602b174ec7
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");
14 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
15 if (file_exists($EXPORT_INC)) {
16 include_once($EXPORT_INC);
17 $BILLING_EXPORT = true;
20 $bill_info = array();
22 $bat_type = ''; // will be edi or hcfa
23 $bat_sendid = '';
24 $bat_recvid = '';
25 $bat_content = '';
26 $bat_gscount = 0;
27 $bat_stcount = 0;
28 $bat_time = time();
29 $bat_hhmm = date('Hi' , $bat_time);
30 $bat_yymmdd = date('ymd', $bat_time);
31 $bat_yyyymmdd = date('Ymd', $bat_time);
32 // Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
33 $bat_icn = sprintf('%09.0f', $bat_time/60);
34 $bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
35 $bat_filename .= isset($_POST['bn_process_hcfa']) ? 'pdf' : 'txt';
37 if (isset($_POST['bn_process_hcfa'])) {
38 $pdf = new Cezpdf('LETTER');
39 $pdf->ezSetMargins(trim($_POST['top_margin'])+0,0,trim($_POST['left_margin'])+0,0);
40 $pdf->selectFont('Courier');
43 function append_claim(&$segs) {
44 global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
45 global $bat_gscount, $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
47 foreach ($segs as $seg) {
48 if (!$seg) continue;
49 $elems = explode('*', $seg);
50 if ($elems[0] == 'ISA') {
51 if (!$bat_content) {
52 $bat_sendid = trim($elems[6]);
53 $bat_recvid = trim($elems[8]);
54 $bat_sender = $GS02 ? $GS02 : $bat_sendid;
55 $bat_content = substr($seg, 0, 70) .
56 "$bat_yymmdd*$bat_hhmm*" . $elems[11] . "*" . $elems[12] . "*$bat_icn*" .
57 $elems[14] . "*" . $elems[15] . "*:~";
59 continue;
60 } else if (!$bat_content) {
61 die("Error:<br>\nInput must begin with 'ISA'; " .
62 "found '" . htmlentities($elems[0]) . "' instead");
64 if ($elems[0] == 'GS') {
65 if ($bat_gscount == 0) {
66 ++$bat_gscount;
67 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
68 "*$bat_yyyymmdd*$bat_hhmm*1*X*" . $elems[8] . "~";
70 continue;
72 if ($elems[0] == 'ST') {
73 ++$bat_stcount;
74 $bat_content .= sprintf("ST*837*%04d", $bat_stcount);
75 if (!empty($elems[3])) $bat_content .= "*" . $elems[3];
76 $bat_content .= "~";
77 continue;
79 if ($elems[0] == 'SE') {
80 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
81 continue;
83 if ($elems[0] == 'GE' || $elems[0] == 'IEA') continue;
84 $bat_content .= $seg . '~';
88 function append_claim_close() {
89 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
90 if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
91 $bat_content .= "IEA*$bat_gscount*$bat_icn~";
94 function send_batch() {
95 global $bat_content, $bat_filename, $webserver_root;
96 // If a writable edi directory exists, log the batch to it.
97 // I guarantee you'll be glad we did this. :-)
98 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
99 if ($fh) {
100 fwrite($fh, $bat_content);
101 fclose($fh);
103 header("Pragma: public");
104 header("Expires: 0");
105 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
106 header("Content-Type: application/force-download");
107 header("Content-Disposition: attachment; filename=$bat_filename");
108 header("Content-Description: File Transfer");
109 header("Content-Length: " . strlen($bat_content));
110 echo $bat_content;
113 process_form($_POST);
115 function process_form($ar) {
116 global $bill_info, $webserver_root, $bat_filename, $pdf;
118 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter']) || isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
119 if ($GLOBALS['billing_log_option'] == 1) {
120 $hlog = fopen($GLOBALS['OE_SITE_DIR']. "/edi/process_bills.log", 'a');
122 else { // ($GLOBALS['billing_log_option'] == 2)
123 $hlog = fopen($GLOBALS['OE_SITE_DIR']. "/edi/process_bills.log", 'w');
127 if (isset($ar['bn_external'])) {
128 // Open external billing file for output.
129 $be = new BillingExport();
132 $db = $GLOBALS['adodb']['db'];
134 if (empty($ar['claims'])) {
135 $ar['claims'] = array();
137 $claim_count = 0;
138 foreach ($ar['claims'] as $claimid => $claim_array) {
140 $ta = explode("-",$claimid);
141 $patient_id = $ta[0];
142 $encounter = $ta[1];
143 $payer_id = substr($claim_array['payer'], 1);
144 $payer_type = substr($claim_array['payer'], 0, 1);
145 $payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
147 if (isset($claim_array['bill'])) {
149 if (isset($ar['bn_external'])) {
150 // Write external claim.
151 $be->addClaim($patient_id, $encounter);
153 else {
154 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
155 $db->qstr($claim_array['partner']);
156 $result = $db->Execute($sql);
157 $target = "x12";
158 if ($result && !$result->EOF) {
159 $target = $result->fields['processing_format'];
163 $tmp = 1;
164 if (isset($ar['HiddenMarkAsCleared']) && $ar['HiddenMarkAsCleared']=='yes')
166 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);// $sql .= " billed = 1, ";
168 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
169 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
170 } else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
171 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
172 } else if (isset($ar['bn_mark'])) {
173 // $sql .= " billed = 1, ";
174 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
175 } else if (isset($ar['bn_reopen'])) {
176 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
177 } else if (isset($ar['bn_external'])) {
178 // $sql .= " billed = 1, ";
179 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
182 if (!$tmp) {
183 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
185 else {
186 if(isset($ar['bn_mark'])) {
187 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
190 else if (isset($ar['bn_reopen'])) {
191 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
194 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
195 $log = '';
196 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
197 isset($ar['bn_x12_encounter'])));
198 fwrite($hlog, $log);
199 append_claim($segs);
200 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
201 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
206 else if (isset($ar['bn_process_hcfa'])) {
207 $log = '';
208 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
209 fwrite($hlog, $log);
210 $alines = explode("\014", $lines); // form feeds may separate pages
211 foreach ($alines as $tmplines) {
212 if ($claim_count++) $pdf->ezNewPage();
213 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
214 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
216 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
217 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
221 else if (isset($ar['bn_hcfa_txt_file'])) {
222 $log = '';
223 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
224 fwrite($hlog, $log);
225 $bat_content .= $lines;
226 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
227 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
231 else {
232 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
237 } // end if this claim has billing
239 } // end foreach
241 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
242 append_claim_close();
243 fclose($hlog);
244 send_batch();
245 exit;
248 if (isset($ar['bn_process_hcfa'])) {
249 fclose($hlog);
250 // If a writable edi directory exists (and it should), write the pdf to it.
251 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
252 if ($fh) {
253 fwrite($fh, $pdf->ezOutput());
254 fclose($fh);
256 // Send the PDF download.
257 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
258 exit;
261 if (isset($ar['bn_hcfa_txt_file'])) {
262 fclose($hlog);
263 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
264 if ($fh) {
265 fwrite($fh, $bat_content);
266 fclose($fh);
268 header("Pragma: public");
269 header("Expires: 0");
270 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
271 header("Content-Type: application/force-download");
272 header("Content-Disposition: attachment; filename=$bat_filename");
273 header("Content-Description: File Transfer");
274 header("Content-Length: " . strlen($bat_content));
275 echo $bat_content;
276 exit;
279 if (isset($ar['bn_external'])) {
280 // Close external billing file.
281 $be->close();
285 <html>
286 <head>
287 <?php if (function_exists(html_header_show)) html_header_show(); ?>
289 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
290 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
291 <script>
292 $(document).ready( function() {
293 $("#close-link").click( function() {
294 window.close();
297 </script>
299 </head>
300 <body class="body_top">
301 <br><p><h3><?php echo xlt('Billing queue results'); ?>:</h3><a href="#" id="close-link"><?php echo xlt('Close'); ?></a><ul>
302 <?php
303 foreach ($bill_info as $infoline) {
304 echo nl2br($infoline);
307 </ul></p>
308 </body>
309 </html>