forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
billing_report.php
502 lines (458 loc) · 17.5 KB
/
billing_report.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?
include_once("../globals.php");
include_once("../../library/acl.inc");
include_once("$srcdir/patient.inc");
include_once("$srcdir/billrep.inc");
include_once(dirname(__FILE__) . "/../../library/classes/OFX.class.php");
include_once(dirname(__FILE__) . "/../../library/classes/X12Partner.class.php");
$alertmsg = '';
if ($_POST['mode'] == 'export') {
$sdate = $_POST['from_date'];
$edate = $_POST['to_date'];
$sql = "SELECT billing.*, concat(pd.fname, ' ', pd.lname) as name from billing "
. "join patient_data as pd on pd.pid = billing.pid where billed = '1' and "
. "(process_date > '" . mysql_real_escape_string($sdate)
. "' or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($sdate) ."') "
. "and (process_date < '" . mysql_real_escape_string($edate)
. "'or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($edate) ."') "
. "order by pid,encounter";
$db = get_db();
$results = $db->Execute($sql);
$billings = array();
if ($results->RecordCount() == 0) {
echo "No Bills Found to Include in OFX Export<br>";
}
else {
while(!$results->EOF) {
$billings[] = $results->fields;
$results->MoveNext();
}
$ofx = new OFX($billings);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=openemr_ofx.ofx");
header("Content-Type: text/xml");
echo $ofx->get_OFX();
exit;
}
}
if ($_POST['mode'] == 'process') {
if (exec("ps x | grep 'process_bills[.]php'")) {
$alertmsg = 'Request ignored - claims processing is already running!';
}
else {
exec("cd $webserver_root/library/freeb;" .
"php -q process_bills.php bill > process_bills.log 2>&1 &");
$alertmsg = 'Batch processing initiated; this may take a while.';
}
}
//global variables:
if (!isset($_POST["mode"])) {
if (!isset($_POST["from_date"])) {
$from_date=date("Y-m-d");
} else {
$from_date = $_POST["from_date"];
}
if (!isset($_POST["to_date"])) {
$to_date = date("Y-m-d");
} else {
$to_date = $_POST["to_date"];
}
if (!isset($_POST["code_type"])) {
$code_type="all";
} else {
$code_type = $_POST["code_type"];
}
if (!isset($_POST["unbilled"])) {
$unbilled = "on";
} else {
$unbilled = $_POST["unbilled"];
}
if (!isset($_POST["authorized"])) {
$my_authorized = "on";
} else {
$my_authorized = $_POST["authorized"];
}
} else {
$from_date = $_POST["from_date"];
$to_date = $_POST["to_date"];
$code_type = $_POST["code_type"];
$unbilled = $_POST["unbilled"];
$my_authorized = $_POST["authorized"];
}
$ofrom_date = $from_date;
$oto_date = $to_date;
$ocode_type = $code_type;
$ounbilled = $unbilled;
$oauthorized = $my_authorized;
?>
<html>
<head>
<link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
<script>
function select_all() {
for($i=0;$i < document.update_form.length;$i++) {
$name = document.update_form[$i].name;
if ($name.substring(0,7) == "claims[" && $name.substring($name.length -6) == "[bill]") {
document.update_form[$i].checked = true;
}
}
set_button_states();
}
function set_button_states() {
var f = document.update_form;
var count0 = 0;
var count1 = 0;
var count2 = 0;
for($i = 0; $i < f.length; ++$i) {
$name = f[$i].name;
if ($name.substring(0, 7) == "claims[" && $name.substring($name.length -6) == "[bill]" && f[$i].checked == true) {
if (f[$i].value == '0') ++count0;
else if (f[$i].value == '1' || f[$i].value == '5') ++count1;
else ++count2;
}
}
var can_generate = (count0 > 0 || count1 > 0 || count2 > 0);
var can_mark = (count1 == 0 && (count0 > 0 || count2 > 0));
var can_bill = (count0 == 0 && count1 == 0 && count2 > 0);
f.bn_hcfa_print.disabled = !can_generate;
f.bn_hcfa.disabled = !can_generate;
f.bn_x12.disabled = !can_generate;
f.bn_mark.disabled = !can_mark;
f.bn_electronic_file.disabled = !can_bill;
}
</script>
</head>
<body <?echo $top_bg_line;?> topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
<p style='margin-top:5px;margin-bottom:5px;margin-left:5px'>
<?
if ($userauthorized) {
?>
<a href="../main/main.php" target=Main><font class=title>Billing Report</font><font class=more> <?echo $tback;?></font></a>
<?} else {?>
<a href="../main/onotes/office_comments.php" target=Main><font class=title>Billing Report</font><font class=more><?echo $tback;?></font></a>
<?
}
?>
</p>
<form name=the_form method=post action=billing_report.php>
<input type=hidden name=mode value="change">
<table width=100% border="1" cellspacing="0" cellpadding="0">
<tr>
<td nowrap> <span class=text>From: </span><input type=entry name=from_date size=11 value="<?echo $from_date;?>"></td>
<td nowrap> <span class=text>To: </span><input type=entry name=to_date size=11 value="<?echo $to_date;?>">
<input type="hidden" name="code_type" value="%"></td>
<td nowrap><input type=checkbox name=unbilled <?if ($unbilled == "on") {echo "checked";};?>><span class=text>Show Unbilled Only</span></td>
<td nowrap><input type=checkbox name=authorized <?if ($my_authorized == "on") {echo "checked";};?>><span class=text>Show Authorized Only</span></td>
<td align='right' width='10%' nowrap>
<span class=text><a href="javascript:document.the_form.mode.value='change';document.the_form.submit()" class=link_submit>[Change View]</a>
or
<a href="javascript:document.the_form.mode.value='export';document.the_form.submit()" class=link_submit>[Export OFX]</a></span>
</td>
</tr>
<tr>
<td nowrap> <a href="print_billing_report.php?<?print "from_date=".urlencode($ofrom_date)."&to_date=".urlencode($oto_date)."&code_type=".urlencode($ocode_type)."&unbilled=".urlencode($ounbilled)."&authorized=".urlencode($oauthorized);?>" class=link_submit target=new>[View Printable Report]</a></td>
<td nowrap>
<?php
print ' ';
$acct_config = $GLOBALS['oer_config']['ws_accounting'];
if($acct_config['enabled'] == true) {
print '<span class=text><a href="javascript:void window.open(\''.$acct_config['url_path'].'\')">[SQL-Ledger]</a></span>';
if (acl_check('acct', 'rep')) {
print '<span class=text> <a href="javascript:void window.open(\'sl_receipts_report.php\')">[Reports]</a></span>';
}
if (acl_check('acct', 'eob')) {
print '<span class=text> <a href="javascript:void window.open(\'sl_eob_search.php\')">[EOBs]</a></span>';
}
}
?>
</td>
<td colspan='2' nowrap>
<a href="javascript:document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
title="Process all queued bills to create electronic data (and print if requested)">[Start Batch Processing]</a>
<a href='../../library/freeb/process_bills.log' target='_blank' class='link_submit'
title='See messages from the last batch processing run'>[view log]</a></span>
</td>
<td align='right' nowrap>
<a href="javascript:select_all()" class="link_submit">[Select All]</a>
</td>
</tr>
</table>
</form>
<form name=update_form method=post action=billing_process.php>
<center>
<input type="submit" name="bn_hcfa_print" value="Generate HCFA & Print" title="Queue for HCFA batch processing and printing">
<input type="submit" name="bn_hcfa" value="Generate HCFA" title="Queue for HCFA batch processing">
<input type="submit" name="bn_x12" value="Generate X12" title="Queue for X12 batch processing">
<input type="submit" name="bn_mark" value="Mark Bills as Cleared" title="Post to accounting and mark as billed">
<input type="submit" name="bn_electronic_file" value="Generate Electronic Batch & Clear" title="Download billing file, post to accounting and mark as billed">
</center>
<input type=hidden name=mode value="bill">
<input type=hidden name=authorized value="<?echo $my_authorized;?>">
<input type=hidden name=unbilled value="<?echo $unbilled;?>">
<input type=hidden name=code_type value="%">
<input type=hidden name=to_date value="<?echo $to_date;?>">
<input type=hidden name=from_date value="<?echo $from_date;?>">
<?
if ($my_authorized == "on" ) {
$my_authorized = "1";
} else {
$my_authorized = "%";
}
if ($unbilled == "on") {
$unbilled = "0";
} else {
$unbilled = "%";
}
?>
<input type=hidden name=bill_list value="<?
$list = getBillsListBetween($from_date,$to_date,$my_authorized,$unbilled,"%");
print $list;
?>">
<!-- new form for uploading -->
<?php
if (!isset($_POST["mode"])) {
if (!isset($_POST["from_date"])) {
$from_date=date("Y-m-d");
} else {
$from_date = $_POST["from_date"];
}
if (!isset($_POST["to_date"])) {
$to_date = date("Y-m-d");
} else {
$to_date = $_POST["to_date"];
}
if (!isset($_POST["code_type"])) {
$code_type="all";
} else {
$code_type = $_POST["code_type"];
}
if (!isset($_POST["unbilled"])) {
$unbilled = "on";
} else {
$unbilled = $_POST["unbilled"];
}
if (!isset($_POST["authorized"])) {
$my_authorized = "on";
} else {
$my_authorized = $_POST["authorized"];
}
} else {
$from_date = $_POST["from_date"];
$to_date = $_POST["to_date"];
$code_type = $_POST["code_type"];
$unbilled = $_POST["unbilled"];
$my_authorized = $_POST["authorized"];
}
if ($my_authorized == "on" ) {
$my_authorized = "1";
} else {
$my_authorized = "%";
}
if ($unbilled == "on") {
$unbilled = "0";
} else {
$unbilled = "%";
}
?>
<?
if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
billCodesList($list);
}
?>
<p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<?
if ($ret = getBillsBetween($from_date,$to_date,$my_authorized,$unbilled,"%")) {
$loop = 0;
$oldcode = "";
$last_encounter_id = "";
$lhtml = "";
$rhtml = "";
$lcount = 0;
$rcount = 0;
$bgcolor = "";
$skipping = FALSE;
foreach ($ret as $iter) {
$this_encounter_id = $iter['pid'] . "-" . $iter['encounter'];
if ($last_encounter_id != $this_encounter_id) {
if ($lhtml) {
while ($rcount < $lcount) {
$rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
++$rcount;
}
echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
}
$lhtml = "";
$rhtml = "";
// If there are ANY unauthorized items in this encounter and this is
// the normal case of viewing only authorized billing, then skip the
// entire encounter.
//
$skipping = FALSE;
if ($my_authorized == '1') {
$res = sqlQuery("select count(*) as count from billing where " .
"encounter = '" . $iter['encounter'] . "' and " .
"pid='" . $iter['pid'] . "' and " .
"activity = 1 and authorized = 0");
if ($res['count'] > 0) {
$skipping = TRUE;
$last_encounter_id = $this_encounter_id;
continue;
}
}
$name = getPatientData($iter['pid'], "fname, mname, lname");
# Check if patient has primary insurance and a subscriber exists for it.
# If not we will highlight their name in red.
# TBD: more checking here.
#
$res = sqlQuery("select count(*) as count from insurance_data where " .
"pid = " . $iter['pid'] . " and " .
"type='primary' and " .
"subscriber_lname is not null and " .
"subscriber_lname != '' limit 1");
$namecolor = ($res['count'] > 0) ? "black" : "#ff7777";
++$encount;
$bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
$lcount = 3;
$rcount = 0;
$oldcode = "";
$lhtml .= " <span class=bold><font color='$namecolor'>". $name['fname'] . " " . $name['lname'] . "</font></span><span class=small> (" . $iter['pid'] . "-" . $iter['encounter'] . ")</span>";
$lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/encounter/patient_encounter.php?set_encounter=" . $iter['encounter'] . "&pid=" . $iter['pid'] . "\">[To Encounter]</a>";
$lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/summary/demographics_full.php?&pid=" . $iter['pid'] . "\">[To Demographics]</a>";
$lhtml .= "<br />\n";
$lhtml .= " <span class=text>Bill: ";
$lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
$query = "SELECT id.provider as id, id.type, ic.x12_default_partner_id as ic_x12id, ic.name as provider FROM insurance_data as id, insurance_companies as ic WHERE ic.id = id.provider AND pid = '" . mysql_escape_string($iter['pid']) . "' order by type";
$result = sqlStatement($query);
$count = 0;
$default_x12_partner = $iter['ic_x12id'];
while ($row = mysql_fetch_array($result)) {
if (strlen($row['provider']) > 0) {
if ($count == 0) {
$lhtml .= "<option value=\"" .$row['id'] . "\" selected>" . $row['type'] . ": " . $row['provider']. "</option>";
if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
}
else {
$lhtml .= "<option value=\"" . $row['id'] . "\">" . $row['type'] . ": " . $row['provider']. "</option>";
}
}
$count++;
}
$lhtml .= "<option value='-1'>Unassigned</option>\n";
$lhtml .= "</select> \n";
$lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
$x = new X12Partner();
$partners = $x->_utility_array($x->x12_partner_factory());
foreach ($partners as $xid => $xname) {
$lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
if ($xid == $default_x12_partner) {
$lhtml .= "selected";
}
$lhtml .= '>' . $xname . '</option>';
}
$lhtml .= "</select>";
$lhtml .= "<br>\n Claim was initiated: " . $iter['date'];
if ($iter['billed'] == 1) {
$lhtml .= "<br>\n Claim was billed: " . $iter['bill_date'];
++$lcount;
}
if ($iter['bill_process'] == 1) {
$lhtml .= "<br>\n Claim is queued for processing";
++$lcount;
}
if ($iter['bill_process'] == 5) {
$lhtml .= "<br>\n Claim is queued for printing and processing";
++$lcount;
}
if ($iter['bill_process'] == 2) {
$lhtml .= "<br>\n Claim was processed: " . $iter['process_date'];
$lhtml .= '<br>' . "\n" . ' Claim is in file: <a href="get_claim_file.php?key=' . $iter['process_file'] .'">' . $iter['process_file'] . '</a> or ';
$lhtml .= '<a href="get_claim_file.php?action=print&key=' . $iter['process_file'] .'">Print It</a> or ';
$lhtml .= '<a target="_new" href="freebtest.php?format=' . $iter['target'] . '&billkey=' . $iter['pid'] . '-' . $iter['encounter'] . '">Run Test</a>';
$lhtml .= '<input type="hidden" name="claims[' . $this_encounter_id . '][file]" value="' . $iter['process_file'] . '">';
$lcount += 2;
}
if ($iter['bill_process'] == 3) {
$lhtml .= "<br>\n Claim was processed: " . $iter['process_date'] . " but there was an error: ". $iter['process_file'];
++$lcount;
}
}
if ($skipping) continue;
++$rcount;
if ($rhtml) {
$rhtml .= "<tr bgcolor='$bgcolor'>\n";
}
$rhtml .= "<td width='50'>";
if ($oldcode != $iter['code_type']) {
$rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
}
$oldcode = $iter['code_type'];
$rhtml .= "</td>\n";
$justify = "";
if ($iter['code_type'] == "CPT4" || $iter['code_type'] == "HCPCS") {
$js = split(":",$iter['justify']);
$counter = 0;
foreach ($js as $j) {
if(!empty($j)) {
if ($counter == 0) {
$justify .= " (<b>$j</b>)";
}
else {
$justify .= " ($j)";
}
$counter++;
}
}
}
$rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
$rhtml .= '<td align="right"><span style="font-size:8pt;"> ';
if ($iter['fee'] > 0) {
$rhtml .= '$' . $iter['fee'];
}
$rhtml .= "</span></td>\n";
$rhtml .= '<td><span style="font-size:8pt;"> ';
$rhtml .= getProviderName($iter['provider_id']);
$rhtml .= "</span></td>\n";
$rhtml .= '<td width=100> <span style="font-size:8pt;">' . date("Y-m-d",strtotime($iter{"date"})) . "</span></td>\n";
if ($iter['authorized'] != 1) {
$rhtml .= "<td><span class=alert>Note: This code was not entered by an authorized user. Only authorized codes may be uploaded to the Open Medical Billing Network for processing. If you wish to upload these codes, please select an authorized user here.</span></td>\n";
}
else {
$rhtml .= "<td></td>\n";
}
if ($last_encounter_id != $this_encounter_id) {
$rhtml .= "<td><input type='checkbox' value='" . $iter['bill_process'] . "$procstatus' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'> </td>\n";
}
else {
$rhtml .= "<td></td>\n";
}
$rhtml .= "</tr>\n";
$last_encounter_id = $this_encounter_id;
}
if ($lhtml) {
while ($rcount < $lcount) {
$rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
++$rcount;
}
echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
}
}
?>
</table>
</form>
<script>
set_button_states();
<?
if ($alertmsg) {
echo "alert('$alertmsg');\n";
}
?>
</script>
</body>
</html>