added inventory list report
[openemr.git] / library / gen_x12_837.inc.php
blob8fe4fd71cc60ab5051b388189ea5f971f6fa6047
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 ++$edicount;
438 $out .= "REF" . // Referring Provider Secondary Identification
439 "*1G" .
440 "*" . $claim->referrerUPIN() .
441 "~\n";
444 ++$edicount;
445 $out .= "NM1" . // Loop 2310B Rendering Provider
446 "*82" .
447 "*1" .
448 "*" . $claim->providerLastName() .
449 "*" . $claim->providerFirstName() .
450 "*" . $claim->providerMiddleName() .
451 "*" .
452 "*";
453 if ($claim->providerNPI()) { $out .=
454 "*XX" .
455 "*" . $claim->providerNPI();
456 } else { $out .=
457 "*34" .
458 "*" . $claim->providerSSN();
459 $log .= "*** Rendering provider has no NPI.\n";
461 $out .= "~\n";
463 ++$edicount;
464 $out .= "PRV" . // Rendering Provider Information
465 "*PE" .
466 "*ZZ" .
467 "*207Q00000X" .
468 "~\n";
470 // REF*1C is required here for the Medicare provider number if NPI was
471 // specified in NM109. Not sure if other payers require anything here.
472 if ($claim->providerNumber()) {
473 ++$edicount;
474 $out .= "REF" .
475 "*" . $claim->providerNumberType() .
476 "*" . $claim->providerNumber() .
477 "~\n";
480 ++$edicount;
481 $out .= "NM1" . // Loop 2310D Service Location
482 "*77" .
483 "*2" .
484 "*" . $claim->facilityName() .
485 "*" .
486 "*" .
487 "*" .
488 "*";
489 if ($claim->facilityNPI()) { $out .=
490 "*XX*" . $claim->facilityNPI();
491 } else { $out .=
492 "*24*" . $claim->facilityETIN();
493 $log .= "*** Service location has no NPI.\n";
495 $out .= "~\n";
497 ++$edicount;
498 $out .= "N3" .
499 "*" . $claim->facilityStreet() .
500 "~\n";
502 ++$edicount;
503 $out .= "N4" .
504 "*" . $claim->facilityCity() .
505 "*" . $claim->facilityState() .
506 "*" . $claim->facilityZip() .
507 "~\n";
509 $prev_pt_resp = $clm_total_charges; // for computation below
511 // Loops 2320 and 2330*, other subscriber/payer information.
513 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
515 $tmp1 = $claim->claimType($ins);
516 $tmp2 = 'C1'; // Here a kludge. See page 321.
517 if ($tmp1 === 'CI') $tmp2 = 'C1';
518 if ($tmp1 === 'AM') $tmp2 = 'AP';
519 if ($tmp1 === 'HM') $tmp2 = 'HM';
520 if ($tmp1 === 'MB') $tmp2 = 'MB';
521 if ($tmp1 === 'MC') $tmp2 = 'MC';
522 if ($tmp1 === '09') $tmp2 = 'PP';
523 ++$edicount;
524 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
525 "*" . $claim->payerSequence($ins) .
526 "*" . $claim->insuredRelationship($ins) .
527 "*" . $claim->groupNumber($ins) .
528 "*" . $claim->groupName($ins) .
529 "*" . $tmp2 .
530 "*" .
531 "*" .
532 "*" .
533 "*" . $claim->claimType($ins) .
534 "~\n";
536 // Things that apply only to previous payers, not future payers.
538 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
540 // Generate claim-level adjustments.
541 $aarr = $claim->payerAdjustments($ins);
542 foreach ($aarr as $a) {
543 ++$edicount;
544 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
545 "*" . $a[1] .
546 "*" . $a[2] .
547 "*" . $a[3] .
548 "~\n";
551 $payerpaid = $claim->payerTotals($ins);
552 ++$edicount;
553 $out .= "AMT" . // Previous payer's paid amount. Page 332.
554 "*D" .
555 "*" . $payerpaid[1] .
556 "~\n";
558 // Patient responsibility amount as of this previous payer.
559 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
560 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
562 ++$edicount;
563 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
564 "*F2" .
565 "*" . sprintf('%.2f', $prev_pt_resp) .
566 "~\n";
568 } // End of things that apply only to previous payers.
570 ++$edicount;
571 $out .= "DMG" . // Other subscriber demographic information. Page 342.
572 "*D8" .
573 "*" . $claim->insuredDOB($ins) .
574 "*" . $claim->insuredSex($ins) .
575 "~\n";
577 ++$edicount;
578 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
579 "*" .
580 "*" .
581 "*Y" .
582 "*B" .
583 "*" .
584 "*Y" .
585 "~\n";
587 ++$edicount;
588 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
589 "*IL" .
590 "*1" .
591 "*" . $claim->insuredLastName($ins) .
592 "*" . $claim->insuredFirstName($ins) .
593 "*" . $claim->insuredMiddleName($ins) .
594 "*" .
595 "*" .
596 "*MI" .
597 "*" . $claim->policyNumber($ins) .
598 "~\n";
600 ++$edicount;
601 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
602 "*PR" .
603 "*2" .
604 "*" . $claim->payerName($ins) .
605 "*" .
606 "*" .
607 "*" .
608 "*" .
609 "*PI" .
610 "*" . $claim->payerID($ins) .
611 "~\n";
613 // if (!$claim->payerID($ins)) {
614 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
615 // }
617 } // End loops 2320/2330*.
619 $loopcount = 0;
621 // Procedure loop starts here.
623 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
624 ++$loopcount;
626 ++$edicount;
627 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
628 "*$loopcount" .
629 "~\n";
631 ++$edicount;
632 $out .= "SV1" . // Professional Service. Page 400.
633 "*HC:" . $claim->cptKey($prockey) .
634 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
635 "*UN" .
636 "*" . $claim->cptUnits($prockey) .
637 "*" .
638 "*" .
639 "*";
640 $dia = $claim->diagIndexArray($prockey);
641 $separator = '';
642 foreach ($dia as $dindex) {
643 $out .= $separator . $dindex;
644 $separator = ':';
646 $out .= "~\n";
648 if (!$claim->cptCharges($prockey)) {
649 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
652 if (empty($dia)) {
653 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
656 ++$edicount;
657 $out .= "DTP" . // Date of Service. Page 435.
658 "*472" .
659 "*D8" .
660 "*" . $claim->serviceDate() .
661 "~\n";
663 // Loop 2410, Drug Information. Medicaid insurers seem to want this
664 // with HCPCS codes.
666 $ndc = $claim->cptNDCID($prockey);
667 if ($ndc) {
668 ++$edicount;
669 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
670 "*4" .
671 "*N4" .
672 "*" . $ndc .
673 "~\n";
675 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
676 $log .= "*** NDC code '$ndc' has invalid format!\n";
679 ++$edicount;
680 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
681 if (!$tmpunits) $tmpunits = 1;
682 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
683 "*" .
684 "*" .
685 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
686 "*" . $claim->cptNDCQuantity($prockey) .
687 "*" . $claim->cptNDCUOM($prockey) .
688 "~\n";
691 // Loop 2430, adjudication by previous payers.
693 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
694 if ($claim->payerSequence($ins) > $claim->payerSequence())
695 continue; // payer is future, not previous
697 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
698 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
700 if ($payerpaid[1] == 0 && !count($aarr)) {
701 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
702 "' has no payments or adjustments from previous payer!\n";
703 continue;
706 ++$edicount;
707 $out .= "SVD" . // Service line adjudication. Page 554.
708 "*" . $claim->payerID($ins) .
709 "*" . $payerpaid[1] .
710 "*HC:" . $claim->cptKey($prockey) .
711 "*" .
712 "*" . $claim->cptUnits($prockey) .
713 "~\n";
715 $tmpdate = $payerpaid[0];
716 foreach ($aarr as $a) {
717 ++$edicount;
718 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
719 "*" . $a[1] .
720 "*" . $a[2] .
721 "*" . $a[3] .
722 "~\n";
723 if (!$tmpdate) $tmpdate = $a[0];
726 if ($tmpdate) {
727 ++$edicount;
728 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
729 "*573" .
730 "*D8" .
731 "*$tmpdate" .
732 "~\n";
734 } // end loop 2430
735 } // end this procedure
737 ++$edicount;
738 $out .= "SE" . // SE Trailer
739 "*$edicount" .
740 "*0021" .
741 "~\n";
743 $out .= "GE" . // GE Trailer
744 "*1" .
745 "*1" .
746 "~\n";
748 $out .= "IEA" . // IEA Trailer
749 "*1" .
750 "*000000001" .
751 "~\n";
753 $log .= "\n";
754 return $out;