3 * This program creates a 5010 837P file
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)
27 $claim = new Claim($pid, $encounter);
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";
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
51 "*" . $claim->x12gsisa14() .
52 "*" . $claim->x12gsisa15() .
58 "*" . $claim->x12gsgs02() .
59 "*" . trim($claim->x12gs03()) .
60 "*" . date('Ymd', $today) .
61 "*" . date('Hi', $today) .
64 "*" . $claim->x12gsversionstring() .
71 "*" . $claim->x12gsversionstring() .
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
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
96 "*" . // Name Prefix not used
100 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
101 if ($billingFacilityName == '') {
102 $log .= "*** billing facility name in 1000A loop is empty\n";
107 "*" . $billingFacilityName .
114 $out .= "*" . $claim->billingFacilityETIN();
118 $out .= "PER" . // Loop 1000A, Submitter EDI contact information
120 "*" . $claim->billingContactName() .
122 "*" . $claim->billingContactPhone() .
124 "*" . $claim->billingContactEmail();
128 $out .= "NM1" . // Loop 1000B Receiver
131 "*" . $claim->clearingHouseName() .
137 "*" . $claim->clearingHouseETIN() .
142 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
146 "*" . "1" . // 1 indicates there are child segments
149 $HLBillingPayToProvider = $HLcount++
;
151 // Situational PRV segment for provider taxonomy.
152 if ($claim->facilityTaxonomy()) {
157 "*" . $claim->facilityTaxonomy() .
161 // Situational CUR segment (foreign currency information) omitted here.
163 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
164 $firstName = $claim->providerFirstName();
165 $lastName = $claim->providerLastName();
166 $middleName = $claim->providerMiddleName();
173 "*" . // Name Prefix not used
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
183 "*" . $billingFacilityName .
189 if ($claim->billingFacilityNPI()) {
190 $out .= "*XX*" . $claim->billingFacilityNPI();
192 $log .= "*** Billing facility has no NPI.\n";
198 "*" . $claim->billingFacilityStreet() .
203 "*" . $claim->billingFacilityCity() .
204 "*" . $claim->billingFacilityState() .
205 "*" . stripZipCode($claim->billingFacilityZip()) .
208 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
211 if ($claim->federalIdType()) {
212 $out .= "*" . $claim->federalIdType();
214 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
216 $out .= "*" . $claim->billingFacilityETIN() . "~\n";
218 $log .= "*** No billing facility NPI and/or ETIN.\n";
220 if ($claim->providerNumberType() && $claim->providerNumber() && !$claim->billingFacilityNPI()) {
223 "*" . $claim->providerNumberType() .
224 "*" . $claim->providerNumber() .
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()) {
235 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
236 $out .= "NM1" . // Loop 2010AB Pay-To Provider
239 "*" . $billingFacilityName .
244 if ($claim->billingFacilityNPI()) {
245 $out .= "*XX*" . $claim->billingFacilityNPI();
251 "*" . $claim->billingFacilityStreet() .
256 "*" . $claim->billingFacilityCity() .
257 "*" . $claim->billingFacilityState() .
258 "*" . stripZipCode($claim->billingFacilityZip()) .
261 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
265 "*" . $claim->billingFacilityETIN() .
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++
;
277 $out .= "HL" . // Loop 2000B Subscriber HL Loop
278 "*" . $HLSubscriber .
279 "*" . $HLBillingPayToProvider .
284 if (!$claim->payerSequence()) {
285 $log .= "*** Error: Insurance information is missing!\n";
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
298 "*" . $claim->claimType() .
301 // Segment PAT omitted.
304 $out .= "NM1" . // Loop 2010BA Subscriber
306 "*" . "1" . // 1 = person, 2 = non-person
307 "*" . $claim->insuredLastName() .
308 "*" . $claim->insuredFirstName() .
309 "*" . $claim->insuredMiddleName() .
311 "*" . // Name Suffix not used
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() .
320 // For 5010, further subscriber info is sent only if they are the patient.
321 if ($claim->isSelfOfInsured()) {
324 "*" . $claim->insuredStreet() .
329 "*" . $claim->insuredCity() .
330 "*" . $claim->insuredState() .
331 "*" . stripZipCode($claim->insuredZip()) .
337 "*" . $claim->insuredDOB() .
338 "*" . $claim->insuredSex() .
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.
347 $payerName = substr($claim->payerName(), 0, 60);
348 $out .= "NM1" . // Loop 2010BB Payer
357 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
359 if (!$claim->payerID()) {
360 $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
365 "*" . $claim->payerStreet() .
370 "*" . $claim->payerCity() .
371 "*" . $claim->payerState() .
372 "*" . stripZipCode($claim->payerZip()) .
375 // Segment REF (Payer Secondary Identification) omitted.
376 // Segment REF (Billing Provider Secondary Identification) omitted.
378 if (!$claim->isSelfOfInsured()) {
380 $out .= "HL" . // Loop 2000C Patient Information
382 "*" . $HLSubscriber .
390 "*" . $claim->insuredRelationship() .
394 $out .= "NM1" . // Loop 2010CA Patient
397 "*" . $claim->patientLastName() .
398 "*" . $claim->patientFirstName();
400 if ($claim->patientMiddleName() !== '') {
401 $out .= "*" . $claim->patientMiddleName();
408 "*" . $claim->patientStreet() .
413 "*" . $claim->patientCity() .
414 "*" . $claim->patientState() .
415 "*" . stripZipCode($claim->patientZip()) .
421 "*" . $claim->patientDOB() .
422 "*" . $claim->patientSex() .
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";
440 $out .= "CLM" . // Loop 2300 Claim
441 "*" . $pid . "-" . $encounter .
442 "*" . sprintf("%.2f", $clm_total_charges) .
445 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" . "B" . ":" . $claim->frequencyTypeCode() .
448 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
452 if ($claim->onsetDate() && ($claim->onsetDate() !== $claim->serviceDate()) && ($claim->onsetDateValid())) {
454 $out .= "DTP" . // Date of Onset
457 "*" . $claim->onsetDate() .
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())) {
467 $out .= "DTP" . // Date Last Seen
468 "*" . $claim->box14Qualifier() .
470 "*" . $claim->miscOnsetDate() .
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())) {
488 $out .= "DTP" . // Date Last Seen
489 "*" . $claim->box15Qualifier() .
491 "*" . $claim->dateInitialTreatment() .
495 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->onsetDateValid()) {
497 $out .= "DTP" . // Date of Hospitalization
500 "*" . $claim->onsetDate() .
504 // above is for historical use of encounter onset date, now in misc_billing_options
505 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedFromDateValid()) {
507 $out .= "DTP" . // Date of Admission
510 "*" . $claim->hospitalizedFrom() .
514 // Segment DTP*096 (Discharge Date)
515 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedToDateValid()) {
517 $out .= "DTP" . // Date of Discharge
520 "*" . $claim->hospitalizedTo() .
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) {
533 $out .= "AMT" . // Patient paid amount. Page 190/220.
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()) {
546 $out .= "REF" . // Prior Authorization Number
548 "*" . $claim->priorAuth() .
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) {
555 error_log("Method 1: " . $claim->billing_options
['icn_resubmission_number'], 0);
558 "*" . $claim->icnResubmissionNumber() .
562 if ($claim->cliaCode() && ($claim->claimType() === 'MB')) {
563 // Required by Medicare when in-house labs are done.
565 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
567 "*" . $claim->cliaCode() .
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()) {
582 $out .= "NTE" . // comments box 19
584 "*" . $claim->additionalNotes() .
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()) {
599 "*" . $claim->medicaidReferralCode() .
603 // Diagnoses, up to $max_per_seg per HI segment.
605 $da = $claim->diagArray();
606 if ($claim->diagtype
== "ICD9") {
607 $diag_type_code = 'BK';
609 $diag_type_code = 'ABK';
612 foreach ($da as $diag) {
613 if ($tmp %
$max_per_seg == 0) {
618 $out .= "HI"; // Health Diagnosis Codes
620 $out .= "*" . $diag_type_code . ":" . $diag;
621 if ($claim->diagtype
== "ICD9") {
622 $diag_type_code = 'BF';
624 $diag_type_code = 'ABF';
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.
639 $out .= "NM1" . // Loop 2310A Referring Provider
642 "*" . $claim->referrerLastName() .
643 "*" . $claim->referrerFirstName() .
644 "*" . $claim->referrerMiddleName() .
647 if ($claim->referrerNPI()) {
650 "*" . $claim->referrerNPI();
652 $log .= "*** Referring provider has no NPI.\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())) {
661 $out .= "NM1" . // Loop 2310B Rendering Provider
664 "*" . $claim->providerLastName() .
665 "*" . $claim->providerFirstName() .
666 "*" . $claim->providerMiddleName() .
669 if ($claim->providerNPI()) {
672 "*" . $claim->providerNPI();
674 $log .= "*** Rendering provider has no NPI.\n";
678 if ($claim->providerTaxonomy()) {
681 "*" . "PE" . // Performing provider
683 "*" . $claim->providerTaxonomy() .
686 $log .= "*** Performing provider has no taxonomy code.\n";
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()) {
700 "*" . $claim->providerNumberType() .
701 "*" . $claim->providerNumber() .
707 // Loop 2310C is omitted in the case of home visits (POS=12).
708 if ($claim->facilityPOS() != 12 && ($claim->facilityNPI() != $claim->billingFacilityNPI())) {
710 $out .= "NM1" . // Loop 2310C Service Location
713 $facilityName = substr($claim->facilityName(), 0, 60);
714 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) {
718 $log .= "*** Check for invalid facility name, NPI, and/or tax id.\n";
720 if ($claim->facilityNPI() ||
$claim->facilityETIN()) {
726 if ($claim->facilityNPI()) {
728 "*" . "XX" . "*" . $claim->facilityNPI();
731 "*" . "24" . "*" . $claim->facilityETIN();
733 if (!$claim->facilityNPI()) {
734 $log .= "*** Service location has no NPI.\n";
739 if ($claim->facilityStreet()) {
742 "*" . $claim->facilityStreet() .
746 if ($claim->facilityState()) {
749 "*" . $claim->facilityCity() .
750 "*" . $claim->facilityState() .
751 "*" . stripZipCode($claim->facilityZip()) .
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()) {
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()) {
772 "*" . $claim->supervisorNPI();
774 $log .= "*** Supervising Provider has no NPI.\n";
778 if ($claim->supervisorNumber()) {
781 "*" . $claim->supervisorNumberType() .
782 "*" . $claim->supervisorNumber() .
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') {
802 if ($tmp1 === 'AM') {
805 if ($tmp1 === 'HM') {
808 if ($tmp1 === 'MB') {
811 if ($tmp1 === 'MC') {
814 if ($tmp1 === '09') {
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) .
828 "*" . $claim->claimType($ins) .
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) {
837 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
844 $payerpaid = $claim->payerTotals($ins);
846 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
848 "*" . $payerpaid[1] .
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.
855 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
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.
867 // Segment MOA (Medicare Outpatient Adjudication) omitted.
869 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
872 "*" . $claim->insuredLastName($ins) .
873 "*" . $claim->insuredFirstName($ins) .
874 "*" . $claim->insuredMiddleName($ins) .
878 "*" . $claim->policyNumber($ins) .
883 "*" . $claim->insuredStreet($ins) .
888 "*" . $claim->insuredCity($ins) .
889 "*" . $claim->insuredState($ins) .
890 "*" . stripZipCode($claim->insuredZip($ins)) .
893 // Segment REF (Other Subscriber Secondary Identification) omitted.
895 $payerName = substr($claim->payerName($ins), 0, 60);
896 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
905 "*" . $claim->payerID($ins) .
908 if (!$claim->payerID($ins)) {
909 $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
914 "*" . $claim->payerStreet($ins) .
919 "*" . $claim->payerCity($ins) .
920 "*" . $claim->payerState($ins) .
921 "*" . stripZipCode($claim->payerZip($ins)) .
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*.
943 // Procedure loop starts here.
944 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
947 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
952 $out .= "SV1" . // Professional Service. Page 400.
953 "*" . "HC:" . $claim->cptKey($prockey) .
954 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
956 "*" . $claim->cptUnits($prockey) .
960 $dia = $claim->diagIndexArray($prockey);
962 foreach ($dia as $dindex) {
974 if ($claim->epsdtFlag()) {
984 if (!$claim->cptCharges($prockey)) {
985 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
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.
1002 $out .= "DTP" . // Date of Service. Page 435.
1005 "*" . $claim->serviceDate() .
1008 $testnote = rtrim($claim->cptNotecodes($prockey));
1009 if (!empty($testnote)) {
1011 $out .= "NTE" . // Explain Unusual Circumstances.
1013 "*" . $claim->cptNotecodes($prockey) .
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);
1055 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1056 "*" . // Per addendum, LIN01 is not used.
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";
1066 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1070 "*" . $claim->cptNDCQuantity($prockey) .
1071 "*" . $claim->cptNDCUOM($prockey) .
1072 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
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)) {
1084 $out .= "NM1" . // Loop 2420A Rendering Provider
1087 "*" . $claim->providerLastName($prockey) .
1088 "*" . $claim->providerFirstName($prockey) .
1089 "*" . $claim->providerMiddleName($prockey) .
1092 if ($claim->providerNPI($prockey)) {
1095 "*" . $claim->providerNPI($prockey);
1097 $log .= "*** Rendering provider has no NPI.\n";
1101 // Segment PRV*PE (Rendering Provider Specialty Information) .
1103 if ($claim->providerTaxonomy($prockey)) {
1106 "*" . "PE" . // PErforming provider
1108 "*" . $claim->providerTaxonomy($prockey) .
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";
1153 $out .= "SVD" . // Service line adjudication. Page 554.
1154 "*" . $claim->payerID($ins) .
1155 "*" . $payerpaid[1] .
1156 "*" . "HC:" . $claim->cptKey($prockey) .
1158 "*" . $claim->cptUnits($prockey) .
1161 $tmpdate = $payerpaid[0];
1162 foreach ($aarr as $a) {
1164 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1176 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1183 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1184 // Segment LQ (Form Identification Code) omitted.
1185 // Segment FRM (Supporting Documentation) omitted.
1187 } // end this procedure
1190 $out .= "SE" . // SE Trailer
1195 $out .= "GE" . // GE Trailer
1200 $out .= "IEA" . // IEA Trailer
1205 // Remove any trailing empty fields (delimiters) from each segment.
1206 $out = preg_replace('/\*+~/', '~', $out);