assorted bug fixes
[openemr.git] / library / gen_x12_837.inc.php
blobb898cbb4f92517663443011673bee26c3e0606b8
1 <?php
2 // Copyright (C) 2007 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 ++$edicount;
161 $out .= "REF" .
162 "*" . $claim->providerNumberType() .
163 "*" . $claim->providerNumber() .
164 "~\n";
166 ++$edicount;
167 $out .= "NM1" . // Loop 2010AB Pay-To Provider
168 "*87" .
169 "*2" .
170 "*" . $claim->billingFacilityName() .
171 "*" .
172 "*" .
173 "*" .
174 "*";
175 if ($claim->billingFacilityNPI())
176 $out .= "*XX*" . $claim->billingFacilityNPI();
177 else
178 $out .= "*24*" . $claim->billingFacilityETIN();
179 $out .= "~\n";
181 ++$edicount;
182 $out .= "N3" .
183 "*" . $claim->billingFacilityStreet() .
184 "~\n";
186 ++$edicount;
187 $out .= "N4" .
188 "*" . $claim->billingFacilityCity() .
189 "*" . $claim->billingFacilityState() .
190 "*" . $claim->billingFacilityZip() .
191 "~\n";
193 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
194 ++$edicount;
195 $out .= "REF" .
196 "*EI" .
197 "*" . $claim->billingFacilityETIN() .
198 "~\n";
201 $PatientHL = 0;
203 ++$edicount;
204 $out .= "HL" . // Loop 2000B Subscriber HL Loop
205 "*$HLcount" .
206 "*$HLBillingPayToProvider" .
207 "*22" .
208 "*$PatientHL" .
209 "~\n";
211 $HLSubscriber = $HLcount++;
213 ++$edicount;
214 $out .= "SBR" . // Subscriber Information
215 "*" . $claim->payerSequence() .
216 "*" . $claim->insuredRelationship() .
217 "*" . $claim->groupNumber() .
218 "*" . $claim->groupName() .
219 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
220 "*" .
221 "*" .
222 "*" .
223 "*" . $claim->claimType() . // Zirmed replaces this
224 "~\n";
226 ++$edicount;
227 $out .= "NM1" . // Loop 2010BA Subscriber
228 "*IL" .
229 "*1" .
230 "*" . $claim->insuredLastName() .
231 "*" . $claim->insuredFirstName() .
232 "*" . $claim->insuredMiddleName() .
233 "*" .
234 "*" .
235 "*MI" .
236 "*" . $claim->policyNumber() .
237 "~\n";
239 ++$edicount;
240 $out .= "N3" .
241 "*" . $claim->insuredStreet() .
242 "~\n";
244 ++$edicount;
245 $out .= "N4" .
246 "*" . $claim->insuredCity() .
247 "*" . $claim->insuredState() .
248 "*" . $claim->insuredZip() .
249 "~\n";
251 ++$edicount;
252 $out .= "DMG" .
253 "*D8" .
254 "*" . $claim->insuredDOB() .
255 "*" . $claim->insuredSex() .
256 "~\n";
258 ++$edicount;
259 $out .= "NM1" . // Loop 2010BB Payer
260 "*PR" .
261 "*2" .
262 "*" . $claim->payerName() .
263 "*" .
264 "*" .
265 "*" .
266 "*" .
267 "*PI" .
268 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
269 "~\n";
271 // if (!$claim->payerID()) {
272 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
273 // }
275 ++$edicount;
276 $out .= "N3" .
277 "*" . $claim->payerStreet() .
278 "~\n";
280 ++$edicount;
281 $out .= "N4" .
282 "*" . $claim->payerCity() .
283 "*" . $claim->payerState() .
284 "*" . $claim->payerZip() .
285 "~\n";
287 if (! $claim->isSelfOfInsured()) {
288 ++$edicount;
289 $out .= "HL" . // Loop 2000C Patient Information
290 "*$HLcount" .
291 "*$HLSubscriber" .
292 "*23" .
293 "*0" .
294 "~\n";
296 $HLcount++;
298 ++$edicount;
299 $out .= "PAT" .
300 "*" . $claim->insuredRelationship() .
301 "~\n";
303 ++$edicount;
304 $out .= "NM1" . // Loop 2010CA Patient
305 "*QC" .
306 "*1" .
307 "*" . $claim->patientLastName() .
308 "*" . $claim->patientFirstName() .
309 "*" . $claim->patientMiddleName() .
310 "~\n";
312 ++$edicount;
313 $out .= "N3" .
314 "*" . $claim->patientStreet() .
315 "~\n";
317 ++$edicount;
318 $out .= "N4" .
319 "*" . $claim->patientCity() .
320 "*" . $claim->patientState() .
321 "*" . $claim->patientZip() .
322 "~\n";
324 ++$edicount;
325 $out .= "DMG" .
326 "*D8" .
327 "*" . $claim->patientDOB() .
328 "*" . $claim->patientSex() .
329 "~\n";
330 } // end of patient different from insured
332 $proccount = $claim->procCount();
334 $clm_total_charges = 0;
335 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
336 $clm_total_charges += $claim->cptCharges($prockey);
339 if (!$clm_total_charges) {
340 $log .= "*** This claim has no charges!\n";
343 ++$edicount;
344 $out .= "CLM" . // Loop 2300 Claim
345 "*$pid-$encounter" .
346 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
347 "*" .
348 "*" .
349 "*" . $claim->facilityPOS() . "::1" .
350 "*Y" .
351 "*A" .
352 "*Y" .
353 "*Y" .
354 "*C" .
355 "~\n";
357 ++$edicount;
358 $out .= "DTP" . // Date of Onset
359 "*431" .
360 "*D8" .
361 "*" . $claim->onsetDate() .
362 "~\n";
364 if (strcmp($claim->facilityPOS(),'21') == 0) {
365 ++$edicount;
366 $out .= "DTP" . // Date of Hospitalization
367 "*435" .
368 "*D8" .
369 "*" . $claim->onsetDate() .
370 "~\n";
373 $patientpaid = $claim->patientPaidAmount();
374 if ($patientpaid != 0) {
375 ++$edicount;
376 $out .= "AMT" . // Patient paid amount. Page 220.
377 "*F5" .
378 "*" . $patientpaid .
379 "~\n";
382 if ($claim->priorAuth()) {
383 ++$edicount;
384 $out .= "REF" . // Prior Authorization Number
385 "*G1" .
386 "*" . $claim->priorAuth() .
387 "~\n";
390 if ($claim->cliaCode()) {
391 // Required by Medicare when in-house labs are done.
392 ++$edicount;
393 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
394 "*X4" .
395 "*" . $claim->cliaCode() .
396 "~\n";
399 $da = $claim->diagArray();
400 ++$edicount;
401 $out .= "HI"; // Health Diagnosis Codes
402 $diag_type_code = 'BK';
403 foreach ($da as $diag) {
404 $out .= "*$diag_type_code:" . $diag;
405 $diag_type_code = 'BF';
407 $out .= "~\n";
409 if ($claim->referrerLastName()) {
410 // Medicare requires referring provider's name and UPIN.
411 ++$edicount;
412 $out .= "NM1" . // Loop 2310A Referring Provider
413 "*DN" .
414 "*1" .
415 "*" . $claim->referrerLastName() .
416 "*" . $claim->referrerFirstName() .
417 "*" . $claim->referrerMiddleName() .
418 "*" .
419 "*";
420 if ($claim->referrerNPI()) { $out .=
421 "*XX" .
422 "*" . $claim->referrerNPI();
423 } else { $out .=
424 "*34" .
425 "*" . $claim->referrerSSN();
427 $out .= "~\n";
429 ++$edicount;
430 $out .= "REF" . // Referring Provider Secondary Identification
431 "*1G" .
432 "*" . $claim->referrerUPIN() .
433 "~\n";
436 ++$edicount;
437 $out .= "NM1" . // Loop 2310B Rendering Provider
438 "*82" .
439 "*1" .
440 "*" . $claim->providerLastName() .
441 "*" . $claim->providerFirstName() .
442 "*" . $claim->providerMiddleName() .
443 "*" .
444 "*";
445 if ($claim->providerNPI()) { $out .=
446 "*XX" .
447 "*" . $claim->providerNPI();
448 } else { $out .=
449 "*34" .
450 "*" . $claim->providerSSN();
451 $log .= "*** Rendering provider has no NPI.\n";
453 $out .= "~\n";
455 ++$edicount;
456 $out .= "PRV" . // Rendering Provider Information
457 "*PE" .
458 "*ZZ" .
459 "*207Q00000X" .
460 "~\n";
462 // REF*1C is required here for the Medicare provider number if NPI was
463 // specified in NM109. Not sure if other payers require anything here.
464 if ($claim->providerNumber()) {
465 ++$edicount;
466 $out .= "REF" .
467 "*" . $claim->providerNumberType() .
468 "*" . $claim->providerNumber() .
469 "~\n";
472 ++$edicount;
473 $out .= "NM1" . // Loop 2310D Service Location
474 "*77" .
475 "*2" .
476 "*" . $claim->facilityName() .
477 "*" .
478 "*" .
479 "*" .
480 "*";
481 if ($claim->facilityNPI()) { $out .=
482 "*XX*" . $claim->facilityNPI();
483 } else { $out .=
484 "*24*" . $claim->facilityETIN();
485 $log .= "*** Service location has no NPI.\n";
487 $out .= "~\n";
489 ++$edicount;
490 $out .= "N3" .
491 "*" . $claim->facilityStreet() .
492 "~\n";
494 ++$edicount;
495 $out .= "N4" .
496 "*" . $claim->facilityCity() .
497 "*" . $claim->facilityState() .
498 "*" . $claim->facilityZip() .
499 "~\n";
501 $prev_pt_resp = $clm_total_charges; // for computation below
503 // Loops 2320 and 2330*, other subscriber/payer information.
505 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
507 ++$edicount;
508 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
509 "*" . $claim->payerSequence($ins) .
510 "*" . $claim->insuredRelationship($ins) .
511 "*" . $claim->groupNumber($ins) .
512 "*" . $claim->groupName($ins) .
513 "*" .
514 "*" .
515 "*" .
516 "*" .
517 "*" . $claim->claimType($ins) .
518 "~\n";
520 // Things that apply only to previous payers, not future payers.
522 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
524 // Generate claim-level adjustments.
525 $aarr = $claim->payerAdjustments($ins);
526 foreach ($aarr as $a) {
527 ++$edicount;
528 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
529 "*" . $a[1] .
530 "*" . $a[2] .
531 "*" . $a[3] .
532 "~\n";
535 $payerpaid = $claim->payerTotals($ins);
536 ++$edicount;
537 $out .= "AMT" . // Previous payer's paid amount. Page 332.
538 "*D" .
539 "*" . $payerpaid[1] .
540 "~\n";
542 // Patient responsibility amount as of this previous payer.
543 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
544 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
546 ++$edicount;
547 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
548 "*F2" .
549 "*" . sprintf('%.2f', $prev_pt_resp) .
550 "~\n";
552 } // End of things that apply only to previous payers.
554 ++$edicount;
555 $out .= "DMG" . // Other subscriber demographic information. Page 342.
556 "*D8" .
557 "*" . $claim->insuredDOB($ins) .
558 "*" . $claim->insuredSex($ins) .
559 "~\n";
561 ++$edicount;
562 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
563 "*" .
564 "*" .
565 "*Y" .
566 "*B" .
567 "*" .
568 "*Y" .
569 "~\n";
571 ++$edicount;
572 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
573 "*IL" .
574 "*1" .
575 "*" . $claim->insuredLastName($ins) .
576 "*" . $claim->insuredFirstName($ins) .
577 "*" . $claim->insuredMiddleName($ins) .
578 "*" .
579 "*" .
580 "*MI" .
581 "*" . $claim->policyNumber($ins) .
582 "~\n";
584 ++$edicount;
585 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
586 "*PR" .
587 "*2" .
588 "*" . $claim->payerName($ins) .
589 "*" .
590 "*" .
591 "*" .
592 "*" .
593 "*PI" .
594 "*" . $claim->payerID($ins) .
595 "~\n";
597 // if (!$claim->payerID($ins)) {
598 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
599 // }
601 } // End loops 2320/2330*.
603 $loopcount = 0;
605 // Procedure loop starts here.
607 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
608 ++$loopcount;
610 ++$edicount;
611 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
612 "*$loopcount" .
613 "~\n";
615 ++$edicount;
616 $out .= "SV1" . // Professional Service. Page 400.
617 "*HC:" . $claim->cptKey($prockey) .
618 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
619 "*UN" .
620 "*" . $claim->cptUnits($prockey) .
621 "*" .
622 "*" .
623 "*" . $claim->diagIndex($prockey) .
624 "~\n";
626 if (!$claim->cptCharges($prockey)) {
627 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
630 if (!$claim->diagIndex($prockey)) {
631 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
634 ++$edicount;
635 $out .= "DTP" . // Date of Service. Page 435.
636 "*472" .
637 "*D8" .
638 "*" . $claim->serviceDate() .
639 "~\n";
641 // Loop 2410, Drug Information. Medicaid insurers seem to want this
642 // with HCPCS codes.
644 $ndc = $claim->cptNDCID($prockey);
645 if ($ndc) {
646 ++$edicount;
647 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
648 "*4" .
649 "*N4" .
650 "*" . $ndc .
651 "~\n";
653 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
654 $log .= "*** NDC code '$ndc' has invalid format!\n";
657 ++$edicount;
658 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
659 if (!$tmpunits) $tmpunits = 1;
660 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
661 "*" .
662 "*" .
663 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
664 "*" . $claim->cptNDCQuantity($prockey) .
665 "*" . $claim->cptNDCUOM($prockey) .
666 "~\n";
669 // Loop 2430, adjudication by previous payers.
671 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
672 if ($claim->payerSequence($ins) > $claim->payerSequence())
673 continue; // payer is future, not previous
675 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
676 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
678 if ($payerpaid[1] == 0 && !count($aarr)) {
679 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
680 "' has no payments or adjustments from previous payer!\n";
681 continue;
684 ++$edicount;
685 $out .= "SVD" . // Service line adjudication. Page 554.
686 "*" . $claim->payerID($ins) .
687 "*" . $payerpaid[1] .
688 "*HC:" . $claim->cptKey($prockey) .
689 "*" .
690 "*" . $claim->cptUnits($prockey) .
691 "~\n";
693 $tmpdate = $payerpaid[0];
694 foreach ($aarr as $a) {
695 ++$edicount;
696 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
697 "*" . $a[1] .
698 "*" . $a[2] .
699 "*" . $a[3] .
700 "~\n";
701 if (!$tmpdate) $tmpdate = $a[0];
704 if ($tmpdate) {
705 ++$edicount;
706 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
707 "*573" .
708 "*D8" .
709 "*$tmpdate" .
710 "~\n";
712 } // end loop 2430
713 } // end this procedure
715 ++$edicount;
716 $out .= "SE" . // SE Trailer
717 "*$edicount" .
718 "*0021" .
719 "~\n";
721 $out .= "GE" . // GE Trailer
722 "*1" .
723 "*1" .
724 "~\n";
726 $out .= "IEA" . // IEA Trailer
727 "*1" .
728 "*000000001" .
729 "~\n";
731 $log .= "\n";
732 return $out;