mysql 8 fixes (#1639)
[openemr.git] / library / gen_x12_837.inc.php
blob4f0ee059a92e7745809c3d8823773b3be33217c7
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 $temp = preg_replace('/[-\s]*/', '', $zip);
21 if (strlen($temp) == 5) {
22 return $temp . "9999";
23 } else {
24 return $temp;
28 function gen_x12_837($pid, $encounter, &$log, $encounter_claim = false)
30 $today = time();
31 $out = '';
32 $claim = new Claim($pid, $encounter);
33 $edicount = 0;
34 $HLcount = 0;
36 $log .= "Generating claim $pid" . "-" . $encounter . " for " .
37 $claim->patientFirstName() . ' ' .
38 $claim->patientMiddleName() . ' ' .
39 $claim->patientLastName() . ' on ' .
40 date('Y-m-d H:i', $today) . ".\n";
42 $out .= "ISA" .
43 "*" . $claim->x12gsisa01() .
44 "*" . $claim->x12gsisa02() .
45 "*" . $claim->x12gsisa03() .
46 "*" . $claim->x12gsisa04() .
47 "*" . $claim->x12gsisa05() .
48 "*" . $claim->x12gssenderid() .
49 "*" . $claim->x12gsisa07() .
50 "*" . $claim->x12gsreceiverid() .
51 "*" . "030911" . // dummy data replace by billing_process.php
52 "*" . "1630" . // ditto
53 "*" . "^" .
54 "*" . "00501" .
55 "*" . "000000001" .
56 "*" . $claim->x12gsisa14() .
57 "*" . $claim->x12gsisa15() .
58 "*:" .
59 "~\n";
61 $out .= "GS" .
62 "*" . "HC" .
63 "*" . $claim->x12gsgs02() .
64 "*" . trim($claim->x12gs03()) .
65 "*" . date('Ymd', $today) .
66 "*" . date('Hi', $today) .
67 "*" . "1" .
68 "*" . "X" .
69 "*" . $claim->x12gsversionstring() .
70 "~\n";
72 ++$edicount;
73 $out .= "ST" .
74 "*" . "837" .
75 "*" . "0021" .
76 "*" . $claim->x12gsversionstring() .
77 "~\n";
79 ++$edicount;
80 $out .= "BHT" .
81 "*" . "0019" . // 0019 is required here
82 "*" . "00" . // 00 = original transmission
83 "*" . "0123" . // reference identification
84 "*" . date('Ymd', $today) . // transaction creation date
85 "*" . date('Hi', $today) . // transaction creation time
86 "*" . ($encounter_claim ? "RP" : "CH") . // RP = reporting, CH = chargeable
87 "~\n";
89 ++$edicount;
90 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
91 $firstName = $claim->providerFirstName();
92 $lastName = $claim->providerLastName();
93 $middleName = $claim->providerMiddleName();
94 $suffixName = $claim->providerSuffixName();
95 $out .= "NM1" . // Loop 1000A Submitter
96 "*" . "41" .
97 "*" . "1" .
98 "*" . $lastName .
99 "*" . $firstName .
100 "*" . $middleName .
101 "*" . // Name Prefix not used
102 "*" . $suffixName .
103 "*" . "46";
104 } else {
105 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
106 if ($billingFacilityName == '') {
107 $log .= "*** billing facility name in 1000A loop is empty\n";
109 $out .= "NM1" .
110 "*" . "41" .
111 "*" . "2" .
112 "*" . $billingFacilityName .
113 "*" .
114 "*" .
115 "*" .
116 "*" .
117 "*" . "46";
119 $out .= "*" . $claim->billingFacilityETIN();
120 $out .= "~\n";
122 ++$edicount;
123 $out .= "PER" . // Loop 1000A, Submitter EDI contact information
124 "*" . "IC" .
125 "*" . $claim->billingContactName() .
126 "*" . "TE" .
127 "*" . $claim->billingContactPhone() .
128 "*" . "EM" .
129 "*" . $claim->billingContactEmail();
130 $out .= "~\n";
132 ++$edicount;
133 $out .= "NM1" . // Loop 1000B Receiver
134 "*" . "40" .
135 "*" . "2" .
136 "*" . $claim->clearingHouseName() .
137 "*" .
138 "*" .
139 "*" .
140 "*" .
141 "*" . "46" .
142 "*" . $claim->clearingHouseETIN() .
143 "~\n";
145 ++$HLcount;
146 ++$edicount;
147 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
148 "*" . $HLcount .
149 "*" .
150 "*" . "20" .
151 "*" . "1" . // 1 indicates there are child segments
152 "~\n";
154 $HLBillingPayToProvider = $HLcount++;
156 // Situational PRV segment for provider taxonomy.
157 if ($claim->facilityTaxonomy()) {
158 ++$edicount;
159 $out .= "PRV" .
160 "*" . "BI" .
161 "*" . "PXC" .
162 "*" . $claim->facilityTaxonomy() .
163 "~\n";
166 // Situational CUR segment (foreign currency information) omitted here.
167 ++$edicount;
168 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
169 $firstName = $claim->providerFirstName();
170 $lastName = $claim->providerLastName();
171 $middleName = $claim->providerMiddleName();
172 $out .= "NM1" .
173 "*" . "85" .
174 "*" . "1" .
175 "*" . $lastName .
176 "*" . $firstName .
177 "*" . $middleName .
178 "*" . // Name Prefix not used
179 "*";
180 } else {
181 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
182 if ($billingFacilityName == '') {
183 $log .= "*** billing facility name in 2010A loop is empty\n";
185 $out .= "NM1" . // Loop 2010AA Billing Provider
186 "*" . "85" .
187 "*" . "2" .
188 "*" . $billingFacilityName .
189 "*" .
190 "*" .
191 "*" .
192 "*";
194 if ($claim->billingFacilityNPI()) {
195 $out .= "*XX*" . $claim->billingFacilityNPI();
196 } else {
197 $log .= "*** Billing facility has no NPI.\n";
199 $out .= "~\n";
201 ++$edicount;
202 $out .= "N3" .
203 "*" . $claim->billingFacilityStreet() .
204 "~\n";
206 ++$edicount;
207 $out .= "N4" .
208 "*" . $claim->billingFacilityCity() .
209 "*" . $claim->billingFacilityState() .
210 "*" . stripZipCode($claim->billingFacilityZip()) .
211 "~\n";
213 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
214 ++$edicount;
215 $out .= "REF";
216 if ($claim->federalIdType()) {
217 $out .= "*" . $claim->federalIdType();
218 } else {
219 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
221 $out .= "*" . $claim->billingFacilityETIN() . "~\n";
222 } else {
223 $log .= "*** No billing facility NPI and/or ETIN.\n";
225 if ($claim->providerNumberType() && $claim->providerNumber() && !$claim->billingFacilityNPI()) {
226 ++$edicount;
227 $out .= "REF" .
228 "*" . $claim->providerNumberType() .
229 "*" . $claim->providerNumber() .
230 "~\n";
231 } else if ($claim->providerNumber() && !$claim->providerNumberType()) {
232 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
235 // Situational PER*1C segment omitted.
237 // Pay-To Address defaults to billing provider and is no longer required in 5010 but may be useful
238 if ($claim->facilityStreet() != $claim->billingFacilityStreet()) {
239 ++$edicount;
240 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
241 $out .= "NM1" . // Loop 2010AB Pay-To Provider
242 "*" . "87" .
243 "*" . "2" .
244 "*" . $billingFacilityName .
245 "*" .
246 "*" .
247 "*" .
248 "*";
249 if ($claim->billingFacilityNPI()) {
250 $out .= "*XX*" . $claim->billingFacilityNPI();
252 $out .= "~\n";
254 ++$edicount;
255 $out .= "N3" .
256 "*" . $claim->billingFacilityStreet() .
257 "~\n";
259 ++$edicount;
260 $out .= "N4" .
261 "*" . $claim->billingFacilityCity() .
262 "*" . $claim->billingFacilityState() .
263 "*" . stripZipCode($claim->billingFacilityZip()) .
264 "~\n";
267 // Loop 2010AC Pay-To Plan Name omitted. Includes:
268 // NM1*PE, N3, N4, REF*2U, REF*EI
270 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
271 $HLSubscriber = $HLcount++;
273 ++$edicount;
274 $out .= "HL" . // Loop 2000B Subscriber HL Loop
275 "*" . $HLSubscriber .
276 "*" . $HLBillingPayToProvider .
277 "*" . "22" .
278 "*" . $PatientHL .
279 "~\n";
281 if (!$claim->payerSequence()) {
282 $log .= "*** Error: Insurance information is missing!\n";
285 ++$edicount;
286 $out .= "SBR" . // Subscriber Information
287 "*" . $claim->payerSequence() .
288 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
289 "*" . $claim->groupNumber() .
290 "*" . $claim->groupName() .
291 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
292 "*" .
293 "*" .
294 "*" .
295 "*" . $claim->claimType() .
296 "~\n";
298 // Segment PAT omitted.
300 ++$edicount;
301 $out .= "NM1" . // Loop 2010BA Subscriber
302 "*" . "IL" .
303 "*" . "1" . // 1 = person, 2 = non-person
304 "*" . $claim->insuredLastName() .
305 "*" . $claim->insuredFirstName() .
306 "*" . $claim->insuredMiddleName() .
307 "*" .
308 "*" . // Name Suffix not used
309 "*" . "MI" .
310 // "MI" = Member Identification Number
311 // "II" = Standard Unique Health Identifier, "Required if the
312 // HIPAA Individual Patient Identifier is mandated use."
313 // Here we presume that is not true yet.
314 "*" . $claim->policyNumber() .
315 "~\n";
317 // For 5010, further subscriber info is sent only if they are the patient.
318 if ($claim->isSelfOfInsured()) {
319 ++$edicount;
320 $out .= "N3" .
321 "*" . $claim->insuredStreet() .
322 "~\n";
324 ++$edicount;
325 $out .= "N4" .
326 "*" . $claim->insuredCity() .
327 "*" . $claim->insuredState() .
328 "*" . stripZipCode($claim->insuredZip()) .
329 "~\n";
331 ++$edicount;
332 $out .= "DMG" .
333 "*" . "D8" .
334 "*" . $claim->insuredDOB() .
335 "*" . $claim->insuredSex() .
336 "~\n";
339 // Segment REF*SY (Subscriber Secondary Identification) omitted.
340 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
341 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
343 ++$edicount;
344 $payerName = substr($claim->payerName(), 0, 60);
345 $out .= "NM1" . // Loop 2010BB Payer
346 "*" . "PR" .
347 "*" . "2" .
348 "*" . $payerName .
349 "*" .
350 "*" .
351 "*" .
352 "*" .
353 "*" . "PI" .
354 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
355 "~\n";
356 if (!$claim->payerID()) {
357 $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
360 ++$edicount;
361 $out .= "N3" .
362 "*" . $claim->payerStreet() .
363 "~\n";
365 ++$edicount;
366 $out .= "N4" .
367 "*" . $claim->payerCity() .
368 "*" . $claim->payerState() .
369 "*" . stripZipCode($claim->payerZip()) .
370 "~\n";
372 // Segment REF (Payer Secondary Identification) omitted.
373 // Segment REF (Billing Provider Secondary Identification) omitted.
375 if (!$claim->isSelfOfInsured()) {
376 ++$edicount;
377 $out .= "HL" . // Loop 2000C Patient Information
378 "*" . $HLcount .
379 "*" . $HLSubscriber .
380 "*" . "23" .
381 "*" . "0" .
382 "~\n";
384 $HLcount++;
385 ++$edicount;
386 $out .= "PAT" .
387 "*" . $claim->insuredRelationship() .
388 "~\n";
390 ++$edicount;
391 $out .= "NM1" . // Loop 2010CA Patient
392 "*" . "QC" .
393 "*" . "1" .
394 "*" . $claim->patientLastName() .
395 "*" . $claim->patientFirstName();
397 if ($claim->patientMiddleName() !== '') {
398 $out .= "*" . $claim->patientMiddleName();
401 $out .= "~\n";
403 ++$edicount;
404 $out .= "N3" .
405 "*" . $claim->patientStreet() .
406 "~\n";
408 ++$edicount;
409 $out .= "N4" .
410 "*" . $claim->patientCity() .
411 "*" . $claim->patientState() .
412 "*" . stripZipCode($claim->patientZip()) .
413 "~\n";
415 ++$edicount;
416 $out .= "DMG" .
417 "*" . "D8" .
418 "*" . $claim->patientDOB() .
419 "*" . $claim->patientSex() .
420 "~\n";
422 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
423 // Segment REF (Property and Casualty Patient Identifier) omitted.
424 // Segment PER (Property and Casualty Patient Contact Information) omitted.
425 } // end of patient different from insured
427 $proccount = $claim->procCount();
428 $clm_total_charges = 0;
429 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
430 $clm_total_charges += $claim->cptCharges($prockey);
432 if (!$clm_total_charges) {
433 $log .= "*** This claim has no charges!\n";
436 ++$edicount;
437 $out .= "CLM" . // Loop 2300 Claim
438 "*" . $pid . "-" . $encounter .
439 "*" . sprintf("%.2f", $clm_total_charges) .
440 "*" .
441 "*" .
442 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" . "B" . ":" . $claim->frequencyTypeCode() .
443 "*" . "Y" .
444 "*" . "A" .
445 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
446 "*" . "Y" .
447 "~\n";
449 if ($claim->onsetDate() && ($claim->onsetDate() !== $claim->serviceDate()) && ($claim->onsetDateValid())) {
450 ++$edicount;
451 $out .= "DTP" . // Date of Onset
452 "*" . "431" .
453 "*" . "D8" .
454 "*" . $claim->onsetDate() .
455 "~\n";
458 // above is for historical use of encounter onset date, now in misc_billing_options
459 // Segment DTP*431 (Onset of Current Symptoms or Illness)
460 // Segment DTP*484 (Last Menstrual Period Date)
462 if ($claim->miscOnsetDate() && ($claim->box14Qualifier()) && ($claim->miscOnsetDateValid())) {
463 ++$edicount;
464 $out .= "DTP" .
465 "*" . $claim->box14Qualifier() .
466 "*" . "D8" .
467 "*" . $claim->miscOnsetDate() .
468 "~\n";
471 // Segment DTP*304 (Last Seen Date)
472 // Segment DTP*453 (Acute Manifestation Date)
473 // Segment DTP*439 (Accident Date)
474 // Segment DTP*455 (Last X-Ray Date)
475 // Segment DTP*471 (Hearing and Vision Prescription Date)
476 // Segment DTP*314 (Disability) omitted.
477 // Segment DTP*360 (Initial Disability Period Start) omitted.
478 // Segment DTP*361 (Initial Disability Period End) omitted.
479 // Segment DTP*297 (Last Worked Date)
480 // Segment DTP*296 (Authorized Return to Work Date)
482 // Segment DTP*454 (Initial Treatment Date)
484 if ($claim->dateInitialTreatment() && ($claim->box15Qualifier()) && ($claim->dateInitialTreatmentValid())) {
485 ++$edicount;
486 $out .= "DTP" . // Date Last Seen
487 "*" . $claim->box15Qualifier() .
488 "*" . "D8" .
489 "*" . $claim->dateInitialTreatment() .
490 "~\n";
493 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->onsetDateValid()) {
494 ++$edicount;
495 $out .= "DTP" . // Date of Hospitalization
496 "*" . "435" .
497 "*" . "D8" .
498 "*" . $claim->onsetDate() .
499 "~\n";
502 // above is for historical use of encounter onset date, now in misc_billing_options
503 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedFromDateValid()) {
504 ++$edicount;
505 $out .= "DTP" . // Date of Admission
506 "*" . "435" .
507 "*" . "D8" .
508 "*" . $claim->hospitalizedFrom() .
509 "~\n";
512 // Segment DTP*096 (Discharge Date)
513 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedToDateValid()) {
514 ++$edicount;
515 $out .= "DTP" . // Date of Discharge
516 "*" . "96" .
517 "*" . "D8" .
518 "*" . $claim->hospitalizedTo() .
519 "~\n";
522 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
523 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
524 // Segment DTP*050 (Repricer Received Date) omitted.
525 // Segment PWK (Claim Supplemental Information) omitted.
526 // Segment CN1 (Contract Information) omitted.
528 $patientpaid = $claim->patientPaidAmount();
529 if ($patientpaid != 0) {
530 ++$edicount;
531 $out .= "AMT" . // Patient paid amount. Page 190/220.
532 "*" . "F5" .
533 "*" . $patientpaid .
534 "~\n";
537 // Segment REF*4N (Service Authorization Exception Code) omitted.
538 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
539 // Segment REF*EW (Mammography Certification Number) omitted.
540 // Segment REF*9F (Referral Number) omitted.
542 if ($claim->priorAuth()) {
543 ++$edicount;
544 $out .= "REF" . // Prior Authorization Number
545 "*" . "G1" .
546 "*" . $claim->priorAuth() .
547 "~\n";
550 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
551 if (trim($claim->billing_options['icn_resubmission_number']) > 3) {
552 ++$edicount;
553 error_log("Method 1: " . $claim->billing_options['icn_resubmission_number'], 0);
554 $out .= "REF" .
555 "*" . "F8" .
556 "*" . $claim->icnResubmissionNumber() .
557 "~\n";
560 if ($claim->cliaCode() && ($claim->claimType() === 'MB')) {
561 // Required by Medicare when in-house labs are done.
562 ++$edicount;
563 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
564 "*" . "X4" .
565 "*" . $claim->cliaCode() .
566 "~\n";
569 // Segment REF*9A (Repriced Claim Number) omitted.
570 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
571 // Segment REF*LX (Investigational Device Exemption Number) omitted.
572 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
573 // Segment REF*EA (Medical Record Number) omitted.
574 // Segment REF*P4 (Demonstration Project Identifier) omitted.
575 // Segment REF*1J (Care Plan Oversight) omitted.
576 // Segment K3 (File Information) omitted.
577 if ($claim->additionalNotes()) {
578 // Claim note.
579 ++$edicount;
580 $out .= "NTE" . // comments box 19
581 "*" . "ADD" .
582 "*" . $claim->additionalNotes() .
583 "~\n";
586 // Segment CR1 (Ambulance Transport Information) omitted.
587 // Segment CR2 (Spinal Manipulation Service Information) omitted.
588 // Segment CRC (Ambulance Certification) omitted.
589 // Segment CRC (Patient Condition Information: Vision) omitted.
590 // Segment CRC (Homebound Indicator) omitted.
591 // Segment CRC (EPSDT Referral).
592 if ($claim->epsdtFlag()) {
593 ++$edicount;
594 $out .= "CRC" .
595 "*" . "ZZ" .
596 "*" . "Y" .
597 "*" . $claim->medicaidReferralCode() .
598 "~\n";
601 // Diagnoses, up to $max_per_seg per HI segment.
602 $max_per_seg = 12;
603 $da = $claim->diagArray();
604 if ($claim->diagtype == "ICD9") {
605 $diag_type_code = 'BK';
606 } else {
607 $diag_type_code = 'ABK';
609 $tmp = 0;
610 foreach ($da as $diag) {
611 if ($tmp % $max_per_seg == 0) {
612 if ($tmp) {
613 $out .= "~\n";
615 ++$edicount;
616 $out .= "HI"; // Health Diagnosis Codes
618 $out .= "*" . $diag_type_code . ":" . $diag;
619 if ($claim->diagtype == "ICD9") {
620 $diag_type_code = 'BF';
621 } else {
622 $diag_type_code = 'ABF';
624 ++$tmp;
627 if ($tmp) {
628 $out .= "~\n";
631 // Segment HI*BP (Anesthesia Related Procedure) omitted.
632 // Segment HI*BG (Condition Information) omitted.
633 // Segment HCP (Claim Pricing/Repricing Information) omitted.
634 if ($claim->referrerLastName()) {
635 // Medicare requires referring provider's name and UPIN.
636 ++$edicount;
637 $out .= "NM1" . // Loop 2310A Referring Provider
638 "*" . "DN" .
639 "*" . "1" .
640 "*" . $claim->referrerLastName() .
641 "*" . $claim->referrerFirstName() .
642 "*" . $claim->referrerMiddleName() .
643 "*" .
644 "*";
645 if ($claim->referrerNPI()) {
646 $out .=
647 "*" . "XX" .
648 "*" . $claim->referrerNPI();
649 } else {
650 $log .= "*** Referring provider has no NPI.\n";
652 $out .= "~\n";
655 // Per the implementation guide lines, only include this information if it is different
656 // than the Loop 2010AA information
657 if ($claim->providerNPIValid() && ($claim->billingFacilityNPI() !== $claim->providerNPI())) {
658 ++$edicount;
659 $out .= "NM1" . // Loop 2310B Rendering Provider
660 "*" . "82" .
661 "*" . "1" .
662 "*" . $claim->providerLastName() .
663 "*" . $claim->providerFirstName() .
664 "*" . $claim->providerMiddleName() .
665 "*" .
666 "*";
667 if ($claim->providerNPI()) {
668 $out .=
669 "*" . "XX" .
670 "*" . $claim->providerNPI();
671 } else {
672 $log .= "*** Rendering provider has no NPI.\n";
674 $out .= "~\n";
676 if ($claim->providerTaxonomy()) {
677 ++$edicount;
678 $out .= "PRV" .
679 "*" . "PE" . // Performing provider
680 "*" . "PXC" .
681 "*" . $claim->providerTaxonomy() .
682 "~\n";
683 } else {
684 $log .= "*** Performing provider has no taxonomy code.\n";
686 } else {
687 $log .= "*** Rendering provider is billing under a group.\n";
689 if (!$claim->providerNPIValid()) {
690 // If the loop was skipped because the provider NPI was invalid, generate a warning for the log.
691 $log .= "*** Skipping 2310B because " . $claim->providerLastName() . "," . $claim->providerFirstName() . " has invalid NPI.\n";
694 if (!$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B', '1G', 'G2', 'LU'))) {
695 if ($claim->providerNumber()) {
696 ++$edicount;
697 $out .= "REF" .
698 "*" . $claim->providerNumberType() .
699 "*" . $claim->providerNumber() .
700 "~\n";
703 // End of Loop 2310B
705 // Loop 2310C is omitted in the case of home visits (POS=12).
706 if ($claim->facilityPOS() != 12 && ($claim->facilityNPI() != $claim->billingFacilityNPI())) {
707 ++$edicount;
708 $out .= "NM1" . // Loop 2310C Service Location
709 "*" . "77" .
710 "*" . "2";
711 $facilityName = substr($claim->facilityName(), 0, 60);
712 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) {
713 $out .=
714 "*" . $facilityName;
715 } else {
716 $log .= "*** Check for invalid facility name, NPI, and/or tax id.\n";
718 if ($claim->facilityNPI() || $claim->facilityETIN()) {
719 $out .=
720 "*" .
721 "*" .
722 "*" .
723 "*";
724 if ($claim->facilityNPI()) {
725 $out .=
726 "*" . "XX" . "*" . $claim->facilityNPI();
727 } else {
728 $out .=
729 "*" . "24" . "*" . $claim->facilityETIN();
731 if (!$claim->facilityNPI()) {
732 $log .= "*** Service location has no NPI.\n";
736 $out .= "~\n";
737 if ($claim->facilityStreet()) {
738 ++$edicount;
739 $out .= "N3" .
740 "*" . $claim->facilityStreet() .
741 "~\n";
744 if ($claim->facilityState()) {
745 ++$edicount;
746 $out .= "N4" .
747 "*" . $claim->facilityCity() .
748 "*" . $claim->facilityState() .
749 "*" . stripZipCode($claim->facilityZip()) .
750 "~\n";
753 // Segment REF (Service Facility Location Secondary Identification) omitted.
754 // Segment PER (Service Facility Contact Information) omitted.
756 // Loop 2310D, Supervising Provider
757 if (! empty($claim->supervisorLastName())) {
758 ++$edicount;
759 $out .= "NM1" .
760 "*" . "DQ" . // Supervising Physician
761 "*" . "1" . // Person
762 "*" . $claim->supervisorLastName() .
763 "*" . $claim->supervisorFirstName() .
764 "*" . $claim->supervisorMiddleName() .
765 "*" . // NM106 not used
766 "*"; // Name Suffix not used
767 if ($claim->supervisorNPI()) {
768 $out .=
769 "*" . "XX" .
770 "*" . $claim->supervisorNPI();
771 } else {
772 $log .= "*** Supervising Provider has no NPI.\n";
774 $out .= "~\n";
776 if ($claim->supervisorNumber()) {
777 ++$edicount;
778 $out .= "REF" .
779 "*" . $claim->supervisorNumberType() .
780 "*" . $claim->supervisorNumber() .
781 "~\n";
783 } else {
784 $log .= "*** Supervising provider is empty.\n";
787 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
788 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
790 // Loops 2320 and 2330, other subscriber/payer information.
791 // Remember that insurance index 0 is always for the payer being billed
792 // by this claim, and 1 and above are always for the "other" payers.
794 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
795 $tmp1 = $claim->claimType($ins);
796 $tmp2 = 'C1'; // Here a kludge. See page 321.
797 if ($tmp1 === 'CI') {
798 $tmp2 = 'C1';
800 if ($tmp1 === 'AM') {
801 $tmp2 = 'AP';
803 if ($tmp1 === 'HM') {
804 $tmp2 = 'HM';
806 if ($tmp1 === 'MB') {
807 $tmp2 = 'MB';
809 if ($tmp1 === 'MC') {
810 $tmp2 = 'MC';
812 if ($tmp1 === '09') {
813 $tmp2 = 'PP';
816 ++$edicount;
817 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
818 "*" . $claim->payerSequence($ins) .
819 "*" . $claim->insuredRelationship($ins) .
820 "*" . $claim->groupNumber($ins) .
821 "*" . $claim->groupName($ins) .
822 "*" . $claim->insuredTypeCode($ins) .
823 "*" .
824 "*" .
825 "*" .
826 "*" . $claim->claimType($ins) .
827 "~\n";
829 // Things that apply only to previous payers, not future payers.
830 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
831 // Generate claim-level adjustments.
832 $aarr = $claim->payerAdjustments($ins);
833 foreach ($aarr as $a) {
834 ++$edicount;
835 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
836 "*" . $a[1] .
837 "*" . $a[2] .
838 "*" . $a[3] .
839 "~\n";
842 $payerpaid = $claim->payerTotals($ins);
843 ++$edicount;
844 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
845 "*" . "D" .
846 "*" . $payerpaid[1] .
847 "~\n";
848 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
849 // Segment AMT*EAF (Remaining Patient Liability) omitted.
850 } // End of things that apply only to previous payers.
852 ++$edicount;
853 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
854 "*" .
855 "*" .
856 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
857 // For this next item, the 5010 example in the spec does not match its
858 // description. So this might be wrong.
859 "*" .
860 "*" .
861 "*" .
862 "Y" .
863 "~\n";
865 // Segment MOA (Medicare Outpatient Adjudication) omitted.
866 ++$edicount;
867 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
868 "*" . "IL" .
869 "*" . "1" .
870 "*" . $claim->insuredLastName($ins) .
871 "*" . $claim->insuredFirstName($ins) .
872 "*" . $claim->insuredMiddleName($ins) .
873 "*" .
874 "*" .
875 "*" . "MI" .
876 "*" . $claim->policyNumber($ins) .
877 "~\n";
879 ++$edicount;
880 $out .= "N3" .
881 "*" . $claim->insuredStreet($ins) .
882 "~\n";
884 ++$edicount;
885 $out .= "N4" .
886 "*" . $claim->insuredCity($ins) .
887 "*" . $claim->insuredState($ins) .
888 "*" . stripZipCode($claim->insuredZip($ins)) .
889 "~\n";
891 // Segment REF (Other Subscriber Secondary Identification) omitted.
892 ++$edicount;
893 $payerName = substr($claim->payerName($ins), 0, 60);
894 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
895 "*" . "PR" .
896 "*" . "2" .
897 "*" . $payerName .
898 "*" .
899 "*" .
900 "*" .
901 "*" .
902 "*" . "PI" .
903 "*" . $claim->payerID($ins) .
904 "~\n";
906 if (!$claim->payerID($ins)) {
907 $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
910 ++$edicount;
911 $out .= "N3" .
912 "*" . $claim->payerStreet($ins) .
913 "~\n";
915 ++$edicount;
916 $out .= "N4" .
917 "*" . $claim->payerCity($ins) .
918 "*" . $claim->payerState($ins) .
919 "*" . stripZipCode($claim->payerZip($ins)) .
920 "~\n";
921 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
922 // Segment REF (Other Payer Secondary Identifier) omitted.
923 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
924 // Segment REF*9F (Other Payer Referral Number) omitted.
925 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
926 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
927 // Segment NM1 (Other Payer Referring Provider) omitted.
928 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
929 // Segment NM1 (Other Payer Rendering Provider) omitted.
930 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
931 // Segment NM1 (Other Payer Service Facility Location) omitted.
932 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
933 // Segment NM1 (Other Payer Supervising Provider) omitted.
934 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
935 // Segment NM1 (Other Payer Billing Provider) omitted.
936 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
937 } // End loops 2320/2330*.
939 $loopcount = 0;
941 // Loop 2400 Procedure Loop.
944 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
945 ++$loopcount;
946 ++$edicount;
947 $out .= "LX" . // Segment LX, Service Line. Page 398.
948 "*" . $loopcount .
949 "~\n";
951 ++$edicount;
952 $out .= "SV1" . // Segment 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 PWK01 (Line Supplemental Information) omitted.
994 // Segment CR1 (Ambulance Transport Information) omitted.
995 // Segment CR3 (Durable Medical Equipment Certification) omitted.
996 // Segment CRC (Ambulance Certification) omitted.
997 // Segment CRC (Hospice Employee Indicator) omitted.
998 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1000 ++$edicount;
1001 $out .= "DTP" . // Date of Service. Page 435.
1002 "*" . "472" .
1003 "*" . "D8" .
1004 "*" . $claim->serviceDate() .
1005 "~\n";
1007 $testnote = rtrim($claim->cptNotecodes($prockey));
1008 if (!empty($testnote)) {
1009 ++$edicount;
1010 $out .= "NTE" . // Explain Unusual Circumstances.
1011 "*" . "ADD" .
1012 "*" . $claim->cptNotecodes($prockey) .
1013 "~\n";
1016 // Segment DTP*471 (Prescription Date) omitted.
1017 // Segment DTP*607 (Revision/Recertification Date) omitted.
1018 // Segment DTP*463 (Begin Therapy Date) omitted.
1019 // Segment DTP*461 (Last Certification Date) omitted.
1020 // Segment DTP*304 (Last Seen Date) omitted.
1021 // Segment DTP (Test Date) omitted.
1022 // Segment DTP*011 (Shipped Date) omitted.
1023 // Segment DTP*455 (Last X-Ray Date) omitted.
1024 // Segment DTP*454 (Initial Treatment Date) omitted.
1025 // Segment QTY (Ambulance Patient Count) omitted.
1026 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1027 // Segment MEA (Test Result) omitted.
1028 // Segment CN1 (Contract Information) omitted.
1029 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1030 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1031 // Segment REF*G1 (Prior Authorization) omitted.
1032 // Segment REF*6R (Line Item Control Number) omitted.
1033 // (Really oughta have this for robust 835 posting!)
1034 // Segment REF*EW (Mammography Certification Number) omitted.
1035 // Segment REF*X4 (CLIA Number) omitted.
1036 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1037 // Segment REF*BT (Immunization Batch Number) omitted.
1038 // Segment REF*9F (Referral Number) omitted.
1039 // Segment AMT*T (Sales Tax Amount) omitted.
1040 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1041 // Segment K3 (File Information) omitted.
1042 // Segment NTE (Line Note) omitted.
1043 // Segment NTE (Third Party Organization Notes) omitted.
1044 // Segment PS1 (Purchased Service Information) omitted.
1045 // Segment HCP (Line Pricing/Repricing Information) omitted.
1047 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1048 // with HCPCS codes.
1050 $ndc = $claim->cptNDCID($prockey);
1052 if ($ndc) {
1053 ++$edicount;
1054 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1055 "*" . // Per addendum, LIN01 is not used.
1056 "*" . "N4" .
1057 "*" . $ndc .
1058 "~\n";
1060 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1061 $log .= "*** NDC code '$ndc' has invalid format!\n";
1064 ++$edicount;
1065 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1066 "*" .
1067 "*" .
1068 "*" .
1069 "*" . $claim->cptNDCQuantity($prockey) .
1070 "*" . $claim->cptNDCUOM($prockey) .
1071 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1072 "~\n";
1074 // 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).
1113 // REF*1C is required here for the Medicare provider number if NPI was
1114 // specified in NM109. Not sure if other payers require anything here.
1116 if ($claim->providerNumberType($prockey) == "G2") {
1117 ++$edicount; $out .= "REF" . "*" . $claim->providerNumberType($prockey) .
1118 "*" . $claim->providerNumber($prockey) . "~\n";
1120 } // end provider exception
1122 // Segment NM1 (Loop 2420B Purchased Service Provider Name) omitted.
1123 // Segment REF (Loop 2420B Purchased Service Provider Secondary Identification) omitted.
1124 // Segment NM1,N3,N4 (Loop 2420C Service Facility Location) omitted.
1125 // Segment REF (Loop 2420C Service Facility Location Secondary Identification) omitted.
1126 // Segment NM1 (Loop 2420D Supervising Provider Name) omitted.
1127 // Segment REF (Loop 2420D Supervising Provider Secondary Identification) omitted.
1129 // Loop 2420E, Ordering Provider.
1130 // for Medicare DME claims esp @joe on chat.open-emr.org :)
1132 if ($claim->Box17Qualifier() == "DK" && ($claim->claimType() === 'MB')) {
1133 ++$edicount;
1134 $out .= "NM1" .
1135 "*" . $claim->Box17Qualifier() .
1136 "*" . "1" .
1137 "*" . $claim->billingProviderLastName() .
1138 "*" . $claim->billingProviderFirstName() .
1139 "*" . $claim->billingProviderMiddleName() .
1140 "*" .
1141 "*";
1142 if ($claim->billingProviderNPI()) {
1143 $out .=
1144 "*" . "XX" .
1145 "*" . $claim->billingProviderNPI();
1146 } else {
1147 $log .= "*** Ordering provider has no NPI.\n";
1149 $out .= "~\n";
1151 ++$edicount;
1152 $out .= "N3" .
1153 "*" . $claim->billingProviderStreet() .
1154 "*" . $claim->billingProviderStreetB() .
1155 "~\n";
1157 ++$edicount;
1158 $out .= "N4" .
1159 "*" . $claim->billingProviderCity() .
1160 "*" . $claim->billingProviderState() .
1161 "*" . stripZipCode($claim->billingProviderZip()) .
1162 "~\n";
1163 // Segment REF (Ordering Provider Secondary Identification) omitted.
1164 // Segment PER (Ordering Provider Contact Information) omitted.
1168 // Segment NM1 (Referring Provider Name) omitted.
1169 // Segment REF (Referring Provider Secondary Identification) omitted.
1170 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1171 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1173 // Loop 2430, adjudication by previous payers.
1176 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1177 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
1178 continue; // payer is future, not previous
1181 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1182 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1184 if ($payerpaid[1] == 0 && !count($aarr)) {
1185 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1186 "' has no payments or adjustments from previous payer!\n";
1187 continue;
1190 ++$edicount;
1191 $out .= "SVD" . // Service line adjudication. Page 554.
1192 "*" . $claim->payerID($ins) .
1193 "*" . $payerpaid[1] .
1194 "*" . "HC:" . $claim->cptKey($prockey) .
1195 "*" .
1196 "*" . $claim->cptUnits($prockey) .
1197 "~\n";
1199 $tmpdate = $payerpaid[0];
1200 foreach ($aarr as $a) {
1201 ++$edicount;
1202 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1203 "*" . $a[1] .
1204 "*" . $a[2] .
1205 "*" . $a[3] .
1206 "~\n";
1207 if (!$tmpdate) {
1208 $tmpdate = $a[0];
1212 if ($tmpdate) {
1213 ++$edicount;
1214 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1215 "*" . "573" .
1216 "*" . "D8" .
1217 "*" . $tmpdate .
1218 "~\n";
1221 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1222 // Segment LQ (Form Identification Code) omitted.
1223 // Segment FRM (Supporting Documentation) omitted.
1224 } // end loop 2430
1225 } // end this procedure
1227 ++$edicount;
1228 $out .= "SE" . // SE Trailer
1229 "*" . $edicount .
1230 "*" . "0021" .
1231 "~\n";
1233 $out .= "GE" . // GE Trailer
1234 "*" . "1" .
1235 "*" . "1" .
1236 "~\n";
1238 $out .= "IEA" . // IEA Trailer
1239 "*" . "1" .
1240 "*" . "000000001" .
1241 "~\n";
1243 // Remove any trailing empty fields (delimiters) from each segment.
1244 $out = preg_replace('/\*+~/', '~', $out);
1246 $log .= "\n";
1247 return $out;