database customizations for athletic team use
[openemr.git] / library / gen_x12_837.inc.php
blob1f1a4bfe74858c25167c0feb528988a8e124a285
1 <?php
2 // Copyright (C) 2007-2008 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("Claim.class.php");
11 function gen_x12_837($pid, $encounter, &$log) {
13 $today = time();
14 $out = '';
15 $claim = new Claim($pid, $encounter);
16 $edicount = 0;
18 $log .= "Generating claim $pid-$encounter for " .
19 $claim->patientFirstName() . ' ' .
20 $claim->patientMiddleName() . ' ' .
21 $claim->patientLastName() . ' on ' .
22 date('Y-m-d H:i', $today) . ".\n";
24 $out .= "ISA" .
25 "*00" .
26 "* " .
27 "*00" .
28 "* " .
29 "*ZZ" .
30 "*" . $claim->x12gssenderid() .
31 "*ZZ" .
32 "*" . $claim->x12gsreceiverid() .
33 "*030911" .
34 "*1630" .
35 "*U" .
36 "*00401" .
37 "*000000001" .
38 "*0" .
39 "*P" .
40 "*:" .
41 "~\n";
43 $out .= "GS" .
44 "*HC" .
45 "*" . $claim->x12gssenderid() .
46 "*" . $claim->x12gsreceiverid() .
47 "*" . date('Ymd', $today) .
48 "*" . date('Hi', $today) .
49 "*1" .
50 "*X" .
51 "*" . $claim->x12gsversionstring() .
52 "~\n";
54 ++$edicount;
55 $out .= "ST" .
56 "*837" .
57 "*0021" .
58 "~\n";
60 ++$edicount;
61 $out .= "BHT" .
62 "*0019" .
63 "*00" .
64 "*0123" .
65 "*" . date('Ymd', $today) .
66 "*1023" .
67 "*CH" .
68 "~\n";
70 ++$edicount;
71 $out .= "REF" .
72 "*87" .
73 "*" . $claim->x12gsversionstring() .
74 "~\n";
76 ++$edicount;
77 $out .= "NM1" . // Loop 1000A Submitter
78 "*41" .
79 "*2" .
80 "*" . $claim->billingFacilityName() .
81 "*" .
82 "*" .
83 "*" .
84 "*" .
85 "*46" .
86 "*" . $claim->billingFacilityETIN() .
87 "~\n";
89 ++$edicount;
90 $out .= "PER" .
91 "*IC" .
92 "*" . $claim->billingContactName() .
93 "*TE" .
94 "*" . $claim->billingContactPhone() .
95 "~\n";
97 ++$edicount;
98 $out .= "NM1" . // Loop 1000B Receiver
99 "*40" .
100 "*2" .
101 "*" . $claim->clearingHouseName() .
102 "*" .
103 "*" .
104 "*" .
105 "*" .
106 "*46" .
107 "*" . $claim->clearingHouseETIN() .
108 "~\n";
110 $HLcount = 1;
112 ++$edicount;
113 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
114 "*$HLcount" .
115 "*" .
116 "*20" .
117 "*1" .
118 "~\n";
120 $HLBillingPayToProvider = $HLcount++;
122 ++$edicount;
123 $out .= "NM1" . // Loop 2010AA Billing Provider
124 "*85" .
125 "*2" .
126 "*" . $claim->billingFacilityName() .
127 "*" .
128 "*" .
129 "*" .
130 "*";
131 if ($claim->billingFacilityNPI()) {
132 $out .= "*XX*" . $claim->billingFacilityNPI();
133 } else {
134 $log .= "*** Billing facility has no NPI.\n";
135 $out .= "*24*" . $claim->billingFacilityETIN();
137 $out .= "~\n";
139 ++$edicount;
140 $out .= "N3" .
141 "*" . $claim->billingFacilityStreet() .
142 "~\n";
144 ++$edicount;
145 $out .= "N4" .
146 "*" . $claim->billingFacilityCity() .
147 "*" . $claim->billingFacilityState() .
148 "*" . $claim->billingFacilityZip() .
149 "~\n";
151 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
152 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
153 ++$edicount;
154 $out .= "REF" .
155 "*EI" .
156 "*" . $claim->billingFacilityETIN() .
157 "~\n";
160 if ($claim->providerNumberType() && $claim->providerNumber()) {
161 ++$edicount;
162 $out .= "REF" .
163 "*" . $claim->providerNumberType() .
164 "*" . $claim->providerNumber() .
165 "~\n";
167 else if ($claim->providerNumber()) {
168 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
171 ++$edicount;
172 $out .= "NM1" . // Loop 2010AB Pay-To Provider
173 "*87" .
174 "*2" .
175 "*" . $claim->billingFacilityName() .
176 "*" .
177 "*" .
178 "*" .
179 "*";
180 if ($claim->billingFacilityNPI())
181 $out .= "*XX*" . $claim->billingFacilityNPI();
182 else
183 $out .= "*24*" . $claim->billingFacilityETIN();
184 $out .= "~\n";
186 ++$edicount;
187 $out .= "N3" .
188 "*" . $claim->billingFacilityStreet() .
189 "~\n";
191 ++$edicount;
192 $out .= "N4" .
193 "*" . $claim->billingFacilityCity() .
194 "*" . $claim->billingFacilityState() .
195 "*" . $claim->billingFacilityZip() .
196 "~\n";
198 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
199 ++$edicount;
200 $out .= "REF" .
201 "*EI" .
202 "*" . $claim->billingFacilityETIN() .
203 "~\n";
206 $PatientHL = 0;
208 ++$edicount;
209 $out .= "HL" . // Loop 2000B Subscriber HL Loop
210 "*$HLcount" .
211 "*$HLBillingPayToProvider" .
212 "*22" .
213 "*$PatientHL" .
214 "~\n";
216 $HLSubscriber = $HLcount++;
218 if (!$claim->payerSequence()) {
219 $log .= "*** Error: Insurance information is missing!\n";
221 ++$edicount;
222 $out .= "SBR" . // Subscriber Information
223 "*" . $claim->payerSequence() .
224 "*" . $claim->insuredRelationship() .
225 "*" . $claim->groupNumber() .
226 "*" . $claim->groupName() .
227 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
228 "*" .
229 "*" .
230 "*" .
231 "*" . $claim->claimType() . // Zirmed replaces this
232 "~\n";
234 ++$edicount;
235 $out .= "NM1" . // Loop 2010BA Subscriber
236 "*IL" .
237 "*1" .
238 "*" . $claim->insuredLastName() .
239 "*" . $claim->insuredFirstName() .
240 "*" . $claim->insuredMiddleName() .
241 "*" .
242 "*" .
243 "*MI" .
244 "*" . $claim->policyNumber() .
245 "~\n";
247 ++$edicount;
248 $out .= "N3" .
249 "*" . $claim->insuredStreet() .
250 "~\n";
252 ++$edicount;
253 $out .= "N4" .
254 "*" . $claim->insuredCity() .
255 "*" . $claim->insuredState() .
256 "*" . $claim->insuredZip() .
257 "~\n";
259 ++$edicount;
260 $out .= "DMG" .
261 "*D8" .
262 "*" . $claim->insuredDOB() .
263 "*" . $claim->insuredSex() .
264 "~\n";
266 ++$edicount;
267 $out .= "NM1" . // Loop 2010BB Payer
268 "*PR" .
269 "*2" .
270 "*" . $claim->payerName() .
271 "*" .
272 "*" .
273 "*" .
274 "*" .
275 "*PI" .
276 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
277 "~\n";
279 // if (!$claim->payerID()) {
280 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
281 // }
283 ++$edicount;
284 $out .= "N3" .
285 "*" . $claim->payerStreet() .
286 "~\n";
288 ++$edicount;
289 $out .= "N4" .
290 "*" . $claim->payerCity() .
291 "*" . $claim->payerState() .
292 "*" . $claim->payerZip() .
293 "~\n";
295 if (! $claim->isSelfOfInsured()) {
296 ++$edicount;
297 $out .= "HL" . // Loop 2000C Patient Information
298 "*$HLcount" .
299 "*$HLSubscriber" .
300 "*23" .
301 "*0" .
302 "~\n";
304 $HLcount++;
306 ++$edicount;
307 $out .= "PAT" .
308 "*" . $claim->insuredRelationship() .
309 "~\n";
311 ++$edicount;
312 $out .= "NM1" . // Loop 2010CA Patient
313 "*QC" .
314 "*1" .
315 "*" . $claim->patientLastName() .
316 "*" . $claim->patientFirstName() .
317 "*" . $claim->patientMiddleName() .
318 "~\n";
320 ++$edicount;
321 $out .= "N3" .
322 "*" . $claim->patientStreet() .
323 "~\n";
325 ++$edicount;
326 $out .= "N4" .
327 "*" . $claim->patientCity() .
328 "*" . $claim->patientState() .
329 "*" . $claim->patientZip() .
330 "~\n";
332 ++$edicount;
333 $out .= "DMG" .
334 "*D8" .
335 "*" . $claim->patientDOB() .
336 "*" . $claim->patientSex() .
337 "~\n";
338 } // end of patient different from insured
340 $proccount = $claim->procCount();
342 $clm_total_charges = 0;
343 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
344 $clm_total_charges += $claim->cptCharges($prockey);
347 if (!$clm_total_charges) {
348 $log .= "*** This claim has no charges!\n";
351 ++$edicount;
352 $out .= "CLM" . // Loop 2300 Claim
353 "*$pid-$encounter" .
354 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
355 "*" .
356 "*" .
357 "*" . $claim->facilityPOS() . "::1" .
358 "*Y" .
359 "*A" .
360 "*Y" .
361 "*Y" .
362 "*C" .
363 "~\n";
365 ++$edicount;
366 $out .= "DTP" . // Date of Onset
367 "*431" .
368 "*D8" .
369 "*" . $claim->onsetDate() .
370 "~\n";
372 if (strcmp($claim->facilityPOS(),'21') == 0) {
373 ++$edicount;
374 $out .= "DTP" . // Date of Hospitalization
375 "*435" .
376 "*D8" .
377 "*" . $claim->onsetDate() .
378 "~\n";
381 $patientpaid = $claim->patientPaidAmount();
382 if ($patientpaid != 0) {
383 ++$edicount;
384 $out .= "AMT" . // Patient paid amount. Page 220.
385 "*F5" .
386 "*" . $patientpaid .
387 "~\n";
390 if ($claim->priorAuth()) {
391 ++$edicount;
392 $out .= "REF" . // Prior Authorization Number
393 "*G1" .
394 "*" . $claim->priorAuth() .
395 "~\n";
398 if ($claim->cliaCode()) {
399 // Required by Medicare when in-house labs are done.
400 ++$edicount;
401 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
402 "*X4" .
403 "*" . $claim->cliaCode() .
404 "~\n";
407 $da = $claim->diagArray();
408 ++$edicount;
409 $out .= "HI"; // Health Diagnosis Codes
410 $diag_type_code = 'BK';
411 foreach ($da as $diag) {
412 $out .= "*$diag_type_code:" . $diag;
413 $diag_type_code = 'BF';
415 $out .= "~\n";
417 if ($claim->referrerLastName()) {
418 // Medicare requires referring provider's name and UPIN.
419 ++$edicount;
420 $out .= "NM1" . // Loop 2310A Referring Provider
421 "*DN" .
422 "*1" .
423 "*" . $claim->referrerLastName() .
424 "*" . $claim->referrerFirstName() .
425 "*" . $claim->referrerMiddleName() .
426 "*" .
427 "*";
428 if ($claim->referrerNPI()) { $out .=
429 "*XX" .
430 "*" . $claim->referrerNPI();
431 } else { $out .=
432 "*34" .
433 "*" . $claim->referrerSSN();
435 $out .= "~\n";
437 if ($claim->referrerTaxonomy()) {
438 ++$edicount;
439 $out .= "PRV" .
440 "*RF" . // ReFerring provider
441 "*" . $claim->referrerTaxonomy() .
442 "~\n";
445 if ($claim->referrerUPIN()) {
446 ++$edicount;
447 $out .= "REF" . // Referring Provider Secondary Identification
448 "*1G" .
449 "*" . $claim->referrerUPIN() .
450 "~\n";
454 ++$edicount;
455 $out .= "NM1" . // Loop 2310B Rendering Provider
456 "*82" .
457 "*1" .
458 "*" . $claim->providerLastName() .
459 "*" . $claim->providerFirstName() .
460 "*" . $claim->providerMiddleName() .
461 "*" .
462 "*";
463 if ($claim->providerNPI()) { $out .=
464 "*XX" .
465 "*" . $claim->providerNPI();
466 } else { $out .=
467 "*34" .
468 "*" . $claim->providerSSN();
469 $log .= "*** Rendering provider has no NPI.\n";
471 $out .= "~\n";
473 /*******************************************************************
474 ++$edicount;
475 $out .= "PRV" . // Rendering Provider Information
476 "*PE" .
477 "*ZZ" .
478 "*207Q00000X" .
479 "~\n";
480 *******************************************************************/
481 if ($claim->providerTaxonomy()) {
482 ++$edicount;
483 $out .= "PRV" .
484 "*PE" . // PErforming provider
485 "*" . $claim->providerTaxonomy() .
486 "~\n";
488 /******************************************************************/
490 // REF*1C is required here for the Medicare provider number if NPI was
491 // specified in NM109. Not sure if other payers require anything here.
492 if ($claim->providerNumber()) {
493 ++$edicount;
494 $out .= "REF" .
495 "*" . $claim->providerNumberType() .
496 "*" . $claim->providerNumber() .
497 "~\n";
500 ++$edicount;
501 $out .= "NM1" . // Loop 2310D Service Location
502 "*77" .
503 "*2" .
504 "*" . $claim->facilityName() .
505 "*" .
506 "*" .
507 "*" .
508 "*";
509 if ($claim->facilityNPI()) { $out .=
510 "*XX*" . $claim->facilityNPI();
511 } else { $out .=
512 "*24*" . $claim->facilityETIN();
513 $log .= "*** Service location has no NPI.\n";
515 $out .= "~\n";
517 ++$edicount;
518 $out .= "N3" .
519 "*" . $claim->facilityStreet() .
520 "~\n";
522 ++$edicount;
523 $out .= "N4" .
524 "*" . $claim->facilityCity() .
525 "*" . $claim->facilityState() .
526 "*" . $claim->facilityZip() .
527 "~\n";
529 $prev_pt_resp = $clm_total_charges; // for computation below
531 // Loops 2320 and 2330*, other subscriber/payer information.
533 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
535 $tmp1 = $claim->claimType($ins);
536 $tmp2 = 'C1'; // Here a kludge. See page 321.
537 if ($tmp1 === 'CI') $tmp2 = 'C1';
538 if ($tmp1 === 'AM') $tmp2 = 'AP';
539 if ($tmp1 === 'HM') $tmp2 = 'HM';
540 if ($tmp1 === 'MB') $tmp2 = 'MB';
541 if ($tmp1 === 'MC') $tmp2 = 'MC';
542 if ($tmp1 === '09') $tmp2 = 'PP';
543 ++$edicount;
544 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
545 "*" . $claim->payerSequence($ins) .
546 "*" . $claim->insuredRelationship($ins) .
547 "*" . $claim->groupNumber($ins) .
548 "*" . $claim->groupName($ins) .
549 "*" . $tmp2 .
550 "*" .
551 "*" .
552 "*" .
553 "*" . $claim->claimType($ins) .
554 "~\n";
556 // Things that apply only to previous payers, not future payers.
558 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
560 // Generate claim-level adjustments.
561 $aarr = $claim->payerAdjustments($ins);
562 foreach ($aarr as $a) {
563 ++$edicount;
564 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
565 "*" . $a[1] .
566 "*" . $a[2] .
567 "*" . $a[3] .
568 "~\n";
571 $payerpaid = $claim->payerTotals($ins);
572 ++$edicount;
573 $out .= "AMT" . // Previous payer's paid amount. Page 332.
574 "*D" .
575 "*" . $payerpaid[1] .
576 "~\n";
578 // Patient responsibility amount as of this previous payer.
579 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
580 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
582 ++$edicount;
583 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
584 "*F2" .
585 "*" . sprintf('%.2f', $prev_pt_resp) .
586 "~\n";
588 } // End of things that apply only to previous payers.
590 ++$edicount;
591 $out .= "DMG" . // Other subscriber demographic information. Page 342.
592 "*D8" .
593 "*" . $claim->insuredDOB($ins) .
594 "*" . $claim->insuredSex($ins) .
595 "~\n";
597 ++$edicount;
598 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
599 "*" .
600 "*" .
601 "*Y" .
602 "*B" .
603 "*" .
604 "*Y" .
605 "~\n";
607 ++$edicount;
608 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
609 "*IL" .
610 "*1" .
611 "*" . $claim->insuredLastName($ins) .
612 "*" . $claim->insuredFirstName($ins) .
613 "*" . $claim->insuredMiddleName($ins) .
614 "*" .
615 "*" .
616 "*MI" .
617 "*" . $claim->policyNumber($ins) .
618 "~\n";
620 ++$edicount;
621 $out .= "N3" .
622 "*" . $claim->insuredStreet($ins) .
623 "~\n";
625 ++$edicount;
626 $out .= "N4" .
627 "*" . $claim->insuredCity($ins) .
628 "*" . $claim->insuredState($ins) .
629 "*" . $claim->insuredZip($ins) .
630 "~\n";
632 ++$edicount;
633 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
634 "*PR" .
635 "*2" .
636 "*" . $claim->payerName($ins) .
637 "*" .
638 "*" .
639 "*" .
640 "*" .
641 "*PI" .
642 "*" . $claim->payerID($ins) .
643 "~\n";
645 // if (!$claim->payerID($ins)) {
646 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
647 // }
649 // Payer address (N3 and N4) are added below so that Gateway EDI can
650 // auto-generate secondary claims. These do NOT appear in my copy of
651 // the spec! -- Rod 2008-06-12
653 ++$edicount;
654 $out .= "N3" .
655 "*" . $claim->payerStreet($ins) .
656 "~\n";
658 ++$edicount;
659 $out .= "N4" .
660 "*" . $claim->payerCity($ins) .
661 "*" . $claim->payerState($ins) .
662 "*" . $claim->payerZip($ins) .
663 "~\n";
665 } // End loops 2320/2330*.
667 $loopcount = 0;
669 // Procedure loop starts here.
671 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
672 ++$loopcount;
674 ++$edicount;
675 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
676 "*$loopcount" .
677 "~\n";
679 ++$edicount;
680 $out .= "SV1" . // Professional Service. Page 400.
681 "*HC:" . $claim->cptKey($prockey) .
682 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
683 "*UN" .
684 "*" . $claim->cptUnits($prockey) .
685 "*" .
686 "*" .
687 "*";
688 $dia = $claim->diagIndexArray($prockey);
689 $separator = '';
690 foreach ($dia as $dindex) {
691 $out .= $separator . $dindex;
692 $separator = ':';
694 $out .= "~\n";
696 if (!$claim->cptCharges($prockey)) {
697 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
700 if (empty($dia)) {
701 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
704 ++$edicount;
705 $out .= "DTP" . // Date of Service. Page 435.
706 "*472" .
707 "*D8" .
708 "*" . $claim->serviceDate() .
709 "~\n";
711 // Loop 2410, Drug Information. Medicaid insurers seem to want this
712 // with HCPCS codes.
714 $ndc = $claim->cptNDCID($prockey);
715 if ($ndc) {
716 ++$edicount;
717 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
718 "*4" .
719 "*N4" .
720 "*" . $ndc .
721 "~\n";
723 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
724 $log .= "*** NDC code '$ndc' has invalid format!\n";
727 ++$edicount;
728 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
729 if (!$tmpunits) $tmpunits = 1;
730 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
731 "*" .
732 "*" .
733 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
734 "*" . $claim->cptNDCQuantity($prockey) .
735 "*" . $claim->cptNDCUOM($prockey) .
736 "~\n";
739 // Loop 2430, adjudication by previous payers.
741 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
742 if ($claim->payerSequence($ins) > $claim->payerSequence())
743 continue; // payer is future, not previous
745 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
746 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
748 if ($payerpaid[1] == 0 && !count($aarr)) {
749 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
750 "' has no payments or adjustments from previous payer!\n";
751 continue;
754 ++$edicount;
755 $out .= "SVD" . // Service line adjudication. Page 554.
756 "*" . $claim->payerID($ins) .
757 "*" . $payerpaid[1] .
758 "*HC:" . $claim->cptKey($prockey) .
759 "*" .
760 "*" . $claim->cptUnits($prockey) .
761 "~\n";
763 $tmpdate = $payerpaid[0];
764 foreach ($aarr as $a) {
765 ++$edicount;
766 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
767 "*" . $a[1] .
768 "*" . $a[2] .
769 "*" . $a[3] .
770 "~\n";
771 if (!$tmpdate) $tmpdate = $a[0];
774 if ($tmpdate) {
775 ++$edicount;
776 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
777 "*573" .
778 "*D8" .
779 "*$tmpdate" .
780 "~\n";
782 } // end loop 2430
783 } // end this procedure
785 ++$edicount;
786 $out .= "SE" . // SE Trailer
787 "*$edicount" .
788 "*0021" .
789 "~\n";
791 $out .= "GE" . // GE Trailer
792 "*1" .
793 "*1" .
794 "~\n";
796 $out .= "IEA" . // IEA Trailer
797 "*1" .
798 "*000000001" .
799 "~\n";
801 $log .= "\n";
802 return $out;