6 * Copyright 2016 Kevin McCormick <kevin@kt61p>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 * @author Kevin McCormick
24 * @link: https://www.open-emr.org
26 * @subpackage ediHistory
31 * increment loop values ($lpval is a reference)
33 * @param $lptest the prospective loop value
34 * @param &$lpval the present loop value -- reassigned here
35 * @return integer value from strcmp()
37 function edih_change_loop($lptest, &$lpval)
39 // strcmp($str1,$str2) Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
40 if (strcmp($lptest, $lpval) > 0) {
41 //echo "$lptest greater than $lpval" .PHP_EOL;
45 return strcmp($lptest, $lpval);
49 * format segments for display of x12 edi files
51 * @param array $segments
52 * @param string $delimiter
55 function edih_segments_text($segments, $delimiter)
60 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
62 csv_edihist_log('edih_generic_text: invalid argument');
63 $str_html = "Invalid arguments for view of x12 file text<br />";
72 foreach ($segments as $key => $seg) {
75 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
83 * identify loops and format segments for display
84 * of 837 (professional claim) type x12 edi files
86 * @param array $segments
87 * @param string $delimiter
90 function edih_837_text($segments, $delimiter, $err_seg = '')
96 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
98 csv_edihist_log('edih_837_text: invalid argument');
99 $str_html .= "Invalid arguments for view of x12 file text<br />";
103 // to highlight identified errors listed in 999/997 ack
105 $er = edih_errseg_parse($err_seg);
106 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
107 $erseg = (isset($er['err'])) ?
$er['err'] : array();
122 foreach ($segments as $key => $seg) {
128 if ($erstn && ($erstn == $stn)) {
129 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
133 if (strncmp('ST' . $de, $seg, 3) === 0) {
137 $stn = explode($de, $seg)[2];
138 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
143 if (strncmp('BHT' . $de, $seg, 4) === 0) {
146 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
151 if (strncmp('HL' . $de, $seg, 3) === 0) {
152 $sar = explode($de, $seg);
153 if ($sar[3] == '20') { // level code
154 $loopid = '2000A'; // billing provider (clinic)
155 } elseif ($sar[3] == '22') {
156 $loopid = '2000B'; // subscriber
157 } elseif ($sar[3] == '23' ||
$sar[3] == 'PT') {
158 $loopid = '2000C'; // dependent
162 csv_edihist_log('edih_837_text: HL has no level ' . $seg);
166 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
171 if (strncmp('CLM' . $de, $seg, 4) === 0) {
174 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
180 if (strncmp('SBR' . $de, $seg, 4) === 0) {
181 if ($loopid == '2000B') {
182 $title = 'Subscriber';
183 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
185 $title = 'Other Subscriber';
187 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'> -- </td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
195 if (strncmp('LX' . $de, $seg, 3) === 0) {
197 $title = 'Svc Line Number';
198 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
204 if (strncmp('LIN' . $de, $seg, 4) === 0) {
207 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
213 if (strncmp('SVD' . $de, $seg, 4) === 0) {
215 $title = 'Line Adjudication';
216 $str_html .= "<tr></tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
222 if (strncmp('NM1' . $de, $seg, 4) === 0) {
223 $sar = explode($de, $seg);
224 $nm101 = ( isset($sar[1]) ) ?
$sar[1] : '';
225 if ($loopid == 'Begin' ||
strcmp(substr($loopid, 0, 4), '2320') < 0) {
226 if ($nm101 == '41') {
228 $title = 'Submitter';
229 } elseif ($nm101 == '40') {
232 } elseif ($nm101 == '85') {
235 } elseif ($nm101 == '87') {
238 } elseif ($nm101 == 'PE') {
240 $title = 'Pay to Plan';
241 } elseif ($nm101 == 'IL') {
243 $title = 'Subscriber';
244 } elseif ($nm101 == 'PR') {
247 } elseif ($nm101 == 'QC') {
250 } elseif ($nm101 == 'DN') {
252 $title = 'Referring Provider';
253 } elseif ($nm101 == 'P3') {
255 $title = 'Primary Care Provider';
256 } elseif ($nm101 == '82') {
258 $title = 'Rendering Provider';
259 } elseif ($nm101 == '77') {
261 $title = 'Service Facility';
262 } elseif ($nm101 == 'DQ') {
264 $title = 'Supervising Provider';
265 } elseif ($nm101 == 'PW') {
267 $title = 'Ambulance pickup';
268 } elseif ($nm101 == '45') {
270 $title = 'Ambulance dropoff';
272 } elseif (strcmp(substr($loopid, 0, 4), '2400') < 0) {
273 if ($nm101 == 'IL') {
275 $title = 'Other Subscriber';
276 } elseif ($nm101 == 'PR') {
278 $title = 'Other Payer';
279 } elseif ($nm101 == 'PR') {
281 $title = 'Other Referring Provider';
282 } elseif ($nm101 == '82') {
284 $title = 'Other Rendering Provider';
285 } elseif ($nm101 == '77') {
287 $title = 'Other Svc Facility';
288 } elseif ($nm101 == 'DQ') {
290 $title = 'Other Supervising Provider';
291 } elseif ($nm101 == '85') {
293 $title = 'Other Billing Provider';
296 if ($nm101 == '82') {
298 $title = 'Rendering Provider';
299 } elseif ($nm101 == 'QB') {
301 $title = 'Purchased Svc Provider';
302 } elseif ($nm101 == '77') {
304 $title = 'Service Facility';
305 } elseif ($nm101 == 'DQ') {
307 $title = 'Supervising Provider';
308 } elseif ($nm101 == 'DK') {
310 $title = 'Ordering Provider';
311 } elseif ($nm101 == 'DN') {
313 $title = 'Referring Provider';
314 } elseif ($nm101 == 'P3') {
316 $title = 'Primary Care Provider';
317 } elseif ($nm101 == 'PW') {
319 $title = 'Ambulance pickup';
320 } elseif ($nm101 == '45') {
322 $title = 'Ambulance dropoff';
327 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
333 if (strncmp('SE' . $de, $seg, 3) === 0) {
335 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
340 // for all the segments that do not begin loops
341 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'> -- </td><td class='btnum' title='" . attr($key) . "'>" . text($stsegct) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
350 * identify loops and format segments for display
351 * of 270/271 (eligibility) type x12 edi files
353 * @param array $segments
354 * @param string $delimiter
357 function edih_271_text($segments, $delimiter, $err_seg = '')
362 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
364 csv_edihist_log('edih_271_text: invalid argument');
365 $str_html .= "Invalid arguments for view of x12 file text<br />";
378 // to highlight identified errors listed in 999/997 ack (for 270)
380 $er = edih_errseg_parse($err_seg);
381 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
382 $erseg = (isset($er['err'])) ?
$er['err'] : array();
390 $er = edih_errseg_parse($err_seg);
391 if (is_array($er) && count($er)) {
397 foreach ($segments as $key => $seg) {
403 if ($erstn && ($erstn == $stn)) {
404 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
408 if (strncmp('ST' . $de, $seg, 3) === 0) {
409 $sar = explode($de, $seg);
413 $sttp = (isset($seg[1])) ?
$seg[1] : '';
414 $stn = (isset($seg[2])) ?
$seg[2] : '';
415 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
420 if (strncmp('BHT' . $de, $seg, 4) === 0) {
422 // 2nd seg in transaction, ST may not be included if segments are transaction slice
427 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
432 if (strncmp('HL' . $de, $seg, 3) === 0) {
433 $sar = explode($de, $seg);
434 if ($sar[3] == '20') { // level code
435 $loopid = '2000A'; // info source (payer)
436 } elseif ($sar[3] == '21') {
437 $loopid = '2000B'; // info receiver (clinic)
438 } elseif ($sar[3] == '22') {
439 $loopid = '2000C'; // subscriber
441 } elseif ($sar[3] == '23') {
442 $loopid = '2000D'; // dependent
446 csv_edihist_log('edih_271_text: HL has no level ' . $seg);
450 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
451 $prevseg = 'HL' . $de;
456 if (strncmp('NM1' . $de, $seg, 4) === 0) {
457 if (strncmp('NM1' . $de, $prevseg, 4) === 0) {
458 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
462 switch ((string)$loopid) {
465 break; // edih_change_loop($lptest, &$lpval)
479 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
480 $prevseg = 'NM1' . $de;
485 if (strncmp('EB' . $de, $seg, 3) === 0 ||
strncmp('EQ' . $de, $seg, 3) === 0) {
486 // EB* segment is in 271 type, EQ* is corresponding segment in 270 type
487 if (strncmp($seg, $prevseg, 3) === 0) {
488 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
489 $prevseg = substr($seg, 0, 3);
493 if ($loopid = '2100C' ||
$loopid = '2115C' ||
$loopid = '2120C') {
495 } elseif ($loopid = '2100D' ||
$loopid = '2115D' ||
$loopid = '2120D') {
499 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
500 $prevseg = substr($seg, 0, 3);
501 $has_eb = (strncmp('EB' . $de, $seg, 3) === 0);
507 if (strncmp('III' . $de, $seg, 4) === 0 && $has_eb) {
508 // the III segment begins a loop in 271 type, but not in 270
509 if ($loopid = '2110C') {
513 if ($loopid = '2100D') {
518 $str_html .= "<tr><td class='btloop'></td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
520 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
524 $prevseg = substr($seg, 0, 4);
529 if (strncmp('LS' . $de, $seg, 3) === 0) {
530 if ($loopid = '2110C' ||
$loopid = '2115C') {
532 } elseif ($loopid = '2110D' ||
$loopid = '2115D') {
536 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
541 if (strncmp('SE' . $de, $seg, 3) === 0) {
542 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
547 // for all the segments that do not begin loops
548 $prevseg = substr($seg, 0, strpos($seg, $de) +
1);
549 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
558 function edih_835_text($segments, $delimiter, $err_seg = '')
562 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
564 csv_edihist_log('edih_835_text: invalid segments');
565 $str_html .= "Invalid arguments for view of x12 file text<br />";
576 foreach ($segments as $key => $seg) {
579 if (strncmp('ST' . $de, $seg, 3) === 0) {
581 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
586 if (strncmp('N1' . $de, $seg, 3) === 0) {
587 $sar = explode($de, $seg);
588 if ($sar[1] == 'PR') {
590 } elseif ($sar[1] == 'PE') {
594 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
595 $prevseg = 'N1' . $de;
600 if (strncmp('LX' . $de, $seg, 3) === 0) {
602 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
603 $prevseg = 'LX' . $de;
608 if (strncmp('TS3' . $de, $seg, 4) === 0) {
609 if ($loopid == '2000') {
610 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
613 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
616 $prevseg = 'TS3' . $de;
621 if (strncmp('CLP' . $de, $seg, 4) === 0) {
623 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
624 $prevseg = 'CLP' . $de;
629 if (strncmp('SVC' . $de, $seg, 4) === 0) {
631 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
632 $prevseg = 'SVC' . $de;
637 if (strncmp('PLB' . $de, $seg, 4) === 0) {
639 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
640 $prevseg = 'PLB' . $de;
645 if (strncmp('SE' . $de, $seg, 3) === 0) {
647 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
648 $prevseg = 'SE' . $de;
652 // for all the segments that do not begin loops
653 $prevseg = substr($seg, 0, 3);
655 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
665 * identify loops and format segments for display
666 * of 277 (claim status) type x12 edi files
668 * @param array $segments
669 * @param string $delimiter
672 function edih_277_text($segments, $delimiter, $stpos = '')
676 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
678 csv_edihist_log('edih_277_text: invlid segments');
679 $str_html .= "Invalid arguments for view of x12 file text<br />";
684 // to highlight identified errors listed in 999/997 ack (for 276)
686 //$er = edih_errseg_parse($err_seg);
687 //$erstn = (isset($er['trace'])) ? substr($er['trace'], -4) : '';
688 //$erseg = (isset($er['err'])) ? $er['err'] : array();
701 foreach ($segments as $idx => $seg) {
704 $key = ($stpos) ?
$idx - $stpos : $idx;
705 // if 276 transactions are parsed, 999 errors may be present
706 //if ($erstn && ($erstn == $stn)) {
707 //$bterr = (in_array($stsegct, $erseg)) ? 'bterr' : 'btseg';
710 if (strncmp('ST' . $de, $seg, 3) === 0) {
713 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
718 if (strncmp('BHT' . $de, $seg, 4) === 0) {
720 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
725 if (strncmp('HL' . $de, $seg, 3) === 0) {
726 $sar = explode($de, $seg);
727 if ($sar[3] == '20') { // level code
728 $loopid = '2000A'; // info source (payer)
729 } elseif ($sar[3] == '21') {
730 $loopid = '2000B'; // info receiver (clinic)
731 } elseif ($sar[3] == '19') {
732 $loopid = '2000C'; // provider
733 } elseif ($sar[3] == '22' ||
$sar[3] == 'PT') {
734 $loopid = '2000D'; // subscriber
735 } elseif ($sar[3] == '23') {
736 $loopid = '2000E'; // dependent
739 csv_edihist_log('edih_277_text: HL has no level ' . $seg);
743 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
744 $prevseg = 'HL' . $de;
750 if (strncmp('NM1' . $de, $seg, 4) === 0) {
751 if (strncmp('NM1' . $de, $prevseg, 4) === 0) {
752 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
756 switch ((string)$loopid) {
759 break; // edih_change_loop($lptest, &$lpval)
774 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
780 if (strncmp('TRN' . $de, $seg, 4) === 0) {
781 if (strncmp('TRN' . $de, $prevseg, 4) === 0) {
782 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
786 switch ((string)$loopid) {
804 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
810 if (strncmp('SVC' . $de, $seg, 4) === 0) {
811 if (strncmp('SVC' . $de, $prevseg, 4) === 0) {
812 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
816 switch ((string)$loopid) {
825 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
831 if (strncmp('SE' . $de, $seg, 3) === 0) {
833 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
838 // for all the segments that do not begin loops
839 $prevseg = substr($seg, 0, 3);
841 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
849 * identify loops and format segments for display
850 * of 278 (authorization) type x12 edi files
852 * @param array $segments
853 * @param string $delimiter
856 function edih_278_text($segments, $delimiter, $err_seg = '')
861 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
863 csv_edihist_log('edih_278_text(): invalid argument');
864 $str_html .= "Invalid arguments for view of x12 file text<br />";
878 // to highlight identified errors listed in 999/997 ack
880 $er = edih_errseg_parse($err_seg);
881 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
882 $erseg = (isset($er['err'])) ?
$er['err'] : array();
889 foreach ($segments as $key => $seg) {
895 if ($erstn && ($erstn == $stn)) {
896 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
900 if (strncmp('ST' . $de, $seg, 3) === 0) {
904 $stn = explode($de, $seg)[2];
905 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
910 if (strncmp('BHT' . $de, $seg, 4) === 0) {
912 // 2nd seg in transaction, ST may not be included if segments are transaction slice
917 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
922 if (strncmp($seg, 'HL' . $de, 3) === 0) {
923 $sar = explode($de, $seg);
925 $hlpc = $sar[2]; // parent code
927 $hlcc = (isset($sar[4])) ?
$sar[4] : ''; // child code
928 if ($sar[3] == '20') { // level code
929 $loopid = '2000A'; // info source (payer)
930 $title = 'Info Source';
931 } elseif ($sar[3] == '21') {
932 $loopid = '2000B'; // info receiver (clinic)
933 $title = 'Info Receiver';
934 } elseif ($sar[3] == '22') {
935 $loopid = '2000C'; // subscriber
936 $title = 'Subscriber';
937 } elseif ($sar[3] == '23') {
938 $loopid = '2000D'; // dependent
939 $title = 'Dependent';
940 } elseif ($sar[3] == 'EV') {
941 $loopid = '2000E'; // patient event
942 $title = 'Patient Event';
943 } elseif ($sar[3] == 'SS') {
944 $loopid = '2000F'; // service
948 csv_edihist_log('edih_278_text: HL has no level ' . $seg);
952 $str_html .= "<tr><td class='btloop' title='" . attr($title) . "'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
958 if (strncmp($seg, 'NM1' . $de, 4) === 0) {
959 $sar = explode($de, $seg);
961 if ($loopid == '2000A') {
962 $loopid == '2010A'; // Source
963 } elseif ($loopid == '2000B') {
964 $loopid = '2010B'; // Receiver
965 } elseif ($loopid == '2000C') {
966 $loopid = '2010C'; // Subscriber
967 } elseif ($loopid == '2000D') {
968 $loopid = '2010D'; // Dependent
969 } elseif ($loopid == '2000E' ||
strpos($loopid, '010E')) { // Patient Event
970 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010EA' : $loopid;
971 $loopid = (strpos('|45|FS|ND|PW|R3', $nm101) ) ?
'2010EB' : $loopid;
972 $loopid = ($nm101 == 'L5') ?
'2010EC' : $loopid;
973 } elseif ($loopid == '2000F' ||
strpos($loopid, '010F')) { // Service
974 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010FA' : $loopid;
978 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
984 if (strncmp('SE' . $de, $seg, 3) === 0) {
985 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
991 // for all the segments that do not begin loops
992 $prevseg = substr($seg, 0, strpos($seg, $de));
993 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1002 * identify loops and format segments for display
1003 * of 997/999 (acknowledgement) type x12 edi files
1005 * @param array $segments
1006 * @param string $delimiter
1009 function edih_997_text($segments, $delimiter)
1013 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
1015 csv_edihist_log('edih_997_text(): invalid segments');
1023 //echo 'edih_997_text() foreach segment count: '.count($segments).PHP_EOL;
1025 foreach ($segments as $key => $seg) {
1027 //echo var_dump($seg).PHP_EOL;
1029 if (strncmp('TA1' . $de, $seg, 4) === 0) {
1030 $sar = explode($de, $seg);
1032 $loopid = 'ACK'; // not official
1033 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1035 $loopid = ''; // reset loop for subsequent segments
1040 if (strncmp('ST' . $de, $seg, 3) === 0) {
1042 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1047 if (strncmp('AK1' . $de, $seg, 4) === 0) {
1048 $sar = explode($de, $seg);
1049 $rsptp = csv_file_type($sar[1]);
1050 if ($rspicn && $rsptp) {
1051 $rspfile = csv_file_by_controlnum($rsptp, $rspicn);
1054 $title = ($rspfile) ?
'response to ' . $rspfile : '';
1056 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td title='" . attr($title) . "' class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1062 if (strncmp('AK2' . $de, $seg, 4) === 0) {
1064 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1069 if (strncmp('IK3' . $de, $seg, 4) === 0 ||
strncmp('AK3' . $de, $seg, 4) === 0) {
1071 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1076 if (strncmp('IK4' . $de, $seg, 4) === 0 ||
strncmp('AK4' . $de, $seg, 4) === 0) {
1078 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1083 if (strncmp('SE' . $de, $seg, 3) === 0) {
1084 $loopid = 'Trailer';
1085 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1089 // for all the segments that do not begin loops ;
1090 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1098 * Display of x12 edi transaction listing or all segments in the files.
1099 * When using $err_info, you must use the filepath of the submitted file
1101 * @param string $filepath path to desired file
1102 * @param string $filetype used when filepath is just filename
1103 * @param string optional $claimid CLM01, or BHT03 to identify a transaction or a trace value
1104 * @param bool false: $claimid is pt transaction, true: $claimid is trace from 835 or 999
1105 * @param string optional $err_info the prepared error info from a 997/999 response
1106 * @return string html for display of file segments
1108 function edih_display_text($filepath, $filetype = '', $claimid = '', $trace = false, $err_info = '')
1115 $ft = ($filetype) ?
$filetype : '';
1116 $errs = ( strlen($err_info) ) ?
$err_info : '';
1120 $x12obj = csv_check_x12_obj($filepath, $ft);
1122 if ($x12obj && 'edih_x12_file' == get_class($x12obj)) {
1123 $ftype = $x12obj->edih_type();
1124 $ft = csv_file_type($ftype);
1125 $delims = $x12obj->edih_delimiters();
1127 $fn = $x12obj->edih_filename();
1128 $segs_ar = $x12obj->edih_segments();
1129 $env_ar = $x12obj->edih_envelopes();
1130 if (!$de ||
strlen($de) != 1) {
1133 $str_html = 'edih_display_text(): error in delimiters<br />' . PHP_EOL
;
1134 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1138 if (!is_array($segs_ar) ||
!count($segs_ar)) {
1140 $str_html = "<p>unknown error retrieving segments for " . text($fn) . "</p>" . PHP_EOL
;
1141 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1146 csv_edihist_log('edih_transaction_text(): invalid path ' . $filepath);
1147 $str_html = 'edih_display_text(): error accessing file<br />' . PHP_EOL
;
1153 // claimid can be for transaction, payment, or error response
1154 if ($trace && array_key_exists($claimid, $env_ar['ISA'])) {
1155 $arg_ar = array('ISA13' => $claimid, 'keys' => true);
1156 $segments = $x12obj->edih_x12_slice($arg_ar);
1158 // claimid alone can be clm01 or bht03, if trace=true, expect trn02 for claimid
1159 foreach ($env_ar['ST'] as $st) {
1160 if ($trace && $claimid == $st['trace']) {
1161 $arg_ar = array('ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'trace' => $claimid, 'keys' => true);
1162 $segments = $x12obj->edih_x12_slice($arg_ar);
1164 } elseif (in_array($claimid, $st['acct'])) {
1166 $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
1167 $segments = $x12obj->edih_x12_slice($arg_ar);
1169 // request for individual transaction segments
1170 $segments = $x12obj->edih_x12_transaction($claimid);
1174 } elseif (in_array($claimid, $st['bht03'])) {
1175 // also possible that bht03 number is given for claimid
1176 // this will likely be a 27x
1178 $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
1179 $segments = $x12obj->edih_x12_slice($arg_ar);
1181 $segments = $x12obj->edih_x12_transaction($claimid);
1190 $segments = $segs_ar;
1194 // now check if we have segments
1195 if (empty($segments) ||
!count($segments)) {
1197 $str_html = "<p>error: transaction " . text($claimid) . " not found in " . text($fn) . "</p>" . PHP_EOL
;
1198 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1202 $str_html = "<p>unknown error retrieving segments for " . text($fn) . "</p>" . PHP_EOL
;
1203 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1208 // if the segments are from a slice or transaction
1209 // a multidimensional array segs[i][j] must be flattened
1210 $ar_sngl = csv_singlerecord_test($segments);
1211 // false when segments are a transaction or trace only
1214 // append segments to single array
1215 // keys should not duplicate since all segments
1216 // are from the same x12 file
1218 for ($i = 0; $i < count($segments); $i++
) {
1219 $trnsegs = array_merge($trnsegs, $segments[$i]);
1222 $segments = $trnsegs;
1228 $tbl_id = ($claimid) ?
$claimid : $fn;
1230 //'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
1231 switch ((string)$ftype) {
1233 $capstr = "Claim "; //$cls = "txt837";
1234 $trn_html = edih_837_text($segments, $de, $errs);
1237 $capstr = "Payment "; //$cls = "txt835";
1238 $trn_html = edih_835_text($segments, $de);
1241 $capstr = "Status Query "; //$cls = "txt276";
1242 $trn_html = edih_277_text($segments, $de, $errs);
1245 $capstr = "Claim Status "; //$cls = "txt277";
1246 $trn_html = edih_277_text($segments, $de, $stsegkey);
1249 $capstr = "Eligibility Query "; //$cls = "txt270";
1250 $trn_html = edih_271_text($segments, $de, $errs);
1253 $capstr = "Eligibility Report "; //$cls = "txt271";
1254 $trn_html = edih_271_text($segments, $de);
1257 $capstr = "Authorization "; //$cls = "txt278";
1258 $trn_html = edih_278_text($segments, $de, $errs);
1261 $capstr = "Batch Acknowledgment "; //$cls = "txt997";
1262 $trn_html = edih_997_text($segments, $de);
1265 $capstr = "x12 $ftype "; //$cls = "txt_x12";
1266 $trn_html = edih_segments_text($segments, $de);
1271 $capstr .= ($claimid) ?
" <em>ID:</em> " . text($claimid) : "";
1273 $str_html .= "<table id=" . attr($tbl_id) . " cols=3 class='segtxt'><caption>" . text($capstr) . "</caption>" . PHP_EOL
;
1274 $str_html .= "<thead>" . PHP_EOL
;
1275 $str_html .= "<tr><th class='btloop'>Loop</th><th class='btloop'>Num</th>";
1276 $str_html .= "<th class='segtxt'>Segment (<em>File:</em> " . text($fn) . ")</th></tr>" . PHP_EOL
;
1277 $str_html .= "</thead>" . PHP_EOL
. "<tbody>" . PHP_EOL
;
1279 $str_html .= $trn_html;
1281 $str_html .= "</tbody></table>" . PHP_EOL
;