fix for IEA01
[openemr.git] / interface / billing / billing_process.php
blobc86ea898c45e7b253dbc590c1b1d55f8c1a04693
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 ++$bat_gscount;
64 $bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
65 "*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
66 continue;
68 if ($elems[0] == 'ST') {
69 ++$bat_stcount;
70 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
71 continue;
73 if ($elems[0] == 'SE') {
74 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
75 continue;
77 if ($elems[0] == 'GE' || $elems[0] == 'IEA') continue;
78 $bat_content .= $seg . '~';
82 function append_claim_close() {
83 global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
84 $bat_content .= "GE*$bat_stcount*1~IEA*$bat_gscount*$bat_icn~";
87 function send_batch() {
88 global $bat_content, $bat_filename, $webserver_root;
89 // If a writable edi directory exists, log the batch to it.
90 // I guarantee you'll be glad we did this. :-)
91 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
92 if ($fh) {
93 fwrite($fh, $bat_content);
94 fclose($fh);
96 header("Pragma: public");
97 header("Expires: 0");
98 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
99 header("Content-Type: application/force-download");
100 header("Content-Disposition: attachment; filename=$bat_filename");
101 header("Content-Description: File Transfer");
102 header("Content-Length: " . strlen($bat_content));
103 echo $bat_content;
106 process_form($_POST);
108 function process_form($ar) {
109 global $bill_info, $webserver_root, $bat_filename, $pdf;
111 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter']) || isset($ar['bn_process_hcfa'])) {
112 $hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
115 if (isset($ar['bn_external'])) {
116 // Open external billing file for output.
117 $be = new BillingExport();
120 $db = $GLOBALS['adodb']['db'];
122 if (empty($ar['claims'])) {
123 $ar['claims'] = array();
125 $claim_count = 0;
126 foreach ($ar['claims'] as $claimid => $claim_array) {
128 $ta = split("-",$claimid);
129 $patient_id = $ta[0];
130 $encounter = $ta[1];
131 $payer_id = substr($claim_array['payer'], 1);
132 $payer_type = substr($claim_array['payer'], 0, 1);
133 $payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
135 if (isset($claim_array['bill'])) {
137 if (isset($ar['bn_external'])) {
138 // Write external claim.
139 $be->addClaim($patient_id, $encounter);
141 else {
142 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
143 $db->qstr($claim_array['partner']);
144 $result = $db->Execute($sql);
145 $target = "x12";
146 if ($result && !$result->EOF) {
147 $target = $result->fields['processing_format'];
151 $tmp = 1;
153 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
154 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
155 } else if (isset($ar['bn_process_hcfa'])) {
156 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
157 } else if (isset($ar['bn_mark'])) {
158 // $sql .= " billed = 1, ";
159 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
160 } else if (isset($ar['bn_reopen'])) {
161 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
162 } else if (isset($ar['bn_external'])) {
163 // $sql .= " billed = 1, ";
164 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
167 if (!$tmp) {
168 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
170 else {
171 if(isset($ar['bn_mark'])) {
172 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
175 else if (isset($ar['bn_reopen'])) {
176 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
179 else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
180 $log = '';
181 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
182 isset($ar['bn_x12_encounter'])));
183 fwrite($hlog, $log);
184 append_claim($segs);
185 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
186 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
191 else if (isset($ar['bn_process_hcfa'])) {
192 $log = '';
193 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
194 fwrite($hlog, $log);
195 $alines = explode("\014", $lines); // form feeds may separate pages
196 foreach ($alines as $tmplines) {
197 if ($claim_count++) $pdf->ezNewPage();
198 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
199 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
201 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
202 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
206 else {
207 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
212 } // end if this claim has billing
214 } // end foreach
216 if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
217 append_claim_close();
218 fclose($hlog);
219 send_batch();
220 exit;
223 if (isset($ar['bn_process_hcfa'])) {
224 fclose($hlog);
225 // If a writable edi directory exists (and it should), write the pdf to it.
226 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
227 if ($fh) {
228 fwrite($fh, $pdf->ezOutput());
229 fclose($fh);
231 // Send the PDF download.
232 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
233 exit;
236 if (isset($ar['bn_external'])) {
237 // Close external billing file.
238 $be->close();
242 <html>
243 <head>
244 <?php if (function_exists(html_header_show)) html_header_show(); ?>
246 <link rel="stylesheet" href="<?echo $css_header;?>" type="text/css">
248 </head>
249 <body class="body_top">
250 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
251 <?php
252 foreach ($bill_info as $infoline) {
253 echo nl2br($infoline);
256 </ul></p>
257 </body>
258 </html>