fix for attendance form
[openemr.git] / library / gen_x12_837.inc.php
blobcc4134a9547eeebac00c68d7f613945545f2e8bf
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 && (!$CMS_5010 ||
818 ($claim->facilityNPI() != $claim->billingFacilityNPI() ||
819 ($claim->facilityNPI() == $claim->billingFacilityNPI() &&
820 $claim->facilityStreet() != $claim->billingFacilityStreet()))))
822 ++$edicount;
823 $out .= "NM1" . // Loop 2310D Service Location
824 "*77" .
825 "*2";
826 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
827 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ? 60 : 35);
828 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
829 "*" . $facilityName;
831 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
832 "*" .
833 "*" .
834 "*" .
835 "*";
836 if ($CMS_5010 || $claim->facilityNPI()) { $out .=
837 "*XX*" . $claim->facilityNPI();
838 } else { $out .=
839 "*24*" . $claim->facilityETIN();
841 if (!$claim->facilityNPI()) {
842 $log .= "*** Service location has no NPI.\n";
845 $out .= "~\n";
846 if ($claim->facilityStreet()) {
847 ++$edicount;
848 $out .= "N3" .
849 "*" . $claim->facilityStreet() .
850 "~\n";
852 if ($claim->facilityState()) {
853 ++$edicount;
854 $out .= "N4" .
855 "*" . $claim->facilityCity() .
856 "*" . $claim->facilityState() .
857 "*" . stripZipCode($claim->facilityZip()) .
858 "~\n";
862 // Segment REF (Service Facility Location Secondary Identification) omitted.
863 // Segment PER (Service Facility Contact Information) omitted.
865 // Loop 2310E, Supervising Provider
867 if ($claim->supervisorLastName()) {
868 ++$edicount;
869 $out .= "NM1" .
870 "*DQ" . // Supervising Physician
871 "*1" . // Person
872 "*" . $claim->supervisorLastName() .
873 "*" . $claim->supervisorFirstName() .
874 "*" . $claim->supervisorMiddleName() .
875 "*" . // NM106 not used
876 "*"; // Name Suffix
877 if ($CMS_5010 || $claim->supervisorNPI()) { $out .=
878 "*XX" .
879 "*" . $claim->supervisorNPI();
880 } else { $out .=
881 "*34" .
882 "*" . $claim->supervisorSSN();
884 if (!$claim->supervisorNPI()) {
885 $log .= "*** Supervising Provider has no NPI.\n";
887 $out .= "~\n";
889 if ($claim->supervisorNumber()) {
890 ++$edicount;
891 $out .= "REF" .
892 "*" . $claim->supervisorNumberType() .
893 "*" . $claim->supervisorNumber() .
894 "~\n";
898 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
899 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
901 $prev_pt_resp = $clm_total_charges; // for computation below
903 // Loops 2320 and 2330*, other subscriber/payer information.
904 // Remember that insurance index 0 is always for the payer being billed
905 // by this claim, and 1 and above are always for the "other" payers.
907 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
909 $tmp1 = $claim->claimType($ins);
910 $tmp2 = 'C1'; // Here a kludge. See page 321.
911 if ($tmp1 === 'CI') $tmp2 = 'C1';
912 if ($tmp1 === 'AM') $tmp2 = 'AP';
913 if ($tmp1 === 'HM') $tmp2 = 'HM';
914 if ($tmp1 === 'MB') $tmp2 = 'MB';
915 if ($tmp1 === 'MC') $tmp2 = 'MC';
916 if ($tmp1 === '09') $tmp2 = 'PP';
917 ++$edicount;
918 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
919 "*" . $claim->payerSequence($ins) .
920 "*" . $claim->insuredRelationship($ins) .
921 "*" . $claim->groupNumber($ins) .
922 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ? '' : $claim->groupName($ins)) .
923 "*" . ($CMS_5010 ? $claim->insuredTypeCode($ins) : $tmp2) .
924 "*" .
925 "*" .
926 "*" .
927 "*" . $claim->claimType($ins) .
928 "~\n";
930 // Things that apply only to previous payers, not future payers.
932 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
934 // Generate claim-level adjustments.
935 $aarr = $claim->payerAdjustments($ins);
936 foreach ($aarr as $a) {
937 ++$edicount;
938 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
939 "*" . $a[1] .
940 "*" . $a[2] .
941 "*" . $a[3] .
942 "~\n";
945 $payerpaid = $claim->payerTotals($ins);
946 ++$edicount;
947 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
948 "*D" .
949 "*" . $payerpaid[1] .
950 "~\n";
952 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
953 // Segment AMT*EAF (Remaining Patient Liability) omitted.
955 if (!$CMS_5010) {
956 // Patient responsibility amount as of this previous payer.
957 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
958 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
960 ++$edicount;
961 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
962 "*B6" .
963 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
964 "~\n";
966 ++$edicount;
967 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
968 "*F2" .
969 "*" . sprintf('%.2f', $prev_pt_resp) .
970 "~\n";
972 } // End of things that apply only to previous payers.
974 if (!$CMS_5010) {
975 ++$edicount;
976 $out .= "DMG" . // Other subscriber demographic information. Page 342.
977 "*D8" .
978 "*" . $claim->insuredDOB($ins) .
979 "*" . $claim->insuredSex($ins) .
980 "~\n";
983 ++$edicount;
984 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
985 "*" .
986 "*" .
987 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
988 // For this next item, the 5010 example in the spec does not match its
989 // description. So this might be wrong.
990 "*" . ($CMS_5010 ? '' : 'B') .
991 "*" .
992 "*Y" .
993 "~\n";
995 // Segment MOA (Medicare Outpatient Adjudication) omitted.
997 ++$edicount;
998 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
999 "*IL" .
1000 "*1" .
1001 "*" . $claim->insuredLastName($ins) .
1002 "*" . $claim->insuredFirstName($ins) .
1003 "*" . $claim->insuredMiddleName($ins) .
1004 "*" .
1005 "*" .
1006 "*MI" .
1007 "*" . $claim->policyNumber($ins) .
1008 "~\n";
1010 ++$edicount;
1011 $out .= "N3" .
1012 "*" . $claim->insuredStreet($ins) .
1013 "~\n";
1015 ++$edicount;
1016 $out .= "N4" .
1017 "*" . $claim->insuredCity($ins) .
1018 "*" . $claim->insuredState($ins) .
1019 "*" . stripZipCode($claim->insuredZip($ins)) .
1020 "~\n";
1022 // Segment REF (Other Subscriber Secondary Identification) omitted.
1024 ++$edicount;
1025 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
1026 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ? 60 : 35);
1027 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
1028 "*PR" .
1029 "*2" .
1030 "*" . $payerName .
1031 "*" .
1032 "*" .
1033 "*" .
1034 "*" .
1035 "*PI" .
1036 "*" . $claim->payerID($ins) .
1037 "~\n";
1039 // if (!$claim->payerID($ins)) {
1040 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
1041 // }
1043 // Payer address (N3 and N4) are added below so that Gateway EDI can
1044 // auto-generate secondary claims. These do NOT appear in my copy of
1045 // the spec! -- Rod 2008-06-12
1047 if ($CMS_5010 || trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
1048 ++$edicount;
1049 $out .= "N3" .
1050 "*" . $claim->payerStreet($ins) .
1051 "~\n";
1053 ++$edicount;
1054 $out .= "N4" .
1055 "*" . $claim->payerCity($ins) .
1056 "*" . $claim->payerState($ins) .
1057 "*" . stripZipCode($claim->payerZip($ins)) .
1058 "~\n";
1059 } // end Gateway EDI
1061 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
1062 // Segment REF (Other Payer Secondary Identifier) omitted.
1063 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
1064 // Segment REF*9F (Other Payer Referral Number) omitted.
1065 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
1066 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
1067 // Segment NM1 (Other Payer Referring Provider) omitted.
1068 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
1069 // Segment NM1 (Other Payer Rendering Provider) omitted.
1070 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
1071 // Segment NM1 (Other Payer Service Facility Location) omitted.
1072 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
1073 // Segment NM1 (Other Payer Supervising Provider) omitted.
1074 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
1075 // Segment NM1 (Other Payer Billing Provider) omitted.
1076 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
1078 } // End loops 2320/2330*.
1080 $loopcount = 0;
1082 // Procedure loop starts here.
1084 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
1085 ++$loopcount;
1087 ++$edicount;
1088 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
1089 "*$loopcount" .
1090 "~\n";
1092 ++$edicount;
1093 $out .= "SV1" . // Professional Service. Page 400.
1094 "*HC:" . $claim->cptKey($prockey) .
1095 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
1096 "*UN" .
1097 "*" . $claim->cptUnits($prockey) .
1098 "*" .
1099 "*" .
1100 "*";
1101 $dia = $claim->diagIndexArray($prockey);
1102 $i = 0;
1103 foreach ($dia as $dindex) {
1104 if ($i) $out .= ':';
1105 $out .= $dindex;
1106 if (++$i >= 4) break;
1108 # needed for epstd
1109 if($claim->epsdtFlag()) {
1110 $out .= "*" .
1111 "*" .
1112 "*" .
1113 "*Y" .
1114 "~\n";
1116 else
1118 $out .= "~\n";
1121 if (!$claim->cptCharges($prockey)) {
1122 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
1125 if (empty($dia)) {
1126 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
1129 // Segment SV5 (Durable Medical Equipment Service) omitted.
1130 // Segment PWK (Line Supplemental Information) omitted.
1131 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
1132 // Segment CR1 (Ambulance Transport Information) omitted.
1133 // Segment CR3 (Durable Medical Equipment Certification) omitted.
1134 // Segment CRC (Ambulance Certification) omitted.
1135 // Segment CRC (Hospice Employee Indicator) omitted.
1136 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1138 ++$edicount;
1139 $out .= "DTP" . // Date of Service. Page 435.
1140 "*472" .
1141 "*D8" .
1142 "*" . $claim->serviceDate() .
1143 "~\n";
1145 $testnote = rtrim($claim->cptNotecodes($prockey));
1146 if (!empty($testnote)) {
1147 ++$edicount;
1148 $out .= "NTE" . // Explain Unusual Circumstances.
1149 "*ADD" .
1150 "*" . $claim->cptNotecodes($prockey) .
1151 "~\n";
1154 // Segment DTP*471 (Prescription Date) omitted.
1155 // Segment DTP*607 (Revision/Recertification Date) omitted.
1156 // Segment DTP*463 (Begin Therapy Date) omitted.
1157 // Segment DTP*461 (Last Certification Date) omitted.
1158 // Segment DTP*304 (Last Seen Date) omitted.
1159 // Segment DTP (Test Date) omitted.
1160 // Segment DTP*011 (Shipped Date) omitted.
1161 // Segment DTP*455 (Last X-Ray Date) omitted.
1162 // Segment DTP*454 (Initial Treatment Date) omitted.
1163 // Segment QTY (Ambulance Patient Count) omitted.
1164 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1165 // Segment MEA (Test Result) omitted.
1166 // Segment CN1 (Contract Information) omitted.
1167 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1168 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1169 // Segment REF*G1 (Prior Authorization) omitted.
1170 // Segment REF*6R (Line Item Control Number) omitted.
1171 // (Really oughta have this for robust 835 posting!)
1172 // Segment REF*EW (Mammography Certification Number) omitted.
1173 // Segment REF*X4 (CLIA Number) omitted.
1174 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1175 // Segment REF*BT (Immunization Batch Number) omitted.
1176 // Segment REF*9F (Referral Number) omitted.
1177 // Segment AMT*T (Sales Tax Amount) omitted.
1178 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1179 // Segment K3 (File Information) omitted.
1180 // Segment NTE (Line Note) omitted.
1181 // Segment NTE (Third Party Organization Notes) omitted.
1182 // Segment PS1 (Purchased Service Information) omitted.
1183 // Segment HCP (Line Pricing/Repricing Information) omitted.
1185 if (!$CMS_5010) {
1186 // This segment was deleted for 5010.
1188 // AMT*AAE segment for Approved Amount from previous payer.
1189 // Medicare secondaries seem to require this.
1191 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1192 if ($claim->payerSequence($ins) > $claim->payerSequence())
1193 continue; // payer is future, not previous
1194 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1195 ++$edicount;
1196 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1197 "*AAE" .
1198 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1199 "~\n";
1200 break;
1204 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1205 // with HCPCS codes.
1207 $ndc = $claim->cptNDCID($prockey);
1208 if ($ndc) {
1209 ++$edicount;
1210 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1211 "*" . // Per addendum, LIN01 is not used.
1212 "*N4" .
1213 "*" . $ndc .
1214 "~\n";
1216 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1217 $log .= "*** NDC code '$ndc' has invalid format!\n";
1220 ++$edicount;
1221 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1222 if (!$tmpunits) $tmpunits = 1;
1223 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1224 "*" .
1225 "*" .
1226 "*" . ($CMS_5010 ? '' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1227 "*" . $claim->cptNDCQuantity($prockey) .
1228 "*" . $claim->cptNDCUOM($prockey) .
1229 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1230 "~\n";
1233 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1235 // Loop 2420A, Rendering Provider (service-specific).
1236 // Used if the rendering provider for this service line is different
1237 // from that in loop 2310B.
1239 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1240 ++$edicount;
1241 $out .= "NM1" . // Loop 2310B Rendering Provider
1242 "*82" .
1243 "*1" .
1244 "*" . $claim->providerLastName($prockey) .
1245 "*" . $claim->providerFirstName($prockey) .
1246 "*" . $claim->providerMiddleName($prockey) .
1247 "*" .
1248 "*";
1249 if ($CMS_5010 || $claim->providerNPI($prockey)) { $out .=
1250 "*XX" .
1251 "*" . $claim->providerNPI($prockey);
1252 } else { $out .=
1253 "*34" . // Not allowed for 5010
1254 "*" . $claim->providerSSN($prockey);
1256 if (!$claim->providerNPI($prockey)) {
1257 $log .= "*** Rendering provider has no NPI.\n";
1259 $out .= "~\n";
1261 if ($claim->providerTaxonomy($prockey)) {
1262 ++$edicount;
1263 $out .= "PRV" .
1264 "*PE" . // PErforming provider
1265 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
1266 "*" . $claim->providerTaxonomy($prockey) .
1267 "~\n";
1270 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1271 // Segment REF (Rendering Provider Secondary Identification) omitted.
1272 // Segment NM1 (Purchased Service Provider Name) omitted.
1273 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1274 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1275 // Segment REF (Service Facility Location Secondary Identification) omitted.
1276 // Segment NM1 (Supervising Provider Name) omitted.
1277 // Segment REF (Supervising Provider Secondary Identification) omitted.
1278 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1279 // Segment REF (Ordering Provider Secondary Identification) omitted.
1280 // Segment PER (Ordering Provider Contact Information) omitted.
1281 // Segment NM1 (Referring Provider Name) omitted.
1282 // Segment REF (Referring Provider Secondary Identification) omitted.
1283 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1284 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1286 // REF*1C is required here for the Medicare provider number if NPI was
1287 // specified in NM109. Not sure if other payers require anything here.
1288 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1289 ++$edicount;
1290 $out .= "REF" .
1291 "*" . $claim->providerNumberType($prockey) .
1292 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1293 "*" . $claim->providerNumber($prockey) .
1294 "~\n";
1298 // Loop 2430, adjudication by previous payers.
1300 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1301 if ($claim->payerSequence($ins) > $claim->payerSequence())
1302 continue; // payer is future, not previous
1304 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1305 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1307 if ($payerpaid[1] == 0 && !count($aarr)) {
1308 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1309 "' has no payments or adjustments from previous payer!\n";
1310 continue;
1313 ++$edicount;
1314 $out .= "SVD" . // Service line adjudication. Page 554.
1315 "*" . $claim->payerID($ins) .
1316 "*" . $payerpaid[1] .
1317 "*HC:" . $claim->cptKey($prockey) .
1318 "*" .
1319 "*" . $claim->cptUnits($prockey) .
1320 "~\n";
1322 $tmpdate = $payerpaid[0];
1323 foreach ($aarr as $a) {
1324 ++$edicount;
1325 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1326 "*" . $a[1] .
1327 "*" . $a[2] .
1328 "*" . $a[3] .
1329 "~\n";
1330 if (!$tmpdate) $tmpdate = $a[0];
1332 // WTH is this??
1333 /*************************************************************
1334 if ( isset($a[4]) &&
1335 $a[4] != null ) {
1336 $out .= "CAS02" . // Previous payer's adjustment reason
1337 "*" . $a[4] .
1338 "~\n";
1340 *************************************************************/
1343 if ($tmpdate) {
1344 ++$edicount;
1345 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1346 "*573" .
1347 "*D8" .
1348 "*$tmpdate" .
1349 "~\n";
1352 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1353 // Segment LQ (Form Identification Code) omitted.
1354 // Segment FRM (Supporting Documentation) omitted.
1356 } // end loop 2430
1357 } // end this procedure
1359 ++$edicount;
1360 $out .= "SE" . // SE Trailer
1361 "*$edicount" .
1362 "*0021" .
1363 "~\n";
1365 $out .= "GE" . // GE Trailer
1366 "*1" .
1367 "*1" .
1368 "~\n";
1370 $out .= "IEA" . // IEA Trailer
1371 "*1" .
1372 "*000000001" .
1373 "~\n";
1375 // Remove any trailing empty fields (delimiters) from each segment.
1376 $out = preg_replace('/\*+~/', '~', $out);
1378 $log .= "\n";
1379 return $out;