7 * @link http://www.open-emr.org
8 * @author Kevin McCormick Longview, Texas
9 * @author Stephen Waite <stephen.waite@cmsvt.com>
10 * @copyright Copyright (c) 2016 Kevin McCormick Longview, Texas
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 * increment loop values ($lpval is a reference)
18 * @param $lptest the prospective loop value
19 * @param &$lpval the present loop value -- reassigned here
20 * @return integer value from strcmp()
22 function edih_change_loop($lptest, &$lpval)
24 // strcmp($str1,$str2) Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
25 if (strcmp($lptest, $lpval) > 0) {
26 //echo "$lptest greater than $lpval" .PHP_EOL;
30 return strcmp($lptest, $lpval);
34 * format segments for display of x12 edi files
36 * @param array $segments
37 * @param string $delimiter
40 function edih_segments_text($segments, $delimiter)
45 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
47 csv_edihist_log('edih_generic_text: invalid argument');
48 $str_html = "Invalid arguments for view of x12 file text<br />";
57 foreach ($segments as $key => $seg) {
60 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
68 * identify loops and format segments for display
69 * of 837 (professional claim) type x12 edi files
71 * @param array $segments
72 * @param string $delimiter
75 function edih_837_text($segments, $delimiter, $err_seg = '')
81 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
83 csv_edihist_log('edih_837_text: invalid argument');
84 $str_html .= "Invalid arguments for view of x12 file text<br />";
88 // to highlight identified errors listed in 999/997 ack
90 $er = edih_errseg_parse($err_seg);
91 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
92 $erseg = (isset($er['err'])) ?
$er['err'] : array();
107 foreach ($segments as $key => $seg) {
113 if ($erstn && ($erstn == $stn)) {
114 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
118 if (strncmp('ST' . $de, $seg, 3) === 0) {
122 $stn = explode($de, $seg)[2];
123 $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
;
128 if (strncmp('BHT' . $de, $seg, 4) === 0) {
131 $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
;
136 if (strncmp('HL' . $de, $seg, 3) === 0) {
137 $sar = explode($de, $seg);
138 if ($sar[3] == '20') { // level code
139 $loopid = '2000A'; // billing provider (clinic)
140 } elseif ($sar[3] == '22') {
141 $loopid = '2000B'; // subscriber
142 } elseif ($sar[3] == '23' ||
$sar[3] == 'PT') {
143 $loopid = '2000C'; // dependent
147 csv_edihist_log('edih_837_text: HL has no level ' . $seg);
151 $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
;
156 if (strncmp('CLM' . $de, $seg, 4) === 0) {
159 $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
;
165 if (strncmp('SBR' . $de, $seg, 4) === 0) {
166 if ($loopid == '2000B') {
167 $title = 'Subscriber';
168 $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
;
170 $title = 'Other Subscriber';
172 $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
;
180 if (strncmp('LX' . $de, $seg, 3) === 0) {
182 $title = 'Svc Line Number';
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
;
189 if (strncmp('LIN' . $de, $seg, 4) === 0) {
192 $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
;
198 if (strncmp('SVD' . $de, $seg, 4) === 0) {
200 $title = 'Line Adjudication';
201 $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
;
207 if (strncmp('NM1' . $de, $seg, 4) === 0) {
208 $sar = explode($de, $seg);
209 $nm101 = ( isset($sar[1]) ) ?
$sar[1] : '';
210 if ($loopid == 'Begin' ||
strcmp(substr($loopid, 0, 4), '2320') < 0) {
211 if ($nm101 == '41') {
213 $title = 'Submitter';
214 } elseif ($nm101 == '40') {
217 } elseif ($nm101 == '85') {
220 } elseif ($nm101 == '87') {
223 } elseif ($nm101 == 'PE') {
225 $title = 'Pay to Plan';
226 } elseif ($nm101 == 'IL') {
228 $title = 'Subscriber';
229 } elseif ($nm101 == 'PR') {
232 } elseif ($nm101 == 'QC') {
235 } elseif ($nm101 == 'DN') {
237 $title = 'Referring Provider';
238 } elseif ($nm101 == 'P3') {
240 $title = 'Primary Care Provider';
241 } elseif ($nm101 == '82') {
243 $title = 'Rendering Provider';
244 } elseif ($nm101 == '77') {
246 $title = 'Service Facility';
247 } elseif ($nm101 == 'DQ') {
249 $title = 'Supervising Provider';
250 } elseif ($nm101 == 'PW') {
252 $title = 'Ambulance pickup';
253 } elseif ($nm101 == '45') {
255 $title = 'Ambulance dropoff';
257 } elseif (strcmp(substr($loopid, 0, 4), '2400') < 0) {
258 if ($nm101 == 'IL') {
260 $title = 'Other Subscriber';
261 } elseif ($nm101 == 'PR') {
263 $title = 'Other Payer';
264 } elseif ($nm101 == 'PR') {
266 $title = 'Other Referring Provider';
267 } elseif ($nm101 == '82') {
269 $title = 'Other Rendering Provider';
270 } elseif ($nm101 == '77') {
272 $title = 'Other Svc Facility';
273 } elseif ($nm101 == 'DQ') {
275 $title = 'Other Supervising Provider';
276 } elseif ($nm101 == '85') {
278 $title = 'Other Billing Provider';
281 if ($nm101 == '82') {
283 $title = 'Rendering Provider';
284 } elseif ($nm101 == 'QB') {
286 $title = 'Purchased Svc Provider';
287 } elseif ($nm101 == '77') {
289 $title = 'Service Facility';
290 } elseif ($nm101 == 'DQ') {
292 $title = 'Supervising Provider';
293 } elseif ($nm101 == 'DK') {
295 $title = 'Ordering Provider';
296 } elseif ($nm101 == 'DN') {
298 $title = 'Referring Provider';
299 } elseif ($nm101 == 'P3') {
301 $title = 'Primary Care Provider';
302 } elseif ($nm101 == 'PW') {
304 $title = 'Ambulance pickup';
305 } elseif ($nm101 == '45') {
307 $title = 'Ambulance dropoff';
312 $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
;
318 if (strncmp('SE' . $de, $seg, 3) === 0) {
320 $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
;
325 // for all the segments that do not begin loops
326 $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
;
335 * identify loops and format segments for display
336 * of 270/271 (eligibility) type x12 edi files
338 * @param array $segments
339 * @param string $delimiter
342 function edih_271_text($segments, $delimiter, $err_seg = '')
347 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
349 csv_edihist_log('edih_271_text: invalid argument');
350 $str_html .= "Invalid arguments for view of x12 file text<br />";
363 // to highlight identified errors listed in 999/997 ack (for 270)
365 $er = edih_errseg_parse($err_seg);
366 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
367 $erseg = (isset($er['err'])) ?
$er['err'] : array();
375 $er = edih_errseg_parse($err_seg);
376 if (is_array($er) && count($er)) {
382 foreach ($segments as $key => $seg) {
388 if ($erstn && ($erstn == $stn)) {
389 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
393 if (strncmp('ST' . $de, $seg, 3) === 0) {
394 $sar = explode($de, $seg);
398 $sttp = (isset($seg[1])) ?
$seg[1] : '';
399 $stn = (isset($seg[2])) ?
$seg[2] : '';
400 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
405 if (strncmp('BHT' . $de, $seg, 4) === 0) {
407 // 2nd seg in transaction, ST may not be included if segments are transaction slice
412 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
417 if (strncmp('HL' . $de, $seg, 3) === 0) {
418 $sar = explode($de, $seg);
419 if ($sar[3] == '20') { // level code
420 $loopid = '2000A'; // info source (payer)
421 } elseif ($sar[3] == '21') {
422 $loopid = '2000B'; // info receiver (clinic)
423 } elseif ($sar[3] == '22') {
424 $loopid = '2000C'; // subscriber
426 } elseif ($sar[3] == '23') {
427 $loopid = '2000D'; // dependent
431 csv_edihist_log('edih_271_text: HL has no level ' . $seg);
435 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
436 $prevseg = 'HL' . $de;
441 if (strncmp('NM1' . $de, $seg, 4) === 0) {
442 if (strncmp('NM1' . $de, $prevseg, 4) === 0) {
443 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
447 switch ((string)$loopid) {
450 break; // edih_change_loop($lptest, &$lpval)
464 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
465 $prevseg = 'NM1' . $de;
470 if (strncmp('EB' . $de, $seg, 3) === 0 ||
strncmp('EQ' . $de, $seg, 3) === 0) {
471 // EB* segment is in 271 type, EQ* is corresponding segment in 270 type
472 if (strncmp($seg, $prevseg, 3) === 0) {
473 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
474 $prevseg = substr($seg, 0, 3);
478 if ($loopid = '2100C' ||
$loopid = '2115C' ||
$loopid = '2120C') {
480 } elseif ($loopid = '2100D' ||
$loopid = '2115D' ||
$loopid = '2120D') {
484 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
485 $prevseg = substr($seg, 0, 3);
486 $has_eb = (strncmp('EB' . $de, $seg, 3) === 0);
492 if (strncmp('III' . $de, $seg, 4) === 0 && $has_eb) {
493 // the III segment begins a loop in 271 type, but not in 270
494 if ($loopid = '2110C') {
498 if ($loopid = '2100D') {
503 $str_html .= "<tr><td class='btloop'></td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
505 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
509 $prevseg = substr($seg, 0, 4);
514 if (strncmp('LS' . $de, $seg, 3) === 0) {
515 if ($loopid = '2110C' ||
$loopid = '2115C') {
517 } elseif ($loopid = '2110D' ||
$loopid = '2115D') {
521 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
526 if (strncmp('SE' . $de, $seg, 3) === 0) {
527 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
532 // for all the segments that do not begin loops
533 $prevseg = substr($seg, 0, strpos($seg, $de) +
1);
534 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
543 function edih_835_text($segments, $delimiter, $err_seg = '')
547 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
549 csv_edihist_log('edih_835_text: invalid segments');
550 $str_html .= "Invalid arguments for view of x12 file text<br />";
561 foreach ($segments as $key => $seg) {
564 if (strncmp('ST' . $de, $seg, 3) === 0) {
566 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
571 if (strncmp('N1' . $de, $seg, 3) === 0) {
572 $sar = explode($de, $seg);
573 if ($sar[1] == 'PR') {
575 } elseif ($sar[1] == 'PE') {
579 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
580 $prevseg = 'N1' . $de;
585 if (strncmp('LX' . $de, $seg, 3) === 0) {
587 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
588 $prevseg = 'LX' . $de;
593 if (strncmp('TS3' . $de, $seg, 4) === 0) {
594 if ($loopid == '2000') {
595 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
598 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
601 $prevseg = 'TS3' . $de;
606 if (strncmp('CLP' . $de, $seg, 4) === 0) {
608 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
609 $prevseg = 'CLP' . $de;
614 if (strncmp('SVC' . $de, $seg, 4) === 0) {
616 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
617 $prevseg = 'SVC' . $de;
622 if (strncmp('PLB' . $de, $seg, 4) === 0) {
624 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
625 $prevseg = 'PLB' . $de;
630 if (strncmp('SE' . $de, $seg, 3) === 0) {
632 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
633 $prevseg = 'SE' . $de;
637 // for all the segments that do not begin loops
638 $prevseg = substr($seg, 0, 3);
640 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
650 * identify loops and format segments for display
651 * of 277 (claim status) type x12 edi files
653 * @param array $segments
654 * @param string $delimiter
657 function edih_277_text($segments, $delimiter, $stpos = '')
661 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
663 csv_edihist_log('edih_277_text: invlid segments');
664 $str_html .= "Invalid arguments for view of x12 file text<br />";
669 // to highlight identified errors listed in 999/997 ack (for 276)
671 //$er = edih_errseg_parse($err_seg);
672 //$erstn = (isset($er['trace'])) ? substr($er['trace'], -4) : '';
673 //$erseg = (isset($er['err'])) ? $er['err'] : array();
686 foreach ($segments as $idx => $seg) {
689 $key = ($stpos) ?
$idx - $stpos : $idx;
690 // if 276 transactions are parsed, 999 errors may be present
691 //if ($erstn && ($erstn == $stn)) {
692 //$bterr = (in_array($stsegct, $erseg)) ? 'bterr' : 'btseg';
695 if (strncmp('ST' . $de, $seg, 3) === 0) {
698 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
703 if (strncmp('BHT' . $de, $seg, 4) === 0) {
705 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
710 if (strncmp('HL' . $de, $seg, 3) === 0) {
711 $sar = explode($de, $seg);
712 if ($sar[3] == '20') { // level code
713 $loopid = '2000A'; // info source (payer)
714 } elseif ($sar[3] == '21') {
715 $loopid = '2000B'; // info receiver (clinic)
716 } elseif ($sar[3] == '19') {
717 $loopid = '2000C'; // provider
718 } elseif ($sar[3] == '22' ||
$sar[3] == 'PT') {
719 $loopid = '2000D'; // subscriber
720 } elseif ($sar[3] == '23') {
721 $loopid = '2000E'; // dependent
724 csv_edihist_log('edih_277_text: HL has no level ' . $seg);
728 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
729 $prevseg = 'HL' . $de;
735 if (strncmp('NM1' . $de, $seg, 4) === 0) {
736 if (strncmp('NM1' . $de, $prevseg, 4) === 0) {
737 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
741 switch ((string)$loopid) {
744 break; // edih_change_loop($lptest, &$lpval)
759 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
765 if (strncmp('TRN' . $de, $seg, 4) === 0) {
766 if (strncmp('TRN' . $de, $prevseg, 4) === 0) {
767 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
771 switch ((string)$loopid) {
789 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
795 if (strncmp('SVC' . $de, $seg, 4) === 0) {
796 if (strncmp('SVC' . $de, $prevseg, 4) === 0) {
797 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
801 switch ((string)$loopid) {
810 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
816 if (strncmp('SE' . $de, $seg, 3) === 0) {
818 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
823 // for all the segments that do not begin loops
824 $prevseg = substr($seg, 0, 3);
826 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
834 * identify loops and format segments for display
835 * of 278 (authorization) type x12 edi files
837 * @param array $segments
838 * @param string $delimiter
841 function edih_278_text($segments, $delimiter, $err_seg = '')
846 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
848 csv_edihist_log('edih_278_text(): invalid argument');
849 $str_html .= "Invalid arguments for view of x12 file text<br />";
863 // to highlight identified errors listed in 999/997 ack
865 $er = edih_errseg_parse($err_seg);
866 $erstn = (isset($er['trace'])) ?
substr($er['trace'], -4) : '';
867 $erseg = (isset($er['err'])) ?
$er['err'] : array();
874 foreach ($segments as $key => $seg) {
880 if ($erstn && ($erstn == $stn)) {
881 $bterr = (in_array($stsegct, $erseg)) ?
'bterr' : 'btseg';
885 if (strncmp('ST' . $de, $seg, 3) === 0) {
889 $stn = explode($de, $seg)[2];
890 $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
;
895 if (strncmp('BHT' . $de, $seg, 4) === 0) {
897 // 2nd seg in transaction, ST may not be included if segments are transaction slice
902 $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
;
907 if (strncmp($seg, 'HL' . $de, 3) === 0) {
908 $sar = explode($de, $seg);
910 $hlpc = $sar[2]; // parent code
912 $hlcc = (isset($sar[4])) ?
$sar[4] : ''; // child code
913 if ($sar[3] == '20') { // level code
914 $loopid = '2000A'; // info source (payer)
915 $title = 'Info Source';
916 } elseif ($sar[3] == '21') {
917 $loopid = '2000B'; // info receiver (clinic)
918 $title = 'Info Receiver';
919 } elseif ($sar[3] == '22') {
920 $loopid = '2000C'; // subscriber
921 $title = 'Subscriber';
922 } elseif ($sar[3] == '23') {
923 $loopid = '2000D'; // dependent
924 $title = 'Dependent';
925 } elseif ($sar[3] == 'EV') {
926 $loopid = '2000E'; // patient event
927 $title = 'Patient Event';
928 } elseif ($sar[3] == 'SS') {
929 $loopid = '2000F'; // service
933 csv_edihist_log('edih_278_text: HL has no level ' . $seg);
937 $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
;
943 if (strncmp($seg, 'NM1' . $de, 4) === 0) {
944 $sar = explode($de, $seg);
946 if ($loopid == '2000A') {
947 $loopid == '2010A'; // Source
948 } elseif ($loopid == '2000B') {
949 $loopid = '2010B'; // Receiver
950 } elseif ($loopid == '2000C') {
951 $loopid = '2010C'; // Subscriber
952 } elseif ($loopid == '2000D') {
953 $loopid = '2010D'; // Dependent
954 } elseif ($loopid == '2000E' ||
strpos($loopid, '010E')) { // Patient Event
955 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010EA' : $loopid;
956 $loopid = (strpos('|45|FS|ND|PW|R3', $nm101) ) ?
'2010EB' : $loopid;
957 $loopid = ($nm101 == 'L5') ?
'2010EC' : $loopid;
958 } elseif ($loopid == '2000F' ||
strpos($loopid, '010F')) { // Service
959 $loopid = (strpos('|71|72|73|77|AAJ|DD|DK|DN|FA|G3|P3|QB|QV|SJ', $nm101) ) ?
'2010FA' : $loopid;
963 $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
;
969 if (strncmp('SE' . $de, $seg, 3) === 0) {
970 $str_html .= "<tr><td class='btloop'>Trailer</td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
976 // for all the segments that do not begin loops
977 $prevseg = substr($seg, 0, strpos($seg, $de));
978 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='" . attr($bterr) . "'>" . text($seg) . "</td></tr>" . PHP_EOL
;
987 * identify loops and format segments for display
988 * of 997/999 (acknowledgement) type x12 edi files
990 * @param array $segments
991 * @param string $delimiter
994 function edih_997_text($segments, $delimiter)
998 if (!is_array($segments) ||
!count($segments) ||
strlen($delimiter) != 1) {
1000 csv_edihist_log('edih_997_text(): invalid segments');
1008 //echo 'edih_997_text() foreach segment count: '.count($segments).PHP_EOL;
1010 foreach ($segments as $key => $seg) {
1012 //echo var_dump($seg).PHP_EOL;
1014 if (strncmp('TA1' . $de, $seg, 4) === 0) {
1015 $sar = explode($de, $seg);
1017 $loopid = 'ACK'; // not official
1018 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1020 $loopid = ''; // reset loop for subsequent segments
1025 if (strncmp('ST' . $de, $seg, 3) === 0) {
1027 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1032 if (strncmp('AK1' . $de, $seg, 4) === 0) {
1033 $sar = explode($de, $seg);
1034 $rsptp = csv_file_type($sar[1]);
1035 if ($rspicn && $rsptp) {
1036 $rspfile = csv_file_by_controlnum($rsptp, $rspicn);
1039 $title = ($rspfile) ?
'response to ' . $rspfile : '';
1041 $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
;
1047 if (strncmp('AK2' . $de, $seg, 4) === 0) {
1049 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1054 if (strncmp('IK3' . $de, $seg, 4) === 0 ||
strncmp('AK3' . $de, $seg, 4) === 0) {
1056 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1061 if (strncmp('IK4' . $de, $seg, 4) === 0 ||
strncmp('AK4' . $de, $seg, 4) === 0) {
1063 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1068 if (strncmp('SE' . $de, $seg, 3) === 0) {
1069 $loopid = 'Trailer';
1070 $str_html .= "<tr><td class='btloop'>" . text($loopid) . "</td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1074 // for all the segments that do not begin loops ;
1075 $str_html .= "<tr><td class='btloop'> -- </td><td class='btnum'>" . text($key) . "</td><td class='btseg'>" . text($seg) . "</td></tr>" . PHP_EOL
;
1083 * Display of x12 edi transaction listing or all segments in the files.
1084 * When using $err_info, you must use the filepath of the submitted file
1086 * @param string $filepath path to desired file
1087 * @param string $filetype used when filepath is just filename
1088 * @param string optional $claimid CLM01, or BHT03 to identify a transaction or a trace value
1089 * @param bool false: $claimid is pt transaction, true: $claimid is trace from 835 or 999
1090 * @param string optional $err_info the prepared error info from a 997/999 response
1091 * @return string html for display of file segments
1093 function edih_display_text($filepath, $filetype = '', $claimid = '', $trace = false, $err_info = '')
1100 $ft = ($filetype) ?
$filetype : '';
1101 $errs = ( strlen($err_info) ) ?
$err_info : '';
1105 $x12obj = csv_check_x12_obj($filepath, $ft);
1107 if ($x12obj && 'edih_x12_file' == get_class($x12obj)) {
1108 $ftype = $x12obj->edih_type();
1109 $ft = csv_file_type($ftype);
1110 $delims = $x12obj->edih_delimiters();
1112 $fn = $x12obj->edih_filename();
1113 $segs_ar = $x12obj->edih_segments();
1114 $env_ar = $x12obj->edih_envelopes();
1115 if (!$de ||
strlen($de) != 1) {
1118 $str_html = 'edih_display_text(): error in delimiters<br />' . PHP_EOL
;
1119 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1123 if (!is_array($segs_ar) ||
!count($segs_ar)) {
1125 $str_html = "<p>unknown error retrieving segments for " . text($fn) . "</p>" . PHP_EOL
;
1126 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1131 csv_edihist_log('edih_transaction_text(): invalid path ' . $filepath);
1132 $str_html = 'edih_display_text(): error accessing file<br />' . PHP_EOL
;
1138 // claimid can be for transaction, payment, or error response
1139 if ($trace && array_key_exists($claimid, $env_ar['ISA'])) {
1140 $arg_ar = array('ISA13' => $claimid, 'keys' => true);
1141 $segments = $x12obj->edih_x12_slice($arg_ar);
1143 // claimid alone can be clm01 or bht03, if trace=true, expect trn02 for claimid
1144 foreach ($env_ar['ST'] as $st) {
1145 if ($trace && $claimid == $st['trace']) {
1146 $arg_ar = array('ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'trace' => $claimid, 'keys' => true);
1147 $segments = $x12obj->edih_x12_slice($arg_ar);
1149 } elseif (in_array($claimid, $st['acct'])) {
1151 $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
1152 $segments = $x12obj->edih_x12_slice($arg_ar);
1154 // request for individual transaction segments
1155 $segments = $x12obj->edih_x12_transaction($claimid);
1159 } elseif (in_array($claimid, $st['bht03'])) {
1160 // also possible that bht03 number is given for claimid
1161 // this will likely be a 27x
1163 $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
1164 $segments = $x12obj->edih_x12_slice($arg_ar);
1166 $segments = $x12obj->edih_x12_transaction($claimid);
1175 $segments = $segs_ar;
1179 // now check if we have segments
1180 if (empty($segments) ||
!count($segments)) {
1182 $str_html = "<p>error: transaction " . text($claimid) . " not found in " . text($fn) . "</p>" . PHP_EOL
;
1183 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1187 $str_html = "<p>unknown error retrieving segments for " . text($fn) . "</p>" . PHP_EOL
;
1188 $str_html .= $x12obj->edih_message() . PHP_EOL
;
1193 // if the segments are from a slice or transaction
1194 // a multidimensional array segs[i][j] must be flattened
1195 $ar_sngl = csv_singlerecord_test($segments);
1196 // false when segments are a transaction or trace only
1199 // append segments to single array
1200 // keys should not duplicate since all segments
1201 // are from the same x12 file
1203 for ($i = 0; $i < count($segments); $i++
) {
1204 $trnsegs = array_merge($trnsegs, $segments[$i]);
1207 $segments = $trnsegs;
1213 $tbl_id = ($claimid) ?
$claimid : $fn;
1215 //'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
1216 switch ((string)$ftype) {
1218 $capstr = "Claim "; //$cls = "txt837";
1219 $trn_html = edih_837_text($segments, $de, $errs);
1222 $capstr = "Payment "; //$cls = "txt835";
1223 $trn_html = edih_835_text($segments, $de);
1226 $capstr = "Status Query "; //$cls = "txt276";
1227 $trn_html = edih_277_text($segments, $de, $errs);
1230 $capstr = "Claim Status "; //$cls = "txt277";
1231 $trn_html = edih_277_text($segments, $de, $stsegkey);
1234 $capstr = "Eligibility Query "; //$cls = "txt270";
1235 $trn_html = edih_271_text($segments, $de, $errs);
1238 $capstr = "Eligibility Report "; //$cls = "txt271";
1239 $trn_html = edih_271_text($segments, $de);
1242 $capstr = "Authorization "; //$cls = "txt278";
1243 $trn_html = edih_278_text($segments, $de, $errs);
1246 $capstr = "Batch Acknowledgment "; //$cls = "txt997";
1247 $trn_html = edih_997_text($segments, $de);
1250 $capstr = "x12 $ftype "; //$cls = "txt_x12";
1251 $trn_html = edih_segments_text($segments, $de);
1256 $capstr .= ($claimid) ?
" ID: " . text($claimid) : "";
1258 $str_html .= "<table id=" . attr($tbl_id) . " cols=3 class='segtxt'><caption>" . text($capstr) . "</caption>" . PHP_EOL
;
1259 $str_html .= "<thead>" . PHP_EOL
;
1260 $str_html .= "<tr><th class='btloop'>Loop</th><th class='btloop'>Num</th>";
1261 $str_html .= "<th class='segtxt'>Segment (<em>File:</em> " . text($fn) . ")</th></tr>" . PHP_EOL
;
1262 $str_html .= "</thead>" . PHP_EOL
. "<tbody>" . PHP_EOL
;
1264 $str_html .= $trn_html;
1266 $str_html .= "</tbody></table>" . PHP_EOL
;