5 * Copyright 2016 Kevin McCormick <kevin@kt61p>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * @author Kevin McCormick
23 * @link: http://www.open-emr.org
25 * @subpackage ediHistory
30 * increment loop values ($lpval is a reference)
32 * @param $lptest the prospective loop value
33 * @param &$lpval the present loop value -- reassigned here
34 * @return integer value from strcmp()
36 function edih_change_loop($lptest, &$lpval)
38 // strcmp($str1,$str2) Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
39 if (strcmp($lptest, $lpval) > 0) {
40 //echo "$lptest greater than $lpval" .PHP_EOL;
44 return strcmp($lptest, $lpval);
48 * format segments for display of x12 edi files
50 * @param array $segments
51 * @param string $delimiter
54 function edih_segments_text($segments, $delimiter)
59 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
61 csv_edihist_log('edih_generic_text: invalid argument');
62 $str_html = "Invalid arguments for view of x12 file text<br>";
71 foreach ($segments as $key => $seg) {
74 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
82 * identify loops and format segments for display
83 * of 837 (professional claim) type x12 edi files
85 * @param array $segments
86 * @param string $delimiter
89 function edih_837_text($segments, $delimiter, $err_seg = '')
95 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
97 csv_edihist_log('edih_837_text: invalid argument');
98 $str_html .= "Invalid arguments for view of x12 file text<br>";
102 // to highlight identified errors listed in 999/997 ack
104 $er = edih_errseg_parse($err_seg);
105 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
106 $erseg = (isset($er['err'])) ?
$er['err'] : array();
121 foreach ($segments as $key => $seg) {
127 if ($erstn && ($erstn == $stn)) {
128 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
132 if (strncmp('ST'.$de, $seg, 3) === 0) {
136 $stn = explode($de, $seg)[2];
137 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
142 if (strncmp('BHT'.$de, $seg, 4) === 0) {
145 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
150 if (strncmp('HL'.$de, $seg, 3) === 0) {
151 $sar = explode($de, $seg);
152 if ($sar[3] == '20') { // level code
153 $loopid = '2000A'; // billing provider (clinic)
154 } elseif ($sar[3] == '22') {
155 $loopid = '2000B'; // subscriber
156 } elseif ($sar[3] == '23' ||
$sar[3] == 'PT') {
157 $loopid = '2000C'; // dependent
161 csv_edihist_log('edih_837_text: HL has no level '.$seg);
165 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
170 if (strncmp('CLM'.$de, $seg, 4) === 0) {
173 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
179 if (strncmp('SBR'.$de, $seg, 4) === 0) {
180 if ($loopid == '2000B') {
181 $title = 'Subscriber';
182 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
184 $title = 'Other Subscriber';
186 $str_html .= "<tr><td class='btloop' title='$title'> -- </td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
194 if (strncmp('LX'.$de, $seg, 3) === 0) {
196 $title = 'Svc Line Number';
197 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
203 if (strncmp('LIN'.$de, $seg, 4) === 0) {
206 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
212 if (strncmp('SVD'.$de, $seg, 4) === 0) {
214 $title = 'Line Adjudication';
215 $str_html .= "<tr></tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
221 if (strncmp('NM1'.$de, $seg, 4) === 0) {
222 $sar = explode($de, $seg);
223 $nm101 = ( isset($sar[1]) ) ?
$sar[1] : '';
224 if ($loopid == 'Begin' ||
strcmp(substr($loopid, 0, 4), '2320') < 0) {
225 if ($nm101 == '41') {
227 $title = 'Submitter';
228 } elseif ($nm101 == '40') {
231 } elseif ($nm101 == '85') {
234 } elseif ($nm101 == '87') {
237 } elseif ($nm101 == 'PE') {
239 $title = 'Pay to Plan';
240 } elseif ($nm101 == 'IL') {
242 $title = 'Subscriber';
243 } elseif ($nm101 == 'PR') {
246 } elseif ($nm101 == 'QC') {
249 } elseif ($nm101 == 'DN') {
251 $title = 'Referring Provider';
252 } elseif ($nm101 == 'P3') {
254 $title = 'Primary Care Provider';
255 } elseif ($nm101 == '82') {
257 $title = 'Rendering Provider';
258 } elseif ($nm101 == '77') {
260 $title = 'Service Facility';
261 } elseif ($nm101 == 'DQ') {
263 $title = 'Supervising Provider';
264 } elseif ($nm101 == 'PW') {
266 $title = 'Ambulance pickup';
267 } elseif ($nm101 == '45') {
269 $title = 'Ambulance dropoff';
271 } elseif (strcmp(substr($loopid, 0, 4), '2400') < 0) {
272 if ($nm101 == 'IL') {
274 $title = 'Other Subscriber';
275 } elseif ($nm101 == 'PR') {
277 $title = 'Other Payer';
278 } elseif ($nm101 == 'PR') {
280 $title = 'Other Referring Provider';
281 } elseif ($nm101 == '82') {
283 $title = 'Other Rendering Provider';
284 } elseif ($nm101 == '77') {
286 $title = 'Other Svc Facility';
287 } elseif ($nm101 == 'DQ') {
289 $title = 'Other Supervising Provider';
290 } elseif ($nm101 == '85') {
292 $title = 'Other Billing Provider';
295 if ($nm101 == '82') {
297 $title = 'Rendering Provider';
298 } elseif ($nm101 == 'QB') {
300 $title = 'Purchased Svc Provider';
301 } elseif ($nm101 == '77') {
303 $title = 'Service Facility';
304 } elseif ($nm101 == 'DQ') {
306 $title = 'Supervising Provider';
307 } elseif ($nm101 == 'DK') {
309 $title = 'Ordering Provider';
310 } elseif ($nm101 == 'DN') {
312 $title = 'Referring Provider';
313 } elseif ($nm101 == 'P3') {
315 $title = 'Primary Care Provider';
316 } elseif ($nm101 == 'PW') {
318 $title = 'Ambulance pickup';
319 } elseif ($nm101 == '45') {
321 $title = 'Ambulance dropoff';
326 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
332 if (strncmp('SE'.$de, $seg, 3) === 0) {
334 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
339 // for all the segments that do not begin loops
340 $str_html .= "<tr><td class='btloop' title='$title'> -- </td><td class='btnum' title='$key'>$stsegct</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
349 * identify loops and format segments for display
350 * of 270/271 (eligibility) type x12 edi files
352 * @param array $segments
353 * @param string $delimiter
356 function edih_271_text($segments, $delimiter, $err_seg = '')
361 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
363 csv_edihist_log('edih_271_text: invalid argument');
364 $str_html .= "Invalid arguments for view of x12 file text<br>";
377 // to highlight identified errors listed in 999/997 ack (for 270)
379 $er = edih_errseg_parse($err_seg);
380 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
381 $erseg = (isset($er['err'])) ?
$er['err'] : array();
389 $er = edih_errseg_parse($err_seg);
390 if (is_array($er) && count($er)) {
396 foreach ($segments as $key => $seg) {
402 if ($erstn && ($erstn == $stn)) {
403 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
407 if (strncmp('ST'.$de, $seg, 3) === 0) {
408 $sar = explode($de, $seg);
412 $sttp = (isset($seg[1])) ?
$seg[1] : '';
413 $stn = (isset($seg[2])) ?
$seg[2] : '';
414 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
419 if (strncmp('BHT'.$de, $seg, 4) === 0) {
421 // 2nd seg in transaction, ST may not be included if segments are transaction slice
426 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
431 if (strncmp('HL'.$de, $seg, 3) === 0) {
432 $sar = explode($de, $seg);
433 if ($sar[3] == '20') { // level code
434 $loopid = '2000A'; // info source (payer)
435 } elseif ($sar[3] == '21') {
436 $loopid = '2000B'; // info receiver (clinic)
437 } elseif ($sar[3] == '22') {
438 $loopid = '2000C'; // subscriber
440 } elseif ($sar[3] == '23') {
441 $loopid = '2000D'; // dependent
445 csv_edihist_log('edih_271_text: HL has no level '.$seg);
449 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
455 if (strncmp('NM1'.$de, $seg, 4) === 0) {
456 if (strncmp('NM1'.$de, $prevseg, 4) === 0) {
457 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
461 switch ((string)$loopid) {
464 break; // edih_change_loop($lptest, &$lpval)
478 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
479 $prevseg = 'NM1'.$de;
484 if (strncmp('EB'.$de, $seg, 3) === 0 ||
strncmp('EQ'.$de, $seg, 3) === 0) {
485 // EB* segment is in 271 type, EQ* is corresponding segment in 270 type
486 if (strncmp($seg, $prevseg, 3) === 0) {
487 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
488 $prevseg = substr($seg, 0, 3);
492 if ($loopid = '2100C' ||
$loopid = '2115C' ||
$loopid = '2120C') {
494 } elseif ($loopid = '2100D' ||
$loopid = '2115D' ||
$loopid = '2120D') {
498 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
499 $prevseg = substr($seg, 0, 3);
500 $has_eb = (strncmp('EB'.$de, $seg, 3) === 0);
506 if (strncmp('III'.$de, $seg, 4) === 0 && $has_eb) {
507 // the III segment begins a loop in 271 type, but not in 270
508 if ($loopid = '2110C') {
512 if ($loopid = '2100D') {
517 $str_html .= "<tr><td class='btloop'></td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
519 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
523 $prevseg = substr($seg, 0, 4);
528 if (strncmp('LS'.$de, $seg, 3) === 0) {
529 if ($loopid = '2110C' ||
$loopid = '2115C') {
531 } elseif ($loopid = '2110D' ||
$loopid = '2115D') {
535 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
540 if (strncmp('SE'.$de, $seg, 3) === 0) {
541 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
546 // for all the segments that do not begin loops
547 $prevseg = substr($seg, 0, strpos($seg, $de)+
1);
548 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
557 function edih_835_text($segments, $delimiter, $err_seg = '')
561 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
563 csv_edihist_log('edih_835_text: invalid segments');
564 $str_html .= "Invalid arguments for view of x12 file text<br>";
575 foreach ($segments as $key => $seg) {
578 if (strncmp('ST'.$de, $seg, 3) === 0) {
580 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
585 if (strncmp('N1'.$de, $seg, 3) === 0) {
586 $sar = explode($de, $seg);
587 if ($sar[1] == 'PR') {
589 } elseif ($sar[1] == 'PE') {
593 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
599 if (strncmp('LX'.$de, $seg, 3) === 0) {
601 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
607 if (strncmp('TS3'.$de, $seg, 4) === 0) {
608 if ($loopid == '2000') {
609 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
612 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
615 $prevseg = 'TS3'.$de;
620 if (strncmp('CLP'.$de, $seg, 4) === 0) {
622 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
623 $prevseg = 'CLP'.$de;
628 if (strncmp('SVC'.$de, $seg, 4) === 0) {
630 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
631 $prevseg = 'SVC'.$de;
636 if (strncmp('PLB'.$de, $seg, 4) === 0) {
638 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
639 $prevseg = 'PLB'.$de;
644 if (strncmp('SE'.$de, $seg, 3) === 0) {
646 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
651 // for all the segments that do not begin loops
652 $prevseg = substr($seg, 0, 3);
654 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
664 * identify loops and format segments for display
665 * of 277 (claim status) type x12 edi files
667 * @param array $segments
668 * @param string $delimiter
671 function edih_277_text($segments, $delimiter, $stpos = '')
675 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
677 csv_edihist_log('edih_277_text: invlid segments');
678 $str_html .= "Invalid arguments for view of x12 file text<br>";
683 // to highlight identified errors listed in 999/997 ack (for 276)
685 //$er = edih_errseg_parse($err_seg);
686 //$erstn = (isset($er['trace'])) ? substr($er['trace'], -4) : '';
687 //$erseg = (isset($er['err'])) ? $er['err'] : array();
700 foreach ($segments as $idx => $seg) {
703 $key = ($stpos) ?
$idx - $stpos : $idx;
704 // if 276 transactions are parsed, 999 errors may be present
705 //if ($erstn && ($erstn == $stn)) {
706 //$bterr = (in_array($stsegct, $erseg)) ? 'bterr' : 'btseg';
709 if (strncmp('ST'.$de, $seg, 3) === 0) {
712 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
717 if (strncmp('BHT'.$de, $seg, 4) === 0) {
719 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
724 if (strncmp('HL'.$de, $seg, 3) === 0) {
725 $sar = explode($de, $seg);
726 if ($sar[3] == '20') { // level code
727 $loopid = '2000A'; // info source (payer)
728 } elseif ($sar[3] == '21') {
729 $loopid = '2000B'; // info receiver (clinic)
730 } elseif ($sar[3] == '19') {
731 $loopid = '2000C'; // provider
732 } elseif ($sar[3] == '22' ||
$sar[3] == 'PT') {
733 $loopid = '2000D'; // subscriber
734 } elseif ($sar[3] == '23') {
735 $loopid = '2000E'; // dependent
738 csv_edihist_log('edih_277_text: HL has no level '.$seg);
742 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
749 if (strncmp('NM1'.$de, $seg, 4) === 0) {
750 if (strncmp('NM1'.$de, $prevseg, 4) === 0) {
751 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
755 switch ((string)$loopid) {
758 break; // edih_change_loop($lptest, &$lpval)
773 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
779 if (strncmp('TRN'.$de, $seg, 4) === 0) {
780 if (strncmp('TRN'.$de, $prevseg, 4) === 0) {
781 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
785 switch ((string)$loopid) {
803 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
809 if (strncmp('SVC'.$de, $seg, 4) === 0) {
810 if (strncmp('SVC'.$de, $prevseg, 4) === 0) {
811 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
815 switch ((string)$loopid) {
824 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
830 if (strncmp('SE'.$de, $seg, 3) === 0) {
832 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
837 // for all the segments that do not begin loops
838 $prevseg = substr($seg, 0, 3);
840 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
848 * identify loops and format segments for display
849 * of 278 (authorization) type x12 edi files
851 * @param array $segments
852 * @param string $delimiter
855 function edih_278_text($segments, $delimiter, $err_seg = '')
860 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
862 csv_edihist_log('edih_278_text(): invalid argument');
863 $str_html .= "Invalid arguments for view of x12 file text<br>";
877 // to highlight identified errors listed in 999/997 ack
879 $er = edih_errseg_parse($err_seg);
880 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
881 $erseg = (isset($er['err'])) ?
$er['err'] : array();
888 foreach ($segments as $key => $seg) {
894 if ($erstn && ($erstn == $stn)) {
895 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
899 if (strncmp('ST'.$de, $seg, 3) === 0) {
903 $stn = explode($de, $seg)[2];
904 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
909 if (strncmp('BHT'.$de, $seg, 4) === 0) {
911 // 2nd seg in transaction, ST may not be included if segments are transaction slice
916 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
921 if (strncmp($seg, 'HL'.$de, 3) === 0) {
922 $sar = explode($de, $seg);
924 $hlpc = $sar[2]; // parent code
926 $hlcc = (isset($sar[4])) ?
$sar[4] : ''; // child code
927 if ($sar[3] == '20') { // level code
928 $loopid = '2000A'; // info source (payer)
929 $title = 'Info Source';
930 } elseif ($sar[3] == '21') {
931 $loopid = '2000B'; // info receiver (clinic)
932 $title = 'Info Receiver';
933 } elseif ($sar[3] == '22') {
934 $loopid = '2000C'; // subscriber
935 $title = 'Subscriber';
936 } elseif ($sar[3] == '23') {
937 $loopid = '2000D'; // dependent
938 $title = 'Dependent';
939 } elseif ($sar[3] == 'EV') {
940 $loopid = '2000E'; // patient event
941 $title = 'Patient Event';
942 } elseif ($sar[3] == 'SS') {
943 $loopid = '2000F'; // service
947 csv_edihist_log('edih_278_text: HL has no level '.$seg);
951 $str_html .= "<tr><td class='btloop' title='$title'>$loopid</td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
957 if (strncmp($seg, 'NM1'.$de, 4) === 0) {
958 $sar = explode($de, $seg);
960 if ($loopid == '2000A') {
961 $loopid == '2010A'; // Source
962 } elseif ($loopid == '2000B') {
963 $loopid = '2010B'; // Receiver
964 } elseif ($loopid == '2000C') {
965 $loopid = '2010C'; // Subscriber
966 } elseif ($loopid == '2000D') {
967 $loopid = '2010D'; // Dependent
968 } elseif ($loopid == '2000E' ||
strpos($loopid, '010E')) { // Patient Event
969 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010EA' : $loopid;
970 $loopid = (strpos('|45|FS|ND|PW|R3', $nm101) ) ?
'2010EB' : $loopid;
971 $loopid = ($nm101 == 'L5') ?
'2010EC' : $loopid;
972 } elseif ($loopid == '2000F' ||
strpos($loopid, '010F')) { // Service
973 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010FA' : $loopid;
977 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
983 if (strncmp('SE'.$de, $seg, 3) === 0) {
984 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
990 // for all the segments that do not begin loops
991 $prevseg = substr($seg, 0, strpos($seg, $de));
992 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='$bterr'>$seg</td></tr>" .PHP_EOL
;
1001 * identify loops and format segments for display
1002 * of 997/999 (acknowledgement) type x12 edi files
1004 * @param array $segments
1005 * @param string $delimiter
1008 function edih_997_text($segments, $delimiter)
1012 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
1014 csv_edihist_log('edih_997_text(): invalid segments');
1022 //echo 'edih_997_text() foreach segment count: '.count($segments).PHP_EOL;
1024 foreach ($segments as $key => $seg) {
1026 //echo var_dump($seg).PHP_EOL;
1028 if (strncmp('TA1'.$de, $seg, 4) === 0) {
1029 $sar = explode($de, $seg);
1031 $loopid = 'ACK'; // not official
1032 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1034 $loopid = ''; // reset loop for subsequent segments
1039 if (strncmp('ST'.$de, $seg, 3) === 0) {
1041 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1046 if (strncmp('AK1'.$de, $seg, 4) === 0) {
1047 $sar = explode($de, $seg);
1048 $rsptp = csv_file_type($sar[1]);
1049 if ($rspicn && $rsptp) {
1050 $rspfile = csv_file_by_controlnum($rsptp, $rspicn);
1053 $title = ($rspfile) ?
'response to '.$rspfile : '';
1055 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td title='$title' class='btseg'>$seg</td></tr>" .PHP_EOL
;
1061 if (strncmp('AK2'.$de, $seg, 4) === 0) {
1063 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1068 if (strncmp('IK3'.$de, $seg, 4) === 0 ||
strncmp('AK3'.$de, $seg, 4) === 0) {
1070 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1075 if (strncmp('IK4'.$de, $seg, 4) === 0 ||
strncmp('AK4'.$de, $seg, 4) === 0) {
1077 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1082 if (strncmp('SE'.$de, $seg, 3) === 0) {
1083 $loopid = 'Trailer';
1084 $str_html .= "<tr><td class='btloop'>$loopid</td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1088 // for all the segments that do not begin loops ;
1089 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>$key</td><td class='btseg'>$seg</td></tr>" .PHP_EOL
;
1097 * Display of x12 edi transaction listing or all segments in the files.
1098 * When using $err_info, you must use the filepath of the submitted file
1100 * @param string $filepath path to desired file
1101 * @param string $filetype used when filepath is just filename
1102 * @param string optional $claimid CLM01, or BHT03 to identify a transaction or a trace value
1103 * @param bool false: $claimid is pt transaction, true: $claimid is trace from 835 or 999
1104 * @param string optional $err_info the prepared error info from a 997/999 response
1105 * @return string html for display of file segments
1107 function edih_display_text($filepath, $filetype = '', $claimid = '', $trace = false, $err_info = '')
1114 $ft = ($filetype) ?
$filetype : '';
1115 $errs = ( strlen($err_info) ) ?
$err_info : '';
1119 $x12obj = csv_check_x12_obj($filepath, $ft);
1121 if ($x12obj && 'edih_x12_file' == get_class($x12obj)) {
1122 $ftype = $x12obj->edih_type();
1123 $ft = csv_file_type($ftype);
1124 $delims = $x12obj->edih_delimiters();
1126 $fn = $x12obj->edih_filename();
1127 $segs_ar = $x12obj->edih_segments();
1128 $env_ar = $x12obj->edih_envelopes();
1129 if (!$de ||
strlen($de) != 1) {
1132 $str_html = 'edih_display_text(): error in delimiters<br />'.PHP_EOL
;
1133 $str_html .= $x12obj->edih_message().PHP_EOL
;
1137 if (!is_array($segs_ar) ||
!count($segs_ar)) {
1139 $str_html = "<p>unknown error retrieving segments for $fn</p>".PHP_EOL
;
1140 $str_html .= $x12obj->edih_message().PHP_EOL
;
1145 csv_edihist_log('edih_transaction_text(): invalid path '.$filepath);
1146 $str_html = 'edih_display_text(): error accessing file<br />'.PHP_EOL
;
1152 // claimid can be for transaction, payment, or error response
1153 if ($trace && array_key_exists($claimid, $env_ar['ISA'])) {
1154 $arg_ar = array('ISA13'=>$claimid, 'keys'=>true);
1155 $segments = $x12obj->edih_x12_slice($arg_ar);
1157 // claimid alone can be clm01 or bht03, if trace=true, expect trn02 for claimid
1158 foreach ($env_ar['ST'] as $st) {
1159 if ($trace && $claimid == $st['trace']) {
1160 $arg_ar = array('ISA13'=>$st['icn'], 'GS06'=>$st['gsn'], 'trace'=>$claimid, 'keys'=>true);
1161 $segments = $x12obj->edih_x12_slice($arg_ar);
1163 } elseif (in_array($claimid, $st['acct'])) {
1165 $arg_ar = array('ST02'=>$st['stn'], 'ISA13'=>$st['icn'], 'GS06'=>$st['gsn'], 'keys'=>true);
1166 $segments = $x12obj->edih_x12_slice($arg_ar);
1168 // request for individual transaction segments
1169 $segments = $x12obj->edih_x12_transaction($claimid);
1173 } elseif (in_array($claimid, $st['bht03'])) {
1174 // also possible that bht03 number is given for claimid
1175 // this will likely be a 27x
1177 $arg_ar = array('ST02'=>$st['stn'], 'ISA13'=>$st['icn'], 'GS06'=>$st['gsn'], 'keys'=>true);
1178 $segments = $x12obj->edih_x12_slice($arg_ar);
1180 $segments = $x12obj->edih_x12_transaction($claimid);
1189 $segments = $segs_ar;
1193 // now check if we have segments
1194 if (empty($segments) ||
!count($segments)) {
1196 $str_html = "<p>error: transaction $claimid not found in $fn</p>".PHP_EOL
;
1197 $str_html .= $x12obj->edih_message().PHP_EOL
;
1201 $str_html = "<p>unknown error retrieving segments for $fn</p>".PHP_EOL
;
1202 $str_html .= $x12obj->edih_message().PHP_EOL
;
1207 // if the segments are from a slice or transaction
1208 // a multidimensional array segs[i][j] must be flattened
1209 $ar_sngl = csv_singlerecord_test($segments);
1210 // false when segments are a transaction or trace only
1213 // append segments to single array
1214 // keys should not duplicate since all segments
1215 // are from the same x12 file
1217 for ($i=0; $i<count($segments); $i++
) {
1218 $trnsegs = array_merge($trnsegs, $segments[$i]);
1221 $segments = $trnsegs;
1227 $tbl_id = ($claimid) ?
$claimid : $fn;
1229 //'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
1230 switch ((string)$ftype) {
1232 $capstr = "Claim "; //$cls = "txt837";
1233 $trn_html = edih_837_text($segments, $de, $errs);
1236 $capstr = "Payment "; //$cls = "txt835";
1237 $trn_html = edih_835_text($segments, $de);
1240 $capstr = "Status Query "; //$cls = "txt276";
1241 $trn_html = edih_277_text($segments, $de, $errs);
1244 $capstr = "Claim Status "; //$cls = "txt277";
1245 $trn_html = edih_277_text($segments, $de, $stsegkey);
1248 $capstr = "Eligibility Query "; //$cls = "txt270";
1249 $trn_html = edih_271_text($segments, $de, $errs);
1252 $capstr = "Eligibility Report "; //$cls = "txt271";
1253 $trn_html = edih_271_text($segments, $de);
1256 $capstr = "Authorization "; //$cls = "txt278";
1257 $trn_html = edih_278_text($segments, $de, $errs);
1260 $capstr = "Batch Acknowledgment "; //$cls = "txt997";
1261 $trn_html = edih_997_text($segments, $de);
1264 $capstr = "x12 $ftype "; //$cls = "txt_x12";
1265 $trn_html = edih_segments_text($segments, $de);
1270 $capstr .= ($claimid) ?
" <em>ID:</em> $claimid" : "";
1272 $str_html .= "<table id=$tbl_id cols=3 class='segtxt'><caption>$capstr</caption>".PHP_EOL
;
1273 $str_html .= "<thead>".PHP_EOL
;
1274 $str_html .= "<tr><th class='btloop'>Loop</th><th class='btloop'>Num</th>";
1275 $str_html .= "<th class='segtxt'>Segment (<em>File:</em> $fn)</th></tr>".PHP_EOL
;
1276 $str_html .= "</thead>".PHP_EOL
."<tbody>".PHP_EOL
;
1278 $str_html .= $trn_html;
1280 $str_html .= "</tbody></table>".PHP_EOL
;