added VIS statement date to immunizations
[openemr.git] / interface / billing / billing_process.php
blob1ab8620d875e831b58a05917e22a61a5cb5c8445
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 // This is a kludge to enter some parameters that are not in the X12
18 // Partners table, but should be.
20 // The following works for Zirmed:
21 $ISA07 = 'ZZ'; // ZZ = mutually defined, 01 = Duns, etc.
22 $ISA14 = '0'; // 1 = Acknowledgment requested, else 0
23 $ISA15 = 'P'; // T = testing, P = production
24 $GS02 = ''; // Empty to use the sender ID from the ISA segment
25 $PER06 = ''; // The submitter's EDI Access Number, if any
26 /**** For Availity:
27 $ISA07 = '01'; // ZZ = mutually defined, 01 = Duns, etc.
28 $ISA14 = '1'; // 1 = Acknowledgment requested, else 0
29 $ISA15 = 'T'; // T = testing, P = production
30 $GS02 = 'AV01101957';
31 $PER06 = 'xxxxxx'; // The submitter's EDI Access Number
32 ****/
34 $fconfig = $GLOBALS['oer_config']['freeb'];
35 $bill_info = array();
37 $bat_type = ''; // will be edi or hcfa
38 $bat_sendid = '';
39 $bat_recvid = '';
40 $bat_content = '';
41 $bat_stcount = 0;
42 $bat_time = time();
43 $bat_hhmm = date('Hi' , $bat_time);
44 $bat_yymmdd = date('ymd', $bat_time);
45 $bat_yyyymmdd = date('Ymd', $bat_time);
46 // Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
47 $bat_icn = sprintf('%09.0f', $bat_time/60);
48 $bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
49 $bat_filename .= isset($_POST['bn_process_hcfa']) ? 'pdf' : 'txt';
51 if (isset($_POST['bn_process_hcfa'])) {
52 $pdf =& new Cezpdf('LETTER');
53 $pdf->ezSetMargins(trim($_POST['top_margin'])+0,0,trim($_POST['left_margin'])+0,0);
54 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Courier.afm");
57 function append_claim(&$segs) {
58 global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
59 global $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
60 global $GS02, $ISA07, $ISA14, $ISA15, $PER06;
62 foreach ($segs as $seg) {
63 if (!$seg) continue;
64 $elems = explode('*', $seg);
65 if ($elems[0] == 'ISA') {
66 if (!$bat_content) {
67 $bat_sendid = trim($elems[6]);
68 $bat_recvid = trim($elems[8]);
69 $bat_sender = $GS02 ? $GS02 : $bat_sendid;
70 $bat_content = substr($seg, 0, 51) .
71 $ISA07 . substr($seg, 53, 17) .
72 "$bat_yymmdd*$bat_hhmm*U*00401*$bat_icn*$ISA14*$ISA15*:~" .
73 "GS*HC*$bat_sender*$bat_recvid*$bat_yyyymmdd*$bat_hhmm*1*X*004010X098A1~";
75 continue;
76 } else if (!$bat_content) {
77 die("Error:<br>\nInput must begin with 'ISA'; " .
78 "found '" . htmlentities($elems[0]) . "' instead");
80 if ($elems[0] == 'ST') {
81 ++$bat_stcount;
82 $bat_content .= sprintf("ST*837*%04d~", $bat_stcount);
83 continue;
85 if ($elems[0] == 'SE') {
86 $bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
87 continue;
89 if ($elems[0] == 'GS' || $elems[0] == 'GE' || $elems[0] == 'IEA') continue;
90 if ($elems[0] == 'PER' && $PER06 && !$elems[5]) {
91 $seg .= "*ED*$PER06";
93 $bat_content .= $seg . '~';
97 function append_claim_close() {
98 global $bat_content, $bat_stcount, $bat_icn;
99 $bat_content .= "GE*$bat_stcount*1~IEA*1*$bat_icn~";
102 function send_batch() {
103 global $bat_content, $bat_filename, $webserver_root;
104 // If a writable edi directory exists, log the batch to it.
105 // I guarantee you'll be glad we did this. :-)
106 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
107 if ($fh) {
108 fwrite($fh, $bat_content);
109 fclose($fh);
111 header("Pragma: public");
112 header("Expires: 0");
113 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
114 header("Content-Type: application/force-download");
115 header("Content-Disposition: attachment; filename=$bat_filename");
116 header("Content-Description: File Transfer");
117 header("Content-Length: " . strlen($bat_content));
118 echo $bat_content;
121 process_form($_POST);
123 function process_form($ar) {
124 global $bill_info, $webserver_root, $bat_filename, $pdf;
126 if (isset($ar['bn_x12']) || isset($ar['bn_process_hcfa'])) {
127 $hlog = fopen("$webserver_root/library/freeb/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 = split("-",$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;
168 if (isset($ar['bn_x12'])) {
169 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
170 } else if (isset($ar['bn_process_hcfa'])) {
171 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
172 } else if (isset($ar['bn_mark'])) {
173 // $sql .= " billed = 1, ";
174 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
175 } else if (isset($ar['bn_reopen'])) {
176 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
177 } else if (isset($ar['bn_external'])) {
178 // $sql .= " billed = 1, ";
179 $tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
182 if (!$tmp) {
183 die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
185 else {
186 if(isset($ar['bn_mark'])) {
187 $bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
190 else if (isset($ar['bn_reopen'])) {
191 $bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
194 else if (isset($ar['bn_x12'])) {
195 $log = '';
196 $segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log));
197 fwrite($hlog, $log);
198 append_claim($segs);
199 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
200 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
205 else if (isset($ar['bn_process_hcfa'])) {
206 $log = '';
207 $lines = gen_hcfa_1500($patient_id, $encounter, $log);
208 fwrite($hlog, $log);
209 $alines = explode("\014", $lines); // form feeds may separate pages
210 foreach ($alines as $tmplines) {
211 if ($claim_count++) $pdf->ezNewPage();
212 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
213 $pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
215 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
216 $bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
220 else {
221 $bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
226 } // end if this claim has billing
228 } // end foreach
230 if (isset($ar['bn_x12'])) {
231 append_claim_close();
232 fclose($hlog);
233 send_batch();
234 exit;
237 if (isset($ar['bn_process_hcfa'])) {
238 fclose($hlog);
239 // If a writable edi directory exists (and it should), write the pdf to it.
240 $fh = @fopen("$webserver_root/edi/$bat_filename", 'a');
241 if ($fh) {
242 fwrite($fh, $pdf->ezOutput());
243 fclose($fh);
245 // Send the PDF download.
246 $pdf->ezStream(array('Content-Disposition' => $bat_filename));
247 exit;
250 if (isset($ar['bn_external'])) {
251 // Close external billing file.
252 $be->close();
256 <html>
257 <head>
258 <?php if (function_exists(html_header_show)) html_header_show(); ?>
260 <link rel="stylesheet" href="<?echo $css_header;?>" type="text/css">
262 </head>
263 <body class="body_top">
264 <br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
265 <?php
266 foreach ($bill_info as $infoline) {
267 echo nl2br($infoline);
270 </ul></p>
271 </body>
272 </html>