acknowledgments update
[openemr.git] / interface / billing / billing_process.php
blob0d4d9db35c2eca5ef72c12b60a0a156a46a5e6eb
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 $hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
125 if (isset($ar['bn_external'])) {
126 // Open external billing file for output.
127 $be = new BillingExport();
130 $db = $GLOBALS['adodb']['db'];
132 if (empty($ar['claims'])) {
133 $ar['claims'] = array();
135 $claim_count = 0;
136 foreach ($ar['claims'] as $claimid => $claim_array) {
138 $ta = split("-",$claimid);
139 $patient_id = $ta[0];
140 $encounter = $ta[1];
141 $payer_id = substr($claim_array['payer'], 1);
142 $payer_type = substr($claim_array['payer'], 0, 1);
143 $payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
145 if (isset($claim_array['bill'])) {
147 if (isset($ar['bn_external'])) {
148 // Write external claim.
149 $be->addClaim($patient_id, $encounter);
151 else {
152 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
153 $db->qstr($claim_array['partner']);
154 $result = $db->Execute($sql);
155 $target = "x12";
156 if ($result && !$result->EOF) {
157 $target = $result->fields['processing_format'];
161 $tmp = 1;
162 if (isset($ar['HiddenMarkAsCleared']) && $ar['HiddenMarkAsCleared']=='yes')
164 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);// $sql .= " billed = 1, ";
166 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
167 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
168 } else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
169 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
170 } else if (isset($ar['bn_mark'])) {
171 // $sql .= " billed = 1, ";
172 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
173 } else if (isset($ar['bn_reopen'])) {
174 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
175 } else if (isset($ar['bn_external'])) {
176 // $sql .= " billed = 1, ";
177 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
180 if (!$tmp) {
181 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
183 else {
184 if(isset($ar['bn_mark'])) {
185 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
188 else if (isset($ar['bn_reopen'])) {
189 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
192 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
193 $log = '';
194 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
195 isset($ar['bn_x12_encounter'])));
196 fwrite($hlog, $log);
197 append_claim($segs);
198 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
199 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
204 else if (isset($ar['bn_process_hcfa'])) {
205 $log = '';
206 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
207 fwrite($hlog, $log);
208 $alines = explode("\014", $lines); // form feeds may separate pages
209 foreach ($alines as $tmplines) {
210 if ($claim_count++) $pdf->ezNewPage();
211 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
212 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
214 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
215 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
219 else if (isset($ar['bn_hcfa_txt_file'])) {
220 $log = '';
221 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
222 fwrite($hlog, $log);
223 $bat_content .= $lines;
224 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
225 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
229 else {
230 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
235 } // end if this claim has billing
237 } // end foreach
239 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
240 append_claim_close();
241 fclose($hlog);
242 send_batch();
243 exit;
246 if (isset($ar['bn_process_hcfa'])) {
247 fclose($hlog);
248 // If a writable edi directory exists (and it should), write the pdf to it.
249 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
250 if ($fh) {
251 fwrite($fh, $pdf->ezOutput());
252 fclose($fh);
254 // Send the PDF download.
255 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
256 exit;
259 if (isset($ar['bn_hcfa_txt_file'])) {
260 fclose($hlog);
261 $fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
262 if ($fh) {
263 fwrite($fh, $bat_content);
264 fclose($fh);
266 header("Pragma: public");
267 header("Expires: 0");
268 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
269 header("Content-Type: application/force-download");
270 header("Content-Disposition: attachment; filename=$bat_filename");
271 header("Content-Description: File Transfer");
272 header("Content-Length: " . strlen($bat_content));
273 echo $bat_content;
274 exit;
277 if (isset($ar['bn_external'])) {
278 // Close external billing file.
279 $be->close();
283 <html>
284 <head>
285 <?php if (function_exists(html_header_show)) html_header_show(); ?>
287 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
289 </head>
290 <body class="body_top">
291 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
292 <?php
293 foreach ($bill_info as $infoline) {
294 echo nl2br($infoline);
297 </ul></p>
298 </body>
299 </html>