forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClaim.class.php
1483 lines (1254 loc) · 49 KB
/
Claim.class.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* Claim Class php
*
* claim.class.php contains function for processing claims
*
* The changes to this file as of November 16 2016 to include the exclusion of information from claims
* are covered under the terms of the Mozilla Public License, v. 2.0
*
* @copyright Copyright (C) 2015-2017 Terry Hill <[email protected]>
*
* Copyright (C) 2007-2009 Rod Roark <[email protected]>
*
* LICENSE: 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 3
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://opensource.org/licenses/gpl-license.php.
*
* LICENSE: This Source Code is subject to the terms of the Mozilla Public License, v. 2.0.
* See the Mozilla Public License for more details.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* @package LibreHealth EHR
* @author Rod Roark <[email protected]>
* @author Terry Hill <[email protected]>
* @link http://librehealth.io
*
* Please help the overall project by sending changes you make to the author and to the LibreEHR community.
*
*/
require_once(dirname(__FILE__) . "/classes/Address.class.php");
require_once(dirname(__FILE__) . "/classes/InsuranceCompany.class.php");
require_once(dirname(__FILE__) . "/invoice_summary.inc.php");
// This enforces the X12 Basic Character Set. Page A2.
//
function x12clean($str) {
return preg_replace('/[^A-Z0-9!"\\&\'()+,\\-.\\/;?= ]/', '', strtoupper($str));
}
// Make sure dates have no formatting and zero filled becomes blank
// Handles date time stamp formats as well
//
function cleanDate($date_field)
{
$cleandate = str_replace('-', '', substr($date_field, 0, 10));
if(substr_count($cleandate,'0')==8)
{
$cleandate='';
}
return ($cleandate);
}
class Claim {
var $pid; // patient id
var $encounter_id; // encounter id
var $procs; // array of procedure rows from billing table
var $diags; // array of icd9 codes from billing table
var $diagtype= "ICD9"; // diagnosis code_type.Assume ICD9 unless otherwise specified.
var $x12_partner; // row from x12_partners table
var $encounter; // row from form_encounter table
var $facility; // row from facility table
var $billing_facility; // row from facility table
var $provider; // row from users table (rendering provider)
var $referrer; // row from users table (referring provider)
var $supervisor; // row from users table (supervising provider)
var $ordering; // row from users table (ordering provider)
var $referring; // row from users table (referring provider from encounter)
var $contract; // row from users table (contract provider)
var $insurance_numbers; // row from insurance_numbers table for current payer
var $supervisor_numbers; // row from insurance_numbers table for current payer
var $patient_data; // row from patient_data table
var $billing_options; // row from form_misc_billing_options table
var $ub04_options; // row from form_ub04_billing_options table
var $invoice; // result from get_invoice_summary()
var $payers; // array of arrays, for all payers
var $copay; // total of copays from the ar_activity table
function loadPayerInfo(&$billrow) {
global $sl_err;
$encounter_date = substr($this->encounter['date'], 0, 10);
// Create the $payers array. This contains data for all insurances
// with the current one always at index 0, and the others in payment
// order starting at index 1.
//
$this->payers = array();
$this->payers[0] = array();
$query = "SELECT * FROM insurance_data WHERE " .
"pid = '{$this->pid}' AND " .
"date <= '$encounter_date' " .
"ORDER BY type ASC, date DESC";
$dres = sqlStatement($query);
$prevtype = '';
while ($drow = sqlFetchArray($dres)) {
if (strcmp($prevtype, $drow['type']) == 0) continue;
$prevtype = $drow['type'];
// Very important to look at entries with a missing provider because
// they indicate no insurance as of the given date.
if (empty($drow['provider'])) continue;
$ins = count($this->payers);
if ($drow['provider'] == $billrow['payer_id'] && empty($this->payers[0]['data'])) $ins = 0;
$crow = sqlQuery("SELECT * FROM insurance_companies WHERE " .
"id = '" . $drow['provider'] . "'");
$orow = new InsuranceCompany($drow['provider']);
$this->payers[$ins] = array();
$this->payers[$ins]['data'] = $drow;
$this->payers[$ins]['company'] = $crow;
$this->payers[$ins]['object'] = $orow;
}
// This kludge hands most cases of a rare ambiguous situation, where
// the primary insurance company is the same as the secondary. It seems
// nobody planned for that!
//
for ($i = 1; $i < count($this->payers); ++$i) {
if ($billrow['process_date'] &&
$this->payers[0]['data']['provider'] == $this->payers[$i]['data']['provider'])
{
$tmp = $this->payers[0];
$this->payers[0] = $this->payers[$i];
$this->payers[$i] = $tmp;
}
}
$this->using_modifiers = true;
// Get payment and adjustment details if there are any previous payers.
//
$this->invoice = array();
if ($this->payerSequence() != 'P') {
$this->invoice = ar_get_invoice_summary($this->pid, $this->encounter_id, true);
// Secondary claims might not have modifiers in SQL-Ledger data.
// In that case, note that we should not try to match on them.
$this->using_modifiers = false;
foreach ($this->invoice as $key => $trash) {
if (strpos($key, ':')) $this->using_modifiers = true;
}
}
}
// Constructor. Loads relevant database information.
//
function Claim($pid, $encounter_id) {
$this->pid = $pid;
$this->encounter_id = $encounter_id;
$this->procs = array();
$this->diags = array();
$this->copay = 0;
// We need the encounter date before we can identify the payers.
$sql = "SELECT * FROM form_encounter WHERE " .
"pid = '{$this->pid}' AND " .
"encounter = '{$this->encounter_id}'";
$this->encounter = sqlQuery($sql);
// Sort by procedure timestamp in order to get some consistency.
$sql = "SELECT b.id, b.date, b.code_type, b.code, b.pid, b.provider_id, " .
"b.user, b.groupname, b.authorized, b.encounter, b.code_text, b.billed, " .
"b.activity, b.payer_id, b.bill_process, b.bill_date, b.process_date, " .
"b.process_file, b.modifier, b.units, b.fee, b.justify, b.target, b.x12_partner_id, " .
"b.ndc_info, b.notecodes, ct.ct_diag, b.exclude_from_insurance_billing " .
"FROM billing as b INNER JOIN code_types as ct " .
"ON b.code_type = ct.ct_key " .
"WHERE ct.ct_claim = '1' AND ct.ct_active = '1' AND " .
"b.encounter = '{$this->encounter_id}' AND b.pid = '{$this->pid}' AND " .
"b.activity = '1' ORDER BY b.date, b.id";
$res = sqlStatement($sql);
while ($row = sqlFetchArray($res)) {
// Save all diagnosis codes.
if ($row['ct_diag'] == '1') {
if($row['exclude_from_insurance_billing'] == 1)continue;
$this->diags[$row['code']] = $row['code'];
continue;
}
if (!$row['units']) $row['units'] = 1;
// Load prior payer data at the first opportunity in order to get
// the using_modifiers flag that is referenced below.
if (empty($this->procs)) $this->loadPayerInfo($row);
// The consolidate duplicate procedures, which was previously here, was removed
// from codebase on 12/9/15. Reason: Some insurance companies decline consolidated
// procedures, and this can be left up to the billing coder when they input the items.
// If there is a row-specific provider then get its details.
if (!empty($row['provider_id'])) {
// Get service provider data for this row.
$sql = "SELECT * FROM users WHERE id = '" . $row['provider_id'] . "'";
$row['provider'] = sqlQuery($sql);
// Get insurance numbers for this row's provider.
$sql = "SELECT * FROM insurance_numbers WHERE " .
"(insurance_company_id = '" . $row['payer_id'] .
"' OR insurance_company_id is NULL) AND " .
"provider_id = '" . $row['provider_id'] . "' " .
"ORDER BY insurance_company_id DESC LIMIT 1";
$row['insurance_numbers'] = sqlQuery($sql);
}
$this->procs[] = $row;
}
$resMoneyGot = sqlStatement("SELECT pay_amount as PatientPay,session_id as id,".
"date(post_time) as date FROM ar_activity where pid ='{$this->pid}' and encounter ='{$this->encounter_id}' ".
"and payer_type=0 and account_code='PCP'");
//new fees screen copay gives account_code='PCP'
while($rowMoneyGot = sqlFetchArray($resMoneyGot)){
$PatientPay=$rowMoneyGot['PatientPay']*-1;
$this->copay -= $PatientPay;
}
$sql = "SELECT * FROM x12_partners WHERE " .
"id = '" . $this->procs[0]['x12_partner_id'] . "'";
$this->x12_partner = sqlQuery($sql);
$sql = "SELECT * FROM facility WHERE " .
"id = '" . addslashes($this->encounter['facility_id']) . "' " .
"LIMIT 1";
$this->facility = sqlQuery($sql);
/*****************************************************************
$provider_id = $this->procs[0]['provider_id'];
*****************************************************************/
$provider_id = $this->encounter['provider_id'];
$sql = "SELECT * FROM users WHERE id = '$provider_id'";
$this->provider = sqlQuery($sql);
// Selecting the billing facility assigned to the encounter. If none,
// try the first (and hopefully only) facility marked as a billing location.
if (empty($this->encounter['billing_facility'])) {
$sql = "SELECT * FROM facility " .
"ORDER BY billing_location, primary_business_entity DESC, id ASC LIMIT 1";
}
else {
$sql = "SELECT * FROM facility " .
" where id ='" . addslashes($this->encounter['billing_facility']) . "' ";
}
$this->billing_facility = sqlQuery($sql);
$sql = "SELECT * FROM insurance_numbers WHERE " .
"(insurance_company_id = '" . $this->procs[0]['payer_id'] .
"' OR insurance_company_id is NULL) AND " .
"provider_id = '$provider_id' " .
"ORDER BY insurance_company_id DESC LIMIT 1";
$this->insurance_numbers = sqlQuery($sql);
$sql = "SELECT * FROM patient_data WHERE " .
"pid = '{$this->pid}' " .
"ORDER BY id LIMIT 1";
$this->patient_data = sqlQuery($sql);
$sql = "SELECT fpa.* FROM forms JOIN form_misc_billing_options AS fpa " .
"ON fpa.id = forms.form_id WHERE " .
"forms.encounter = '{$this->encounter_id}' AND " .
"forms.pid = '{$this->pid}' AND " .
"forms.deleted = 0 AND " .
"forms.formdir = 'misc_billing_options' " .
"ORDER BY forms.date";
$this->billing_options = sqlQuery($sql);
if ($GLOBALS['claim_type'] =='1' || $GLOBALS['claim_type'] =='2') {
$sql = "SELECT fpa.* FROM forms JOIN form_UB04_billing_options AS fpa " .
"ON fpa.id = forms.form_id WHERE " .
"forms.encounter = '{$this->encounter_id}' AND " .
"forms.pid = '{$this->pid}' AND " .
"forms.deleted = 0 AND " .
"forms.formdir = 'ub04_billing_options' " .
"ORDER BY forms.date";
$this->ub04_options = sqlQuery($sql);
}
$referrer_id = (empty($GLOBALS['MedicareReferrerIsRenderer']) ||
$this->insurance_numbers['provider_number_type'] != '1C') ?
$this->patient_data['ref_providerID'] : $provider_id;
$sql = "SELECT * FROM users WHERE id = '$referrer_id'";
$this->referrer = sqlQuery($sql);
if (!$this->referrer) $this->referrer = array();
$supervisor_id = $this->encounter['supervisor_id'];
$sql = "SELECT * FROM users WHERE id = '$supervisor_id'";
$this->supervisor = sqlQuery($sql);
if (!$this->supervisor) $this->supervisor = array();
$ordering_id = $this->encounter['ordering_physician'];
$sql = "SELECT * FROM users WHERE id = '$ordering_id'";
$this->ordering = sqlQuery($sql);
if (!$this->ordering) $this->ordering = array();
$referring_id = $this->encounter['referring_physician'];
$sql = "SELECT * FROM users WHERE id = '$referring_id'";
$this->referring = sqlQuery($sql);
if (!$this->referring) $this->referring = array();
$contract_id = $this->encounter['contract_physician'];
$sql = "SELECT * FROM users WHERE id = '$contract_id'";
$this->contract = sqlQuery($sql);
if (!$this->contract) $this->contract = array();
$billing_options_id = $this->billing_options['provider_id'];
$sql = "SELECT * FROM users WHERE id = ?";
$this->billing_prov_id = sqlQuery($sql, array($billing_options_id));
if (!$this->billing_prov_id) $this->billing_prov_id = array();
$sql = "SELECT * FROM insurance_numbers WHERE " .
"(insurance_company_id = '" . $this->procs[0]['payer_id'] .
"' OR insurance_company_id is NULL) AND " .
"provider_id = '$supervisor_id' " .
"ORDER BY insurance_company_id DESC LIMIT 1";
$this->supervisor_numbers = sqlQuery($sql);
if (!$this->supervisor_numbers) $this->supervisor_numbers = array();
} // end constructor
// Return an array of adjustments from the designated prior payer for the
// designated procedure key (might be procedure:modifier), or for the claim
// level. For each adjustment give date, group code, reason code, amount.
// Note this will include "patient responsibility" adjustments which are
// not adjustments to OUR invoice, but they reduce the amount that the
// insurance company pays.
//
function payerAdjustments($ins, $code='Claim') {
$aadj = array();
// If we have no modifiers stored in SQL-Ledger for this claim,
// then we cannot use a modifier passed in with the key.
$tmp = strpos($code, ':');
if ($tmp && !$this->using_modifiers) $code = substr($code, 0, $tmp);
// For payments, source always starts with "Ins" or "Pt".
// Nonzero adjustment reason examples:
// Ins1 adjust code 42 (Charges exceed ... (obsolete))
// Ins1 adjust code 45 (Charges exceed your contracted/ legislated fee arrangement)
// Ins1 adjust code 97 (Payment is included in the allowance for another service/procedure)
// Ins1 adjust code A2 (Contractual adjustment)
// Ins adjust Ins1
// adjust code 45
// Zero adjustment reason examples:
// Co-pay: 25.00
// Coinsurance: 11.46 (code 2) Note: fix remits to identify insurance
// To deductible: 0.22 (code 1) Note: fix remits to identify insurance
// To copay Ins1 (manual entry)
// To ded'ble Ins1 (manual entry)
if (!empty($this->invoice[$code])) {
$date = '';
$deductible = 0;
$coinsurance = 0;
$inslabel = ($this->payerSequence($ins) == 'S') ? 'Ins2' : 'Ins1';
$insnumber = substr($inslabel, 3);
// Compute this procedure's patient responsibility amount as of this
// prior payer, which is the original charge minus all insurance
// payments and "hard" adjustments up to this payer.
$ptresp = $this->invoice[$code]['chg'] + $this->invoice[$code]['adj'];
foreach ($this->invoice[$code]['dtl'] as $key => $value) {
// plv (from ar_activity.payer_type) exists to
// indicate the payer level.
if (isset($value['pmt']) && $value['pmt'] != 0) {
if ($value['plv'] > 0 && $value['plv'] <= $insnumber)
$ptresp -= $value['pmt'];
}
else if (isset($value['chg']) && trim(substr($key, 0, 10))) {
// non-blank key indicates this is an adjustment and not a charge
if ($value['plv'] > 0 && $value['plv'] <= $insnumber)
$ptresp += $value['chg']; // adjustments are negative charges
}
$msp = isset( $value['msp'] ) ? $value['msp'] : null; // record the reason for adjustment
}
if ($ptresp < 0) $ptresp = 0; // we may be insane but try to hide it
// Main loop, to extract adjustments for this payer and procedure.
foreach ($this->invoice[$code]['dtl'] as $key => $value) {
$tmp = str_replace('-', '', trim(substr($key, 0, 10)));
if ($tmp) $date = $tmp;
if ($tmp && $value['pmt'] == 0) { // not original charge and not a payment
$rsn = $value['rsn'];
$chg = 0 - $value['chg']; // adjustments are negative charges
$gcode = 'CO'; // default group code = contractual obligation
$rcode = '45'; // default reason code = max fee exceeded (code 42 is obsolete)
if (preg_match("/Ins adjust $inslabel/i", $rsn, $tmp)) {
// From manual post. Take the defaults.
}
else if (preg_match("/To copay $inslabel/i", $rsn, $tmp) && !$chg) {
$coinsurance = $ptresp; // from manual post
continue;
}
else if (preg_match("/To ded'ble $inslabel/i", $rsn, $tmp) && !$chg) {
$deductible = $ptresp; // from manual post
continue;
}
else if (preg_match("/$inslabel copay: (\S+)/i", $rsn, $tmp) && !$chg) {
$coinsurance = $tmp[1]; // from 835 as of 6/2007
continue;
}
else if (preg_match("/$inslabel coins: (\S+)/i", $rsn, $tmp) && !$chg) {
$coinsurance = $tmp[1]; // from 835 and manual post as of 6/2007
continue;
}
else if (preg_match("/$inslabel dedbl: (\S+)/i", $rsn, $tmp) && !$chg) {
$deductible = $tmp[1]; // from 835 and manual post as of 6/2007
continue;
}
else if (preg_match("/$inslabel ptresp: (\S+)/i", $rsn, $tmp) && !$chg) {
continue; // from 835 as of 6/2007
}
else if (preg_match("/$inslabel adjust code (\S+)/i", $rsn, $tmp)) {
$rcode = $tmp[1]; // from 835
}
else if (preg_match("/$inslabel/i", $rsn, $tmp)) {
// Take the defaults.
}
else if (preg_match('/Ins(\d)/i', $rsn, $tmp) && $tmp[1] != $insnumber) {
continue; // it's for some other payer
}
else if ($insnumber == '1') {
if (preg_match("/\$\s*adjust code (\S+)/i", $rsn, $tmp)) {
$rcode = $tmp[1]; // from 835
}
else if ($chg) {
// Other adjustments default to Ins1.
}
else if (preg_match("/Co-pay: (\S+)/i", $rsn, $tmp) ||
preg_match("/Coinsurance: (\S+)/i", $rsn, $tmp)) {
$coinsurance = 0 + $tmp[1]; // from 835 before 6/2007
continue;
}
else if (preg_match("/To deductible: (\S+)/i", $rsn, $tmp)) {
$deductible = 0 + $tmp[1]; // from 835 before 6/2007
continue;
}
else {
continue; // there is no adjustment amount
}
}
else {
continue; // it's for primary and that's not us
}
if ($rcode == '42') $rcode= '45'; // reason 42 is obsolete
$aadj[] = array($date, $gcode, $rcode, sprintf('%.2f', $chg));
} // end if
} // end foreach
// If we really messed it up, at least avoid negative numbers.
if ($coinsurance > $ptresp) $coinsurance = $ptresp;
if ($deductible > $ptresp) $deductible = $ptresp;
// Find out if this payer paid anything at all on this claim. This will
// help us allocate any unknown patient responsibility amounts.
$thispaidanything = 0;
foreach($this->invoice as $codekey => $codeval) {
foreach ($codeval['dtl'] as $key => $value) {
// plv exists to indicate the payer level.
if ($value['plv'] == $insnumber) {
$thispaidanything += $value['pmt'];
}
}
}
// Allocate any unknown patient responsibility by guessing if the
// deductible has been satisfied.
if ($thispaidanything)
$coinsurance = $ptresp - $deductible;
else
$deductible = $ptresp - $coinsurance;
$deductible = sprintf('%.2f', $deductible);
$coinsurance = sprintf('%.2f', $coinsurance);
if ($date && $deductible != 0)
$aadj[] = array($date, 'PR', '1', $deductible, $msp);
if ($date && $coinsurance != 0)
$aadj[] = array($date, 'PR', '2', $coinsurance, $msp);
} // end if
return $aadj;
}
// Return date, total payments and total "hard" adjustments from the given
// prior payer. If $code is specified then only that procedure key is
// selected, otherwise it's for the whole claim.
//
function payerTotals($ins, $code='') {
// If we have no modifiers stored in SQL-Ledger for this claim,
// then we cannot use a modifier passed in with the key.
$tmp = strpos($code, ':');
if ($tmp && !$this->using_modifiers) $code = substr($code, 0, $tmp);
$inslabel = ($this->payerSequence($ins) == 'S') ? 'Ins2' : 'Ins1';
$insnumber = substr($inslabel, 3);
$paytotal = 0;
$adjtotal = 0;
$date = '';
foreach($this->invoice as $codekey => $codeval) {
if ($code && strcmp($codekey,$code) != 0) continue;
foreach ($codeval['dtl'] as $key => $value) {
// plv (from ar_activity.payer_type) exists to
// indicate the payer level.
if ($value['plv'] == $insnumber) {
if (!$date) $date = str_replace('-', '', trim(substr($key, 0, 10)));
$paytotal += $value['pmt'];
}
}
$aarr = $this->payerAdjustments($ins, $codekey);
foreach ($aarr as $a) {
if (strcmp($a[1],'PR') != 0) $adjtotal += $a[3];
if (!$date) $date = $a[0];
}
}
return array($date, sprintf('%.2f', $paytotal), sprintf('%.2f', $adjtotal));
}
// Return the amount already paid by the patient.
//
function patientPaidAmount() {
// For primary claims $this->invoice is not loaded, so get the co-pay
// from the ar_activity table instead.
if (empty($this->invoice)) return $this->copay;
//
$amount = 0;
foreach($this->invoice as $codekey => $codeval) {
foreach ($codeval['dtl'] as $key => $value) {
// plv exists to indicate the payer level.
if ($value['plv'] == 0) { // 0 indicates patient
$amount += $value['pmt'];
}
}
}
return sprintf('%.2f', $amount);
}
// Return invoice total, including adjustments but not payments.
//
function invoiceTotal() {
$amount = 0;
foreach($this->invoice as $codekey => $codeval) {
$amount += $codeval['chg'];
}
return sprintf('%.2f', $amount);
}
// Number of procedures in this claim.
function procCount() {
return count($this->procs);
}
// Number of payers for this claim. Ranges from 1 to 3.
function payerCount() {
return count($this->payers);
}
function x12gsversionstring() {
return x12clean(trim($this->x12_partner['x12_version']));
}
function x12gssenderid() {
$tmp = $this->x12_partner['x12_sender_id'];
while (strlen($tmp) < 15) $tmp .= " ";
return $tmp;
}
function x12gs03() {
/*
* GS03: Application Receiver's Code
* Code Identifying Party Receiving Transmission
*
* In most cases, the ISA08 and GS03 are the same. However
*
* In some clearing houses ISA08 and GS03 are different
* Example: http://www.acs-gcro.com/downloads/DOL/DOL_CG_X12N_5010_837_v1_02.pdf - Page 18
* In this .pdf, the ISA08 is specified to be 100000 while the GS03 is specified to be 77044
*
* Therefore if the x12_gs03 segement is explicitly specified we use that value,
* otherwise we simply use the same receiver ID as specified for ISA03
*/
if($this->x12_partner['x12_gs03'] !== '')
return $this->x12_partner['x12_gs03'];
else
return $this->x12_partner['x12_receiver_id'];
}
function x12gsreceiverid() {
$tmp = $this->x12_partner['x12_receiver_id'];
while (strlen($tmp) < 15) $tmp .= " ";
return $tmp;
}
function x12gsisa05() {
return $this->x12_partner['x12_isa05'];
}
//adding in functions for isa 01 - isa 04
function x12gsisa01() {
return $this->x12_partner['x12_isa01'];
}
function x12gsisa02() {
return $this->x12_partner['x12_isa02'];
}
function x12gsisa03() {
return $this->x12_partner['x12_isa03'];
}
function x12gsisa04() {
return $this->x12_partner['x12_isa04'];
}
/////////
function x12gsisa07() {
return $this->x12_partner['x12_isa07'];
}
function x12gsisa14() {
return $this->x12_partner['x12_isa14'];
}
function x12gsisa15() {
return $this->x12_partner['x12_isa15'];
}
function x12gsgs02() {
$tmp = $this->x12_partner['x12_gs02'];
if ($tmp === '') $tmp = $this->x12_partner['x12_sender_id'];
return $tmp;
}
function x12gsper06() {
return $this->x12_partner['x12_per06'];
}
function cliaCode() {
return x12clean(trim($this->facility['domain_identifier']));
}
function billingFacilityName() {
return x12clean(trim($this->billing_facility['alias']));
}
function billingFacilityStreet() {
return x12clean(trim($this->billing_facility['street']));
}
function billingFacilityCity() {
return x12clean(trim($this->billing_facility['city']));
}
function billingFacilityState() {
return x12clean(trim($this->billing_facility['state']));
}
function billingFacilityZip() {
return x12clean(trim($this->billing_facility['postal_code']));
}
function billingFacilityETIN() {
return x12clean(trim(str_replace('-', '', $this->billing_facility['federal_ein'])));
}
function billingFacilityNPI() {
return x12clean(trim($this->billing_facility['facility_npi']));
}
function excludeEntry($prockey = 0)
{
return $this->procs[$prockey]['exclude_from_insurance_billing'];
}
function federalIdType() {
if ($this->billing_facility['tax_id_type'])
{
return $this->billing_facility['tax_id_type'];
}
else{
return null;
}
}
# The billing facility and the patient must both accept for this to return true.
function billingFacilityAssignment($ins=0) {
$tmp = strtoupper($this->payers[$ins]['data']['accept_assignment']);
if (strcmp($tmp,'FALSE') == 0) return '0';
return !empty($this->billing_facility['accepts_assignment']);
}
function billingContactName() {
return x12clean(trim($this->billing_facility['attn']));
}
function billingContactPhone() {
if (preg_match("/([2-9]\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
$this->billing_facility['phone'], $tmp))
{
return $tmp[1] . $tmp[2] . $tmp[3];
}
return '';
}
function facilityName() {
return x12clean(trim($this->facility['name']));
}
function facilityStreet() {
return x12clean(trim($this->facility['street']));
}
function facilityCity() {
return x12clean(trim($this->facility['city']));
}
function facilityState() {
return x12clean(trim($this->facility['state']));
}
function facilityZip() {
return x12clean(trim($this->facility['postal_code']));
}
function facilityETIN() {
return x12clean(trim(str_replace('-', '', $this->facility['federal_ein'])));
}
function facilityNPI() {
return x12clean(trim($this->facility['facility_npi']));
}
function facilityPOS() {
return sprintf('%02d', trim($this->facility['pos_code']));
}
function clearingHouseName() {
return x12clean(trim($this->x12_partner['name']));
}
function clearingHouseETIN() {
return x12clean(trim(str_replace('-', '', $this->x12_partner['id_number'])));
}
function providerNumberType($prockey=-1) {
$tmp = ($prockey < 0 || empty($this->procs[$prockey]['provider_id'])) ?
$this->insurance_numbers : $this->procs[$prockey]['insurance_numbers'];
return $tmp['provider_number_type'];
}
function providerNumber($prockey=-1) {
$tmp = ($prockey < 0 || empty($this->procs[$prockey]['provider_id'])) ?
$this->insurance_numbers : $this->procs[$prockey]['insurance_numbers'];
return x12clean(trim(str_replace('-', '', $tmp['provider_number'])));
}
function providerGroupNumber($prockey=-1) {
$tmp = ($prockey < 0 || empty($this->procs[$prockey]['provider_id'])) ?
$this->insurance_numbers : $this->procs[$prockey]['insurance_numbers'];
return x12clean(trim(str_replace('-', '', $tmp['group_number'])));
}
// Returns 'P', 'S' or 'T'.
//
function payerSequence($ins=0) {
return strtoupper(substr($this->payers[$ins]['data']['type'], 0, 1));
}
// Returns the HIPAA code of the patient-to-subscriber relationship.
//
function insuredRelationship($ins=0) {
$tmp = strtolower($this->payers[$ins]['data']['subscriber_relationship']);
if (strcmp($tmp,'self' ) == 0) return '18';
if (strcmp($tmp,'spouse') == 0) return '01';
if (strcmp($tmp,'child' ) == 0) return '19';
if (strcmp($tmp,'other' ) == 0) return 'G8';
return $tmp; // should not happen
}
function insuredTypeCode($ins=0) {
if (strcmp($this->claimType($ins),'MB') == 0 && $this->payerSequence($ins) != 'P')
return $this->payers[$ins]['data']['policy_type'];
return '';
}
// Is the patient also the subscriber?
//
function isSelfOfInsured($ins=0) {
$tmp = strtolower($this->payers[$ins]['data']['subscriber_relationship']);
return (strcmp($tmp,'self') == 0);
}
function planName($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['plan_name']));
}
function policyNumber($ins=0) { // "ID"
return x12clean(trim($this->payers[$ins]['data']['policy_number']));
}
function groupNumber($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['group_number']));
}
function groupName($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_employer']));
}
// Claim types are:
// 16 Other HCFA
// MB Medicare Part B
// MC Medicaid
// CH ChampUSVA
// CH ChampUS
// BL Blue Cross Blue Shield
// 16 FECA
// 09 Self Pay
// 10 Central Certification
// 11 Other Non-Federal Programs
// 12 Preferred Provider Organization (PPO)
// 13 Point of Service (POS)
// 14 Exclusive Provider Organization (EPO)
// 15 Indemnity Insurance
// 16 Health Maintenance Organization (HMO) Medicare Risk
// AM Automobile Medical
// CI Commercial Insurance Co.
// DS Disability
// HM Health Maintenance Organization
// LI Liability
// LM Liability Medical
// OF Other Federal Program
// TV Title V
// VA Veterans Administration Plan
// WC Workers Compensation Health Plan
// ZZ Mutually Defined
//
function claimType($ins=0) {
if (empty($this->payers[$ins]['object'])) return '';
return $this->payers[$ins]['object']->get_ins_claim_type();
}
function claimTypeRaw($ins=0) {
if (empty($this->payers[$ins]['object'])) return 0;
return $this->payers[$ins]['object']->get_ins_type_code();
}
function insuredLastName($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_lname']));
}
function insuredFirstName($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_fname']));
}
function insuredMiddleName($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_mname']));
}
function insuredStreet($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_street']));
}
function insuredCity($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_city']));
}
function insuredState($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_state']));
}
function insuredZip($ins=0) {
return x12clean(trim($this->payers[$ins]['data']['subscriber_postal_code']));
}
function insuredPhone($ins=0) {
if (preg_match("/([2-9]\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
$this->payers[$ins]['data']['subscriber_phone'], $tmp))
return $tmp[1] . $tmp[2] . $tmp[3];
return '';
}
function insuredDOB($ins=0) {
return str_replace('-', '', $this->payers[$ins]['data']['subscriber_DOB']);
}
function insuredSex($ins=0) {
return strtoupper(substr($this->payers[$ins]['data']['subscriber_sex'], 0, 1));
}
function payerName($ins=0) {
return x12clean(trim($this->payers[$ins]['company']['name']));
}
function payerAttn($ins=0) {
return x12clean(trim($this->payers[$ins]['company']['attn']));
}
function payerStreet($ins=0) {
if (empty($this->payers[$ins]['object'])) return '';
$tmp = $this->payers[$ins]['object'];
$tmp = $tmp->get_address();
return x12clean(trim($tmp->get_line1()));
}
function payerCity($ins=0) {
if (empty($this->payers[$ins]['object'])) return '';
$tmp = $this->payers[$ins]['object'];
$tmp = $tmp->get_address();
return x12clean(trim($tmp->get_city()));
}
function payerState($ins=0) {
if (empty($this->payers[$ins]['object'])) return '';
$tmp = $this->payers[$ins]['object'];
$tmp = $tmp->get_address();
return x12clean(trim($tmp->get_state()));
}
function payerZip($ins=0) {
if (empty($this->payers[$ins]['object'])) return '';
$tmp = $this->payers[$ins]['object'];
$tmp = $tmp->get_address();
return x12clean(trim($tmp->get_zip()));
}
function payerID($ins=0) {
return x12clean(trim($this->payers[$ins]['company']['cms_id']));
}
function payerAltID($ins=0) {
return x12clean(trim($this->payers[$ins]['company']['alt_cms_id']));
}
function patientLastName() {
return x12clean(trim($this->patient_data['lname']));
}
function patientFirstName() {
return x12clean(trim($this->patient_data['fname']));
}
function patientMiddleName() {
return x12clean(trim($this->patient_data['mname']));
}
function patientStreet() {
return x12clean(trim($this->patient_data['street']));
}
function patientCity() {
return x12clean(trim($this->patient_data['city']));
}
function patientState() {
return x12clean(trim($this->patient_data['state']));
}
function patientZip() {
return x12clean(trim($this->patient_data['postal_code']));
}
function patientPhone() {
$ptphone = $this->patient_data['phone_home'];
if (!$ptphone) $ptphone = $this->patient_data['phone_biz'];
if (!$ptphone) $ptphone = $this->patient_data['phone_cell'];
if (preg_match("/([2-9]\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $ptphone, $tmp))
return $tmp[1] . $tmp[2] . $tmp[3];
return '';
}
function patientDOB() {
return str_replace('-', '', $this->patient_data['DOB']);
}
function patientSex() {
return strtoupper(substr($this->patient_data['sex'], 0, 1));
}
// Patient Marital Status: M = Married, S = Single, or something else.
function patientStatus() {
return strtoupper(substr($this->patient_data['status'], 0, 1));
}
// This should be UNEMPLOYED, STUDENT, PT STUDENT, or anything else to
// indicate employed.
function patientOccupation() {
return strtoupper(x12clean(trim($this->patient_data['occupation'])));
}
function cptCode($prockey) {
return x12clean(trim($this->procs[$prockey]['code']));
}