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 $temp = preg_replace('/[-\s]*/', '', $zip);
21 if (strlen($temp) == 5) {
22 return $temp . "9999";
28 function gen_x12_837($pid, $encounter, &$log, $encounter_claim = false)
32 $claim = new Claim($pid, $encounter);
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";
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
56 "*" . $claim->x12gsisa14() .
57 "*" . $claim->x12gsisa15() .
63 "*" . $claim->x12gsgs02() .
64 "*" . trim($claim->x12gs03()) .
65 "*" . date('Ymd', $today) .
66 "*" . date('Hi', $today) .
69 "*" . $claim->x12gsversionstring() .
76 "*" . $claim->x12gsversionstring() .
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
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
101 "*" . // Name Prefix not used
105 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
106 if ($billingFacilityName == '') {
107 $log .= "*** billing facility name in 1000A loop is empty\n";
112 "*" . $billingFacilityName .
119 $out .= "*" . $claim->billingFacilityETIN();
123 $out .= "PER" . // Loop 1000A, Submitter EDI contact information
125 "*" . $claim->billingContactName() .
127 "*" . $claim->billingContactPhone() .
129 "*" . $claim->billingContactEmail();
133 $out .= "NM1" . // Loop 1000B Receiver
136 "*" . $claim->clearingHouseName() .
142 "*" . $claim->clearingHouseETIN() .
147 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
151 "*" . "1" . // 1 indicates there are child segments
154 $HLBillingPayToProvider = $HLcount++
;
156 // Situational PRV segment for provider taxonomy.
157 if ($claim->facilityTaxonomy()) {
162 "*" . $claim->facilityTaxonomy() .
166 // Situational CUR segment (foreign currency information) omitted here.
168 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
169 $firstName = $claim->providerFirstName();
170 $lastName = $claim->providerLastName();
171 $middleName = $claim->providerMiddleName();
178 "*" . // Name Prefix not used
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
188 "*" . $billingFacilityName .
194 if ($claim->billingFacilityNPI()) {
195 $out .= "*XX*" . $claim->billingFacilityNPI();
197 $log .= "*** Billing facility has no NPI.\n";
203 "*" . $claim->billingFacilityStreet() .
208 "*" . $claim->billingFacilityCity() .
209 "*" . $claim->billingFacilityState() .
210 "*" . stripZipCode($claim->billingFacilityZip()) .
213 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
216 if ($claim->federalIdType()) {
217 $out .= "*" . $claim->federalIdType();
219 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
221 $out .= "*" . $claim->billingFacilityETIN() . "~\n";
223 $log .= "*** No billing facility NPI and/or ETIN.\n";
225 if ($claim->providerNumberType() && $claim->providerNumber() && !$claim->billingFacilityNPI()) {
228 "*" . $claim->providerNumberType() .
229 "*" . $claim->providerNumber() .
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()) {
240 $billingFacilityName = substr($claim->billingFacilityName(), 0, 60);
241 $out .= "NM1" . // Loop 2010AB Pay-To Provider
244 "*" . $billingFacilityName .
249 if ($claim->billingFacilityNPI()) {
250 $out .= "*XX*" . $claim->billingFacilityNPI();
256 "*" . $claim->billingFacilityStreet() .
261 "*" . $claim->billingFacilityCity() .
262 "*" . $claim->billingFacilityState() .
263 "*" . stripZipCode($claim->billingFacilityZip()) .
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++
;
274 $out .= "HL" . // Loop 2000B Subscriber HL Loop
275 "*" . $HLSubscriber .
276 "*" . $HLBillingPayToProvider .
281 if (!$claim->payerSequence()) {
282 $log .= "*** Error: Insurance information is missing!\n";
286 $out .= "SBR" . // Subscriber Information
287 "*" . $claim->payerSequence() .
288 "*" . ($claim->isSelfOfInsured() ?
'18' : '') .
289 "*" . $claim->groupNumber() .
290 "*" . ($claim->groupNumber() ?
'' : $claim->groupName()) . // if groupNumber no groupName
291 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
295 "*" . $claim->claimType() .
298 // Segment PAT omitted.
301 $out .= "NM1" . // Loop 2010BA Subscriber
303 "*" . "1" . // 1 = person, 2 = non-person
304 "*" . $claim->insuredLastName() .
305 "*" . $claim->insuredFirstName() .
306 "*" . $claim->insuredMiddleName() .
308 "*" . // Name Suffix not used
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() .
317 // For 5010, further subscriber info is sent only if they are the patient.
318 if ($claim->isSelfOfInsured()) {
321 "*" . $claim->insuredStreet() .
326 "*" . $claim->insuredCity() .
327 "*" . $claim->insuredState() .
328 "*" . stripZipCode($claim->insuredZip()) .
334 "*" . $claim->insuredDOB() .
335 "*" . $claim->insuredSex() .
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.
344 $payerName = substr($claim->payerName(), 0, 60);
345 $out .= "NM1" . // Loop 2010BB Payer
354 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
356 if (!$claim->payerID()) {
357 $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
362 "*" . $claim->payerStreet() .
367 "*" . $claim->payerCity() .
368 "*" . $claim->payerState() .
369 "*" . stripZipCode($claim->payerZip()) .
372 // Segment REF (Payer Secondary Identification) omitted.
373 // Segment REF (Billing Provider Secondary Identification) omitted.
375 if (!$claim->isSelfOfInsured()) {
377 $out .= "HL" . // Loop 2000C Patient Information
379 "*" . $HLSubscriber .
387 "*" . $claim->insuredRelationship() .
391 $out .= "NM1" . // Loop 2010CA Patient
394 "*" . $claim->patientLastName() .
395 "*" . $claim->patientFirstName();
397 if ($claim->patientMiddleName() !== '') {
398 $out .= "*" . $claim->patientMiddleName();
405 "*" . $claim->patientStreet() .
410 "*" . $claim->patientCity() .
411 "*" . $claim->patientState() .
412 "*" . stripZipCode($claim->patientZip()) .
418 "*" . $claim->patientDOB() .
419 "*" . $claim->patientSex() .
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";
437 $out .= "CLM" . // Loop 2300 Claim
438 "*" . $pid . "-" . $encounter .
439 "*" . sprintf("%.2f", $clm_total_charges) .
442 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" . "B" . ":" . $claim->frequencyTypeCode() .
445 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
449 // above is for historical use of encounter onset date, now in misc_billing_options
450 // Segment DTP*431 (Onset of Current Symptoms or Illness)
451 // Segment DTP*484 (Last Menstrual Period Date)
453 if ($claim->onsetDate() && ($claim->onsetDate() !== $claim->serviceDate()) && ($claim->onsetDateValid())) {
455 $out .= "DTP" . // Date of Onset
458 "*" . $claim->onsetDate() .
460 } else if ($claim->miscOnsetDate() && ($claim->miscOnsetDate() !== $claim->serviceDate())
461 && ($claim->box14Qualifier()) && ($claim->miscOnsetDateValid())) {
464 "*" . $claim->box14Qualifier() .
466 "*" . $claim->miscOnsetDate() .
470 // Segment DTP*304 (Last Seen Date)
471 // Segment DTP*453 (Acute Manifestation Date)
472 // Segment DTP*439 (Accident Date)
473 // Segment DTP*455 (Last X-Ray Date)
474 // Segment DTP*471 (Hearing and Vision Prescription Date)
475 // Segment DTP*314 (Disability) omitted.
476 // Segment DTP*360 (Initial Disability Period Start) omitted.
477 // Segment DTP*361 (Initial Disability Period End) omitted.
478 // Segment DTP*297 (Last Worked Date)
479 // Segment DTP*296 (Authorized Return to Work Date)
481 // Segment DTP*454 (Initial Treatment Date)
483 if ($claim->dateInitialTreatment() && ($claim->box15Qualifier()) && ($claim->dateInitialTreatmentValid())) {
485 $out .= "DTP" . // Date Last Seen
486 "*" . $claim->box15Qualifier() .
488 "*" . $claim->dateInitialTreatment() .
492 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->onsetDateValid()) {
494 $out .= "DTP" . // Date of Hospitalization
497 "*" . $claim->onsetDate() .
501 // above is for historical use of encounter onset date, now in misc_billing_options
502 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedFromDateValid()) {
504 $out .= "DTP" . // Date of Admission
507 "*" . $claim->hospitalizedFrom() .
511 // Segment DTP*096 (Discharge Date)
512 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedToDateValid()) {
514 $out .= "DTP" . // Date of Discharge
517 "*" . $claim->hospitalizedTo() .
521 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
522 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
523 // Segment DTP*050 (Repricer Received Date) omitted.
524 // Segment PWK (Claim Supplemental Information) omitted.
525 // Segment CN1 (Contract Information) omitted.
527 $patientpaid = $claim->patientPaidAmount();
528 if ($patientpaid != 0) {
530 $out .= "AMT" . // Patient paid amount. Page 190/220.
536 // Segment REF*4N (Service Authorization Exception Code) omitted.
537 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
538 // Segment REF*EW (Mammography Certification Number) omitted.
539 // Segment REF*9F (Referral Number) omitted.
541 if ($claim->priorAuth()) {
543 $out .= "REF" . // Prior Authorization Number
545 "*" . $claim->priorAuth() .
549 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
550 if (trim($claim->billing_options
['icn_resubmission_number']) > 3) {
552 error_log("Method 1: " . $claim->billing_options
['icn_resubmission_number'], 0);
555 "*" . $claim->icnResubmissionNumber() .
559 if ($claim->cliaCode() && ($claim->claimType() === 'MB')) {
560 // Required by Medicare when in-house labs are done.
562 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
564 "*" . $claim->cliaCode() .
568 // Segment REF*9A (Repriced Claim Number) omitted.
569 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
570 // Segment REF*LX (Investigational Device Exemption Number) omitted.
571 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
572 // Segment REF*EA (Medical Record Number) omitted.
573 // Segment REF*P4 (Demonstration Project Identifier) omitted.
574 // Segment REF*1J (Care Plan Oversight) omitted.
575 // Segment K3 (File Information) omitted.
576 if ($claim->additionalNotes()) {
579 $out .= "NTE" . // comments box 19
581 "*" . $claim->additionalNotes() .
585 // Segment CR1 (Ambulance Transport Information) omitted.
586 // Segment CR2 (Spinal Manipulation Service Information) omitted.
587 // Segment CRC (Ambulance Certification) omitted.
588 // Segment CRC (Patient Condition Information: Vision) omitted.
589 // Segment CRC (Homebound Indicator) omitted.
590 // Segment CRC (EPSDT Referral).
591 if ($claim->epsdtFlag()) {
596 "*" . $claim->medicaidReferralCode() .
600 // Diagnoses, up to $max_per_seg per HI segment.
602 $da = $claim->diagArray();
603 if ($claim->diagtype
== "ICD9") {
604 $diag_type_code = 'BK';
606 $diag_type_code = 'ABK';
609 foreach ($da as $diag) {
610 if ($tmp %
$max_per_seg == 0) {
615 $out .= "HI"; // Health Diagnosis Codes
617 $out .= "*" . $diag_type_code . ":" . $diag;
618 if ($claim->diagtype
== "ICD9") {
619 $diag_type_code = 'BF';
621 $diag_type_code = 'ABF';
630 // Segment HI*BP (Anesthesia Related Procedure) omitted.
631 // Segment HI*BG (Condition Information) omitted.
632 // Segment HCP (Claim Pricing/Repricing Information) omitted.
633 if ($claim->referrerLastName()) {
634 // Medicare requires referring provider's name and UPIN.
636 $out .= "NM1" . // Loop 2310A Referring Provider
639 "*" . $claim->referrerLastName() .
640 "*" . $claim->referrerFirstName() .
641 "*" . $claim->referrerMiddleName() .
644 if ($claim->referrerNPI()) {
647 "*" . $claim->referrerNPI();
649 $log .= "*** Referring provider has no NPI.\n";
654 // Per the implementation guide lines, only include this information if it is different
655 // than the Loop 2010AA information
656 if ($claim->providerNPIValid() && ($claim->billingFacilityNPI() !== $claim->providerNPI())) {
658 $out .= "NM1" . // Loop 2310B Rendering Provider
661 "*" . $claim->providerLastName() .
662 "*" . $claim->providerFirstName() .
663 "*" . $claim->providerMiddleName() .
666 if ($claim->providerNPI()) {
669 "*" . $claim->providerNPI();
671 $log .= "*** Rendering provider has no NPI.\n";
675 if ($claim->providerTaxonomy()) {
678 "*" . "PE" . // Performing provider
680 "*" . $claim->providerTaxonomy() .
683 $log .= "*** Performing provider has no taxonomy code.\n";
686 $log .= "*** Rendering provider is billing under a group.\n";
688 if (!$claim->providerNPIValid()) {
689 // If the loop was skipped because the provider NPI was invalid, generate a warning for the log.
690 $log .= "*** Skipping 2310B because " . $claim->providerLastName() . "," . $claim->providerFirstName() . " has invalid NPI.\n";
693 if (!$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B', '1G', 'G2', 'LU'))) {
694 if ($claim->providerNumber()) {
697 "*" . $claim->providerNumberType() .
698 "*" . $claim->providerNumber() .
704 // Loop 2310C is omitted in the case of home visits (POS=12).
705 if ($claim->facilityPOS() != 12 && ($claim->facilityNPI() != $claim->billingFacilityNPI())) {
707 $out .= "NM1" . // Loop 2310C Service Location
710 $facilityName = substr($claim->facilityName(), 0, 60);
711 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) {
715 $log .= "*** Check for invalid facility name, NPI, and/or tax id.\n";
717 if ($claim->facilityNPI() ||
$claim->facilityETIN()) {
723 if ($claim->facilityNPI()) {
725 "*" . "XX" . "*" . $claim->facilityNPI();
728 "*" . "24" . "*" . $claim->facilityETIN();
730 if (!$claim->facilityNPI()) {
731 $log .= "*** Service location has no NPI.\n";
736 if ($claim->facilityStreet()) {
739 "*" . $claim->facilityStreet() .
743 if ($claim->facilityState()) {
746 "*" . $claim->facilityCity() .
747 "*" . $claim->facilityState() .
748 "*" . stripZipCode($claim->facilityZip()) .
752 // Segment REF (Service Facility Location Secondary Identification) omitted.
753 // Segment PER (Service Facility Contact Information) omitted.
755 // Loop 2310D, Supervising Provider
756 if (! empty($claim->supervisorLastName())) {
759 "*" . "DQ" . // Supervising Physician
760 "*" . "1" . // Person
761 "*" . $claim->supervisorLastName() .
762 "*" . $claim->supervisorFirstName() .
763 "*" . $claim->supervisorMiddleName() .
764 "*" . // NM106 not used
765 "*"; // Name Suffix not used
766 if ($claim->supervisorNPI()) {
769 "*" . $claim->supervisorNPI();
771 $log .= "*** Supervising Provider has no NPI.\n";
775 if ($claim->supervisorNumber()) {
778 "*" . $claim->supervisorNumberType() .
779 "*" . $claim->supervisorNumber() .
784 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
785 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
787 // Loops 2320 and 2330, other subscriber/payer information.
788 // Remember that insurance index 0 is always for the payer being billed
789 // by this claim, and 1 and above are always for the "other" payers.
791 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
792 $tmp1 = $claim->claimType($ins);
793 $tmp2 = 'C1'; // Here a kludge. See page 321.
794 if ($tmp1 === 'CI') {
797 if ($tmp1 === 'AM') {
800 if ($tmp1 === 'HM') {
803 if ($tmp1 === 'MB') {
806 if ($tmp1 === 'MC') {
809 if ($tmp1 === '09') {
814 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
815 "*" . $claim->payerSequence($ins) .
816 "*" . $claim->insuredRelationship($ins) .
817 "*" . $claim->groupNumber($ins) .
818 "*" . ($claim->groupNumber() ?
'' : $claim->groupName()) .
819 "*" . $claim->insuredTypeCode($ins) .
823 "*" . $claim->claimType($ins) .
826 // Things that apply only to previous payers, not future payers.
827 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
828 // Generate claim-level adjustments.
829 $aarr = $claim->payerAdjustments($ins);
830 foreach ($aarr as $a) {
832 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
839 $payerpaid = $claim->payerTotals($ins);
841 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
843 "*" . $payerpaid[1] .
845 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
846 // Segment AMT*EAF (Remaining Patient Liability) omitted.
847 } // End of things that apply only to previous payers.
850 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
853 "*" . ($claim->billingFacilityAssignment($ins) ?
'Y' : 'N') .
854 // For this next item, the 5010 example in the spec does not match its
855 // description. So this might be wrong.
862 // Segment MOA (Medicare Outpatient Adjudication) omitted.
864 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
867 "*" . $claim->insuredLastName($ins) .
868 "*" . $claim->insuredFirstName($ins) .
869 "*" . $claim->insuredMiddleName($ins) .
873 "*" . $claim->policyNumber($ins) .
878 "*" . $claim->insuredStreet($ins) .
883 "*" . $claim->insuredCity($ins) .
884 "*" . $claim->insuredState($ins) .
885 "*" . stripZipCode($claim->insuredZip($ins)) .
888 // Segment REF (Other Subscriber Secondary Identification) omitted.
890 $payerName = substr($claim->payerName($ins), 0, 60);
891 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
900 "*" . $claim->payerID($ins) .
903 if (!$claim->payerID($ins)) {
904 $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
909 "*" . $claim->payerStreet($ins) .
914 "*" . $claim->payerCity($ins) .
915 "*" . $claim->payerState($ins) .
916 "*" . stripZipCode($claim->payerZip($ins)) .
918 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
919 // Segment REF (Other Payer Secondary Identifier) omitted.
920 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
921 // Segment REF*9F (Other Payer Referral Number) omitted.
922 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
923 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
924 // Segment NM1 (Other Payer Referring Provider) omitted.
925 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
926 // Segment NM1 (Other Payer Rendering Provider) omitted.
927 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
928 // Segment NM1 (Other Payer Service Facility Location) omitted.
929 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
930 // Segment NM1 (Other Payer Supervising Provider) omitted.
931 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
932 // Segment NM1 (Other Payer Billing Provider) omitted.
933 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
934 } // End loops 2320/2330*.
938 // Loop 2400 Procedure Loop.
941 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
944 $out .= "LX" . // Segment LX, Service Line. Page 398.
949 $out .= "SV1" . // Segment SV1, Professional Service. Page 400.
950 "*" . "HC:" . $claim->cptKey($prockey) .
951 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
953 "*" . $claim->cptUnits($prockey) .
957 $dia = $claim->diagIndexArray($prockey);
959 foreach ($dia as $dindex) {
971 if ($claim->epsdtFlag()) {
981 if (!$claim->cptCharges($prockey)) {
982 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
986 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
989 // Segment SV5 (Durable Medical Equipment Service) omitted.
990 // Segment PWK01 (Line Supplemental Information) omitted.
991 // Segment CR1 (Ambulance Transport Information) omitted.
992 // Segment CR3 (Durable Medical Equipment Certification) omitted.
993 // Segment CRC (Ambulance Certification) omitted.
994 // Segment CRC (Hospice Employee Indicator) omitted.
995 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
998 $out .= "DTP" . // Date of Service. Page 435.
1001 "*" . $claim->serviceDate() .
1004 $testnote = rtrim($claim->cptNotecodes($prockey));
1005 if (!empty($testnote)) {
1007 $out .= "NTE" . // Explain Unusual Circumstances.
1009 "*" . $claim->cptNotecodes($prockey) .
1013 // Segment DTP*471 (Prescription Date) omitted.
1014 // Segment DTP*607 (Revision/Recertification Date) omitted.
1015 // Segment DTP*463 (Begin Therapy Date) omitted.
1016 // Segment DTP*461 (Last Certification Date) omitted.
1017 // Segment DTP*304 (Last Seen Date) omitted.
1018 // Segment DTP (Test Date) omitted.
1019 // Segment DTP*011 (Shipped Date) omitted.
1020 // Segment DTP*455 (Last X-Ray Date) omitted.
1021 // Segment DTP*454 (Initial Treatment Date) omitted.
1022 // Segment QTY (Ambulance Patient Count) omitted.
1023 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1024 // Segment MEA (Test Result) omitted.
1025 // Segment CN1 (Contract Information) omitted.
1026 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1027 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1028 // Segment REF*G1 (Prior Authorization) omitted.
1029 // Segment REF*6R (Line Item Control Number) omitted.
1030 // (Really oughta have this for robust 835 posting!)
1031 // Segment REF*EW (Mammography Certification Number) omitted.
1032 // Segment REF*X4 (CLIA Number) omitted.
1033 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1034 // Segment REF*BT (Immunization Batch Number) omitted.
1035 // Segment REF*9F (Referral Number) omitted.
1036 // Segment AMT*T (Sales Tax Amount) omitted.
1037 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1038 // Segment K3 (File Information) omitted.
1039 // Segment NTE (Line Note) omitted.
1040 // Segment NTE (Third Party Organization Notes) omitted.
1041 // Segment PS1 (Purchased Service Information) omitted.
1042 // Segment HCP (Line Pricing/Repricing Information) omitted.
1044 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1045 // with HCPCS codes.
1047 $ndc = $claim->cptNDCID($prockey);
1051 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1052 "*" . // Per addendum, LIN01 is not used.
1057 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1058 $log .= "*** NDC code '$ndc' has invalid format!\n";
1062 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1066 "*" . $claim->cptNDCQuantity($prockey) .
1067 "*" . $claim->cptNDCUOM($prockey) .
1068 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1071 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1075 // Loop 2420A, Rendering Provider (service-specific).
1076 // Used if the rendering provider for this service line is different
1077 // from that in loop 2310B.
1079 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1081 $out .= "NM1" . // Loop 2420A Rendering Provider
1084 "*" . $claim->providerLastName($prockey) .
1085 "*" . $claim->providerFirstName($prockey) .
1086 "*" . $claim->providerMiddleName($prockey) .
1089 if ($claim->providerNPI($prockey)) {
1092 "*" . $claim->providerNPI($prockey);
1094 $log .= "*** Rendering provider has no NPI.\n";
1098 // Segment PRV*PE (Rendering Provider Specialty Information) .
1100 if ($claim->providerTaxonomy($prockey)) {
1103 "*" . "PE" . // PErforming provider
1105 "*" . $claim->providerTaxonomy($prockey) .
1109 // Segment REF (Rendering Provider Secondary Identification).
1110 // REF*1C is required here for the Medicare provider number if NPI was
1111 // specified in NM109. Not sure if other payers require anything here.
1113 if ($claim->providerNumberType($prockey) == "G2") {
1114 ++
$edicount; $out .= "REF" . "*" . $claim->providerNumberType($prockey) .
1115 "*" . $claim->providerNumber($prockey) . "~\n";
1117 } // end provider exception
1119 // Segment NM1 (Loop 2420B Purchased Service Provider Name) omitted.
1120 // Segment REF (Loop 2420B Purchased Service Provider Secondary Identification) omitted.
1121 // Segment NM1,N3,N4 (Loop 2420C Service Facility Location) omitted.
1122 // Segment REF (Loop 2420C Service Facility Location Secondary Identification) omitted.
1123 // Segment NM1 (Loop 2420D Supervising Provider Name) omitted.
1124 // Segment REF (Loop 2420D Supervising Provider Secondary Identification) omitted.
1126 // Loop 2420E, Ordering Provider.
1127 // for Medicare DME claims esp @joe on chat.open-emr.org :)
1129 if ($claim->Box17Qualifier() == "DK" && ($claim->claimType() === 'MB')) {
1132 "*" . $claim->Box17Qualifier() .
1134 "*" . $claim->billingProviderLastName() .
1135 "*" . $claim->billingProviderFirstName() .
1136 "*" . $claim->billingProviderMiddleName() .
1139 if ($claim->billingProviderNPI()) {
1142 "*" . $claim->billingProviderNPI();
1144 $log .= "*** Ordering provider has no NPI.\n";
1150 "*" . $claim->billingProviderStreet() .
1151 "*" . $claim->billingProviderStreetB() .
1156 "*" . $claim->billingProviderCity() .
1157 "*" . $claim->billingProviderState() .
1158 "*" . stripZipCode($claim->billingProviderZip()) .
1160 // Segment REF (Ordering Provider Secondary Identification) omitted.
1161 // Segment PER (Ordering Provider Contact Information) omitted.
1165 // Segment NM1 (Referring Provider Name) omitted.
1166 // Segment REF (Referring Provider Secondary Identification) omitted.
1167 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1168 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1170 // Loop 2430, adjudication by previous payers.
1173 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
1174 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
1175 continue; // payer is future, not previous
1178 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1179 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1181 if ($payerpaid[1] == 0 && !count($aarr)) {
1182 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1183 "' has no payments or adjustments from previous payer!\n";
1188 $out .= "SVD" . // Service line adjudication. Page 554.
1189 "*" . $claim->payerID($ins) .
1190 "*" . $payerpaid[1] .
1191 "*" . "HC:" . $claim->cptKey($prockey) .
1193 "*" . $claim->cptUnits($prockey) .
1196 $tmpdate = $payerpaid[0];
1197 foreach ($aarr as $a) {
1199 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1211 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1218 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1219 // Segment LQ (Form Identification Code) omitted.
1220 // Segment FRM (Supporting Documentation) omitted.
1222 } // end this procedure
1225 $out .= "SE" . // SE Trailer
1230 $out .= "GE" . // GE Trailer
1235 $out .= "IEA" . // IEA Trailer
1240 // Remove any trailing empty fields (delimiters) from each segment.
1241 $out = preg_replace('/\*+~/', '~', $out);