Billing screen change, take 2.
[openemr.git] / interface / billing / billing_process.php
blobc12fd9d8a761e23d8b4e7e9489718c540b01fc4e
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*" . $elems[11] . "*" . $elems[12] . "*$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*" . $elems[8] . "~";
73 continue;
75 if ($elems[0] == 'ST') {
76 ++$bat_stcount;
77 $bat_content .= sprintf("ST*837*%04d", $bat_stcount);
78 if (!empty($elems[3])) $bat_content .= "*" . $elems[3];
79 $bat_content .= "~";
80 continue;
82 if ($elems[0] == 'SE') {
83 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
84 continue;
86 if ($elems[0] == 'GE' || $elems[0] == 'IEA') continue;
87 $bat_content .= $seg . '~';
91 function append_claim_close() {
92 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
93 if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
94 $bat_content .= "IEA*$bat_gscount*$bat_icn~";
97 function send_batch() {
98 global $bat_content, $bat_filename, $webserver_root;
99 // If a writable edi directory exists, log the batch to it.
100 // I guarantee you'll be glad we did this. :-)
101 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
102 if ($fh) {
103 fwrite($fh, $bat_content);
104 fclose($fh);
106 header("Pragma: public");
107 header("Expires: 0");
108 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
109 header("Content-Type: application/force-download");
110 header("Content-Disposition: attachment; filename=$bat_filename");
111 header("Content-Description: File Transfer");
112 header("Content-Length: " . strlen($bat_content));
113 echo $bat_content;
116 process_form($_POST);
118 function process_form($ar) {
119 global $bill_info, $webserver_root, $bat_filename, $pdf;
121 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter']) || isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
122 if ($GLOBALS['billing_log_option'] == 1) {
123 $hlog = fopen($GLOBALS['OE_SITE_DIR']. "/edi/process_bills.log", 'a');
125 else { // ($GLOBALS['billing_log_option'] == 2)
126 $hlog = fopen($GLOBALS['OE_SITE_DIR']. "/edi/process_bills.log", 'w');
130 if (isset($ar['bn_external'])) {
131 // Open external billing file for output.
132 $be = new BillingExport();
135 $db = $GLOBALS['adodb']['db'];
137 if (empty($ar['claims'])) {
138 $ar['claims'] = array();
140 $claim_count = 0;
141 foreach ($ar['claims'] as $claimid => $claim_array) {
143 $ta = explode("-",$claimid);
144 $patient_id = $ta[0];
145 $encounter = $ta[1];
146 $payer_id = substr($claim_array['payer'], 1);
147 $payer_type = substr($claim_array['payer'], 0, 1);
148 $payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
150 if (isset($claim_array['bill'])) {
152 if (isset($ar['bn_external'])) {
153 // Write external claim.
154 $be->addClaim($patient_id, $encounter);
156 else {
157 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
158 $db->qstr($claim_array['partner']);
159 $result = $db->Execute($sql);
160 $target = "x12";
161 if ($result && !$result->EOF) {
162 $target = $result->fields['processing_format'];
166 $tmp = 1;
167 if (isset($ar['HiddenMarkAsCleared']) && $ar['HiddenMarkAsCleared']=='yes')
169 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);// $sql .= " billed = 1, ";
171 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
172 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
173 } else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
174 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
175 } else if (isset($ar['bn_mark'])) {
176 // $sql .= " billed = 1, ";
177 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
178 } else if (isset($ar['bn_reopen'])) {
179 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
180 } else if (isset($ar['bn_external'])) {
181 // $sql .= " billed = 1, ";
182 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
185 if (!$tmp) {
186 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
188 else {
189 if(isset($ar['bn_mark'])) {
190 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
193 else if (isset($ar['bn_reopen'])) {
194 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
197 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
198 $log = '';
199 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
200 isset($ar['bn_x12_encounter'])));
201 fwrite($hlog, $log);
202 append_claim($segs);
203 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
204 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
209 else if (isset($ar['bn_process_hcfa'])) {
210 $log = '';
211 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
212 fwrite($hlog, $log);
213 $alines = explode("\014", $lines); // form feeds may separate pages
214 foreach ($alines as $tmplines) {
215 if ($claim_count++) $pdf->ezNewPage();
216 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
217 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
219 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
220 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
224 else if (isset($ar['bn_hcfa_txt_file'])) {
225 $log = '';
226 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
227 fwrite($hlog, $log);
228 $bat_content .= $lines;
229 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
230 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
234 else {
235 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
240 } // end if this claim has billing
242 } // end foreach
244 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
245 append_claim_close();
246 fclose($hlog);
247 send_batch();
248 exit;
251 if (isset($ar['bn_process_hcfa'])) {
252 fclose($hlog);
253 // If a writable edi directory exists (and it should), write the pdf to it.
254 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
255 if ($fh) {
256 fwrite($fh, $pdf->ezOutput());
257 fclose($fh);
259 // Send the PDF download.
260 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
261 exit;
264 if (isset($ar['bn_hcfa_txt_file'])) {
265 fclose($hlog);
266 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
267 if ($fh) {
268 fwrite($fh, $bat_content);
269 fclose($fh);
271 header("Pragma: public");
272 header("Expires: 0");
273 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
274 header("Content-Type: application/force-download");
275 header("Content-Disposition: attachment; filename=$bat_filename");
276 header("Content-Description: File Transfer");
277 header("Content-Length: " . strlen($bat_content));
278 echo $bat_content;
279 exit;
282 if (isset($ar['bn_external'])) {
283 // Close external billing file.
284 $be->close();
288 <html>
289 <head>
290 <?php if (function_exists(html_header_show)) html_header_show(); ?>
292 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
293 <script type="text/javascript" src="../../library/js/jquery-1.9.1.min.js"></script>
294 <script>
295 $(document).ready( function() {
296 $("#close-link").click( function() {
297 window.close();
300 </script>
302 </head>
303 <body class="body_top">
304 <br><p><h3><?php echo xlt('Billing queue results'); ?>:</h3><a href="#" id="close-link"><?php echo xlt('Close'); ?></a><ul>
305 <?php
306 foreach ($bill_info as $infoline) {
307 echo nl2br($infoline);
310 </ul></p>
311 </body>
312 </html>