minor fix in hover for prior commit
[openemr.git] / library / gen_x12_837.inc.php
blobeba0614583eb817b379bb441fa5ca742db991e76
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;
90 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
91 $tempName = $claim->billingFacilityName();
92 $partsName = explode(' ', $tempName);// Loop 1000A submitter entity == person
93 $num_parts = count($partsName);
94 switch ($num_parts) {
95 case "2":
96 $firstName = $partsName[0];
97 $middleName = '';
98 $lastName = $partsName[1];
99 $suffixName = '';
100 break;
101 case "3":
102 $firstName = $partsName[0];
103 $middleName = $partsName[1];
104 $lastName = $partsName[2];
105 $suffixName = '';
106 break;
107 case "4":
108 $firstName = $partsName[0];
109 $middleName = $partsName[1];
110 $lastName = $partsName[2];
111 $suffixName = $partsName[3];
112 break;
113 default:
114 $log .= "*** submitter name in 1000A loop has more than 4 parts, may not be desirable\n";
115 $firstName = $partsName[0];
116 $middleName = $partsName[1];
117 $lastName = $partsName[2];
118 $suffixName = $partsName[3];
120 $out .= "NM1" . // Loop 1000A Submitter
121 "*41" .
122 "*1" .
123 "*" . $lastName .
124 "*" . $firstName .
125 "*" . $middleName .
126 "*" . // Name Prefix not used
127 "*" . // Name Suffix not used
128 "*46";
129 } else { //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
130 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
131 if ($billingFacilityName == '') $log .= "*** billing facility name in 1000A loop is empty\n";
132 $out .= "NM1" .
133 "*41" .
134 "*2" .
135 "*" . $billingFacilityName .
136 "*" .
137 "*" .
138 "*" .
139 "*" .
140 "*46";
143 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
144 $out .= "*" . $claim->clearingHouseETIN();
145 } else {
146 $out .= "*" . $claim->billingFacilityETIN();
148 $out .= "~\n";
150 ++$edicount;
151 $out .= "PER" .
152 "*IC" .
153 "*" . $claim->billingContactName() .
154 "*TE" .
155 "*" . $claim->billingContactPhone();
156 if (!$CMS_5010 && $claim->x12gsper06()) {
157 $out .= "*ED*" . $claim->x12gsper06();
159 $out .= "~\n";
161 ++$edicount;
162 $out .= "NM1" . // Loop 1000B Receiver
163 "*40" .
164 "*2" .
165 "*" . $claim->clearingHouseName() .
166 "*" .
167 "*" .
168 "*" .
169 "*" .
170 "*46" .
171 "*" . $claim->clearingHouseETIN() .
172 "~\n";
174 $HLcount = 1;
176 ++$edicount;
177 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
178 "*$HLcount" .
179 "*" .
180 "*20" .
181 "*1" . // 1 indicates there are child segments
182 "~\n";
184 $HLBillingPayToProvider = $HLcount++;
186 // Situational PRV segment for provider taxonomy code for Medicaid.
187 if ($claim->claimType() == 'MC') {
188 ++$edicount;
189 $out .= "PRV*BI*ZZ" .
190 "*" . $claim->providerTaxonomy() .
191 "~\n";
194 // Situational CUR segment (foreign currency information) omitted here.
196 ++$edicount;
197 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
198 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
199 $tempName = $claim->billingFacilityName();
200 $partsName = explode(' ', $tempName);// Loop 2010AA Billing Provider entity == person
201 $num_parts = count($partsName);
202 switch ($num_parts) {
203 case "2":
204 $firstName = $partsName[0];
205 $middleName = '';
206 $lastName = $partsName[1];
207 $suffixName = '';
208 break;
209 case "3":
210 $firstName = $partsName[0];
211 $middleName = $partsName[1];
212 $lastName = $partsName[2];
213 $suffixName = '';
214 break;
215 case "4":
216 $firstName = $partsName[0];
217 $middleName = $partsName[1];
218 $lastName = $partsName[2];
219 $suffixName = $partsName[3];
220 break;
221 default:
222 $log .= "*** billing provider name in 2010AA loop has more than 4 parts, may not be desirable\n";
223 $firstName = $partsName[0];
224 $middleName = $partsName[1];
225 $lastName = $partsName[2];
226 $suffixName = $partsName[3];
228 $out .= "NM1" .
229 "*85" .
230 "*1" .
231 "*" . $lastName .
232 "*" . $firstName .
233 "*" . $middleName .
234 "*" . // Name Prefix not used
235 "*" . $suffixName;
237 else {
238 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
239 if ($billingFacilityName == '') $log .= "*** billing facility name in 2010A loop is empty\n";
240 $out .= "NM1" . // Loop 2010AA Billing Provider
241 "*85" .
242 "*2" .
243 "*" . $billingFacilityName .
244 "*" .
245 "*" .
246 "*" .
247 "*";
249 if ($claim->billingFacilityNPI()) {
250 $out .= "*XX*" . $claim->billingFacilityNPI();
252 else {
253 $log .= "*** Billing facility has no NPI.\n";
254 if ($CMS_5010) {
255 $out .= "*XX*";
257 else {
258 $out .= "*24*" . $claim->billingFacilityETIN();
261 $out .= "~\n";
263 ++$edicount;
264 $out .= "N3" .
265 "*" . $claim->billingFacilityStreet() .
266 "~\n";
268 ++$edicount;
269 $out .= "N4" .
270 "*" . $claim->billingFacilityCity() .
271 "*" . $claim->billingFacilityState() .
272 "*" . stripZipCode($claim->billingFacilityZip()) .
273 "~\n";
275 if ($CMS_5010 || ($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
276 ++$edicount;
277 $out .= "REF" ;
278 if($claim->federalIdType()){
279 $out .= "*" . $claim->federalIdType();
281 else{
282 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
284 $out .= "*" . $claim->billingFacilityETIN() .
285 "~\n";
288 if ($claim->providerNumberType() && $claim->providerNumber() &&
289 !($CMS_5010 && $claim->billingFacilityNPI()))
291 ++$edicount;
292 $out .= "REF" .
293 "*" . $claim->providerNumberType() .
294 "*" . $claim->providerNumber() .
295 "~\n";
297 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
298 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
301 // Situational PER*1C segment omitted.
303 // Pay-To Address defaults to billing provider and is no longer required in 5010.
304 if (!$CMS_5010) {
305 ++$edicount;
306 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
307 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
308 $out .= "NM1" . // Loop 2010AB Pay-To Provider
309 "*87" .
310 "*2" .
311 "*" . $billingFacilityName .
312 "*" .
313 "*" .
314 "*" .
315 "*";
316 if ($claim->billingFacilityNPI())
317 $out .= "*XX*" . $claim->billingFacilityNPI();
318 else
319 $out .= "*24*" . $claim->billingFacilityETIN();
320 $out .= "~\n";
322 ++$edicount;
323 $out .= "N3" .
324 "*" . $claim->billingFacilityStreet() .
325 "~\n";
327 ++$edicount;
328 $out .= "N4" .
329 "*" . $claim->billingFacilityCity() .
330 "*" . $claim->billingFacilityState() .
331 "*" . stripZipCode($claim->billingFacilityZip()) .
332 "~\n";
334 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
335 ++$edicount;
336 $out .= "REF" .
337 "*EI" .
338 "*" . $claim->billingFacilityETIN() .
339 "~\n";
343 // Loop 2010AC Pay-To Plan Name omitted. Includes:
344 // NM1*PE, N3, N4, REF*2U, REF*EI
346 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
347 $HLSubscriber = $HLcount++;
349 ++$edicount;
350 $out .= "HL" . // Loop 2000B Subscriber HL Loop
351 "*$HLSubscriber" .
352 "*$HLBillingPayToProvider" .
353 "*22" .
354 "*$PatientHL" .
355 "~\n";
357 if (!$claim->payerSequence()) {
358 $log .= "*** Error: Insurance information is missing!\n";
361 ++$edicount;
362 $out .= "SBR" . // Subscriber Information
363 "*" . $claim->payerSequence() .
364 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
365 "*" . $claim->groupNumber() .
366 "*" . (($CMS_5010 && $claim->groupNumber()) ? '' : $claim->groupName()) .
367 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
368 "*" .
369 "*" .
370 "*" .
371 "*" . $claim->claimType() . // Zirmed replaces this
372 "~\n";
374 // Segment PAT omitted.
376 ++$edicount;
377 $out .= "NM1" . // Loop 2010BA Subscriber
378 "*IL" .
379 "*1" . // 1 = person, 2 = non-person
380 "*" . $claim->insuredLastName() .
381 "*" . $claim->insuredFirstName() .
382 "*" . $claim->insuredMiddleName() .
383 "*" .
384 "*" . // Name Suffix
385 "*MI" .
386 // "MI" = Member Identification Number
387 // "II" = Standard Unique Health Identifier, "Required if the
388 // HIPAA Individual Patient Identifier is mandated use."
389 // Here we presume that is not true yet.
390 "*" . $claim->policyNumber() .
391 "~\n";
393 // For 5010, further subscriber info is sent only if they are the patient.
394 if (!$CMS_5010 || $claim->isSelfOfInsured()) {
395 ++$edicount;
396 $out .= "N3" .
397 "*" . $claim->insuredStreet() .
398 "~\n";
400 ++$edicount;
401 $out .= "N4" .
402 "*" . $claim->insuredCity() .
403 "*" . $claim->insuredState() .
404 "*" . stripZipCode($claim->insuredZip()) .
405 "~\n";
407 ++$edicount;
408 $out .= "DMG" .
409 "*D8" .
410 "*" . $claim->insuredDOB() .
411 "*" . $claim->insuredSex() .
412 "~\n";
415 // Segment REF*SY (Subscriber Secondary Identification) omitted.
416 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
417 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
419 ++$edicount;
420 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
421 $payerName = substr($claim->payerName(), 0, $CMS_5010 ? 60 : 35);
422 $out .= "NM1" . // Loop 2010BB Payer
423 "*PR" .
424 "*2" .
425 "*" . $payerName .
426 "*" .
427 "*" .
428 "*" .
429 "*" .
430 // The 5010 spec says:
431 // "On or after the mandated implementation date for the HIPAA
432 // National Plan Identifier (National Plan ID), XV must be sent.
433 // Prior to the mandated implementation date and prior to any phase-
434 // in period identified by Federal regulation, PI must be sent."
435 // *************** Anybody know what that date is? ***************
436 // August 2011 - Publish interim final rule
437 // October 1, 2012-March 31, 2013 - Enumeration
438 // April 1, 2013-September 30, 2013 - Testing
439 // October 1, 2013 - Implementation
441 "*PI" .
442 // Zirmed ignores this if using payer name matching:
443 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
444 "~\n";
446 // if (!$claim->payerID()) {
447 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
448 // }
450 if (true) { // !$CMS_5010
451 // The 5010 spec says:
452 // "Required when the payer address is available and the submitter intends
453 // for the claim to be printed on paper at the next EDI location (for example, a
454 // clearinghouse). If not required by this implementation guide, do not send."
456 ++$edicount;
457 $out .= "N3" .
458 "*" . $claim->payerStreet() .
459 "~\n";
461 ++$edicount;
462 $out .= "N4" .
463 "*" . $claim->payerCity() .
464 "*" . $claim->payerState() .
465 "*" . stripZipCode($claim->payerZip()) .
466 "~\n";
469 // Segment REF (Payer Secondary Identification) omitted.
470 // Segment REF (Billing Provider Secondary Identification) omitted.
472 if (! $claim->isSelfOfInsured()) {
473 ++$edicount;
474 $out .= "HL" . // Loop 2000C Patient Information
475 "*$HLcount" .
476 "*$HLSubscriber" .
477 "*23" .
478 "*0" .
479 "~\n";
481 $HLcount++;
483 ++$edicount;
484 $out .= "PAT" .
485 "*" . $claim->insuredRelationship() .
486 "~\n";
488 ++$edicount;
489 $out .= "NM1" . // Loop 2010CA Patient
490 "*QC" .
491 "*1" .
492 "*" . $claim->patientLastName() .
493 "*" . $claim->patientFirstName();
494 if ($claim->patientMiddleName() !== '') $out .= "*"
495 . $claim->patientMiddleName();
496 $out .= "~\n";
498 ++$edicount;
499 $out .= "N3" .
500 "*" . $claim->patientStreet() .
501 "~\n";
503 ++$edicount;
504 $out .= "N4" .
505 "*" . $claim->patientCity() .
506 "*" . $claim->patientState() .
507 "*" . stripZipCode($claim->patientZip()) .
508 "~\n";
510 ++$edicount;
511 $out .= "DMG" .
512 "*D8" .
513 "*" . $claim->patientDOB() .
514 "*" . $claim->patientSex() .
515 "~\n";
517 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
518 // Segment REF (Property and Casualty Patient Identifier) omitted.
519 // Segment PER (Property and Casualty Patient Contact Information) omitted.
521 } // end of patient different from insured
523 $proccount = $claim->procCount();
525 $clm_total_charges = 0;
526 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
527 $clm_total_charges += $claim->cptCharges($prockey);
530 if (!$clm_total_charges) {
531 $log .= "*** This claim has no charges!\n";
534 ++$edicount;
535 $out .= "CLM" . // Loop 2300 Claim
536 "*$pid-$encounter" .
537 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
538 "*" .
539 "*" .
540 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
541 ($CMS_5010 ? "B" : "") . ":" .
542 $claim->frequencyTypeCode() . // Changed to correct single digit output
543 "*Y" .
544 "*A" .
545 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
546 "*Y" .
547 ($CMS_5010 ? "" : "*C") .
548 "~\n";
550 if ($claim->onsetDate() &&
551 ($claim->onsetDate()!== $claim->serviceDate()) &&
552 ($claim->onsetDateValid())
554 ++$edicount;
555 $out .= "DTP" . // Date of Onset
556 "*431" .
557 "*D8" .
558 "*" . $claim->onsetDate() .
559 "~\n";
562 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
563 ++$edicount;
564 $out .= "DTP" . // Date of Initial Treatment
565 "*454" .
566 "*D8" .
567 "*" . $claim->dateInitialTreatment() .
568 "~\n";
571 // Segment DTP*304 (Last Seen Date) omitted.
572 // Segment DTP*453 (Acute Manifestation Date) omitted.
573 // Segment DTP*439 (Accident Date) omitted.
574 // Segment DTP*484 (Last Menstrual Period Date) omitted.
575 // Segment DTP*455 (Last X-Ray Date) omitted.
576 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
577 // Segments DTP (Disability Dates) omitted.
578 // Segment DTP*297 (Last Worked Date) omitted.
579 // Segment DTP*296 (Authorized Return to Work Date) omitted.
581 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
582 ++$edicount;
583 $out .= "DTP" . // Date of Hospitalization
584 "*435" .
585 "*D8" .
586 "*" . $claim->onsetDate() .
587 "~\n";
590 // Segment DTP*096 (Discharge Date) omitted.
591 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
592 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
593 // Segment DTP*050 (Repricer Received Date) omitted.
594 // Segment PWK (Claim Supplemental Information) omitted.
595 // Segment CN1 (Contract Information) omitted.
597 $patientpaid = $claim->patientPaidAmount();
598 if ($patientpaid != 0) {
599 ++$edicount;
600 $out .= "AMT" . // Patient paid amount. Page 190/220.
601 "*F5" .
602 "*" . $patientpaid .
603 "~\n";
606 // Segment REF*4N (Service Authorization Exception Code) omitted.
607 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
608 // Segment REF*EW (Mammography Certification Number) omitted.
609 // Segment REF*9F (Referral Number) omitted.
611 if ($claim->priorAuth()) {
612 ++$edicount;
613 $out .= "REF" . // Prior Authorization Number
614 "*G1" .
615 "*" . $claim->priorAuth() .
616 "~\n";
619 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
621 #if($claim->billing_options['replacement_claim'] == '1'){
622 if(trim($claim->billing_options['icn_resubmission_number']) > 3){
623 ++$edicount;
624 error_log("Method 1: ".$claim->billing_options['icn_resubmission_number'], 0);
625 $out .= "REF" .
626 "*F8" .
627 "*" . $claim->icnResubmissionNumber() .
628 "~\n";
632 if ($claim->cliaCode() && ($CMS_5010 || $claim->claimType() === 'MB')) {
633 // Required by Medicare when in-house labs are done.
634 ++$edicount;
635 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
636 "*X4" .
637 "*" . $claim->cliaCode() .
638 "~\n";
641 // Segment REF*9A (Repriced Claim Number) omitted.
642 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
643 // Segment REF*LX (Investigational Device Exemption Number) omitted.
644 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
645 // Segment REF*EA (Medical Record Number) omitted.
646 // Segment REF*P4 (Demonstration Project Identifier) omitted.
647 // Segment REF*1J (Care Plan Oversight) omitted.
648 // Segment K3 (File Information) omitted.
650 if ($claim->additionalNotes()) {
651 // Claim note.
652 ++$edicount;
653 $out .= "NTE" . // comments box 19
654 "*" . ($CMS_5010 ? "ADD" : "") .
655 "*" . $claim->additionalNotes() .
656 "~\n";
659 // Segment CR1 (Ambulance Transport Information) omitted.
660 // Segment CR2 (Spinal Manipulation Service Information) omitted.
661 // Segment CRC (Ambulance Certification) omitted.
662 // Segment CRC (Patient Condition Information: Vision) omitted.
663 // Segment CRC (Homebound Indicator) omitted.
665 // Segment CRC (EPSDT Referral).
666 if($claim->epsdtFlag()) {
667 ++$edicount;
668 $out .= "CRC" .
669 "*ZZ" .
670 "*Y" .
671 "*" . $claim->medicaidReferralCode() .
672 "~\n";
675 // Diagnoses, up to $max_per_seg per HI segment.
676 $max_per_seg = $CMS_5010 ? 12 : 8;
677 $da = $claim->diagArray();
678 if ($claim->diagtype == "ICD9") {
679 $diag_type_code = 'BK';
681 ELSE
683 $diag_type_code = 'ABK';
685 $tmp = 0;
686 foreach ($da as $diag) {
687 if ($tmp % $max_per_seg == 0) {
688 if ($tmp) $out .= "~\n";
689 ++$edicount;
690 $out .= "HI"; // Health Diagnosis Codes
692 $out .= "*$diag_type_code:" . $diag;
693 if ($claim->diagtype == "ICD9") {
694 $diag_type_code = 'BF';
696 ELSE
698 $diag_type_code = 'ABF';
700 ++$tmp;
702 if ($tmp) $out .= "~\n";
704 // Segment HI*BP (Anesthesia Related Procedure) omitted.
705 // Segment HI*BG (Condition Information) omitted.
706 // Segment HCP (Claim Pricing/Repricing Information) omitted.
708 if ($claim->referrerLastName()) {
709 // Medicare requires referring provider's name and UPIN.
710 ++$edicount;
711 $out .= "NM1" . // Loop 2310A Referring Provider
712 "*DN" .
713 "*1" .
714 "*" . $claim->referrerLastName() .
715 "*" . $claim->referrerFirstName() .
716 "*" . $claim->referrerMiddleName() .
717 "*" .
718 "*";
719 if ($CMS_5010 || $claim->referrerNPI()) { $out .=
720 "*XX" .
721 "*" . $claim->referrerNPI();
722 } else { $out .=
723 "*34" . // not allowed for 5010
724 "*" . $claim->referrerSSN();
726 $out .= "~\n";
728 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
729 ++$edicount;
730 $out .= "PRV" .
731 "*RF" . // ReFerring provider
732 "*ZZ" .
733 "*" . $claim->referrerTaxonomy() .
734 "~\n";
737 if (!CMS_5010 && $claim->referrerUPIN()) {
738 ++$edicount;
739 $out .= "REF" . // Referring Provider Secondary Identification
740 "*1G" .
741 "*" . $claim->referrerUPIN() .
742 "~\n";
747 /* Per the implementation guide lines, only include this information if it is different
748 * than the Loop 2010AA information
750 if(!$CMS_5010 ||
751 ($claim->providerNPIValid() &&
752 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
754 ++$edicount;
755 $out .= "NM1" . // Loop 2310B Rendering Provider
756 "*82" .
757 "*1" .
758 "*" . $claim->providerLastName() .
759 "*" . $claim->providerFirstName() .
760 "*" . $claim->providerMiddleName() .
761 "*" .
762 "*";
763 if ($CMS_5010 || $claim->providerNPI()) { $out .=
764 "*XX" .
765 "*" . $claim->providerNPI();
766 } else { $out .=
767 "*34" . // not allowed for 5010
768 "*" . $claim->providerSSN();
769 $log .= "*** Rendering provider has no NPI.\n";
771 $out .= "~\n";
773 if ($claim->providerTaxonomy()) {
774 ++$edicount;
775 $out .= "PRV" .
776 "*PE" . // Performing provider
777 "*" .($claim->claimType() != 'MC' ? "PXC" : "ZZ") .
778 "*" . $claim->providerTaxonomy() .
779 "~\n";
781 // End of Loop 2310B
783 else
785 // This loop can only get skipped if we are generating a 5010 claim
786 if(!($claim->providerNPIValid()))
788 /* If the loop was skipped because the provider NPI was invalid, generate
789 * a warning for the log.*/
790 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
792 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
793 * is a normal condition, so no need to warn.
798 // 4010: REF*1C is required here for the Medicare provider number if NPI was
799 // specified in NM109. Not sure if other payers require anything here.
800 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
802 // 5010 spec says nothing here if NPI was specified.
804 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
805 || (!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
807 if ($claim->providerNumber()) {
808 ++$edicount;
809 $out .= "REF" .
810 "*" . $claim->providerNumberType() .
811 "*" . $claim->providerNumber() .
812 "~\n";
816 // Loop 2310D is omitted in the case of home visits (POS=12).
817 if ($claim->facilityPOS() != 12 &&
818 (!$CMS_5010 || $claim->facilityNPI() != $claim->billingFacilityNPI()))
820 ++$edicount;
821 $out .= "NM1" . // Loop 2310D Service Location
822 "*77" .
823 "*2";
824 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
825 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ? 60 : 35);
826 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
827 "*" . $facilityName;
829 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
830 "*" .
831 "*" .
832 "*" .
833 "*";
834 if ($CMS_5010 || $claim->facilityNPI()) { $out .=
835 "*XX*" . $claim->facilityNPI();
836 } else { $out .=
837 "*24*" . $claim->facilityETIN();
839 if (!$claim->facilityNPI()) {
840 $log .= "*** Service location has no NPI.\n";
843 $out .= "~\n";
844 if ($claim->facilityStreet()) {
845 ++$edicount;
846 $out .= "N3" .
847 "*" . $claim->facilityStreet() .
848 "~\n";
850 if ($claim->facilityState()) {
851 ++$edicount;
852 $out .= "N4" .
853 "*" . $claim->facilityCity() .
854 "*" . $claim->facilityState() .
855 "*" . stripZipCode($claim->facilityZip()) .
856 "~\n";
860 // Segment REF (Service Facility Location Secondary Identification) omitted.
861 // Segment PER (Service Facility Contact Information) omitted.
863 // Loop 2310E, Supervising Provider
865 if ($claim->supervisorLastName()) {
866 ++$edicount;
867 $out .= "NM1" .
868 "*DQ" . // Supervising Physician
869 "*1" . // Person
870 "*" . $claim->supervisorLastName() .
871 "*" . $claim->supervisorFirstName() .
872 "*" . $claim->supervisorMiddleName() .
873 "*" . // NM106 not used
874 "*"; // Name Suffix
875 if ($CMS_5010 || $claim->supervisorNPI()) { $out .=
876 "*XX" .
877 "*" . $claim->supervisorNPI();
878 } else { $out .=
879 "*34" .
880 "*" . $claim->supervisorSSN();
882 if (!$claim->supervisorNPI()) {
883 $log .= "*** Supervising Provider has no NPI.\n";
885 $out .= "~\n";
887 if ($claim->supervisorNumber()) {
888 ++$edicount;
889 $out .= "REF" .
890 "*" . $claim->supervisorNumberType() .
891 "*" . $claim->supervisorNumber() .
892 "~\n";
896 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
897 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
899 $prev_pt_resp = $clm_total_charges; // for computation below
901 // Loops 2320 and 2330*, other subscriber/payer information.
902 // Remember that insurance index 0 is always for the payer being billed
903 // by this claim, and 1 and above are always for the "other" payers.
905 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
907 $tmp1 = $claim->claimType($ins);
908 $tmp2 = 'C1'; // Here a kludge. See page 321.
909 if ($tmp1 === 'CI') $tmp2 = 'C1';
910 if ($tmp1 === 'AM') $tmp2 = 'AP';
911 if ($tmp1 === 'HM') $tmp2 = 'HM';
912 if ($tmp1 === 'MB') $tmp2 = 'MB';
913 if ($tmp1 === 'MC') $tmp2 = 'MC';
914 if ($tmp1 === '09') $tmp2 = 'PP';
915 ++$edicount;
916 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
917 "*" . $claim->payerSequence($ins) .
918 "*" . $claim->insuredRelationship($ins) .
919 "*" . $claim->groupNumber($ins) .
920 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ? '' : $claim->groupName($ins)) .
921 "*" . ($CMS_5010 ? $claim->insuredTypeCode($ins) : $tmp2) .
922 "*" .
923 "*" .
924 "*" .
925 "*" . $claim->claimType($ins) .
926 "~\n";
928 // Things that apply only to previous payers, not future payers.
930 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
932 // Generate claim-level adjustments.
933 $aarr = $claim->payerAdjustments($ins);
934 foreach ($aarr as $a) {
935 ++$edicount;
936 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
937 "*" . $a[1] .
938 "*" . $a[2] .
939 "*" . $a[3] .
940 "~\n";
943 $payerpaid = $claim->payerTotals($ins);
944 ++$edicount;
945 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
946 "*D" .
947 "*" . $payerpaid[1] .
948 "~\n";
950 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
951 // Segment AMT*EAF (Remaining Patient Liability) omitted.
953 if (!$CMS_5010) {
954 // Patient responsibility amount as of this previous payer.
955 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
956 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
958 ++$edicount;
959 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
960 "*B6" .
961 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
962 "~\n";
964 ++$edicount;
965 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
966 "*F2" .
967 "*" . sprintf('%.2f', $prev_pt_resp) .
968 "~\n";
970 } // End of things that apply only to previous payers.
972 if (!$CMS_5010) {
973 ++$edicount;
974 $out .= "DMG" . // Other subscriber demographic information. Page 342.
975 "*D8" .
976 "*" . $claim->insuredDOB($ins) .
977 "*" . $claim->insuredSex($ins) .
978 "~\n";
981 ++$edicount;
982 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
983 "*" .
984 "*" .
985 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
986 // For this next item, the 5010 example in the spec does not match its
987 // description. So this might be wrong.
988 "*" . ($CMS_5010 ? '' : 'B') .
989 "*" .
990 "*Y" .
991 "~\n";
993 // Segment MOA (Medicare Outpatient Adjudication) omitted.
995 ++$edicount;
996 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
997 "*IL" .
998 "*1" .
999 "*" . $claim->insuredLastName($ins) .
1000 "*" . $claim->insuredFirstName($ins) .
1001 "*" . $claim->insuredMiddleName($ins) .
1002 "*" .
1003 "*" .
1004 "*MI" .
1005 "*" . $claim->policyNumber($ins) .
1006 "~\n";
1008 ++$edicount;
1009 $out .= "N3" .
1010 "*" . $claim->insuredStreet($ins) .
1011 "~\n";
1013 ++$edicount;
1014 $out .= "N4" .
1015 "*" . $claim->insuredCity($ins) .
1016 "*" . $claim->insuredState($ins) .
1017 "*" . stripZipCode($claim->insuredZip($ins)) .
1018 "~\n";
1020 // Segment REF (Other Subscriber Secondary Identification) omitted.
1022 ++$edicount;
1023 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
1024 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ? 60 : 35);
1025 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
1026 "*PR" .
1027 "*2" .
1028 "*" . $payerName .
1029 "*" .
1030 "*" .
1031 "*" .
1032 "*" .
1033 "*PI" .
1034 "*" . $claim->payerID($ins) .
1035 "~\n";
1037 // if (!$claim->payerID($ins)) {
1038 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
1039 // }
1041 // Payer address (N3 and N4) are added below so that Gateway EDI can
1042 // auto-generate secondary claims. These do NOT appear in my copy of
1043 // the spec! -- Rod 2008-06-12
1045 if ($CMS_5010 || trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
1046 ++$edicount;
1047 $out .= "N3" .
1048 "*" . $claim->payerStreet($ins) .
1049 "~\n";
1051 ++$edicount;
1052 $out .= "N4" .
1053 "*" . $claim->payerCity($ins) .
1054 "*" . $claim->payerState($ins) .
1055 "*" . stripZipCode($claim->payerZip($ins)) .
1056 "~\n";
1057 } // end Gateway EDI
1059 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
1060 // Segment REF (Other Payer Secondary Identifier) omitted.
1061 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
1062 // Segment REF*9F (Other Payer Referral Number) omitted.
1063 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
1064 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
1065 // Segment NM1 (Other Payer Referring Provider) omitted.
1066 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
1067 // Segment NM1 (Other Payer Rendering Provider) omitted.
1068 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
1069 // Segment NM1 (Other Payer Service Facility Location) omitted.
1070 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
1071 // Segment NM1 (Other Payer Supervising Provider) omitted.
1072 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
1073 // Segment NM1 (Other Payer Billing Provider) omitted.
1074 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
1076 } // End loops 2320/2330*.
1078 $loopcount = 0;
1080 // Procedure loop starts here.
1082 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
1083 ++$loopcount;
1085 ++$edicount;
1086 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
1087 "*$loopcount" .
1088 "~\n";
1090 ++$edicount;
1091 $out .= "SV1" . // Professional Service. Page 400.
1092 "*HC:" . $claim->cptKey($prockey) .
1093 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
1094 "*UN" .
1095 "*" . $claim->cptUnits($prockey) .
1096 "*" .
1097 "*" .
1098 "*";
1099 $dia = $claim->diagIndexArray($prockey);
1100 $i = 0;
1101 foreach ($dia as $dindex) {
1102 if ($i) $out .= ':';
1103 $out .= $dindex;
1104 if (++$i >= 4) break;
1106 # needed for epstd
1107 if($claim->epsdtFlag()) {
1108 $out .= "*" .
1109 "*" .
1110 "*" .
1111 "*Y" .
1112 "~\n";
1114 else
1116 $out .= "~\n";
1119 if (!$claim->cptCharges($prockey)) {
1120 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
1123 if (empty($dia)) {
1124 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
1127 // Segment SV5 (Durable Medical Equipment Service) omitted.
1128 // Segment PWK (Line Supplemental Information) omitted.
1129 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
1130 // Segment CR1 (Ambulance Transport Information) omitted.
1131 // Segment CR3 (Durable Medical Equipment Certification) omitted.
1132 // Segment CRC (Ambulance Certification) omitted.
1133 // Segment CRC (Hospice Employee Indicator) omitted.
1134 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1136 ++$edicount;
1137 $out .= "DTP" . // Date of Service. Page 435.
1138 "*472" .
1139 "*D8" .
1140 "*" . $claim->serviceDate() .
1141 "~\n";
1143 $testnote = rtrim($claim->cptNotecodes($prockey));
1144 if (!empty($testnote)) {
1145 ++$edicount;
1146 $out .= "NTE" . // Explain Unusual Circumstances.
1147 "*ADD" .
1148 "*" . $claim->cptNotecodes($prockey) .
1149 "~\n";
1152 // Segment DTP*471 (Prescription Date) omitted.
1153 // Segment DTP*607 (Revision/Recertification Date) omitted.
1154 // Segment DTP*463 (Begin Therapy Date) omitted.
1155 // Segment DTP*461 (Last Certification Date) omitted.
1156 // Segment DTP*304 (Last Seen Date) omitted.
1157 // Segment DTP (Test Date) omitted.
1158 // Segment DTP*011 (Shipped Date) omitted.
1159 // Segment DTP*455 (Last X-Ray Date) omitted.
1160 // Segment DTP*454 (Initial Treatment Date) omitted.
1161 // Segment QTY (Ambulance Patient Count) omitted.
1162 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1163 // Segment MEA (Test Result) omitted.
1164 // Segment CN1 (Contract Information) omitted.
1165 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1166 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1167 // Segment REF*G1 (Prior Authorization) omitted.
1168 // Segment REF*6R (Line Item Control Number) omitted.
1169 // (Really oughta have this for robust 835 posting!)
1170 // Segment REF*EW (Mammography Certification Number) omitted.
1171 // Segment REF*X4 (CLIA Number) omitted.
1172 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1173 // Segment REF*BT (Immunization Batch Number) omitted.
1174 // Segment REF*9F (Referral Number) omitted.
1175 // Segment AMT*T (Sales Tax Amount) omitted.
1176 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1177 // Segment K3 (File Information) omitted.
1178 // Segment NTE (Line Note) omitted.
1179 // Segment NTE (Third Party Organization Notes) omitted.
1180 // Segment PS1 (Purchased Service Information) omitted.
1181 // Segment HCP (Line Pricing/Repricing Information) omitted.
1183 if (!$CMS_5010) {
1184 // This segment was deleted for 5010.
1186 // AMT*AAE segment for Approved Amount from previous payer.
1187 // Medicare secondaries seem to require this.
1189 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1190 if ($claim->payerSequence($ins) > $claim->payerSequence())
1191 continue; // payer is future, not previous
1192 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1193 ++$edicount;
1194 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1195 "*AAE" .
1196 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1197 "~\n";
1198 break;
1202 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1203 // with HCPCS codes.
1205 $ndc = $claim->cptNDCID($prockey);
1206 if ($ndc) {
1207 ++$edicount;
1208 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1209 "*" . // Per addendum, LIN01 is not used.
1210 "*N4" .
1211 "*" . $ndc .
1212 "~\n";
1214 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1215 $log .= "*** NDC code '$ndc' has invalid format!\n";
1218 ++$edicount;
1219 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1220 if (!$tmpunits) $tmpunits = 1;
1221 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1222 "*" .
1223 "*" .
1224 "*" . ($CMS_5010 ? '' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1225 "*" . $claim->cptNDCQuantity($prockey) .
1226 "*" . $claim->cptNDCUOM($prockey) .
1227 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1228 "~\n";
1231 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1233 // Loop 2420A, Rendering Provider (service-specific).
1234 // Used if the rendering provider for this service line is different
1235 // from that in loop 2310B.
1237 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1238 ++$edicount;
1239 $out .= "NM1" . // Loop 2310B Rendering Provider
1240 "*82" .
1241 "*1" .
1242 "*" . $claim->providerLastName($prockey) .
1243 "*" . $claim->providerFirstName($prockey) .
1244 "*" . $claim->providerMiddleName($prockey) .
1245 "*" .
1246 "*";
1247 if ($CMS_5010 || $claim->providerNPI($prockey)) { $out .=
1248 "*XX" .
1249 "*" . $claim->providerNPI($prockey);
1250 } else { $out .=
1251 "*34" . // Not allowed for 5010
1252 "*" . $claim->providerSSN($prockey);
1254 if (!$claim->providerNPI($prockey)) {
1255 $log .= "*** Rendering provider has no NPI.\n";
1257 $out .= "~\n";
1259 if ($claim->providerTaxonomy($prockey)) {
1260 ++$edicount;
1261 $out .= "PRV" .
1262 "*PE" . // PErforming provider
1263 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
1264 "*" . $claim->providerTaxonomy($prockey) .
1265 "~\n";
1268 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1269 // Segment REF (Rendering Provider Secondary Identification) omitted.
1270 // Segment NM1 (Purchased Service Provider Name) omitted.
1271 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1272 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1273 // Segment REF (Service Facility Location Secondary Identification) omitted.
1274 // Segment NM1 (Supervising Provider Name) omitted.
1275 // Segment REF (Supervising Provider Secondary Identification) omitted.
1276 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1277 // Segment REF (Ordering Provider Secondary Identification) omitted.
1278 // Segment PER (Ordering Provider Contact Information) omitted.
1279 // Segment NM1 (Referring Provider Name) omitted.
1280 // Segment REF (Referring Provider Secondary Identification) omitted.
1281 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1282 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1284 // REF*1C is required here for the Medicare provider number if NPI was
1285 // specified in NM109. Not sure if other payers require anything here.
1286 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1287 ++$edicount;
1288 $out .= "REF" .
1289 "*" . $claim->providerNumberType($prockey) .
1290 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1291 "*" . $claim->providerNumber($prockey) .
1292 "~\n";
1296 // Loop 2430, adjudication by previous payers.
1298 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1299 if ($claim->payerSequence($ins) > $claim->payerSequence())
1300 continue; // payer is future, not previous
1302 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1303 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1305 if ($payerpaid[1] == 0 && !count($aarr)) {
1306 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1307 "' has no payments or adjustments from previous payer!\n";
1308 continue;
1311 ++$edicount;
1312 $out .= "SVD" . // Service line adjudication. Page 554.
1313 "*" . $claim->payerID($ins) .
1314 "*" . $payerpaid[1] .
1315 "*HC:" . $claim->cptKey($prockey) .
1316 "*" .
1317 "*" . $claim->cptUnits($prockey) .
1318 "~\n";
1320 $tmpdate = $payerpaid[0];
1321 foreach ($aarr as $a) {
1322 ++$edicount;
1323 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1324 "*" . $a[1] .
1325 "*" . $a[2] .
1326 "*" . $a[3] .
1327 "~\n";
1328 if (!$tmpdate) $tmpdate = $a[0];
1330 // WTH is this??
1331 /*************************************************************
1332 if ( isset($a[4]) &&
1333 $a[4] != null ) {
1334 $out .= "CAS02" . // Previous payer's adjustment reason
1335 "*" . $a[4] .
1336 "~\n";
1338 *************************************************************/
1341 if ($tmpdate) {
1342 ++$edicount;
1343 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1344 "*573" .
1345 "*D8" .
1346 "*$tmpdate" .
1347 "~\n";
1350 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1351 // Segment LQ (Form Identification Code) omitted.
1352 // Segment FRM (Supporting Documentation) omitted.
1354 } // end loop 2430
1355 } // end this procedure
1357 ++$edicount;
1358 $out .= "SE" . // SE Trailer
1359 "*$edicount" .
1360 "*0021" .
1361 "~\n";
1363 $out .= "GE" . // GE Trailer
1364 "*1" .
1365 "*1" .
1366 "~\n";
1368 $out .= "IEA" . // IEA Trailer
1369 "*1" .
1370 "*000000001" .
1371 "~\n";
1373 // Remove any trailing empty fields (delimiters) from each segment.
1374 $out = preg_replace('/\*+~/', '~', $out);
1376 $log .= "\n";
1377 return $out;