yet another security fix related to prior commits
[openemr.git] / library / gen_x12_837.inc.php
blobe655fd9e6c3f3fb82cc5b5823c780fe9bade1397
1 <?php
2 /*
3 * This program creates a 5010 837P file
5 * @package OpenEMR
6 * @author Rod Roark <rod@sunsetsystems.com>
7 * @author Stephen Waite <stephen.waite@cmsvt.com>
8 * @copyright Copyright (c) 2009 Rod Roark <rod@sunsetsystems.com>
9 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
10 * @link https://github.com/openemr/openemr/tree/master
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once(dirname(__FILE__) . "/invoice_summary.inc.php");
16 use OpenEMR\Billing\Claim;
18 function stripZipCode($zip)
20 return preg_replace('/[-\s]*/', '', $zip);
23 function gen_x12_837($pid, $encounter, &$log, $encounter_claim = false)
25 $today = time();
26 $out = '';
27 $claim = new Claim($pid, $encounter);
28 $edicount = 0;
29 $HLcount = 0;
31 $log .= "Generating claim $pid" . "-" . $encounter . " for " .
32 $claim->patientFirstName() . ' ' .
33 $claim->patientMiddleName() . ' ' .
34 $claim->patientLastName() . ' on ' .
35 date('Y-m-d H:i', $today) . ".\n";
37 $out .= "ISA" .
38 "*" . $claim->x12gsisa01() .
39 "*" . $claim->x12gsisa02() .
40 "*" . $claim->x12gsisa03() .
41 "*" . $claim->x12gsisa04() .
42 "*" . $claim->x12gsisa05() .
43 "*" . $claim->x12gssenderid() .
44 "*" . $claim->x12gsisa07() .
45 "*" . $claim->x12gsreceiverid() .
46 "*" . "030911" . // dummy data replace by billing_process.php
47 "*" . "1630" . // ditto
48 "*" . "^" .
49 "*" . "00501" .
50 "*" . "000000001" .
51 "*" . $claim->x12gsisa14() .
52 "*" . $claim->x12gsisa15() .
53 "*:" .
54 "~\n";
56 $out .= "GS" .
57 "*" . "HC" .
58 "*" . $claim->x12gsgs02() .
59 "*" . trim($claim->x12gs03()) .
60 "*" . date('Ymd', $today) .
61 "*" . date('Hi', $today) .
62 "*" . "1" .
63 "*" . "X" .
64 "*" . $claim->x12gsversionstring() .
65 "~\n";
67 ++$edicount;
68 $out .= "ST" .
69 "*" . "837" .
70 "*" . "0021" .
71 "*" . $claim->x12gsversionstring() .
72 "~\n";
74 ++$edicount;
75 $out .= "BHT" .
76 "*" . "0019" . // 0019 is required here
77 "*" . "00" . // 00 = original transmission
78 "*" . "0123" . // reference identification
79 "*" . date('Ymd', $today) . // transaction creation date
80 "*" . date('Hi', $today) . // transaction creation time
81 "*" . ($encounter_claim ? "RP" : "CH") . // RP = reporting, CH = chargeable
82 "~\n";
84 ++$edicount;
85 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
86 $firstName = $claim->providerFirstName();
87 $lastName = $claim->providerLastName();
88 $middleName = $claim->providerMiddleName();
89 $suffixName = $claim->providerSuffixName();
90 $out .= "NM1" . // Loop 1000A Submitter
91 "*" . "41" .
92 "*" . "1" .
93 "*" . $lastName .
94 "*" . $firstName .
95 "*" . $middleName .
96 "*" . // Name Prefix not used
97 "*" . $suffixName .
98 "*" . "46";
99 } else {
100 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
101 if ($billingFacilityName == '') {
102 $log .= "*** billing facility name in 1000A loop is empty\n";
104 $out .= "NM1" .
105 "*" . "41" .
106 "*" . "2" .
107 "*" . $billingFacilityName .
108 "*" .
109 "*" .
110 "*" .
111 "*" .
112 "*" . "46";
114 $out .= "*" . $claim->billingFacilityETIN();
115 $out .= "~\n";
117 ++$edicount;
118 $out .= "PER" . // Loop 1000A, Submitter EDI contact information
119 "*" . "IC" .
120 "*" . $claim->billingContactName() .
121 "*" . "TE" .
122 "*" . $claim->billingContactPhone() .
123 "*" . "EM" .
124 "*" . $claim->billingContactEmail();
125 $out .= "~\n";
127 ++$edicount;
128 $out .= "NM1" . // Loop 1000B Receiver
129 "*" . "40" .
130 "*" . "2" .
131 "*" . $claim->clearingHouseName() .
132 "*" .
133 "*" .
134 "*" .
135 "*" .
136 "*" . "46" .
137 "*" . $claim->clearingHouseETIN() .
138 "~\n";
140 ++$HLcount;
141 ++$edicount;
142 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
143 "*" . $HLcount .
144 "*" .
145 "*" . "20" .
146 "*" . "1" . // 1 indicates there are child segments
147 "~\n";
149 $HLBillingPayToProvider = $HLcount++;
151 // Situational PRV segment for provider taxonomy.
152 if ($claim->facilityTaxonomy()) {
153 ++$edicount;
154 $out .= "PRV" .
155 "*" . "BI" .
156 "*" . "PXC" .
157 "*" . $claim->facilityTaxonomy() .
158 "~\n";
161 // Situational CUR segment (foreign currency information) omitted here.
162 ++$edicount;
163 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
164 $firstName = $claim->providerFirstName();
165 $lastName = $claim->providerLastName();
166 $middleName = $claim->providerMiddleName();
167 $out .= "NM1" .
168 "*" . "85" .
169 "*" . "1" .
170 "*" . $lastName .
171 "*" . $firstName .
172 "*" . $middleName .
173 "*" . // Name Prefix not used
174 "*";
175 } else {
176 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
177 if ($billingFacilityName == '') {
178 $log .= "*** billing facility name in 2010A loop is empty\n";
180 $out .= "NM1" . // Loop 2010AA Billing Provider
181 "*" . "85" .
182 "*" . "2" .
183 "*" . $billingFacilityName .
184 "*" .
185 "*" .
186 "*" .
187 "*";
189 if ($claim->billingFacilityNPI()) {
190 $out .= "*XX*" . $claim->billingFacilityNPI();
191 } else {
192 $log .= "*** Billing facility has no NPI.\n";
194 $out .= "~\n";
196 ++$edicount;
197 $out .= "N3" .
198 "*" . $claim->billingFacilityStreet() .
199 "~\n";
201 ++$edicount;
202 $out .= "N4" .
203 "*" . $claim->billingFacilityCity() .
204 "*" . $claim->billingFacilityState() .
205 "*" . stripZipCode($claim->billingFacilityZip()) .
206 "~\n";
208 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
209 ++$edicount;
210 $out .= "REF";
211 if ($claim->federalIdType()) {
212 $out .= "*" . $claim->federalIdType();
213 } else {
214 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
216 $out .= "*" . $claim->billingFacilityETIN() . "~\n";
217 } else {
218 $log .= "*** No billing facility NPI and/or ETIN.\n";
220 if ($claim->providerNumberType() && $claim->providerNumber() && !$claim->billingFacilityNPI()) {
221 ++$edicount;
222 $out .= "REF" .
223 "*" . $claim->providerNumberType() .
224 "*" . $claim->providerNumber() .
225 "~\n";
226 } else if ($claim->providerNumber() && !$claim->providerNumberType()) {
227 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
230 // Situational PER*1C segment omitted.
232 // Pay-To Address defaults to billing provider and is no longer required in 5010 but may be useful
233 if ($claim->facilityStreet() != $claim->billingFacilityStreet()) {
234 ++$edicount;
235 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
236 $out .= "NM1" . // Loop 2010AB Pay-To Provider
237 "*" . "87" .
238 "*" . "2" .
239 "*" . $billingFacilityName .
240 "*" .
241 "*" .
242 "*" .
243 "*";
244 if ($claim->billingFacilityNPI()) {
245 $out .= "*XX*" . $claim->billingFacilityNPI();
247 $out .= "~\n";
249 ++$edicount;
250 $out .= "N3" .
251 "*" . $claim->billingFacilityStreet() .
252 "~\n";
254 ++$edicount;
255 $out .= "N4" .
256 "*" . $claim->billingFacilityCity() .
257 "*" . $claim->billingFacilityState() .
258 "*" . stripZipCode($claim->billingFacilityZip()) .
259 "~\n";
261 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
262 ++$edicount;
263 $out .= "REF" .
264 "*" . "EI" .
265 "*" . $claim->billingFacilityETIN() .
266 "~\n";
270 // Loop 2010AC Pay-To Plan Name omitted. Includes:
271 // NM1*PE, N3, N4, REF*2U, REF*EI
273 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
274 $HLSubscriber = $HLcount++;
276 ++$edicount;
277 $out .= "HL" . // Loop 2000B Subscriber HL Loop
278 "*" . $HLSubscriber .
279 "*" . $HLBillingPayToProvider .
280 "*" . "22" .
281 "*" . $PatientHL .
282 "~\n";
284 if (!$claim->payerSequence()) {
285 $log .= "*** Error: Insurance information is missing!\n";
288 ++$edicount;
289 $out .= "SBR" . // Subscriber Information
290 "*" . $claim->payerSequence() .
291 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
292 "*" . $claim->groupNumber() .
293 "*" . $claim->groupName() .
294 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
295 "*" .
296 "*" .
297 "*" .
298 "*" . $claim->claimType() .
299 "~\n";
301 // Segment PAT omitted.
303 ++$edicount;
304 $out .= "NM1" . // Loop 2010BA Subscriber
305 "*" . "IL" .
306 "*" . "1" . // 1 = person, 2 = non-person
307 "*" . $claim->insuredLastName() .
308 "*" . $claim->insuredFirstName() .
309 "*" . $claim->insuredMiddleName() .
310 "*" .
311 "*" . // Name Suffix not used
312 "*" . "MI" .
313 // "MI" = Member Identification Number
314 // "II" = Standard Unique Health Identifier, "Required if the
315 // HIPAA Individual Patient Identifier is mandated use."
316 // Here we presume that is not true yet.
317 "*" . $claim->policyNumber() .
318 "~\n";
320 // For 5010, further subscriber info is sent only if they are the patient.
321 if ($claim->isSelfOfInsured()) {
322 ++$edicount;
323 $out .= "N3" .
324 "*" . $claim->insuredStreet() .
325 "~\n";
327 ++$edicount;
328 $out .= "N4" .
329 "*" . $claim->insuredCity() .
330 "*" . $claim->insuredState() .
331 "*" . stripZipCode($claim->insuredZip()) .
332 "~\n";
334 ++$edicount;
335 $out .= "DMG" .
336 "*" . "D8" .
337 "*" . $claim->insuredDOB() .
338 "*" . $claim->insuredSex() .
339 "~\n";
342 // Segment REF*SY (Subscriber Secondary Identification) omitted.
343 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
344 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
346 ++$edicount;
347 $payerName = substr($claim->payerName(), 0, 60);
348 $out .= "NM1" . // Loop 2010BB Payer
349 "*" . "PR" .
350 "*" . "2" .
351 "*" . $payerName .
352 "*" .
353 "*" .
354 "*" .
355 "*" .
356 "*" . "PI" .
357 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
358 "~\n";
359 if (!$claim->payerID()) {
360 $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
363 ++$edicount;
364 $out .= "N3" .
365 "*" . $claim->payerStreet() .
366 "~\n";
368 ++$edicount;
369 $out .= "N4" .
370 "*" . $claim->payerCity() .
371 "*" . $claim->payerState() .
372 "*" . stripZipCode($claim->payerZip()) .
373 "~\n";
375 // Segment REF (Payer Secondary Identification) omitted.
376 // Segment REF (Billing Provider Secondary Identification) omitted.
378 if (!$claim->isSelfOfInsured()) {
379 ++$edicount;
380 $out .= "HL" . // Loop 2000C Patient Information
381 "*" . $HLcount .
382 "*" . $HLSubscriber .
383 "*" . "23" .
384 "*" . "0" .
385 "~\n";
387 $HLcount++;
388 ++$edicount;
389 $out .= "PAT" .
390 "*" . $claim->insuredRelationship() .
391 "~\n";
393 ++$edicount;
394 $out .= "NM1" . // Loop 2010CA Patient
395 "*" . "QC" .
396 "*" . "1" .
397 "*" . $claim->patientLastName() .
398 "*" . $claim->patientFirstName();
400 if ($claim->patientMiddleName() !== '') {
401 $out .= "*" . $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.
428 } // end of patient different from insured
430 $proccount = $claim->procCount();
431 $clm_total_charges = 0;
432 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
433 $clm_total_charges += $claim->cptCharges($prockey);
435 if (!$clm_total_charges) {
436 $log .= "*** This claim has no charges!\n";
439 ++$edicount;
440 $out .= "CLM" . // Loop 2300 Claim
441 "*" . $pid . "-" . $encounter .
442 "*" . sprintf("%.2f", $clm_total_charges) .
443 "*" .
444 "*" .
445 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" . "B" . ":" . $claim->frequencyTypeCode() .
446 "*" . "Y" .
447 "*" . "A" .
448 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
449 "*" . "Y" .
450 "~\n";
452 if ($claim->onsetDate() && ($claim->onsetDate() !== $claim->serviceDate()) && ($claim->onsetDateValid())) {
453 ++$edicount;
454 $out .= "DTP" . // Date of Onset
455 "*" . "431" .
456 "*" . "D8" .
457 "*" . $claim->onsetDate() .
458 "~\n";
461 // above is for historical use of encounter onset date, now in misc_billing_options
462 // Segment DTP*431 (Onset of Current Symptoms or Illness)
463 // Segment DTP*484 (Last Menstrual Period Date)
465 if ($claim->miscOnsetDate() && ($claim->box14Qualifier()) && ($claim->miscOnsetDateValid())) {
466 ++$edicount;
467 $out .= "DTP" . // Date Last Seen
468 "*" . $claim->box14Qualifier() .
469 "*" . "D8" .
470 "*" . $claim->miscOnsetDate() .
471 "~\n";
474 // Segment DTP*454 (Initial Treatment Date)
475 // Segment DTP*304 (Last Seen Date)
476 // Segment DTP*453 (Acute Manifestation Date)
477 // Segment DTP*439 (Accident Date)
478 // Segment DTP*455 (Last X-Ray Date)
479 // Segment DTP*471 (Hearing and Vision Prescription Date)
480 // Segment DTP*314 (Disability) omitted.
481 // Segment DTP*360 (Initial Disability Period Start) omitted.
482 // Segment DTP*361 (Initial Disability Period End) omitted.
483 // Segment DTP*297 (Last Worked Date)
484 // Segment DTP*296 (Authorized Return to Work Date)
486 if ($claim->dateInitialTreatment() && ($claim->box15Qualifier()) && ($claim->dateInitialTreatmentValid())) {
487 ++$edicount;
488 $out .= "DTP" . // Date Last Seen
489 "*" . $claim->box15Qualifier() .
490 "*" . "D8" .
491 "*" . $claim->dateInitialTreatment() .
492 "~\n";
495 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->onsetDateValid()) {
496 ++$edicount;
497 $out .= "DTP" . // Date of Hospitalization
498 "*" . "435" .
499 "*" . "D8" .
500 "*" . $claim->onsetDate() .
501 "~\n";
504 // above is for historical use of encounter onset date, now in misc_billing_options
505 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedFromDateValid()) {
506 ++$edicount;
507 $out .= "DTP" . // Date of Admission
508 "*" . "435" .
509 "*" . "D8" .
510 "*" . $claim->hospitalizedFrom() .
511 "~\n";
514 // Segment DTP*096 (Discharge Date)
515 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedToDateValid()) {
516 ++$edicount;
517 $out .= "DTP" . // Date of Discharge
518 "*" . "96" .
519 "*" . "D8" .
520 "*" . $claim->hospitalizedTo() .
521 "~\n";
524 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
525 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
526 // Segment DTP*050 (Repricer Received Date) omitted.
527 // Segment PWK (Claim Supplemental Information) omitted.
528 // Segment CN1 (Contract Information) omitted.
530 $patientpaid = $claim->patientPaidAmount();
531 if ($patientpaid != 0) {
532 ++$edicount;
533 $out .= "AMT" . // Patient paid amount. Page 190/220.
534 "*" . "F5" .
535 "*" . $patientpaid .
536 "~\n";
539 // Segment REF*4N (Service Authorization Exception Code) omitted.
540 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
541 // Segment REF*EW (Mammography Certification Number) omitted.
542 // Segment REF*9F (Referral Number) omitted.
544 if ($claim->priorAuth()) {
545 ++$edicount;
546 $out .= "REF" . // Prior Authorization Number
547 "*" . "G1" .
548 "*" . $claim->priorAuth() .
549 "~\n";
552 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
553 if (trim($claim->billing_options['icn_resubmission_number']) > 3) {
554 ++$edicount;
555 error_log("Method 1: " . $claim->billing_options['icn_resubmission_number'], 0);
556 $out .= "REF" .
557 "*" . "F8" .
558 "*" . $claim->icnResubmissionNumber() .
559 "~\n";
562 if ($claim->cliaCode() && ($claim->claimType() === 'MB')) {
563 // Required by Medicare when in-house labs are done.
564 ++$edicount;
565 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
566 "*" . "X4" .
567 "*" . $claim->cliaCode() .
568 "~\n";
571 // Segment REF*9A (Repriced Claim Number) omitted.
572 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
573 // Segment REF*LX (Investigational Device Exemption Number) omitted.
574 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
575 // Segment REF*EA (Medical Record Number) omitted.
576 // Segment REF*P4 (Demonstration Project Identifier) omitted.
577 // Segment REF*1J (Care Plan Oversight) omitted.
578 // Segment K3 (File Information) omitted.
579 if ($claim->additionalNotes()) {
580 // Claim note.
581 ++$edicount;
582 $out .= "NTE" . // comments box 19
583 "*" . "ADD" .
584 "*" . $claim->additionalNotes() .
585 "~\n";
588 // Segment CR1 (Ambulance Transport Information) omitted.
589 // Segment CR2 (Spinal Manipulation Service Information) omitted.
590 // Segment CRC (Ambulance Certification) omitted.
591 // Segment CRC (Patient Condition Information: Vision) omitted.
592 // Segment CRC (Homebound Indicator) omitted.
593 // Segment CRC (EPSDT Referral).
594 if ($claim->epsdtFlag()) {
595 ++$edicount;
596 $out .= "CRC" .
597 "*" . "ZZ" .
598 "*" . "Y" .
599 "*" . $claim->medicaidReferralCode() .
600 "~\n";
603 // Diagnoses, up to $max_per_seg per HI segment.
604 $max_per_seg = 12;
605 $da = $claim->diagArray();
606 if ($claim->diagtype == "ICD9") {
607 $diag_type_code = 'BK';
608 } else {
609 $diag_type_code = 'ABK';
611 $tmp = 0;
612 foreach ($da as $diag) {
613 if ($tmp % $max_per_seg == 0) {
614 if ($tmp) {
615 $out .= "~\n";
617 ++$edicount;
618 $out .= "HI"; // Health Diagnosis Codes
620 $out .= "*" . $diag_type_code . ":" . $diag;
621 if ($claim->diagtype == "ICD9") {
622 $diag_type_code = 'BF';
623 } else {
624 $diag_type_code = 'ABF';
626 ++$tmp;
629 if ($tmp) {
630 $out .= "~\n";
633 // Segment HI*BP (Anesthesia Related Procedure) omitted.
634 // Segment HI*BG (Condition Information) omitted.
635 // Segment HCP (Claim Pricing/Repricing Information) omitted.
636 if ($claim->referrerLastName()) {
637 // Medicare requires referring provider's name and UPIN.
638 ++$edicount;
639 $out .= "NM1" . // Loop 2310A Referring Provider
640 "*" . "DN" .
641 "*" . "1" .
642 "*" . $claim->referrerLastName() .
643 "*" . $claim->referrerFirstName() .
644 "*" . $claim->referrerMiddleName() .
645 "*" .
646 "*";
647 if ($claim->referrerNPI()) {
648 $out .=
649 "*" . "XX" .
650 "*" . $claim->referrerNPI();
651 } else {
652 $log .= "*** Referring provider has no NPI.\n";
654 $out .= "~\n";
657 // Per the implementation guide lines, only include this information if it is different
658 // than the Loop 2010AA information
659 if ($claim->providerNPIValid() && ($claim->billingFacilityNPI() !== $claim->providerNPI())) {
660 ++$edicount;
661 $out .= "NM1" . // Loop 2310B Rendering Provider
662 "*" . "82" .
663 "*" . "1" .
664 "*" . $claim->providerLastName() .
665 "*" . $claim->providerFirstName() .
666 "*" . $claim->providerMiddleName() .
667 "*" .
668 "*";
669 if ($claim->providerNPI()) {
670 $out .=
671 "*" . "XX" .
672 "*" . $claim->providerNPI();
673 } else {
674 $log .= "*** Rendering provider has no NPI.\n";
676 $out .= "~\n";
678 if ($claim->providerTaxonomy()) {
679 ++$edicount;
680 $out .= "PRV" .
681 "*" . "PE" . // Performing provider
682 "*" . "PXC" .
683 "*" . $claim->providerTaxonomy() .
684 "~\n";
685 } else {
686 $log .= "*** Performing provider has no taxonomy code.\n";
688 } else {
689 $log .= "*** Rendering provider is billing under a group.\n";
691 if (!$claim->providerNPIValid()) {
692 // If the loop was skipped because the provider NPI was invalid, generate a warning for the log.
693 $log .= "*** Skipping 2310B because " . $claim->providerLastName() . "," . $claim->providerFirstName() . " has invalid NPI.\n";
696 if (!$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B', '1G', 'G2', 'LU'))) {
697 if ($claim->providerNumber()) {
698 ++$edicount;
699 $out .= "REF" .
700 "*" . $claim->providerNumberType() .
701 "*" . $claim->providerNumber() .
702 "~\n";
705 // End of Loop 2310B
707 // Loop 2310C is omitted in the case of home visits (POS=12).
708 if ($claim->facilityPOS() != 12 && ($claim->facilityNPI() != $claim->billingFacilityNPI())) {
709 ++$edicount;
710 $out .= "NM1" . // Loop 2310C Service Location
711 "*" . "77" .
712 "*" . "2";
713 $facilityName = substr($claim->facilityName(), 0, 60);
714 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) {
715 $out .=
716 "*" . $facilityName;
717 } else {
718 $log .= "*** Check for invalid facility name, NPI, and/or tax id.\n";
720 if ($claim->facilityNPI() || $claim->facilityETIN()) {
721 $out .=
722 "*" .
723 "*" .
724 "*" .
725 "*";
726 if ($claim->facilityNPI()) {
727 $out .=
728 "*" . "XX" . "*" . $claim->facilityNPI();
729 } else {
730 $out .=
731 "*" . "24" . "*" . $claim->facilityETIN();
733 if (!$claim->facilityNPI()) {
734 $log .= "*** Service location has no NPI.\n";
738 $out .= "~\n";
739 if ($claim->facilityStreet()) {
740 ++$edicount;
741 $out .= "N3" .
742 "*" . $claim->facilityStreet() .
743 "~\n";
746 if ($claim->facilityState()) {
747 ++$edicount;
748 $out .= "N4" .
749 "*" . $claim->facilityCity() .
750 "*" . $claim->facilityState() .
751 "*" . stripZipCode($claim->facilityZip()) .
752 "~\n";
755 // Segment REF (Service Facility Location Secondary Identification) omitted.
756 // Segment PER (Service Facility Contact Information) omitted.
758 // Loop 2310E, Supervising Provider
759 if ($claim->supervisorLastName()) {
760 ++$edicount;
761 $out .= "NM1" .
762 "*" . "DQ" . // Supervising Physician
763 "*" . "1" . // Person
764 "*" . $claim->supervisorLastName() .
765 "*" . $claim->supervisorFirstName() .
766 "*" . $claim->supervisorMiddleName() .
767 "*" . // NM106 not used
768 "*"; // Name Suffix not used
769 if ($claim->supervisorNPI()) {
770 $out .=
771 "*" . "XX" .
772 "*" . $claim->supervisorNPI();
773 } else {
774 $log .= "*** Supervising Provider has no NPI.\n";
776 $out .= "~\n";
778 if ($claim->supervisorNumber()) {
779 ++$edicount;
780 $out .= "REF" .
781 "*" . $claim->supervisorNumberType() .
782 "*" . $claim->supervisorNumber() .
783 "~\n";
785 } else {
786 $log .= "*** Supervising provider has invalid last name.\n";
789 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
790 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
792 // Loops 2320 and 2330, other subscriber/payer information.
793 // Remember that insurance index 0 is always for the payer being billed
794 // by this claim, and 1 and above are always for the "other" payers.
796 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
797 $tmp1 = $claim->claimType($ins);
798 $tmp2 = 'C1'; // Here a kludge. See page 321.
799 if ($tmp1 === 'CI') {
800 $tmp2 = 'C1';
802 if ($tmp1 === 'AM') {
803 $tmp2 = 'AP';
805 if ($tmp1 === 'HM') {
806 $tmp2 = 'HM';
808 if ($tmp1 === 'MB') {
809 $tmp2 = 'MB';
811 if ($tmp1 === 'MC') {
812 $tmp2 = 'MC';
814 if ($tmp1 === '09') {
815 $tmp2 = 'PP';
818 ++$edicount;
819 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
820 "*" . $claim->payerSequence($ins) .
821 "*" . $claim->insuredRelationship($ins) .
822 "*" . $claim->groupNumber($ins) .
823 "*" . $claim->groupName($ins) .
824 "*" . $claim->insuredTypeCode($ins) .
825 "*" .
826 "*" .
827 "*" .
828 "*" . $claim->claimType($ins) .
829 "~\n";
831 // Things that apply only to previous payers, not future payers.
832 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
833 // Generate claim-level adjustments.
834 $aarr = $claim->payerAdjustments($ins);
835 foreach ($aarr as $a) {
836 ++$edicount;
837 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
838 "*" . $a[1] .
839 "*" . $a[2] .
840 "*" . $a[3] .
841 "~\n";
844 $payerpaid = $claim->payerTotals($ins);
845 ++$edicount;
846 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
847 "*" . "D" .
848 "*" . $payerpaid[1] .
849 "~\n";
850 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
851 // Segment AMT*EAF (Remaining Patient Liability) omitted.
852 } // End of things that apply only to previous payers.
854 ++$edicount;
855 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
856 "*" .
857 "*" .
858 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
859 // For this next item, the 5010 example in the spec does not match its
860 // description. So this might be wrong.
861 "*" .
862 "*" .
863 "*" .
864 "Y" .
865 "~\n";
867 // Segment MOA (Medicare Outpatient Adjudication) omitted.
868 ++$edicount;
869 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
870 "*" . "IL" .
871 "*" . "1" .
872 "*" . $claim->insuredLastName($ins) .
873 "*" . $claim->insuredFirstName($ins) .
874 "*" . $claim->insuredMiddleName($ins) .
875 "*" .
876 "*" .
877 "*" . "MI" .
878 "*" . $claim->policyNumber($ins) .
879 "~\n";
881 ++$edicount;
882 $out .= "N3" .
883 "*" . $claim->insuredStreet($ins) .
884 "~\n";
886 ++$edicount;
887 $out .= "N4" .
888 "*" . $claim->insuredCity($ins) .
889 "*" . $claim->insuredState($ins) .
890 "*" . stripZipCode($claim->insuredZip($ins)) .
891 "~\n";
893 // Segment REF (Other Subscriber Secondary Identification) omitted.
894 ++$edicount;
895 $payerName = substr($claim->payerName($ins), 0, 60);
896 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
897 "*" . "PR" .
898 "*" . "2" .
899 "*" . $payerName .
900 "*" .
901 "*" .
902 "*" .
903 "*" .
904 "*" . "PI" .
905 "*" . $claim->payerID($ins) .
906 "~\n";
908 if (!$claim->payerID($ins)) {
909 $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
912 ++$edicount;
913 $out .= "N3" .
914 "*" . $claim->payerStreet($ins) .
915 "~\n";
917 ++$edicount;
918 $out .= "N4" .
919 "*" . $claim->payerCity($ins) .
920 "*" . $claim->payerState($ins) .
921 "*" . stripZipCode($claim->payerZip($ins)) .
922 "~\n";
923 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
924 // Segment REF (Other Payer Secondary Identifier) omitted.
925 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
926 // Segment REF*9F (Other Payer Referral Number) omitted.
927 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
928 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
929 // Segment NM1 (Other Payer Referring Provider) omitted.
930 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
931 // Segment NM1 (Other Payer Rendering Provider) omitted.
932 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
933 // Segment NM1 (Other Payer Service Facility Location) omitted.
934 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
935 // Segment NM1 (Other Payer Supervising Provider) omitted.
936 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
937 // Segment NM1 (Other Payer Billing Provider) omitted.
938 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
939 } // End loops 2320/2330*.
941 $loopcount = 0;
943 // Procedure loop starts here.
944 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
945 ++$loopcount;
946 ++$edicount;
947 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
948 "*" . $loopcount .
949 "~\n";
951 ++$edicount;
952 $out .= "SV1" . // Professional Service. Page 400.
953 "*" . "HC:" . $claim->cptKey($prockey) .
954 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
955 "*" . "UN" .
956 "*" . $claim->cptUnits($prockey) .
957 "*" .
958 "*" .
959 "*";
960 $dia = $claim->diagIndexArray($prockey);
961 $i = 0;
962 foreach ($dia as $dindex) {
963 if ($i) {
964 $out .= ':';
967 $out .= $dindex;
968 if (++$i >= 4) {
969 break;
973 # needed for epstd
974 if ($claim->epsdtFlag()) {
975 $out .= "*" .
976 "*" .
977 "*" .
978 "*" . "Y" .
979 "~\n";
980 } else {
981 $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 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1049 // with HCPCS codes.
1051 $ndc = $claim->cptNDCID($prockey);
1053 if ($ndc) {
1054 ++$edicount;
1055 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1056 "*" . // Per addendum, LIN01 is not used.
1057 "*" . "N4" .
1058 "*" . $ndc .
1059 "~\n";
1061 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1062 $log .= "*** NDC code '$ndc' has invalid format!\n";
1065 ++$edicount;
1066 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1067 "*" .
1068 "*" .
1069 "*" .
1070 "*" . $claim->cptNDCQuantity($prockey) .
1071 "*" . $claim->cptNDCUOM($prockey) .
1072 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1073 "~\n";
1076 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1078 // Loop 2420A, Rendering Provider (service-specific).
1079 // Used if the rendering provider for this service line is different
1080 // from that in loop 2310B.
1082 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1083 ++$edicount;
1084 $out .= "NM1" . // Loop 2420A Rendering Provider
1085 "*" . "82" .
1086 "*" . "1" .
1087 "*" . $claim->providerLastName($prockey) .
1088 "*" . $claim->providerFirstName($prockey) .
1089 "*" . $claim->providerMiddleName($prockey) .
1090 "*" .
1091 "*";
1092 if ($claim->providerNPI($prockey)) {
1093 $out .=
1094 "*" . "XX" .
1095 "*" . $claim->providerNPI($prockey);
1096 } else {
1097 $log .= "*** Rendering provider has no NPI.\n";
1099 $out .= "~\n";
1101 // Segment PRV*PE (Rendering Provider Specialty Information) .
1103 if ($claim->providerTaxonomy($prockey)) {
1104 ++$edicount;
1105 $out .= "PRV" .
1106 "*" . "PE" . // PErforming provider
1107 "*" . "PXC" .
1108 "*" . $claim->providerTaxonomy($prockey) .
1109 "~\n";
1112 // Segment REF (Rendering Provider Secondary Identification) omitted.
1113 // Segment NM1 (Purchased Service Provider Name) omitted.
1114 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1115 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1116 // Segment REF (Service Facility Location Secondary Identification) omitted.
1117 // Segment NM1 (Supervising Provider Name) omitted.
1118 // Segment REF (Supervising Provider Secondary Identification) omitted.
1119 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1120 // Segment REF (Ordering Provider Secondary Identification) omitted.
1121 // Segment PER (Ordering Provider Contact Information) omitted.
1122 // Segment NM1 (Referring Provider Name) omitted.
1123 // Segment REF (Referring Provider Secondary Identification) omitted.
1124 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1125 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1127 // REF*1C is required here for the Medicare provider number if NPI was
1128 // specified in NM109. Not sure if other payers require anything here.
1130 if ($claim->providerNumberType($prockey) == "G2") {
1131 ++$edicount; $out .= "REF" . "*" . $claim->providerNumberType($prockey) .
1132 "*" . $claim->providerNumber($prockey) . "~\n";
1134 } // end provider exception
1136 // Loop 2430, adjudication by previous payers.
1138 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1139 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
1140 continue; // payer is future, not previous
1143 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1144 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1146 if ($payerpaid[1] == 0 && !count($aarr)) {
1147 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1148 "' has no payments or adjustments from previous payer!\n";
1149 continue;
1152 ++$edicount;
1153 $out .= "SVD" . // Service line adjudication. Page 554.
1154 "*" . $claim->payerID($ins) .
1155 "*" . $payerpaid[1] .
1156 "*" . "HC:" . $claim->cptKey($prockey) .
1157 "*" .
1158 "*" . $claim->cptUnits($prockey) .
1159 "~\n";
1161 $tmpdate = $payerpaid[0];
1162 foreach ($aarr as $a) {
1163 ++$edicount;
1164 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1165 "*" . $a[1] .
1166 "*" . $a[2] .
1167 "*" . $a[3] .
1168 "~\n";
1169 if (!$tmpdate) {
1170 $tmpdate = $a[0];
1174 if ($tmpdate) {
1175 ++$edicount;
1176 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1177 "*" . "573" .
1178 "*" . "D8" .
1179 "*" . $tmpdate .
1180 "~\n";
1183 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1184 // Segment LQ (Form Identification Code) omitted.
1185 // Segment FRM (Supporting Documentation) omitted.
1186 } // end loop 2430
1187 } // end this procedure
1189 ++$edicount;
1190 $out .= "SE" . // SE Trailer
1191 "*" . $edicount .
1192 "*" . "0021" .
1193 "~\n";
1195 $out .= "GE" . // GE Trailer
1196 "*" . "1" .
1197 "*" . "1" .
1198 "~\n";
1200 $out .= "IEA" . // IEA Trailer
1201 "*" . "1" .
1202 "*" . "000000001" .
1203 "~\n";
1205 // Remove any trailing empty fields (delimiters) from each segment.
1206 $out = preg_replace('/\*+~/', '~', $out);
1208 $log .= "\n";
1209 return $out;