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->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 if ($claim->onsetDate() && ($claim->onsetDate() !== $claim->serviceDate()) && ($claim->onsetDateValid())) {
451 $out .= "DTP" . // Date of Onset
454 "*" . $claim->onsetDate() .
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())) {
465 "*" . $claim->box14Qualifier() .
467 "*" . $claim->miscOnsetDate() .
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())) {
486 $out .= "DTP" . // Date Last Seen
487 "*" . $claim->box15Qualifier() .
489 "*" . $claim->dateInitialTreatment() .
493 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->onsetDateValid()) {
495 $out .= "DTP" . // Date of Hospitalization
498 "*" . $claim->onsetDate() .
502 // above is for historical use of encounter onset date, now in misc_billing_options
503 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedFromDateValid()) {
505 $out .= "DTP" . // Date of Admission
508 "*" . $claim->hospitalizedFrom() .
512 // Segment DTP*096 (Discharge Date)
513 if (strcmp($claim->facilityPOS(), '21') == 0 && $claim->hospitalizedToDateValid()) {
515 $out .= "DTP" . // Date of Discharge
518 "*" . $claim->hospitalizedTo() .
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) {
531 $out .= "AMT" . // Patient paid amount. Page 190/220.
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()) {
544 $out .= "REF" . // Prior Authorization Number
546 "*" . $claim->priorAuth() .
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) {
553 error_log("Method 1: " . $claim->billing_options
['icn_resubmission_number'], 0);
556 "*" . $claim->icnResubmissionNumber() .
560 if ($claim->cliaCode() && ($claim->claimType() === 'MB')) {
561 // Required by Medicare when in-house labs are done.
563 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
565 "*" . $claim->cliaCode() .
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()) {
580 $out .= "NTE" . // comments box 19
582 "*" . $claim->additionalNotes() .
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()) {
597 "*" . $claim->medicaidReferralCode() .
601 // Diagnoses, up to $max_per_seg per HI segment.
603 $da = $claim->diagArray();
604 if ($claim->diagtype
== "ICD9") {
605 $diag_type_code = 'BK';
607 $diag_type_code = 'ABK';
610 foreach ($da as $diag) {
611 if ($tmp %
$max_per_seg == 0) {
616 $out .= "HI"; // Health Diagnosis Codes
618 $out .= "*" . $diag_type_code . ":" . $diag;
619 if ($claim->diagtype
== "ICD9") {
620 $diag_type_code = 'BF';
622 $diag_type_code = 'ABF';
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.
637 $out .= "NM1" . // Loop 2310A Referring Provider
640 "*" . $claim->referrerLastName() .
641 "*" . $claim->referrerFirstName() .
642 "*" . $claim->referrerMiddleName() .
645 if ($claim->referrerNPI()) {
648 "*" . $claim->referrerNPI();
650 $log .= "*** Referring provider has no NPI.\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())) {
659 $out .= "NM1" . // Loop 2310B Rendering Provider
662 "*" . $claim->providerLastName() .
663 "*" . $claim->providerFirstName() .
664 "*" . $claim->providerMiddleName() .
667 if ($claim->providerNPI()) {
670 "*" . $claim->providerNPI();
672 $log .= "*** Rendering provider has no NPI.\n";
676 if ($claim->providerTaxonomy()) {
679 "*" . "PE" . // Performing provider
681 "*" . $claim->providerTaxonomy() .
684 $log .= "*** Performing provider has no taxonomy code.\n";
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()) {
698 "*" . $claim->providerNumberType() .
699 "*" . $claim->providerNumber() .
705 // Loop 2310C is omitted in the case of home visits (POS=12).
706 if ($claim->facilityPOS() != 12 && ($claim->facilityNPI() != $claim->billingFacilityNPI())) {
708 $out .= "NM1" . // Loop 2310C Service Location
711 $facilityName = substr($claim->facilityName(), 0, 60);
712 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) {
716 $log .= "*** Check for invalid facility name, NPI, and/or tax id.\n";
718 if ($claim->facilityNPI() ||
$claim->facilityETIN()) {
724 if ($claim->facilityNPI()) {
726 "*" . "XX" . "*" . $claim->facilityNPI();
729 "*" . "24" . "*" . $claim->facilityETIN();
731 if (!$claim->facilityNPI()) {
732 $log .= "*** Service location has no NPI.\n";
737 if ($claim->facilityStreet()) {
740 "*" . $claim->facilityStreet() .
744 if ($claim->facilityState()) {
747 "*" . $claim->facilityCity() .
748 "*" . $claim->facilityState() .
749 "*" . stripZipCode($claim->facilityZip()) .
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())) {
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()) {
770 "*" . $claim->supervisorNPI();
772 $log .= "*** Supervising Provider has no NPI.\n";
776 if ($claim->supervisorNumber()) {
779 "*" . $claim->supervisorNumberType() .
780 "*" . $claim->supervisorNumber() .
785 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
786 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
788 // Loops 2320 and 2330, other subscriber/payer information.
789 // Remember that insurance index 0 is always for the payer being billed
790 // by this claim, and 1 and above are always for the "other" payers.
792 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
793 $tmp1 = $claim->claimType($ins);
794 $tmp2 = 'C1'; // Here a kludge. See page 321.
795 if ($tmp1 === 'CI') {
798 if ($tmp1 === 'AM') {
801 if ($tmp1 === 'HM') {
804 if ($tmp1 === 'MB') {
807 if ($tmp1 === 'MC') {
810 if ($tmp1 === '09') {
815 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
816 "*" . $claim->payerSequence($ins) .
817 "*" . $claim->insuredRelationship($ins) .
818 "*" . $claim->groupNumber($ins) .
819 "*" . $claim->groupName($ins) .
820 "*" . $claim->insuredTypeCode($ins) .
824 "*" . $claim->claimType($ins) .
827 // Things that apply only to previous payers, not future payers.
828 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
829 // Generate claim-level adjustments.
830 $aarr = $claim->payerAdjustments($ins);
831 foreach ($aarr as $a) {
833 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
840 $payerpaid = $claim->payerTotals($ins);
842 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
844 "*" . $payerpaid[1] .
846 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
847 // Segment AMT*EAF (Remaining Patient Liability) omitted.
848 } // End of things that apply only to previous payers.
851 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
854 "*" . ($claim->billingFacilityAssignment($ins) ?
'Y' : 'N') .
855 // For this next item, the 5010 example in the spec does not match its
856 // description. So this might be wrong.
863 // Segment MOA (Medicare Outpatient Adjudication) omitted.
865 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
868 "*" . $claim->insuredLastName($ins) .
869 "*" . $claim->insuredFirstName($ins) .
870 "*" . $claim->insuredMiddleName($ins) .
874 "*" . $claim->policyNumber($ins) .
879 "*" . $claim->insuredStreet($ins) .
884 "*" . $claim->insuredCity($ins) .
885 "*" . $claim->insuredState($ins) .
886 "*" . stripZipCode($claim->insuredZip($ins)) .
889 // Segment REF (Other Subscriber Secondary Identification) omitted.
891 $payerName = substr($claim->payerName($ins), 0, 60);
892 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
901 "*" . $claim->payerID($ins) .
904 if (!$claim->payerID($ins)) {
905 $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
910 "*" . $claim->payerStreet($ins) .
915 "*" . $claim->payerCity($ins) .
916 "*" . $claim->payerState($ins) .
917 "*" . stripZipCode($claim->payerZip($ins)) .
919 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
920 // Segment REF (Other Payer Secondary Identifier) omitted.
921 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
922 // Segment REF*9F (Other Payer Referral Number) omitted.
923 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
924 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
925 // Segment NM1 (Other Payer Referring Provider) omitted.
926 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
927 // Segment NM1 (Other Payer Rendering Provider) omitted.
928 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
929 // Segment NM1 (Other Payer Service Facility Location) omitted.
930 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
931 // Segment NM1 (Other Payer Supervising Provider) omitted.
932 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
933 // Segment NM1 (Other Payer Billing Provider) omitted.
934 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
935 } // End loops 2320/2330*.
939 // Loop 2400 Procedure Loop.
942 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
945 $out .= "LX" . // Segment LX, Service Line. Page 398.
950 $out .= "SV1" . // Segment SV1, Professional Service. Page 400.
951 "*" . "HC:" . $claim->cptKey($prockey) .
952 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
954 "*" . $claim->cptUnits($prockey) .
958 $dia = $claim->diagIndexArray($prockey);
960 foreach ($dia as $dindex) {
972 if ($claim->epsdtFlag()) {
982 if (!$claim->cptCharges($prockey)) {
983 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
987 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
990 // Segment SV5 (Durable Medical Equipment Service) omitted.
991 // Segment PWK01 (Line Supplemental Information) omitted.
992 // Segment CR1 (Ambulance Transport Information) omitted.
993 // Segment CR3 (Durable Medical Equipment Certification) omitted.
994 // Segment CRC (Ambulance Certification) omitted.
995 // Segment CRC (Hospice Employee Indicator) omitted.
996 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
999 $out .= "DTP" . // Date of Service. Page 435.
1002 "*" . $claim->serviceDate() .
1005 $testnote = rtrim($claim->cptNotecodes($prockey));
1006 if (!empty($testnote)) {
1008 $out .= "NTE" . // Explain Unusual Circumstances.
1010 "*" . $claim->cptNotecodes($prockey) .
1014 // Segment DTP*471 (Prescription Date) omitted.
1015 // Segment DTP*607 (Revision/Recertification Date) omitted.
1016 // Segment DTP*463 (Begin Therapy Date) omitted.
1017 // Segment DTP*461 (Last Certification Date) omitted.
1018 // Segment DTP*304 (Last Seen Date) omitted.
1019 // Segment DTP (Test Date) omitted.
1020 // Segment DTP*011 (Shipped Date) omitted.
1021 // Segment DTP*455 (Last X-Ray Date) omitted.
1022 // Segment DTP*454 (Initial Treatment Date) omitted.
1023 // Segment QTY (Ambulance Patient Count) omitted.
1024 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1025 // Segment MEA (Test Result) omitted.
1026 // Segment CN1 (Contract Information) omitted.
1027 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1028 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1029 // Segment REF*G1 (Prior Authorization) omitted.
1030 // Segment REF*6R (Line Item Control Number) omitted.
1031 // (Really oughta have this for robust 835 posting!)
1032 // Segment REF*EW (Mammography Certification Number) omitted.
1033 // Segment REF*X4 (CLIA Number) omitted.
1034 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1035 // Segment REF*BT (Immunization Batch Number) omitted.
1036 // Segment REF*9F (Referral Number) omitted.
1037 // Segment AMT*T (Sales Tax Amount) omitted.
1038 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1039 // Segment K3 (File Information) omitted.
1040 // Segment NTE (Line Note) omitted.
1041 // Segment NTE (Third Party Organization Notes) omitted.
1042 // Segment PS1 (Purchased Service Information) omitted.
1043 // Segment HCP (Line Pricing/Repricing Information) omitted.
1045 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1046 // with HCPCS codes.
1048 $ndc = $claim->cptNDCID($prockey);
1052 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1053 "*" . // Per addendum, LIN01 is not used.
1058 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1059 $log .= "*** NDC code '$ndc' has invalid format!\n";
1063 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1067 "*" . $claim->cptNDCQuantity($prockey) .
1068 "*" . $claim->cptNDCUOM($prockey) .
1069 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1072 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1076 // Loop 2420A, Rendering Provider (service-specific).
1077 // Used if the rendering provider for this service line is different
1078 // from that in loop 2310B.
1080 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1082 $out .= "NM1" . // Loop 2420A Rendering Provider
1085 "*" . $claim->providerLastName($prockey) .
1086 "*" . $claim->providerFirstName($prockey) .
1087 "*" . $claim->providerMiddleName($prockey) .
1090 if ($claim->providerNPI($prockey)) {
1093 "*" . $claim->providerNPI($prockey);
1095 $log .= "*** Rendering provider has no NPI.\n";
1099 // Segment PRV*PE (Rendering Provider Specialty Information) .
1101 if ($claim->providerTaxonomy($prockey)) {
1104 "*" . "PE" . // PErforming provider
1106 "*" . $claim->providerTaxonomy($prockey) .
1110 // Segment REF (Rendering Provider Secondary Identification).
1111 // REF*1C is required here for the Medicare provider number if NPI was
1112 // specified in NM109. Not sure if other payers require anything here.
1114 if ($claim->providerNumberType($prockey) == "G2") {
1115 ++
$edicount; $out .= "REF" . "*" . $claim->providerNumberType($prockey) .
1116 "*" . $claim->providerNumber($prockey) . "~\n";
1118 } // end provider exception
1120 // Segment NM1 (Loop 2420B Purchased Service Provider Name) omitted.
1121 // Segment REF (Loop 2420B Purchased Service Provider Secondary Identification) omitted.
1122 // Segment NM1,N3,N4 (Loop 2420C Service Facility Location) omitted.
1123 // Segment REF (Loop 2420C Service Facility Location Secondary Identification) omitted.
1124 // Segment NM1 (Loop 2420D Supervising Provider Name) omitted.
1125 // Segment REF (Loop 2420D Supervising Provider Secondary Identification) omitted.
1127 // Loop 2420E, Ordering Provider.
1128 // for Medicare DME claims esp @joe on chat.open-emr.org :)
1130 if ($claim->Box17Qualifier() == "DK" && ($claim->claimType() === 'MB')) {
1133 "*" . $claim->Box17Qualifier() .
1135 "*" . $claim->billingProviderLastName() .
1136 "*" . $claim->billingProviderFirstName() .
1137 "*" . $claim->billingProviderMiddleName() .
1140 if ($claim->billingProviderNPI()) {
1143 "*" . $claim->billingProviderNPI();
1145 $log .= "*** Ordering provider has no NPI.\n";
1151 "*" . $claim->billingProviderStreet() .
1152 "*" . $claim->billingProviderStreetB() .
1157 "*" . $claim->billingProviderCity() .
1158 "*" . $claim->billingProviderState() .
1159 "*" . stripZipCode($claim->billingProviderZip()) .
1161 // Segment REF (Ordering Provider Secondary Identification) omitted.
1162 // Segment PER (Ordering Provider Contact Information) omitted.
1166 // Segment NM1 (Referring Provider Name) omitted.
1167 // Segment REF (Referring Provider Secondary Identification) omitted.
1168 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1169 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1171 // Loop 2430, adjudication by previous payers.
1174 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
1175 if ($claim->payerSequence($ins) > $claim->payerSequence()) {
1176 continue; // payer is future, not previous
1179 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1180 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1182 if ($payerpaid[1] == 0 && !count($aarr)) {
1183 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1184 "' has no payments or adjustments from previous payer!\n";
1189 $out .= "SVD" . // Service line adjudication. Page 554.
1190 "*" . $claim->payerID($ins) .
1191 "*" . $payerpaid[1] .
1192 "*" . "HC:" . $claim->cptKey($prockey) .
1194 "*" . $claim->cptUnits($prockey) .
1197 $tmpdate = $payerpaid[0];
1198 foreach ($aarr as $a) {
1200 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1212 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1219 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1220 // Segment LQ (Form Identification Code) omitted.
1221 // Segment FRM (Supporting Documentation) omitted.
1223 } // end this procedure
1226 $out .= "SE" . // SE Trailer
1231 $out .= "GE" . // GE Trailer
1236 $out .= "IEA" . // IEA Trailer
1241 // Remove any trailing empty fields (delimiters) from each segment.
1242 $out = preg_replace('/\*+~/', '~', $out);