forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_x12_837.inc.php
1326 lines (1176 loc) · 40.7 KB
/
gen_x12_837.inc.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
/*
* Generate X12 File
*
* Copyright (C) 2015-2017 Terry Hill <[email protected]>
* Copyright (C) 2007-2011 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
*/
require_once("Claim.class.php");
function stripZipCode($zip)
{
return preg_replace('/[-\s]*/','',$zip);
}
function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
$today = time();
$out = '';
$claim = new Claim($pid, $encounter);
$edicount = 0;
// This is true for the 5010 standard, the only one that matters anymore.
// x12gsversionstring() should be "005010X222A1". Preserving the version ID check for future.
//$CMS_5010 = strpos($claim->x12gsversionstring(), '5010') !== false;
$CMS_5010 = true;
$log .= "Generating claim $pid-$encounter for " .
$claim->patientFirstName() . ' ' .
$claim->patientMiddleName() . ' ' .
$claim->patientLastName() . ' on ' .
date('Y-m-d H:i', $today) . ".\n";
$out .= "ISA" .
"*" . $claim->x12gsisa01() .
"*" . $claim->x12gsisa02() .
"*" . $claim->x12gsisa03() .
"*" . $claim->x12gsisa04() .
"*" . $claim->x12gsisa05() .
"*" . $claim->x12gssenderid() .
"*" . $claim->x12gsisa07() .
"*" . $claim->x12gsreceiverid() .
"*030911" .
"*1630" .
"*^".
"*00501" .
"*000000001" .
"*" . $claim->x12gsisa14() .
"*" . $claim->x12gsisa15() .
"*:" .
"~\n";
$out .= "GS" .
"*HC" .
"*" . $claim->x12gsgs02() .
"*" . trim($claim->x12gs03()) .
"*" . date('Ymd', $today) .
"*" . date('Hi', $today) .
"*1" .
"*X" .
"*" . $claim->x12gsversionstring() .
"~\n";
++$edicount;
$out .= "ST" .
"*837" .
"*0021" .
// Spec says the following is optional, so should be able to leave it out.
"*" . $claim->x12gsversionstring() .
"~\n";
++$edicount;
$out .= "BHT" .
"*0019" . // 0019 is required here
"*00" . // 00 = original transmission
"*0123" . // reference identification
"*" . date('Ymd', $today) . // transaction creation date
"*" . date('Hi', $today) . // transaction creation time
($encounter_claim ? "*RP" : "*CH") . // RP = reporting, CH = chargeable
"~\n";
++$edicount;
if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
$tempName = $claim->billingFacilityName();
$partsName = explode(' ', $tempName);// Loop 1000A submitter entity == person
$num_parts = count($partsName);
switch ($num_parts) {
case "2":
$firstName = $partsName[0];
$middleName = '';
$lastName = $partsName[1];
$suffixName = '';
break;
case "3":
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = '';
break;
case "4":
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = $partsName[3];
break;
default:
$log .= "*** submitter name in 1000A loop has more than 4 parts, may not be desirable\n";
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = $partsName[3];
}
$out .= "NM1" . // Loop 1000A Submitter
"*41" .
"*1" .
"*" . $lastName .
"*" . $firstName .
"*" . $middleName .
"*" . // Name Prefix not used
"*" . // Name Suffix not used
"*46";
} else { //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
$billingFacilityName = substr($claim->billingFacilityName(), 0, 60 );
if ($billingFacilityName == '') $log .= "*** billing facility name in 1000A loop is empty\n";
$out .= "NM1" .
"*41" .
"*2" .
"*" . $billingFacilityName .
"*" .
"*" .
"*" .
"*" .
"*46";
}
if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
$out .= "*" . $claim->clearingHouseETIN();
} else {
$out .= "*" . $claim->billingFacilityETIN();
}
$out .= "~\n";
++$edicount;
$out .= "PER" .
"*IC" .
"*" . $claim->billingContactName() .
"*TE" .
"*" . $claim->billingContactPhone();
$out .= "~\n";
++$edicount;
$out .= "NM1" . // Loop 1000B Receiver
"*40" .
"*2" .
"*" . $claim->clearingHouseName() .
"*" .
"*" .
"*" .
"*" .
"*46" .
"*" . $claim->clearingHouseETIN() .
"~\n";
$HLcount = 1;
++$edicount;
$out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
"*$HLcount" .
"*" .
"*20" .
"*1" . // 1 indicates there are child segments
"~\n";
$HLBillingPayToProvider = $HLcount++;
// Situational PRV segment for provider taxonomy code for Medicaid.
# if ($claim->claimType() == 'MC') {
# ++$edicount;
# $out .= "PRV*BI*ZZ" .
# "*" . $claim->providerTaxonomy() .
# "~\n";
# }
// Situational CUR segment (foreign currency information) omitted here.
++$edicount;
//Field length is limited to 35. See nucc dataset page 63 www.nucc.org
if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
$tempName = $claim->billingFacilityName();
$partsName = explode(' ', $tempName);// Loop 2010AA Billing Provider entity == person
$num_parts = count($partsName);
switch ($num_parts) {
case "2":
$firstName = $partsName[0];
$middleName = '';
$lastName = $partsName[1];
$suffixName = '';
break;
case "3":
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = '';
break;
case "4":
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = $partsName[3];
break;
default:
$log .= "*** billing provider name in 2010AA loop has more than 4 parts, may not be desirable\n";
$firstName = $partsName[0];
$middleName = $partsName[1];
$lastName = $partsName[2];
$suffixName = $partsName[3];
}
$out .= "NM1" .
"*85" .
"*1" .
"*" . $lastName .
"*" . $firstName .
"*" . $middleName .
"*" . // Name Prefix not used
"*" . $suffixName;
}
else {
$billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
if ($billingFacilityName == '') $log .= "*** billing facility name in 2010A loop is empty\n";
$out .= "NM1" . // Loop 2010AA Billing Provider
"*85" .
"*2" .
"*" . $billingFacilityName .
"*" .
"*" .
"*" .
"*";
}
if ($claim->billingFacilityNPI()) {
$out .= "*XX*" . $claim->billingFacilityNPI();
}
else {
$log .= "*** Billing facility has no NPI.\n";
$out .= "*XX*";
}
$out .= "~\n";
++$edicount;
$out .= "N3" .
"*" . $claim->billingFacilityStreet() .
"~\n";
++$edicount;
$out .= "N4" .
"*" . $claim->billingFacilityCity() .
"*" . $claim->billingFacilityState() .
"*" . stripZipCode($claim->billingFacilityZip()) .
"~\n";
++$edicount;
$out .= "REF" ;
if($claim->federalIdType()){ //need to check if this is needed in any way.
$out .= "*" . $claim->federalIdType();
}
else{
$out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
}
$out .= "*" . $claim->billingFacilityETIN() .
"~\n";
if ($claim->providerNumber() && !$claim->providerNumberType()) {
$log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
}
// Situational PER*1C segment omitted.
// Loop 2010AC Pay-To Plan Name omitted. Includes:
// NM1*PE, N3, N4, REF*2U, REF*EI
$PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
$HLSubscriber = $HLcount++;
++$edicount;
$out .= "HL" . // Loop 2000B Subscriber HL Loop
"*$HLSubscriber" .
"*$HLBillingPayToProvider" .
"*22" .
"*$PatientHL" .
"~\n";
if (!$claim->payerSequence()) {
$log .= "*** Error: Insurance information is missing!\n";
}
++$edicount;
$out .= "SBR" . // Subscriber Information
"*" . $claim->payerSequence() .
"*" . ($claim->isSelfOfInsured() ? '18' : '') .
"*" . $claim->groupNumber() .
"*" . ( $claim->groupNumber() ? '' : $claim->groupName()) .
"*" . $claim->insuredTypeCode() . // applies for secondary medicare
"*" .
"*" .
"*" .
"*" . $claim->claimType() . // Exceptions may be required here.
"~\n";
// Segment PAT omitted.
++$edicount;
$out .= "NM1" . // Loop 2010BA Subscriber
"*IL" .
"*1" . // 1 = person, 2 = non-person
"*" . $claim->insuredLastName() .
"*" . $claim->insuredFirstName() .
"*" . $claim->insuredMiddleName() .
"*" .
"*" . // Name Suffix
"*MI" .
// "MI" = Member Identification Number
// "II" = Standard Unique Health Identifier, "Required if the
// HIPAA Individual Patient Identifier is mandated use."
// Here we presume that is not true yet.
"*" . $claim->policyNumber() .
"~\n";
// For 5010, further subscriber info is sent only if they are the patient.
if ( $claim->isSelfOfInsured()) {
++$edicount;
$out .= "N3" .
"*" . $claim->insuredStreet() .
"~\n";
++$edicount;
$out .= "N4" .
"*" . $claim->insuredCity() .
"*" . $claim->insuredState() .
"*" . stripZipCode($claim->insuredZip()) .
"~\n";
++$edicount;
$out .= "DMG" .
"*D8" .
"*" . $claim->insuredDOB() .
"*" . $claim->insuredSex() .
"~\n";
}
// Segment REF*SY (Subscriber Secondary Identification) omitted.
// Segment REF*Y4 (Property and Casualty Claim Number) omitted.
// Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
++$edicount;
//Field length is limited to 35. See nucc dataset page 81 www.nucc.org
$payerName = substr($claim->payerName(), 0, 60);
$out .= "NM1" . // Loop 2010BB Payer
"*PR" .
"*2" .
"*" . $payerName .
"*" .
"*" .
"*" .
"*" .
// The 5010 spec says:
// "On or after the mandated implementation date for the HIPAA
// National Plan Identifier (National Plan ID), XV must be sent.
// Prior to the mandated implementation date and prior to any phase-
// in period identified by Federal regulation, PI must be sent."
// HOWEVER, claims with XV are still bouncing.
"*PI" .
// Zirmed ignores this if using payer name matching:
"*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
"~\n";
// if (!$claim->payerID()) {
// $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
// }
/* The next N3 and N4 elements need review.
The 5010 spec says:
"Required when the payer address is available and the submitter intends
for the claim to be printed on paper at the next EDI location (for example, a
clearinghouse). If not required by this implementation guide, do not send."
this piece needs to be tested against a clearinghouse in test mode.
*/
++$edicount;
$out .= "N3" .
"*" . $claim->payerStreet() .
"~\n";
++$edicount;
$out .= "N4" .
"*" . $claim->payerCity() .
"*" . $claim->payerState() .
"*" . stripZipCode($claim->payerZip()) .
"~\n";
//End ToDo section
// Segment REF (Payer Secondary Identification) omitted.
// Segment REF (Billing Provider Secondary Identification) omitted.
if (! $claim->isSelfOfInsured()) {
++$edicount;
$out .= "HL" . // Loop 2000C Patient Information
"*$HLcount" .
"*$HLSubscriber" .
"*23" .
"*0" .
"~\n";
$HLcount++;
++$edicount;
$out .= "PAT" .
"*" . $claim->insuredRelationship() .
"~\n";
++$edicount;
$out .= "NM1" . // Loop 2010CA Patient
"*QC" .
"*1" .
"*" . $claim->patientLastName() .
"*" . $claim->patientFirstName();
if ($claim->patientMiddleName() !== '') $out .= "*"
. $claim->patientMiddleName();
$out .= "~\n";
++$edicount;
$out .= "N3" .
"*" . $claim->patientStreet() .
"~\n";
++$edicount;
$out .= "N4" .
"*" . $claim->patientCity() .
"*" . $claim->patientState() .
"*" . stripZipCode($claim->patientZip()) .
"~\n";
++$edicount;
$out .= "DMG" .
"*D8" .
"*" . $claim->patientDOB() .
"*" . $claim->patientSex() .
"~\n";
// Segment REF*Y4 (Property and Casualty Claim Number) omitted.
// Segment REF (Property and Casualty Patient Identifier) omitted.
// Segment PER (Property and Casualty Patient Contact Information) omitted.
} // end of patient different from insured
$proccount = $claim->procCount();
$clm_total_charges = 0;
for ($prockey = 0; $prockey < $proccount; ++$prockey) {
if ($claim->excludeEntry($prockey) == 1) continue;
$clm_total_charges += $claim->cptCharges($prockey);
}
if (!$clm_total_charges) {
$log .= "*** This claim has no charges!\n";
}
++$edicount;
$out .= "CLM" . // Loop 2300 Claim
"*$pid-$encounter" .
"*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
"*" .
"*" .
"*" . sprintf('%02d', $claim->facilityPOS()) . ":B:" .
$claim->frequencyTypeCode() . // Changed to correct single digit output
"*Y" .
"*A" .
"*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
"*Y" .
"~\n";
if ($claim->onsetDate() &&
($claim->onsetDate()!== $claim->serviceDate()) &&
($claim->onsetDateValid())
) {
++$edicount;
$out .= "DTP" . // Date of Onset
"*431" .
"*D8" .
"*" . $claim->onsetDate() .
"~\n";
}
if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
++$edicount;
$out .= "DTP" . // Date of Initial Treatment
"*454" .
"*D8" .
"*" . $claim->dateInitialTreatment() .
"~\n";
}
// Segment DTP*304 (Last Seen Date) omitted.
// Segment DTP*453 (Acute Manifestation Date) omitted.
// Segment DTP*439 (Accident Date) omitted.
// Segment DTP*484 (Last Menstrual Period Date) omitted.
// Segment DTP*455 (Last X-Ray Date) omitted.
// Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
// Segments DTP (Disability Dates) omitted.
// Segment DTP*297 (Last Worked Date) omitted.
// Segment DTP*296 (Authorized Return to Work Date) omitted.
if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
++$edicount;
$out .= "DTP" . // Date of Hospitalization
"*435" .
"*D8" .
"*" . $claim->onsetDate() .
"~\n";
}
// Segment DTP*096 (Discharge Date) omitted.
// Segments DTP (Assumed and Relinquished Care Dates) omitted.
// Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
// Segment DTP*050 (Repricer Received Date) omitted.
// Segment PWK (Claim Supplemental Information) omitted.
// Segment CN1 (Contract Information) omitted.
$patientpaid = $claim->patientPaidAmount();
if ($patientpaid != 0) {
++$edicount;
$out .= "AMT" . // Patient paid amount. Page 190/220.
"*F5" .
"*" . $patientpaid .
"~\n";
}
// Segment REF*4N (Service Authorization Exception Code) omitted.
// Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
// Segment REF*EW (Mammography Certification Number) omitted.
// Segment REF*9F (Referral Number) omitted.
if ($claim->priorAuth()) {
++$edicount;
$out .= "REF" . // Prior Authorization Number
"*G1" .
"*" . $claim->priorAuth() .
"~\n";
}
// Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
#if($claim->billing_options['replacement_claim'] == '1'){
if(trim($claim->billing_options['icn_resubmission_number']) > 3){
++$edicount;
error_log("Method 1: ".$claim->billing_options['icn_resubmission_number'], 0);
$out .= "REF" .
"*F8" .
"*" . $claim->icnResubmissionNumber() .
"~\n";
}
if ($claim->cliaCode()) {
// Required by Medicare when in-house labs are done.
++$edicount;
$out .= "REF" . // Clinical Laboratory Improvement Amendment Number
"*X4" .
"*" . $claim->cliaCode() .
"~\n";
}
// Segment REF*9A (Repriced Claim Number) omitted.
// Segment REF*9C (Adjusted Repriced Claim Number) omitted.
// Segment REF*LX (Investigational Device Exemption Number) omitted.
// Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
// Segment REF*EA (Medical Record Number) omitted.
// Segment REF*P4 (Demonstration Project Identifier) omitted.
// Segment REF*1J (Care Plan Oversight) omitted.
// Segment K3 (File Information) omitted.
if ($claim->additionalNotes()) {
// Claim note.
++$edicount;
$out .= "NTE" . // comments box 19
"*ADD" .
"*" . $claim->additionalNotes() .
"~\n";
}
// Segment CR1 (Ambulance Transport Information) omitted.
// Segment CR2 (Spinal Manipulation Service Information) omitted.
// Segment CRC (Ambulance Certification) omitted.
// Segment CRC (Patient Condition Information: Vision) omitted.
// Segment CRC (Homebound Indicator) omitted.
// Segment CRC (EPSDT Referral).
if($claim->epsdtFlag()) {
++$edicount;
$out .= "CRC" .
"*ZZ" .
"*Y" .
"*" . $claim->medicaidReferralCode() .
"~\n";
}
// Diagnoses, up to $max_per_seg per HI segment.
$max_per_seg = 12;
$da = $claim->diagArray();
$diag_type_code = 'ABK';
$tmp = 0;
foreach ($da as $diag) {
if ($tmp % $max_per_seg == 0) {
if ($tmp) $out .= "~\n";
++$edicount;
$out .= "HI"; // Health Diagnosis Codes
}
$out .= "*$diag_type_code:" . $diag;
$diag_type_code = 'ABF';
++$tmp;
}
if ($tmp) $out .= "~\n";
// Segment HI*BP (Anesthesia Related Procedure) omitted.
// Segment HI*BG (Condition Information) omitted.
// Segment HCP (Claim Pricing/Repricing Information) omitted.
if ($claim->referrerLastName() || $claim->providerQualifierCode() == "DN" || $claim->referringLastName() ) {
if ($claim->providerQualifierCode() == "DN") {
// Getting Referrer information from Misc_Billing_form.
++$edicount;
$out .= "NM1" . // Loop 2310A Referring Provider
"*DN" .
"*1" .
"*" . $claim->billingProviderLastName() .
"*" . $claim->billingProviderFirstName() .
"*" . $claim->billingProviderMiddleName() .
"*" .
"*";
$out .=
"*XX" .
"*" . $claim->billingProviderNPI().
"~\n";
} else if ( $claim->referringLastName()) {
error_log("referringlast: ".$claim->providerQualifierCode(), 0);
# Use the Referrer information
// Medicare requires referring provider's name and UPIN.
++$edicount;
$out .= "NM1" . // Loop 2310A Referring Provider
"*DN" .
"*1" .
"*" . $claim->referringLastName() .
"*" . $claim->referringFirstName() .
"*" . $claim->referringMiddleName() .
"*" .
"*";
if ($CMS_5010 || $claim->referringNPI()) { $out .=
"*XX" .
"*" . $claim->referringNPI();
} else { $out .=
"*34" . // not allowed for 5010
"*" . $claim->referringSSN();
}
$out .= "~\n";
} else if ($claim->referrerLastName()) {
error_log("referrerlast: ".$claim->providerQualifierCode(), 0);
# Use the Referrer information
// Medicare requires referring provider's name and UPIN.
++$edicount;
$out .= "NM1" . // Loop 2310A Referring Provider
"*DN" .
"*1" .
"*" . $claim->referrerLastName() .
"*" . $claim->referrerFirstName() .
"*" . $claim->referrerMiddleName() .
"*" .
"*";
$out .=
"*XX" .
"*" . $claim->referrerNPI().
"~\n";
}
}
/* Per the implementation guide lines, only include this information if it is different
* than the Loop 2010AA information
*/
if($claim->providerNPIValid() &&
$claim->billingFacilityNPI() !== $claim->providerNPI() )
{
++$edicount;
$out .= "NM1" . // Loop 2310B Rendering Provider
"*82" .
"*1" .
"*" . $claim->providerLastName() .
"*" . $claim->providerFirstName() .
"*" . $claim->providerMiddleName() .
"*" .
"*".
"*XX" .
"*" . $claim->providerNPI().
"~\n";
if ($claim->providerTaxonomy()) {
++$edicount;
$out .= "PRV" .
"*PE" . // Performing provider
// "*" .($claim->claimType() != 'MC' ? "PXC" : "ZZ") .
"*PXC" .
"*" . $claim->providerTaxonomy() .
"~\n";
}
// End of Loop 2310B
}
else
{
// This loop can only get skipped if we are generating a 5010 claim
if(!($claim->providerNPIValid()))
{
/* If the loop was skipped because the provider NPI was invalid, generate
* a warning for the log.*/
$log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
}
/* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
* is a normal condition, so no need to warn.
*/
}
// --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - [email protected]
//
// 5010 spec says nothing here if NPI was specified.
//
if (!$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
{
if ($claim->providerNumber()) {
++$edicount;
$out .= "REF" .
"*" . $claim->providerNumberType() .
"*" . $claim->providerNumber() .
"~\n";
}
}
// Loop 2310D is omitted in the case of home visits (POS=12).
if ($claim->facilityPOS() != 12 &&
$claim->facilityNPI() != $claim->billingFacilityNPI())
{
++$edicount;
$out .= "NM1" . // Loop 2310D Service Location
"*77" .
"*2";
//Field length is limited to 35. See nucc dataset page 77 www.nucc.org
$facilityName = substr($claim->facilityName(), 0, 60);
if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
"*" . $facilityName;
}
if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
"*" .
"*" .
"*" .
"*".
"*XX*" . $claim->facilityNPI();
}
if (!$claim->facilityNPI()) {
$log .= "*** Service location has no NPI.\n";
}
$out .= "~\n";
if ($claim->facilityStreet()) {
++$edicount;
$out .= "N3" .
"*" . $claim->facilityStreet() .
"~\n";
}
if ($claim->facilityState()) {
++$edicount;
$out .= "N4" .
"*" . $claim->facilityCity() .
"*" . $claim->facilityState() .
"*" . stripZipCode($claim->facilityZip()) .
"~\n";
}
}
// Segment REF (Service Facility Location Secondary Identification) omitted.
// Segment PER (Service Facility Contact Information) omitted.
// Loop 2310E, Supervising Provider
//
if ($claim->supervisorLastName() || $claim->providerQualifierCode() == "DQ") {
if ($claim->providerQualifierCode() == "DQ") {
# the Supervising Physician is filled out the Misc Billing Form
++$edicount;
$out .= "NM1" .
"*DQ" . // Supervising Physician
"*1" . // Person
"*" . $claim->billingProviderLastName() .
"*" . $claim->billingProviderFirstName() .
"*" . $claim->billingProviderMiddleName() .
"*" . // NM106 not used
"*". // Name Suffix
"*XX" .
"*" . $claim->billingProviderNPI();
if (!$claim->billingProviderNPI()) {
$log .= "*** Supervising Provider has no NPI.\n";
}
$out .= "~\n";
} else {
# the Supervising Physician is filled out on the fee sheet
++$edicount;
$out .= "NM1" .
"*DQ" . // Supervising Physician
"*1" . // Person
"*" . $claim->supervisorLastName() .
"*" . $claim->supervisorFirstName() .
"*" . $claim->supervisorMiddleName() .
"*" . // NM106 not used
"*". // Name Suffix
"*XX" .
"*" . $claim->supervisorNPI();
if (!$claim->supervisorNPI()) {
$log .= "*** Supervising Provider has no NPI.\n";
}
$out .= "~\n";
if ($claim->supervisorNumber()) {
++$edicount;
$out .= "REF" .
"*" . $claim->supervisorNumberType() .
"*" . $claim->supervisorNumber() .
"~\n";
}
}
}
// Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
// Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
$prev_pt_resp = $clm_total_charges; // for computation below
// Loops 2320 and 2330*, other subscriber/payer information.
// Remember that insurance index 0 is always for the payer being billed
// by this claim, and 1 and above are always for the "other" payers.
//
for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
$tmp1 = $claim->claimType($ins);
$tmp2 = 'C1'; // Here a kludge. See page 321.
if ($tmp1 === 'CI') $tmp2 = 'C1';
if ($tmp1 === 'AM') $tmp2 = 'AP';
if ($tmp1 === 'HM') $tmp2 = 'HM';
if ($tmp1 === 'MB') $tmp2 = 'MB';
if ($tmp1 === 'MC') $tmp2 = 'MC';
if ($tmp1 === '09') $tmp2 = 'PP';
++$edicount;
$out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
"*" . $claim->payerSequence($ins) .
"*" . $claim->insuredRelationship($ins) .
"*" . $claim->groupNumber($ins) .
"*" . ( $claim->groupNumber($ins) ? '' : $claim->groupName($ins)) .
"*" . $claim->insuredTypeCode($ins) .
"*" .
"*" .
"*" .
"*" . $claim->claimType($ins) .
"~\n";
// Things that apply only to previous payers, not future payers.
//
if ($claim->payerSequence($ins) < $claim->payerSequence()) {
// Generate claim-level adjustments.
$aarr = $claim->payerAdjustments($ins);
foreach ($aarr as $a) {
++$edicount;
$out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
"*" . $a[1] .
"*" . $a[2] .
"*" . $a[3] .
"~\n";
}
$payerpaid = $claim->payerTotals($ins);
++$edicount;
$out .= "AMT" . // Previous payer's paid amount. Page 307/332.
"*D" .
"*" . $payerpaid[1] .
"~\n";
// Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
// Segment AMT*EAF (Remaining Patient Liability) omitted.
} // End of things that apply only to previous payers.
++$edicount;
$out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
"*" .
"*" .
"*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
// For this next item, the 5010 example in the spec does not match its
// description. So this might be wrong.
"*" .
"*" .
"*Y" .
"~\n";
// Segment MOA (Medicare Outpatient Adjudication) omitted.
++$edicount;
$out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
"*IL" .
"*1" .
"*" . $claim->insuredLastName($ins) .
"*" . $claim->insuredFirstName($ins) .
"*" . $claim->insuredMiddleName($ins) .
"*" .
"*" .
"*MI" .
"*" . $claim->policyNumber($ins) .
"~\n";
++$edicount;
$out .= "N3" .
"*" . $claim->insuredStreet($ins) .
"~\n";
++$edicount;
$out .= "N4" .
"*" . $claim->insuredCity($ins) .
"*" . $claim->insuredState($ins) .
"*" . stripZipCode($claim->insuredZip($ins)) .
"~\n";
// Segment REF (Other Subscriber Secondary Identification) omitted.
++$edicount;
$payerName = substr($claim->payerName($ins), 0, 60);
$out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
"*PR" .
"*2" .
"*" . $payerName .
"*" .
"*" .
"*" .
"*" .
"*PI" .
"*" . $claim->payerID($ins) .
"~\n";
// if (!$claim->payerID($ins)) {
// $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
// }
++$edicount;
$out .= "N3" .
"*" . $claim->payerStreet($ins) .
"~\n";
//
++$edicount;
$out .= "N4" .
"*" . $claim->payerCity($ins) .
"*" . $claim->payerState($ins) .
"*" . stripZipCode($claim->payerZip($ins)) .
"~\n";
// Segment DTP*573 (Claim Check or Remittance Date) omitted.
// Segment REF (Other Payer Secondary Identifier) omitted.
// Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
// Segment REF*9F (Other Payer Referral Number) omitted.
// Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
// Segment REF*F8 (Other Payer Claim Control Number) omitted.