some more minimal doc formatting
[openemr.git] / library / gen_x12_837.inc.php
blob4aaa62c31790e4e1a488be4f20091673ea681dc4
1 <?php
2 // Copyright (C) 2007-2011 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("Claim.class.php");
10 function stripZipCode($zip)
12 return preg_replace('/[-\s]*/','',$zip);
14 function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
16 $today = time();
17 $out = '';
18 $claim = new Claim($pid, $encounter);
19 $edicount = 0;
21 // This is true for the 5010 standard, false for 4010.
22 // x12gsversionstring() should be "005010X222A1" or "004010X098A1".
23 $CMS_5010 = strpos($claim->x12gsversionstring(), '5010') !== false;
25 $log .= "Generating claim $pid-$encounter for " .
26 $claim->patientFirstName() . ' ' .
27 $claim->patientMiddleName() . ' ' .
28 $claim->patientLastName() . ' on ' .
29 date('Y-m-d H:i', $today) . ".\n";
31 $out .= "ISA" .
32 "*" . $claim->x12gsisa01() .
33 "*" . $claim->x12gsisa02() .
34 "*" . $claim->x12gsisa03() .
35 "*" . $claim->x12gsisa04() .
36 "*" . $claim->x12gsisa05() .
37 "*" . $claim->x12gssenderid() .
38 "*" . $claim->x12gsisa07() .
39 "*" . $claim->x12gsreceiverid() .
40 "*030911" .
41 "*1630" .
42 "*" . ($CMS_5010 ? "^" : "U" ) .
43 "*" . ($CMS_5010 ? "00501" : "00401") .
44 "*000000001" .
45 "*" . $claim->x12gsisa14() .
46 "*" . $claim->x12gsisa15() .
47 "*:" .
48 "~\n";
50 $out .= "GS" .
51 "*HC" .
52 "*" . $claim->x12gsgs02() .
53 "*" . trim($claim->x12gs03()) .
54 "*" . date('Ymd', $today) .
55 "*" . date('Hi', $today) .
56 "*1" .
57 "*X" .
58 "*" . $claim->x12gsversionstring() .
59 "~\n";
61 ++$edicount;
62 $out .= "ST" .
63 "*837" .
64 "*0021" .
65 // Spec says the following is optional, so should be able to leave it out.
66 ($CMS_5010 ? ("*" . $claim->x12gsversionstring()) : "") .
67 "~\n";
69 ++$edicount;
70 $out .= "BHT" .
71 "*0019" . // 0019 is required here
72 "*00" . // 00 = original transmission
73 "*0123" . // reference identification
74 "*" . date('Ymd', $today) . // transaction creation date
75 "*" . date('Hi', $today) . // transaction creation time
76 ($encounter_claim ? "*RP" : "*CH") . // RP = reporting, CH = chargeable
77 "~\n";
79 if (!$CMS_5010) {
80 // This segment was deleted for 5010.
81 ++$edicount;
82 $out .= "REF" .
83 "*87" .
84 "*" . $claim->x12gsversionstring() .
85 "~\n";
88 ++$edicount;
89 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
90 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
91 $out .= "NM1" . // Loop 1000A Submitter
92 "*41" .
93 "*2" .
94 "*" . $billingFacilityName .
95 "*" .
96 "*" .
97 "*" .
98 "*" .
99 "*46";
100 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
101 $out .= "*" . $claim->clearingHouseETIN();
102 } else {
103 $out .= "*" . $claim->billingFacilityETIN();
105 $out .= "~\n";
107 ++$edicount;
108 $out .= "PER" .
109 "*IC" .
110 "*" . $claim->billingContactName() .
111 "*TE" .
112 "*" . $claim->billingContactPhone();
113 if (!$CMS_5010 && $claim->x12gsper06()) {
114 $out .= "*ED*" . $claim->x12gsper06();
116 $out .= "~\n";
118 ++$edicount;
119 $out .= "NM1" . // Loop 1000B Receiver
120 "*40" .
121 "*2" .
122 "*" . $claim->clearingHouseName() .
123 "*" .
124 "*" .
125 "*" .
126 "*" .
127 "*46" .
128 "*" . $claim->clearingHouseETIN() .
129 "~\n";
131 $HLcount = 1;
133 ++$edicount;
134 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
135 "*$HLcount" .
136 "*" .
137 "*20" .
138 "*1" . // 1 indicates there are child segments
139 "~\n";
141 $HLBillingPayToProvider = $HLcount++;
143 // Situational PRV segment (for provider taxonomy code) omitted here.
144 // Situational CUR segment (foreign currency information) omitted here.
146 ++$edicount;
147 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
148 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
149 $out .= "NM1" . // Loop 2010AA Billing Provider
150 "*85" .
151 "*2" .
152 "*" . $billingFacilityName .
153 "*" .
154 "*" .
155 "*" .
156 "*";
157 if ($claim->billingFacilityNPI()) {
158 $out .= "*XX*" . $claim->billingFacilityNPI();
160 else {
161 $log .= "*** Billing facility has no NPI.\n";
162 if ($CMS_5010) {
163 $out .= "*XX*";
165 else {
166 $out .= "*24*" . $claim->billingFacilityETIN();
169 $out .= "~\n";
171 ++$edicount;
172 $out .= "N3" .
173 "*" . $claim->billingFacilityStreet() .
174 "~\n";
176 ++$edicount;
177 $out .= "N4" .
178 "*" . $claim->billingFacilityCity() .
179 "*" . $claim->billingFacilityState() .
180 "*" . stripZipCode($claim->billingFacilityZip()) .
181 "~\n";
183 if ($CMS_5010 || ($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
184 ++$edicount;
185 $out .= "REF" ;
186 if($claim->federalIdType()){
187 $out .= "*" . $claim->federalIdType();
189 else{
190 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
192 $out .= "*" . $claim->billingFacilityETIN() .
193 "~\n";
196 if ($claim->providerNumberType() && $claim->providerNumber() &&
197 !($CMS_5010 && $claim->billingFacilityNPI()))
199 ++$edicount;
200 $out .= "REF" .
201 "*" . $claim->providerNumberType() .
202 "*" . $claim->providerNumber() .
203 "~\n";
205 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
206 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
209 // Situational PER*1C segment omitted.
211 // Pay-To Address defaults to billing provider and is no longer required in 5010.
212 if (!$CMS_5010) {
213 ++$edicount;
214 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
215 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
216 $out .= "NM1" . // Loop 2010AB Pay-To Provider
217 "*87" .
218 "*2" .
219 "*" . $billingFacilityName .
220 "*" .
221 "*" .
222 "*" .
223 "*";
224 if ($claim->billingFacilityNPI())
225 $out .= "*XX*" . $claim->billingFacilityNPI();
226 else
227 $out .= "*24*" . $claim->billingFacilityETIN();
228 $out .= "~\n";
230 ++$edicount;
231 $out .= "N3" .
232 "*" . $claim->billingFacilityStreet() .
233 "~\n";
235 ++$edicount;
236 $out .= "N4" .
237 "*" . $claim->billingFacilityCity() .
238 "*" . $claim->billingFacilityState() .
239 "*" . stripZipCode($claim->billingFacilityZip()) .
240 "~\n";
242 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
243 ++$edicount;
244 $out .= "REF" .
245 "*EI" .
246 "*" . $claim->billingFacilityETIN() .
247 "~\n";
251 // Loop 2010AC Pay-To Plan Name omitted. Includes:
252 // NM1*PE, N3, N4, REF*2U, REF*EI
254 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
255 $HLSubscriber = $HLcount++;
257 ++$edicount;
258 $out .= "HL" . // Loop 2000B Subscriber HL Loop
259 "*$HLSubscriber" .
260 "*$HLBillingPayToProvider" .
261 "*22" .
262 "*$PatientHL" .
263 "~\n";
265 if (!$claim->payerSequence()) {
266 $log .= "*** Error: Insurance information is missing!\n";
269 ++$edicount;
270 $out .= "SBR" . // Subscriber Information
271 "*" . $claim->payerSequence() .
272 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
273 "*" . $claim->groupNumber() .
274 "*" . (($CMS_5010 && $claim->groupNumber()) ? '' : $claim->groupName()) .
275 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
276 "*" .
277 "*" .
278 "*" .
279 "*" . $claim->claimType() . // Zirmed replaces this
280 "~\n";
282 // Segment PAT omitted.
284 ++$edicount;
285 $out .= "NM1" . // Loop 2010BA Subscriber
286 "*IL" .
287 "*1" . // 1 = person, 2 = non-person
288 "*" . $claim->insuredLastName() .
289 "*" . $claim->insuredFirstName() .
290 "*" . $claim->insuredMiddleName() .
291 "*" .
292 "*" . // Name Suffix
293 "*MI" .
294 // "MI" = Member Identification Number
295 // "II" = Standard Unique Health Identifier, "Required if the
296 // HIPAA Individual Patient Identifier is mandated use."
297 // Here we presume that is not true yet.
298 "*" . $claim->policyNumber() .
299 "~\n";
301 // For 5010, further subscriber info is sent only if they are the patient.
302 if (!$CMS_5010 || $claim->isSelfOfInsured()) {
303 ++$edicount;
304 $out .= "N3" .
305 "*" . $claim->insuredStreet() .
306 "~\n";
308 ++$edicount;
309 $out .= "N4" .
310 "*" . $claim->insuredCity() .
311 "*" . $claim->insuredState() .
312 "*" . stripZipCode($claim->insuredZip()) .
313 "~\n";
315 ++$edicount;
316 $out .= "DMG" .
317 "*D8" .
318 "*" . $claim->insuredDOB() .
319 "*" . $claim->insuredSex() .
320 "~\n";
323 // Segment REF*SY (Subscriber Secondary Identification) omitted.
324 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
325 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
327 ++$edicount;
328 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
329 $payerName = substr($claim->payerName(), 0, $CMS_5010 ? 60 : 35);
330 $out .= "NM1" . // Loop 2010BB Payer
331 "*PR" .
332 "*2" .
333 "*" . $payerName .
334 "*" .
335 "*" .
336 "*" .
337 "*" .
338 // The 5010 spec says:
339 // "On or after the mandated implementation date for the HIPAA
340 // National Plan Identifier (National Plan ID), XV must be sent.
341 // Prior to the mandated implementation date and prior to any phase-
342 // in period identified by Federal regulation, PI must be sent."
343 // *************** Anybody know what that date is? ***************
344 // August 2011 - Publish interim final rule
345 // October 1, 2012-March 31, 2013 - Enumeration
346 // April 1, 2013-September 30, 2013 - Testing
347 // October 1, 2013 - Implementation
349 "*XV" .
350 // Zirmed ignores this if using payer name matching:
351 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
352 "~\n";
354 // if (!$claim->payerID()) {
355 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
356 // }
358 if (true) { // !$CMS_5010
359 // The 5010 spec says:
360 // "Required when the payer address is available and the submitter intends
361 // for the claim to be printed on paper at the next EDI location (for example, a
362 // clearinghouse). If not required by this implementation guide, do not send."
364 ++$edicount;
365 $out .= "N3" .
366 "*" . $claim->payerStreet() .
367 "~\n";
369 ++$edicount;
370 $out .= "N4" .
371 "*" . $claim->payerCity() .
372 "*" . $claim->payerState() .
373 "*" . stripZipCode($claim->payerZip()) .
374 "~\n";
377 // Segment REF (Payer Secondary Identification) omitted.
378 // Segment REF (Billing Provider Secondary Identification) omitted.
380 if (! $claim->isSelfOfInsured()) {
381 ++$edicount;
382 $out .= "HL" . // Loop 2000C Patient Information
383 "*$HLcount" .
384 "*$HLSubscriber" .
385 "*23" .
386 "*0" .
387 "~\n";
389 $HLcount++;
391 ++$edicount;
392 $out .= "PAT" .
393 "*" . $claim->insuredRelationship() .
394 "~\n";
396 ++$edicount;
397 $out .= "NM1" . // Loop 2010CA Patient
398 "*QC" .
399 "*1" .
400 "*" . $claim->patientLastName() .
401 "*" . $claim->patientFirstName();
402 if ($claim->patientMiddleName() !== '') $out .= "*"
403 . $claim->patientMiddleName();
404 $out .= "~\n";
406 ++$edicount;
407 $out .= "N3" .
408 "*" . $claim->patientStreet() .
409 "~\n";
411 ++$edicount;
412 $out .= "N4" .
413 "*" . $claim->patientCity() .
414 "*" . $claim->patientState() .
415 "*" . stripZipCode($claim->patientZip()) .
416 "~\n";
418 ++$edicount;
419 $out .= "DMG" .
420 "*D8" .
421 "*" . $claim->patientDOB() .
422 "*" . $claim->patientSex() .
423 "~\n";
425 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
426 // Segment REF (Property and Casualty Patient Identifier) omitted.
427 // Segment PER (Property and Casualty Patient Contact Information) omitted.
429 } // end of patient different from insured
431 $proccount = $claim->procCount();
433 $clm_total_charges = 0;
434 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
435 $clm_total_charges += $claim->cptCharges($prockey);
438 if (!$clm_total_charges) {
439 $log .= "*** This claim has no charges!\n";
442 ++$edicount;
443 $out .= "CLM" . // Loop 2300 Claim
444 "*$pid-$encounter" .
445 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
446 "*" .
447 "*" .
448 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
449 ($CMS_5010 ? "B" : "") . ":" .
450 $claim->frequencyTypeCode() . // Changed to correct single digit output
451 "*Y" .
452 "*A" .
453 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
454 "*Y" .
455 ($CMS_5010 ? "" : "*C") .
456 "~\n";
458 if ($claim->onsetDate() &&
459 ($claim->onsetDate()!== $claim->serviceDate()) &&
460 ($claim->onsetDateValid())
462 ++$edicount;
463 $out .= "DTP" . // Date of Onset
464 "*431" .
465 "*D8" .
466 "*" . $claim->onsetDate() .
467 "~\n";
470 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
471 ++$edicount;
472 $out .= "DTP" . // Date of Initial Treatment
473 "*454" .
474 "*D8" .
475 "*" . $claim->dateInitialTreatment() .
476 "~\n";
479 // Segment DTP*304 (Last Seen Date) omitted.
480 // Segment DTP*453 (Acute Manifestation Date) omitted.
481 // Segment DTP*439 (Accident Date) omitted.
482 // Segment DTP*484 (Last Menstrual Period Date) omitted.
483 // Segment DTP*455 (Last X-Ray Date) omitted.
484 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
485 // Segments DTP (Disability Dates) omitted.
486 // Segment DTP*297 (Last Worked Date) omitted.
487 // Segment DTP*296 (Authorized Return to Work Date) omitted.
489 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
490 ++$edicount;
491 $out .= "DTP" . // Date of Hospitalization
492 "*435" .
493 "*D8" .
494 "*" . $claim->onsetDate() .
495 "~\n";
498 // Segment DTP*096 (Discharge Date) omitted.
499 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
500 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
501 // Segment DTP*050 (Repricer Received Date) omitted.
502 // Segment PWK (Claim Supplemental Information) omitted.
503 // Segment CN1 (Contract Information) omitted.
505 $patientpaid = $claim->patientPaidAmount();
506 if ($patientpaid != 0) {
507 ++$edicount;
508 $out .= "AMT" . // Patient paid amount. Page 190/220.
509 "*F5" .
510 "*" . $patientpaid .
511 "~\n";
514 // Segment REF*4N (Service Authorization Exception Code) omitted.
515 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
516 // Segment REF*EW (Mammography Certification Number) omitted.
517 // Segment REF*9F (Referral Number) omitted.
519 if ($claim->priorAuth()) {
520 ++$edicount;
521 $out .= "REF" . // Prior Authorization Number
522 "*G1" .
523 "*" . $claim->priorAuth() .
524 "~\n";
527 // Segment REF*F8 (Payer Claim Control Number) omitted.
529 if ($claim->cliaCode() && ($CMS_5010 || $claim->claimType() === 'MB')) {
530 // Required by Medicare when in-house labs are done.
531 ++$edicount;
532 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
533 "*X4" .
534 "*" . $claim->cliaCode() .
535 "~\n";
538 // Segment REF*9A (Repriced Claim Number) omitted.
539 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
540 // Segment REF*LX (Investigational Device Exemption Number) omitted.
541 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
542 // Segment REF*EA (Medical Record Number) omitted.
543 // Segment REF*P4 (Demonstration Project Identifier) omitted.
544 // Segment REF*1J (Care Plan Oversight) omitted.
545 // Segment K3 (File Information) omitted.
547 if ($claim->additionalNotes()) {
548 // Claim note.
549 ++$edicount;
550 $out .= "NTE" . // comments box 19
551 "*" . ($CMS_5010 ? "ADD" : "") .
552 "*" . $claim->additionalNotes() .
553 "~\n";
556 // Segment CR1 (Ambulance Transport Information) omitted.
557 // Segment CR2 (Spinal Manipulation Service Information) omitted.
558 // Segment CRC (Ambulance Certification) omitted.
559 // Segment CRC (Patient Condition Information: Vision) omitted.
560 // Segment CRC (Homebound Indicator) omitted.
561 // Segment CRC (EPSDT Referral) omitted.
563 // Diagnoses, up to $max_per_seg per HI segment.
564 $max_per_seg = $CMS_5010 ? 12 : 8;
565 $da = $claim->diagArray();
566 $diag_type_code = 'BK';
567 $tmp = 0;
568 foreach ($da as $diag) {
569 if ($tmp % $max_per_seg == 0) {
570 if ($tmp) $out .= "~\n";
571 ++$edicount;
572 $out .= "HI"; // Health Diagnosis Codes
574 $out .= "*$diag_type_code:" . $diag;
575 $diag_type_code = 'BF';
576 ++$tmp;
578 if ($tmp) $out .= "~\n";
580 // Segment HI*BP (Anesthesia Related Procedure) omitted.
581 // Segment HI*BG (Condition Information) omitted.
582 // Segment HCP (Claim Pricing/Repricing Information) omitted.
584 if ($claim->referrerLastName()) {
585 // Medicare requires referring provider's name and UPIN.
586 ++$edicount;
587 $out .= "NM1" . // Loop 2310A Referring Provider
588 "*DN" .
589 "*1" .
590 "*" . $claim->referrerLastName() .
591 "*" . $claim->referrerFirstName() .
592 "*" . $claim->referrerMiddleName() .
593 "*" .
594 "*";
595 if ($CMS_5010 || $claim->referrerNPI()) { $out .=
596 "*XX" .
597 "*" . $claim->referrerNPI();
598 } else { $out .=
599 "*34" . // not allowed for 5010
600 "*" . $claim->referrerSSN();
602 $out .= "~\n";
604 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
605 ++$edicount;
606 $out .= "PRV" .
607 "*RF" . // ReFerring provider
608 "*ZZ" .
609 "*" . $claim->referrerTaxonomy() .
610 "~\n";
613 if (!CMS_5010 && $claim->referrerUPIN()) {
614 ++$edicount;
615 $out .= "REF" . // Referring Provider Secondary Identification
616 "*1G" .
617 "*" . $claim->referrerUPIN() .
618 "~\n";
623 /* Per the implementation guide lines, only include this information if it is different
624 * than the Loop 2010AA information
626 if(!$CMS_5010 ||
627 ($claim->providerNPIValid() &&
628 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
630 ++$edicount;
631 $out .= "NM1" . // Loop 2310B Rendering Provider
632 "*82" .
633 "*1" .
634 "*" . $claim->providerLastName() .
635 "*" . $claim->providerFirstName() .
636 "*" . $claim->providerMiddleName() .
637 "*" .
638 "*";
639 if ($CMS_5010 || $claim->providerNPI()) { $out .=
640 "*XX" .
641 "*" . $claim->providerNPI();
642 } else { $out .=
643 "*34" . // not allowed for 5010
644 "*" . $claim->providerSSN();
645 $log .= "*** Rendering provider has no NPI.\n";
647 $out .= "~\n";
649 if ($claim->providerTaxonomy()) {
650 ++$edicount;
651 $out .= "PRV" .
652 "*PE" . // PErforming provider
653 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
654 "*" . $claim->providerTaxonomy() .
655 "~\n";
657 // End of Loop 2310B
659 else
661 // This loop can only get skipped if we are generating a 5010 claim
662 if(!($claim->providerNPIValid()))
664 /* If the loop was skipped because the provider NPI was invalid, generate
665 * a warning for the log.*/
666 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
668 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
669 * is a normal condition, so no need to warn.
674 // 4010: REF*1C is required here for the Medicare provider number if NPI was
675 // specified in NM109. Not sure if other payers require anything here.
676 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
678 // 5010 spec says nothing here if NPI was specified.
680 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
681 || (!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
683 if ($claim->providerNumber()) {
684 ++$edicount;
685 $out .= "REF" .
686 "*" . $claim->providerNumberType() .
687 "*" . $claim->providerNumber() .
688 "~\n";
692 // Loop 2310D is omitted in the case of home visits (POS=12).
693 if ($claim->facilityPOS() != 12 &&
694 (!$CMS_5010 || $claim->facilityNPI() != $claim->billingFacilityNPI()))
696 ++$edicount;
697 $out .= "NM1" . // Loop 2310D Service Location
698 "*77" .
699 "*2";
700 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
701 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ? 60 : 35);
702 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
703 "*" . $facilityName;
705 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
706 "*" .
707 "*" .
708 "*" .
709 "*";
710 if ($CMS_5010 || $claim->facilityNPI()) { $out .=
711 "*XX*" . $claim->facilityNPI();
712 } else { $out .=
713 "*24*" . $claim->facilityETIN();
715 if (!$claim->facilityNPI()) {
716 $log .= "*** Service location has no NPI.\n";
719 $out .= "~\n";
720 if ($claim->facilityStreet()) {
721 ++$edicount;
722 $out .= "N3" .
723 "*" . $claim->facilityStreet() .
724 "~\n";
726 if ($claim->facilityState()) {
727 ++$edicount;
728 $out .= "N4" .
729 "*" . $claim->facilityCity() .
730 "*" . $claim->facilityState() .
731 "*" . stripZipCode($claim->facilityZip()) .
732 "~\n";
736 // Segment REF (Service Facility Location Secondary Identification) omitted.
737 // Segment PER (Service Facility Contact Information) omitted.
739 // Loop 2310E, Supervising Provider
741 if ($claim->supervisorLastName()) {
742 ++$edicount;
743 $out .= "NM1" .
744 "*DQ" . // Supervising Physician
745 "*1" . // Person
746 "*" . $claim->supervisorLastName() .
747 "*" . $claim->supervisorFirstName() .
748 "*" . $claim->supervisorMiddleName() .
749 "*" . // NM106 not used
750 "*"; // Name Suffix
751 if ($CMS_5010 || $claim->supervisorNPI()) { $out .=
752 "*XX" .
753 "*" . $claim->supervisorNPI();
754 } else { $out .=
755 "*34" .
756 "*" . $claim->supervisorSSN();
758 if (!$claim->supervisorNPI()) {
759 $log .= "*** Supervising Provider has no NPI.\n";
761 $out .= "~\n";
763 if ($claim->supervisorNumber()) {
764 ++$edicount;
765 $out .= "REF" .
766 "*" . $claim->supervisorNumberType() .
767 "*" . $claim->supervisorNumber() .
768 "~\n";
772 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
773 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
775 $prev_pt_resp = $clm_total_charges; // for computation below
777 // Loops 2320 and 2330*, other subscriber/payer information.
778 // Remember that insurance index 0 is always for the payer being billed
779 // by this claim, and 1 and above are always for the "other" payers.
781 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
783 $tmp1 = $claim->claimType($ins);
784 $tmp2 = 'C1'; // Here a kludge. See page 321.
785 if ($tmp1 === 'CI') $tmp2 = 'C1';
786 if ($tmp1 === 'AM') $tmp2 = 'AP';
787 if ($tmp1 === 'HM') $tmp2 = 'HM';
788 if ($tmp1 === 'MB') $tmp2 = 'MB';
789 if ($tmp1 === 'MC') $tmp2 = 'MC';
790 if ($tmp1 === '09') $tmp2 = 'PP';
791 ++$edicount;
792 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
793 "*" . $claim->payerSequence($ins) .
794 "*" . $claim->insuredRelationship($ins) .
795 "*" . $claim->groupNumber($ins) .
796 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ? '' : $claim->groupName($ins)) .
797 "*" . ($CMS_5010 ? $claim->insuredTypeCode($ins) : $tmp2) .
798 "*" .
799 "*" .
800 "*" .
801 "*" . $claim->claimType($ins) .
802 "~\n";
804 // Things that apply only to previous payers, not future payers.
806 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
808 // Generate claim-level adjustments.
809 $aarr = $claim->payerAdjustments($ins);
810 foreach ($aarr as $a) {
811 ++$edicount;
812 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
813 "*" . $a[1] .
814 "*" . $a[2] .
815 "*" . $a[3] .
816 "~\n";
819 $payerpaid = $claim->payerTotals($ins);
820 ++$edicount;
821 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
822 "*D" .
823 "*" . $payerpaid[1] .
824 "~\n";
826 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
827 // Segment AMT*EAF (Remaining Patient Liability) omitted.
829 if (!$CMS_5010) {
830 // Patient responsibility amount as of this previous payer.
831 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
832 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
834 ++$edicount;
835 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
836 "*B6" .
837 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
838 "~\n";
840 ++$edicount;
841 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
842 "*F2" .
843 "*" . sprintf('%.2f', $prev_pt_resp) .
844 "~\n";
846 } // End of things that apply only to previous payers.
848 if (!$CMS_5010) {
849 ++$edicount;
850 $out .= "DMG" . // Other subscriber demographic information. Page 342.
851 "*D8" .
852 "*" . $claim->insuredDOB($ins) .
853 "*" . $claim->insuredSex($ins) .
854 "~\n";
857 ++$edicount;
858 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
859 "*" .
860 "*" .
861 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
862 // For this next item, the 5010 example in the spec does not match its
863 // description. So this might be wrong.
864 "*" . ($CMS_5010 ? '' : 'B') .
865 "*" .
866 "*Y" .
867 "~\n";
869 // Segment MOA (Medicare Outpatient Adjudication) omitted.
871 ++$edicount;
872 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
873 "*IL" .
874 "*1" .
875 "*" . $claim->insuredLastName($ins) .
876 "*" . $claim->insuredFirstName($ins) .
877 "*" . $claim->insuredMiddleName($ins) .
878 "*" .
879 "*" .
880 "*MI" .
881 "*" . $claim->policyNumber($ins) .
882 "~\n";
884 ++$edicount;
885 $out .= "N3" .
886 "*" . $claim->insuredStreet($ins) .
887 "~\n";
889 ++$edicount;
890 $out .= "N4" .
891 "*" . $claim->insuredCity($ins) .
892 "*" . $claim->insuredState($ins) .
893 "*" . stripZipCode($claim->insuredZip($ins)) .
894 "~\n";
896 // Segment REF (Other Subscriber Secondary Identification) omitted.
898 ++$edicount;
899 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
900 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ? 60 : 35);
901 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
902 "*PR" .
903 "*2" .
904 "*" . $payerName .
905 "*" .
906 "*" .
907 "*" .
908 "*" .
909 "*PI" .
910 "*" . $claim->payerID($ins) .
911 "~\n";
913 // if (!$claim->payerID($ins)) {
914 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
915 // }
917 // Payer address (N3 and N4) are added below so that Gateway EDI can
918 // auto-generate secondary claims. These do NOT appear in my copy of
919 // the spec! -- Rod 2008-06-12
921 if ($CMS_5010 || trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
922 ++$edicount;
923 $out .= "N3" .
924 "*" . $claim->payerStreet($ins) .
925 "~\n";
927 ++$edicount;
928 $out .= "N4" .
929 "*" . $claim->payerCity($ins) .
930 "*" . $claim->payerState($ins) .
931 "*" . stripZipCode($claim->payerZip($ins)) .
932 "~\n";
933 } // end Gateway EDI
935 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
936 // Segment REF (Other Payer Secondary Identifier) omitted.
937 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
938 // Segment REF*9F (Other Payer Referral Number) omitted.
939 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
940 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
941 // Segment NM1 (Other Payer Referring Provider) omitted.
942 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
943 // Segment NM1 (Other Payer Rendering Provider) omitted.
944 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
945 // Segment NM1 (Other Payer Service Facility Location) omitted.
946 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
947 // Segment NM1 (Other Payer Supervising Provider) omitted.
948 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
949 // Segment NM1 (Other Payer Billing Provider) omitted.
950 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
952 } // End loops 2320/2330*.
954 $loopcount = 0;
956 // Procedure loop starts here.
958 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
959 ++$loopcount;
961 ++$edicount;
962 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
963 "*$loopcount" .
964 "~\n";
966 ++$edicount;
967 $out .= "SV1" . // Professional Service. Page 400.
968 "*HC:" . $claim->cptKey($prockey) .
969 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
970 "*UN" .
971 "*" . $claim->cptUnits($prockey) .
972 "*" .
973 "*" .
974 "*";
975 $dia = $claim->diagIndexArray($prockey);
976 $i = 0;
977 foreach ($dia as $dindex) {
978 if ($i) $out .= ':';
979 $out .= $dindex;
980 if (++$i >= 4) break;
982 $out .= "~\n";
984 if (!$claim->cptCharges($prockey)) {
985 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
988 if (empty($dia)) {
989 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
992 // Segment SV5 (Durable Medical Equipment Service) omitted.
993 // Segment PWK (Line Supplemental Information) omitted.
994 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
995 // Segment CR1 (Ambulance Transport Information) omitted.
996 // Segment CR3 (Durable Medical Equipment Certification) omitted.
997 // Segment CRC (Ambulance Certification) omitted.
998 // Segment CRC (Hospice Employee Indicator) omitted.
999 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1001 ++$edicount;
1002 $out .= "DTP" . // Date of Service. Page 435.
1003 "*472" .
1004 "*D8" .
1005 "*" . $claim->serviceDate() .
1006 "~\n";
1008 $testnote = rtrim($claim->cptNotecodes($prockey));
1009 if (!empty($testnote)) {
1010 ++$edicount;
1011 $out .= "NTE" . // Explain Unusual Circumstances.
1012 "*ADD" .
1013 "*" . $claim->cptNotecodes($prockey) .
1014 "~\n";
1017 // Segment DTP*471 (Prescription Date) omitted.
1018 // Segment DTP*607 (Revision/Recertification Date) omitted.
1019 // Segment DTP*463 (Begin Therapy Date) omitted.
1020 // Segment DTP*461 (Last Certification Date) omitted.
1021 // Segment DTP*304 (Last Seen Date) omitted.
1022 // Segment DTP (Test Date) omitted.
1023 // Segment DTP*011 (Shipped Date) omitted.
1024 // Segment DTP*455 (Last X-Ray Date) omitted.
1025 // Segment DTP*454 (Initial Treatment Date) omitted.
1026 // Segment QTY (Ambulance Patient Count) omitted.
1027 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1028 // Segment MEA (Test Result) omitted.
1029 // Segment CN1 (Contract Information) omitted.
1030 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1031 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1032 // Segment REF*G1 (Prior Authorization) omitted.
1033 // Segment REF*6R (Line Item Control Number) omitted.
1034 // (Really oughta have this for robust 835 posting!)
1035 // Segment REF*EW (Mammography Certification Number) omitted.
1036 // Segment REF*X4 (CLIA Number) omitted.
1037 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1038 // Segment REF*BT (Immunization Batch Number) omitted.
1039 // Segment REF*9F (Referral Number) omitted.
1040 // Segment AMT*T (Sales Tax Amount) omitted.
1041 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1042 // Segment K3 (File Information) omitted.
1043 // Segment NTE (Line Note) omitted.
1044 // Segment NTE (Third Party Organization Notes) omitted.
1045 // Segment PS1 (Purchased Service Information) omitted.
1046 // Segment HCP (Line Pricing/Repricing Information) omitted.
1048 if (!$CMS_5010) {
1049 // This segment was deleted for 5010.
1051 // AMT*AAE segment for Approved Amount from previous payer.
1052 // Medicare secondaries seem to require this.
1054 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1055 if ($claim->payerSequence($ins) > $claim->payerSequence())
1056 continue; // payer is future, not previous
1057 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1058 ++$edicount;
1059 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1060 "*AAE" .
1061 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1062 "~\n";
1063 break;
1067 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1068 // with HCPCS codes.
1070 $ndc = $claim->cptNDCID($prockey);
1071 if ($ndc) {
1072 ++$edicount;
1073 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1074 "*" . // Per addendum, LIN01 is not used.
1075 "*N4" .
1076 "*" . $ndc .
1077 "~\n";
1079 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1080 $log .= "*** NDC code '$ndc' has invalid format!\n";
1083 ++$edicount;
1084 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1085 if (!$tmpunits) $tmpunits = 1;
1086 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1087 "*" .
1088 "*" .
1089 "*" . ($CMS_5010 ? '' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1090 "*" . $claim->cptNDCQuantity($prockey) .
1091 "*" . $claim->cptNDCUOM($prockey) .
1092 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1093 "~\n";
1096 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1098 // Loop 2420A, Rendering Provider (service-specific).
1099 // Used if the rendering provider for this service line is different
1100 // from that in loop 2310B.
1102 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1103 ++$edicount;
1104 $out .= "NM1" . // Loop 2310B Rendering Provider
1105 "*82" .
1106 "*1" .
1107 "*" . $claim->providerLastName($prockey) .
1108 "*" . $claim->providerFirstName($prockey) .
1109 "*" . $claim->providerMiddleName($prockey) .
1110 "*" .
1111 "*";
1112 if ($CMS_5010 || $claim->providerNPI($prockey)) { $out .=
1113 "*XX" .
1114 "*" . $claim->providerNPI($prockey);
1115 } else { $out .=
1116 "*34" . // Not allowed for 5010
1117 "*" . $claim->providerSSN($prockey);
1119 if (!$claim->providerNPI($prockey)) {
1120 $log .= "*** Rendering provider has no NPI.\n";
1122 $out .= "~\n";
1124 if ($claim->providerTaxonomy($prockey)) {
1125 ++$edicount;
1126 $out .= "PRV" .
1127 "*PE" . // PErforming provider
1128 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
1129 "*" . $claim->providerTaxonomy($prockey) .
1130 "~\n";
1133 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1134 // Segment REF (Rendering Provider Secondary Identification) omitted.
1135 // Segment NM1 (Purchased Service Provider Name) omitted.
1136 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1137 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1138 // Segment REF (Service Facility Location Secondary Identification) omitted.
1139 // Segment NM1 (Supervising Provider Name) omitted.
1140 // Segment REF (Supervising Provider Secondary Identification) omitted.
1141 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1142 // Segment REF (Ordering Provider Secondary Identification) omitted.
1143 // Segment PER (Ordering Provider Contact Information) omitted.
1144 // Segment NM1 (Referring Provider Name) omitted.
1145 // Segment REF (Referring Provider Secondary Identification) omitted.
1146 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1147 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1149 // REF*1C is required here for the Medicare provider number if NPI was
1150 // specified in NM109. Not sure if other payers require anything here.
1151 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1152 ++$edicount;
1153 $out .= "REF" .
1154 "*" . $claim->providerNumberType($prockey) .
1155 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1156 "*" . $claim->providerNumber($prockey) .
1157 "~\n";
1161 // Loop 2430, adjudication by previous payers.
1163 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1164 if ($claim->payerSequence($ins) > $claim->payerSequence())
1165 continue; // payer is future, not previous
1167 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1168 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1170 if ($payerpaid[1] == 0 && !count($aarr)) {
1171 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1172 "' has no payments or adjustments from previous payer!\n";
1173 continue;
1176 ++$edicount;
1177 $out .= "SVD" . // Service line adjudication. Page 554.
1178 "*" . $claim->payerID($ins) .
1179 "*" . $payerpaid[1] .
1180 "*HC:" . $claim->cptKey($prockey) .
1181 "*" .
1182 "*" . $claim->cptUnits($prockey) .
1183 "~\n";
1185 $tmpdate = $payerpaid[0];
1186 foreach ($aarr as $a) {
1187 ++$edicount;
1188 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1189 "*" . $a[1] .
1190 "*" . $a[2] .
1191 "*" . $a[3] .
1192 "~\n";
1193 if (!$tmpdate) $tmpdate = $a[0];
1195 // WTH is this??
1196 /*************************************************************
1197 if ( isset($a[4]) &&
1198 $a[4] != null ) {
1199 $out .= "CAS02" . // Previous payer's adjustment reason
1200 "*" . $a[4] .
1201 "~\n";
1203 *************************************************************/
1206 if ($tmpdate) {
1207 ++$edicount;
1208 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1209 "*573" .
1210 "*D8" .
1211 "*$tmpdate" .
1212 "~\n";
1215 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1216 // Segment LQ (Form Identification Code) omitted.
1217 // Segment FRM (Supporting Documentation) omitted.
1219 } // end loop 2430
1220 } // end this procedure
1222 ++$edicount;
1223 $out .= "SE" . // SE Trailer
1224 "*$edicount" .
1225 "*0021" .
1226 "~\n";
1228 $out .= "GE" . // GE Trailer
1229 "*1" .
1230 "*1" .
1231 "~\n";
1233 $out .= "IEA" . // IEA Trailer
1234 "*1" .
1235 "*000000001" .
1236 "~\n";
1238 // Remove any trailing empty fields (delimiters) from each segment.
1239 $out = preg_replace('/\*+~/', '~', $out);
1241 $log .= "\n";
1242 return $out;