add hcfa text forms by bo2999, tracker artifact 2770036
[openemr.git] / interface / billing / billing_process.php
blob8f30bdd5f27c7325bedc7090b737f40ceae59011
1 <?php
2 include_once("../globals.php");
3 include_once("$srcdir/patient.inc");
4 include_once("$srcdir/billrep.inc");
5 include_once("$srcdir/billing.inc");
6 include_once("$srcdir/gen_x12_837.inc.php");
7 include_once("$srcdir/gen_hcfa_1500.inc.php");
8 include_once(dirname(__FILE__) . "/../../library/classes/WSClaim.class.php");
9 require_once("$srcdir/classes/class.ezpdf.php");
11 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
12 if (file_exists($EXPORT_INC)) {
13 include_once($EXPORT_INC);
14 $BILLING_EXPORT = true;
17 $fconfig = $GLOBALS['oer_config']['freeb'];
18 $bill_info = array();
20 $bat_type = ''; // will be edi or hcfa
21 $bat_sendid = '';
22 $bat_recvid = '';
23 $bat_content = '';
24 $bat_gscount = 0;
25 $bat_stcount = 0;
26 $bat_time = time();
27 $bat_hhmm = date('Hi' , $bat_time);
28 $bat_yymmdd = date('ymd', $bat_time);
29 $bat_yyyymmdd = date('Ymd', $bat_time);
30 // Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
31 $bat_icn = sprintf('%09.0f', $bat_time/60);
32 $bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
33 $bat_filename .= isset($_POST['bn_process_hcfa']) ? 'pdf' : 'txt';
35 if (isset($_POST['bn_process_hcfa'])) {
36 $pdf =& new Cezpdf('LETTER');
37 $pdf->ezSetMargins(trim($_POST['top_margin'])+0,0,trim($_POST['left_margin'])+0,0);
38 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Courier.afm");
41 function append_claim(&$segs) {
42 global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
43 global $bat_gscount, $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
45 foreach ($segs as $seg) {
46 if (!$seg) continue;
47 $elems = explode('*', $seg);
48 if ($elems[0] == 'ISA') {
49 if (!$bat_content) {
50 $bat_sendid = trim($elems[6]);
51 $bat_recvid = trim($elems[8]);
52 $bat_sender = $GS02 ? $GS02 : $bat_sendid;
53 $bat_content = substr($seg, 0, 70) .
54 "$bat_yymmdd*$bat_hhmm*U*00401*$bat_icn*" .
55 $elems[14] . "*" . $elems[15] . "*:~";
57 continue;
58 } else if (!$bat_content) {
59 die("Error:<br>\nInput must begin with 'ISA'; " .
60 "found '" . htmlentities($elems[0]) . "' instead");
62 if ($elems[0] == 'GS') {
63 if ($bat_gscount == 0) {
64 ++$bat_gscount;
65 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
66 "*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
68 continue;
70 if ($elems[0] == 'ST') {
71 ++$bat_stcount;
72 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
73 continue;
75 if ($elems[0] == 'SE') {
76 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
77 continue;
79 if ($elems[0] == 'GE' || $elems[0] == 'IEA') continue;
80 $bat_content .= $seg . '~';
84 function append_claim_close() {
85 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
86 if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
87 $bat_content .= "IEA*$bat_gscount*$bat_icn~";
90 function send_batch() {
91 global $bat_content, $bat_filename, $webserver_root;
92 // If a writable edi directory exists, log the batch to it.
93 // I guarantee you'll be glad we did this. :-)
94 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
95 if ($fh) {
96 fwrite($fh, $bat_content);
97 fclose($fh);
99 header("Pragma: public");
100 header("Expires: 0");
101 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102 header("Content-Type: application/force-download");
103 header("Content-Disposition: attachment; filename=$bat_filename");
104 header("Content-Description: File Transfer");
105 header("Content-Length: " . strlen($bat_content));
106 echo $bat_content;
109 process_form($_POST);
111 function process_form($ar) {
112 global $bill_info, $webserver_root, $bat_filename, $pdf;
114 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter']) || isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
115 $hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
118 if (isset($ar['bn_external'])) {
119 // Open external billing file for output.
120 $be = new BillingExport();
123 $db = $GLOBALS['adodb']['db'];
125 if (empty($ar['claims'])) {
126 $ar['claims'] = array();
128 $claim_count = 0;
129 foreach ($ar['claims'] as $claimid => $claim_array) {
131 $ta = split("-",$claimid);
132 $patient_id = $ta[0];
133 $encounter = $ta[1];
134 $payer_id = substr($claim_array['payer'], 1);
135 $payer_type = substr($claim_array['payer'], 0, 1);
136 $payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
138 if (isset($claim_array['bill'])) {
140 if (isset($ar['bn_external'])) {
141 // Write external claim.
142 $be->addClaim($patient_id, $encounter);
144 else {
145 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
146 $db->qstr($claim_array['partner']);
147 $result = $db->Execute($sql);
148 $target = "x12";
149 if ($result && !$result->EOF) {
150 $target = $result->fields['processing_format'];
154 $tmp = 1;
156 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
157 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
158 } else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
159 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
160 } else if (isset($ar['bn_mark'])) {
161 // $sql .= " billed = 1, ";
162 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
163 } else if (isset($ar['bn_reopen'])) {
164 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
165 } else if (isset($ar['bn_external'])) {
166 // $sql .= " billed = 1, ";
167 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
170 if (!$tmp) {
171 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
173 else {
174 if(isset($ar['bn_mark'])) {
175 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
178 else if (isset($ar['bn_reopen'])) {
179 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
182 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
183 $log = '';
184 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
185 isset($ar['bn_x12_encounter'])));
186 fwrite($hlog, $log);
187 append_claim($segs);
188 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
189 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
194 else if (isset($ar['bn_process_hcfa'])) {
195 $log = '';
196 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
197 fwrite($hlog, $log);
198 $alines = explode("\014", $lines); // form feeds may separate pages
199 foreach ($alines as $tmplines) {
200 if ($claim_count++) $pdf->ezNewPage();
201 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
202 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
204 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
205 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
209 else if (isset($ar['bn_hcfa_txt_file'])) {
210 $log = '';
211 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
212 fwrite($hlog, $log);
213 $bat_content .= $lines;
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 {
220 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
225 } // end if this claim has billing
227 } // end foreach
229 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
230 append_claim_close();
231 fclose($hlog);
232 send_batch();
233 exit;
236 if (isset($ar['bn_process_hcfa'])) {
237 fclose($hlog);
238 // If a writable edi directory exists (and it should), write the pdf to it.
239 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
240 if ($fh) {
241 fwrite($fh, $pdf->ezOutput());
242 fclose($fh);
244 // Send the PDF download.
245 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
246 exit;
249 if (isset($ar['bn_hcfa_txt_file'])) {
250 fclose($hlog);
251 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
252 if ($fh) {
253 fwrite($fh, $bat_content);
254 fclose($fh);
256 header("Pragma: public");
257 header("Expires: 0");
258 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
259 header("Content-Type: application/force-download");
260 header("Content-Disposition: attachment; filename=$bat_filename");
261 header("Content-Description: File Transfer");
262 header("Content-Length: " . strlen($bat_content));
263 echo $bat_content;
264 exit;
267 if (isset($ar['bn_external'])) {
268 // Close external billing file.
269 $be->close();
273 <html>
274 <head>
275 <?php if (function_exists(html_header_show)) html_header_show(); ?>
277 <link rel="stylesheet" href="<?echo $css_header;?>" type="text/css">
279 </head>
280 <body class="body_top">
281 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
282 <?php
283 foreach ($bill_info as $infoline) {
284 echo nl2br($infoline);
287 </ul></p>
288 </body>
289 </html>