forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
billing_process.php
299 lines (264 loc) · 10 KB
/
billing_process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
include_once("../globals.php");
include_once("$srcdir/patient.inc");
include_once("$srcdir/billrep.inc");
include_once("$srcdir/billing.inc");
include_once("$srcdir/gen_x12_837.inc.php");
include_once("$srcdir/gen_hcfa_1500.inc.php");
include_once(dirname(__FILE__) . "/../../library/classes/WSClaim.class.php");
require_once("$srcdir/classes/class.ezpdf.php");
$EXPORT_INC = "$webserver_root/custom/BillingExport.php";
if (file_exists($EXPORT_INC)) {
include_once($EXPORT_INC);
$BILLING_EXPORT = true;
}
$fconfig = $GLOBALS['oer_config']['freeb'];
$bill_info = array();
$bat_type = ''; // will be edi or hcfa
$bat_sendid = '';
$bat_recvid = '';
$bat_content = '';
$bat_gscount = 0;
$bat_stcount = 0;
$bat_time = time();
$bat_hhmm = date('Hi' , $bat_time);
$bat_yymmdd = date('ymd', $bat_time);
$bat_yyyymmdd = date('Ymd', $bat_time);
// Minutes since 1/1/1970 00:00:00 GMT will be our interchange control number:
$bat_icn = sprintf('%09.0f', $bat_time/60);
$bat_filename = date("Y-m-d-Hi", $bat_time) . "-batch.";
$bat_filename .= isset($_POST['bn_process_hcfa']) ? 'pdf' : 'txt';
if (isset($_POST['bn_process_hcfa'])) {
$pdf =& new Cezpdf('LETTER');
$pdf->ezSetMargins(trim($_POST['top_margin'])+0,0,trim($_POST['left_margin'])+0,0);
$pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Courier.afm");
}
function append_claim(&$segs) {
global $bat_content, $bat_sendid, $bat_recvid, $bat_sender, $bat_stcount;
global $bat_gscount, $bat_yymmdd, $bat_yyyymmdd, $bat_hhmm, $bat_icn;
foreach ($segs as $seg) {
if (!$seg) continue;
$elems = explode('*', $seg);
if ($elems[0] == 'ISA') {
if (!$bat_content) {
$bat_sendid = trim($elems[6]);
$bat_recvid = trim($elems[8]);
$bat_sender = $GS02 ? $GS02 : $bat_sendid;
$bat_content = substr($seg, 0, 70) .
"$bat_yymmdd*$bat_hhmm*" . $elems[11] . "*" . $elems[12] . "*$bat_icn*" .
$elems[14] . "*" . $elems[15] . "*:~";
}
continue;
} else if (!$bat_content) {
die("Error:<br>\nInput must begin with 'ISA'; " .
"found '" . htmlentities($elems[0]) . "' instead");
}
if ($elems[0] == 'GS') {
if ($bat_gscount == 0) {
++$bat_gscount;
$bat_content .= "GS*HC*" . $elems[2] . "*" . $elems[3] .
"*$bat_yyyymmdd*$bat_hhmm*1*X*" . $elems[8] . "~";
}
continue;
}
if ($elems[0] == 'ST') {
++$bat_stcount;
$bat_content .= sprintf("ST*837*%04d", $bat_stcount);
if (!empty($elems[3])) $bat_content .= "*" . $elems[3];
$bat_content .= "~";
continue;
}
if ($elems[0] == 'SE') {
$bat_content .= sprintf("SE*%d*%04d~", $elems[1], $bat_stcount);
continue;
}
if ($elems[0] == 'GE' || $elems[0] == 'IEA') continue;
$bat_content .= $seg . '~';
}
}
function append_claim_close() {
global $bat_content, $bat_stcount, $bat_gscount, $bat_icn;
if ($bat_gscount) $bat_content .= "GE*$bat_stcount*1~";
$bat_content .= "IEA*$bat_gscount*$bat_icn~";
}
function send_batch() {
global $bat_content, $bat_filename, $webserver_root;
// If a writable edi directory exists, log the batch to it.
// I guarantee you'll be glad we did this. :-)
$fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
if ($fh) {
fwrite($fh, $bat_content);
fclose($fh);
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$bat_filename");
header("Content-Description: File Transfer");
header("Content-Length: " . strlen($bat_content));
echo $bat_content;
}
process_form($_POST);
function process_form($ar) {
global $bill_info, $webserver_root, $bat_filename, $pdf;
if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter']) || isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
$hlog = fopen("$webserver_root/library/freeb/process_bills.log", 'w');
}
if (isset($ar['bn_external'])) {
// Open external billing file for output.
$be = new BillingExport();
}
$db = $GLOBALS['adodb']['db'];
if (empty($ar['claims'])) {
$ar['claims'] = array();
}
$claim_count = 0;
foreach ($ar['claims'] as $claimid => $claim_array) {
$ta = split("-",$claimid);
$patient_id = $ta[0];
$encounter = $ta[1];
$payer_id = substr($claim_array['payer'], 1);
$payer_type = substr($claim_array['payer'], 0, 1);
$payer_type = $payer_type == 'T' ? 3 : $payer_type == 'S' ? 2 : 1;
if (isset($claim_array['bill'])) {
if (isset($ar['bn_external'])) {
// Write external claim.
$be->addClaim($patient_id, $encounter);
}
else {
$sql = "SELECT x.processing_format from x12_partners as x where x.id =" .
$db->qstr($claim_array['partner']);
$result = $db->Execute($sql);
$target = "x12";
if ($result && !$result->EOF) {
$target = $result->fields['processing_format'];
}
}
$tmp = 1;
if (isset($ar['HiddenMarkAsCleared']) && $ar['HiddenMarkAsCleared']=='yes')
{
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);// $sql .= " billed = 1, ";
}
if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', $target, $claim_array['partner']);
} else if (isset($ar['bn_process_hcfa']) || isset($ar['bn_hcfa_txt_file'])) {
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 1, '', 'hcfa');
} else if (isset($ar['bn_mark'])) {
// $sql .= " billed = 1, ";
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
} else if (isset($ar['bn_reopen'])) {
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 1, 0);
} else if (isset($ar['bn_external'])) {
// $sql .= " billed = 1, ";
$tmp = updateClaim(true, $patient_id, $encounter, $payer_id, $payer_type, 2);
}
if (!$tmp) {
die(xl("Claim ") . $claimid . xl(" update failed, not in database?"));
}
else {
if(isset($ar['bn_mark'])) {
$bill_info[] = xl("Claim ") . $claimid . xl(" was marked as billed only.") . "\n";
}
else if (isset($ar['bn_reopen'])) {
$bill_info[] = xl("Claim ") . $claimid . xl(" has been re-opened.") . "\n";
}
else if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
$log = '';
$segs = explode("~\n", gen_x12_837($patient_id, $encounter, $log,
isset($ar['bn_x12_encounter'])));
fwrite($hlog, $log);
append_claim($segs);
if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
$bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
}
}
else if (isset($ar['bn_process_hcfa'])) {
$log = '';
$lines = gen_hcfa_1500($patient_id, $encounter, $log);
fwrite($hlog, $log);
$alines = explode("\014", $lines); // form feeds may separate pages
foreach ($alines as $tmplines) {
if ($claim_count++) $pdf->ezNewPage();
$pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
$pdf->ezText($tmplines, 12, array('justification' => 'left', 'leading' => 12));
}
if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
$bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
}
}
else if (isset($ar['bn_hcfa_txt_file'])) {
$log = '';
$lines = gen_hcfa_1500($patient_id, $encounter, $log);
fwrite($hlog, $log);
$bat_content .= $lines;
if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, 2, $bat_filename)) {
$bill_info[] = xl("Internal error: claim ") . $claimid . xl(" not found!") . "\n";
}
}
else {
$bill_info[] = xl("Claim ") . $claimid . xl(" was queued successfully.") . "\n";
}
}
} // end if this claim has billing
} // end foreach
if (isset($ar['bn_x12']) || isset($ar['bn_x12_encounter'])) {
append_claim_close();
fclose($hlog);
send_batch();
exit;
}
if (isset($ar['bn_process_hcfa'])) {
fclose($hlog);
// If a writable edi directory exists (and it should), write the pdf to it.
$fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
if ($fh) {
fwrite($fh, $pdf->ezOutput());
fclose($fh);
}
// Send the PDF download.
$pdf->ezStream(array('Content-Disposition' => $bat_filename));
exit;
}
if (isset($ar['bn_hcfa_txt_file'])) {
fclose($hlog);
$fh = @fopen($GLOBALS['OE_SITE_DIR'] . "/edi/$bat_filename", 'a');
if ($fh) {
fwrite($fh, $bat_content);
fclose($fh);
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$bat_filename");
header("Content-Description: File Transfer");
header("Content-Length: " . strlen($bat_content));
echo $bat_content;
exit;
}
if (isset($ar['bn_external'])) {
// Close external billing file.
$be->close();
}
}
?>
<html>
<head>
<?php if (function_exists(html_header_show)) html_header_show(); ?>
<link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
</head>
<body class="body_top">
<br><p><h3><?php xl('Billing queue results:','e'); ?></h3><a href="billing_report.php">back</a><ul>
<?php
foreach ($bill_info as $infoline) {
echo nl2br($infoline);
}
?>
</ul></p>
</body>
</html>