Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / interface / billing / billing_process.php
blobd5201d2d98a544e7df5db5de056a9a2c2beb5a2c
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;
161 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
162 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
163 } else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
164 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
165 } else if (isset($ar['bn_mark'])) {
166 // $sql .= " billed = 1, ";
167 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
168 } else if (isset($ar['bn_reopen'])) {
169 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
170 } else if (isset($ar['bn_external'])) {
171 // $sql .= " billed = 1, ";
172 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
175 if (!$tmp) {
176 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
178 else {
179 if(isset($ar['bn_mark'])) {
180 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
183 else if (isset($ar['bn_reopen'])) {
184 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
187 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
188 $log = '';
189 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
190 isset($ar['bn_x12_encounter'])));
191 fwrite($hlog, $log);
192 append_claim($segs);
193 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
194 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
199 else if (isset($ar['bn_process_hcfa'])) {
200 $log = '';
201 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
202 fwrite($hlog, $log);
203 $alines = explode("\014", $lines); // form feeds may separate pages
204 foreach ($alines as $tmplines) {
205 if ($claim_count++) $pdf->ezNewPage();
206 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
207 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
209 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
210 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
214 else if (isset($ar['bn_hcfa_txt_file'])) {
215 $log = '';
216 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
217 fwrite($hlog, $log);
218 $bat_content .= $lines;
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 {
225 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
230 } // end if this claim has billing
232 } // end foreach
234 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
235 append_claim_close();
236 fclose($hlog);
237 send_batch();
238 exit;
241 if (isset($ar['bn_process_hcfa'])) {
242 fclose($hlog);
243 // If a writable edi directory exists (and it should), write the pdf to it.
244 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
245 if ($fh) {
246 fwrite($fh, $pdf->ezOutput());
247 fclose($fh);
249 // Send the PDF download.
250 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
251 exit;
254 if (isset($ar['bn_hcfa_txt_file'])) {
255 fclose($hlog);
256 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
257 if ($fh) {
258 fwrite($fh, $bat_content);
259 fclose($fh);
261 header("Pragma: public");
262 header("Expires: 0");
263 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
264 header("Content-Type: application/force-download");
265 header("Content-Disposition: attachment; filename=$bat_filename");
266 header("Content-Description: File Transfer");
267 header("Content-Length: " . strlen($bat_content));
268 echo $bat_content;
269 exit;
272 if (isset($ar['bn_external'])) {
273 // Close external billing file.
274 $be->close();
278 <html>
279 <head>
280 <?php if (function_exists(html_header_show)) html_header_show(); ?>
282 <link rel="stylesheet" href="<?echo $css_header;?>" type="text/css">
284 </head>
285 <body class="body_top">
286 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
287 <?php
288 foreach ($bill_info as $infoline) {
289 echo nl2br($infoline);
292 </ul></p>
293 </body>
294 </html>