New Http Rest Client (#2023)
[openemr.git] / library / edihistory / edih_x12file_class.php
blob8e688916de3441a4d9827c60ad2b451386926eb1
1 <?php
2 /*
3 * edih_x12file_class.php
5 * Copyright 2014 Kevin McCormick Longview, Texas
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 3 or later. You should have
16 * received a copy of the GNU General Public License along with this program;
17 * if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * <http://opensource.org/licenses/gpl-license.php>
22 * @link: http://www.open-emr.org
23 * @author: Kevin McCormick
24 * @package: OpenEMR
25 * @subpackage: ediHistory
28 /* ********* project notes =================
29 * determine GET and POST array elements
30 * process new files -- type and csv data values
31 * display tables -- links with GET and POST
32 * find files -- find transactions
33 * format display
35 * ==========================================
38 /*********** php code here ****************************************************************/
40 /**
41 * Class to read EDI X12 files in healthcare setting
43 * It is assumed that EDI X12 files will have mime-type text/plain; charset=us-ascii
45 * initialize with file path or as empty object, e.g.
46 * $x12_file = new edih_x12_file(filepath); segment array and envelope array, no file text
47 * $x12_file = new edih_x12_file(filepath, false); no segment or envelope array, no file text
48 * $x12_file = new edih_x12_file(filepath, false, true); no segment or envelope array, yes file text
49 * or
50 * $x12_file = new edih_x12_file(); empty object, ' _x12_ ' methods available if file text supplied as method argument
52 * The properties filename, type, version, valid, isx12, hasGS, hasST, and delimiters should be available
53 * if the valid filepath is provided when creating the object.
55 * @param string $filepath default = ''
56 * @param bool $mk_segs default = true
57 * @param bool $text default = false
58 * @return bool|string true for empty object "ovgis" for validated x12
60 class edih_x12_file
62 // properties
63 private $filepath = '';
64 private $filename = '';
65 private $type = '';
66 private $version = '';
67 private $text = '';
68 private $length = 0;
69 private $valid = false;
70 private $isx12 = false;
71 private $hasGS = false;
72 private $hasST = false;
73 private $message = array();
74 private $delimiters = array();
75 private $segments = array();
76 private $envelopes = array();
78 private $constructing = false;
80 private $gstype_ar = array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HN'=>'277',
81 'HI'=>'278', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
83 function __construct($file_path = '', $mk_segs = true, $text = false)
86 if ($file_path === '') {
87 return true;
91 if (is_file($file_path) && is_readable($file_path)) {
92 $this->filepath = trim($file_path);
93 $this->filename = basename($this->filepath);
94 $f_text = file_get_contents($this->filepath);
96 $testval = ($f_text) ? $this->edih_x12_scan($f_text) : '';
97 $this->valid = ( strpos($testval, 'v') ) ? true : false;
98 $this->isx12 = ( strpos($testval, 'i') ) ? true : false;
99 $this->hasGS = ( strpos($testval, 'g') ) ? true : false;
100 $this->hasST = ( strpos($testval, 's') ) ? true : false;
102 if ($this->valid) {
103 $this->constructing = true;
104 $this->text = ($text) ? $f_text : '';
105 $this->length = ($f_text) ? strlen($f_text) : 0;
106 if ($this->isx12) {
107 $this->delimiters = $this->edih_x12_delimiters(substr($f_text, 0, 126));
108 $this->version = substr($f_text, 84, 5);
109 if ($mk_segs) {
110 $this->segments = $this->edih_x12_segments($f_text);
111 if (is_array($this->segments) && count($this->segments)) {
112 $this->envelopes = $this->edih_x12_envelopes();
113 $this->type = $this->edih_x12_type();
114 } else {
115 $this->message[] = 'edih_x12_file: error in creating segment array ' . text($this->filename) . PHP_EOL;
117 } else {
118 // read file contents to try and determine x12 type
119 $this->type = $this->edih_x12_type($f_text);
123 } else {
124 // invalid file path
125 $this->message[] = 'edih_x12_file: invalid file path ' . text($file_path);
128 $this->constructing = false;
129 return $this->valid;
133 * function to support empty object and '_x12_' functions called with supplied file text
135 * @param string $file_text
136 * @param bool return x12 type
137 * @param bool return delimiters
138 * @param bool return segments
139 * @return array array['filetext'] and maybe ['type'] ['$delimiters'] ['segments']
141 private function edih_file_text($file_text, $type = false, $delimiters = false, $segments = false)
144 $ret_ar = array();
145 if (!$file_text || is_string($file_text) == false) {
146 $this->message[] = 'edih_file_text(): invalid argument';
147 return $ret_ar;
150 // do verifications
151 $v = $this->edih_x12_scan($file_text);
152 if (!strpos($v, 's')) {
153 $this->message[] = 'edih_file_text(): failed scan of file text (' . text($v) . ')';
154 return $ret_ar;
158 $this->constructing = true;
160 if ($type) {
161 $ret_ar['type'] = $this->edih_x12_type($file_text);
164 if ($delimiters) {
165 $ret_ar['delimiters'] = $this->edih_x12_delimiters(substr($file_text, 0, 126));
168 if ($segments) {
169 $ret_ar['segments'] = $this->edih_x12_segments($file_text);
173 $this->constructing = false;
175 return $ret_ar;
179 * functions to return properties
181 public function classname()
183 return get_class($this);
185 public function edih_filepath()
187 return $this->filepath;
189 public function edih_filename()
191 return $this->filename;
193 public function edih_type()
195 return $this->type;
197 public function edih_version()
199 return $this->version;
201 public function edih_text()
203 return $this->text;
205 public function edih_length()
207 return $this->length;
209 public function edih_valid()
211 return $this->valid;
213 public function edih_isx12()
215 return $this->isx12;
217 public function edih_hasGS()
219 return $this->hasGS;
221 public function edih_hasST()
223 return $this->hasST;
225 public function edih_delimiters()
227 return $this->delimiters;
229 public function edih_segments()
231 return $this->segments;
233 public function edih_envelopes()
235 return $this->envelopes;
239 * message statements regarding object or from functions
240 * formatted as html
242 * @return string
244 public function edih_message()
246 $str_html = '<p>' . PHP_EOL;
247 if (count($this->message)) {
248 foreach ($this->message as $msg) {
249 $str_html .= $msg . '<br />' . PHP_EOL;
252 $str_html .= PHP_EOL . '</p>' . PHP_EOL;
253 } else {
254 $str_html = '';
257 return $str_html;
262 * Numeric type of x12 HC file associated with GS01 code
264 * @param string $gs01
265 * @return string|bool
267 public function edih_gs_type($gs01)
269 $tpky = strtoupper($gs01);
270 return ( isset($this->gstype_ar[$tpky]) ) ? $this->gstype_ar[$tpky] : false;
274 * Use PHP FileInfo to check mime type and then scan for unwanted characters
275 * check for Non-basic ASCII character and <%, <asp:, <?, ${, #!, <scr (any other evil script indicators?)
276 * basically allows A-Z a-z 0-9 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ and newline carriage_return
277 * This function accepts the following mime-type: text/plain; charset=us-ascii
279 * The return string can be 'ovigs' ov - valid, igs - ISA GS ST
281 * @param string $filetext the file contents
282 * @return string zero length on failure
284 public function edih_x12_scan($filetext)
286 $hasval = '';
287 $ftxt = ( $filetext && is_string($filetext) ) ? trim($filetext) : $filetext;
288 // possibly $ftxt = trim($filetext, "\x00..\x1F") to remove ASCII control characters
289 // remove newlines
290 if (strpos($ftxt, PHP_EOL)) {
291 $ftxt = str_replace(PHP_EOL, '', $ftxt);
294 $flen = ( $ftxt && is_string($ftxt) ) ? strlen($ftxt) : 0;
295 if (!$flen) {
296 $this->message[] = 'edih_x12_scan: zero length or invalid file text';
297 return $hasval;
300 $de = '';
301 $dt = '';
302 // use finfo php class
303 if (class_exists('finfo')) {
304 $finfo = new finfo(FILEINFO_MIME);
305 $mimeinfo = $finfo->buffer($ftxt);
306 if (strncmp($mimeinfo, 'text/plain; charset=us-ascii', 28) !== 0) {
307 $this->message[] = 'edih_x12_scan: ' . text($this->filename) . ' : invalid mime info: <br />' . text($mimeinfo);
309 return $hasval;
314 if (preg_match('/[^\x20-\x7E\x0A\x0D]|(<\?)|(<%)|(<asp)|(#!)|(\$\{)|(<scr)|(script:)/is', $ftxt, $matches, PREG_OFFSET_CAPTURE)) {
316 $this->message[] = 'edih_x12_scan: suspect characters in file ' . text($this->filename) . '<br />' .
317 ' character: ' . text($matches[0][0]) . ' position: ' . text($matches[0][1]);
319 return $hasval;
322 $hasval = 'ov'; // valid
323 // check for required segments ISA GS ST; assume segment terminator is last character
324 if (substr($ftxt, 0, 3) === 'ISA') {
325 $hasval = 'ovi';
326 $de = substr($ftxt, 3, 1);
327 $dt = substr($ftxt, -1);
328 if (strpos($ftxt, $dt.'GS'.$de, 0)) {
329 $hasval = 'ovig';
332 if (strpos($ftxt, $dt.'ST'.$de, 0)) {
333 $hasval = 'ovigs';
337 return $hasval;
341 * read the GS segments in file contents to determine x12 type, or, if the
342 * object was created with a file path and envelopes, from the GS envelope array
344 * @param string $file_text optional contents of an x12 file
345 * @return string the x12 type, e.g. 837, 835, 277, 999, etc.
347 public function edih_x12_type($file_text = '')
349 $tpstr = '';
350 $tp_tmp = array();
351 $f_text = '';
352 $delims = array();
353 $delimarg = '';
354 $dt = ( isset($this->delimiters['t']) ) ? $this->delimiters['t'] : '';
355 $de = ( isset($this->delimiters['e']) ) ? $this->delimiters['e'] : '';
357 if ($file_text) {
358 // For when '_x12_' function is called with file contents as argument
359 if (!$this->constructing) {
360 $vars = $this->edih_file_text($file_text, false, true, false);
361 $f_text = $file_text;
362 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
363 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : '';
364 } elseif ($this->text) {
365 // called in initial construction, delimiters already created if x12 file
366 $f_text =& $this->text;
367 if (!$dt) {
368 $this->message[] = 'edih_x12_type: not x12 file';
369 return $tpstr;
371 } else {
372 // called after file scan, but no segment array exists
373 $f_text =& $file_text;
374 if (!$dt) {
375 $delims = $this->edih_x12_delimiters(substr($f_text, 0, 126));
376 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
377 $de = ( isset($delims['e']) ) ? $delims['e'] : '';
381 if (!$f_text) {
382 $this->message[] = 'edih_x12_type: failed scan of file content';
383 return $tpstr;
385 } elseif (isset($this->envelopes['GS'])) {
386 // No argument, so if envelopes exist, take values from there
387 foreach ($this->envelopes['GS'] as $gs) {
388 $tp_tmp[] = $gs['type'];
390 } elseif (count($this->segments)) {
391 // No argument and no envelopes, so scan segments
392 if (!$de) {
393 $de = substr(reset($this->segments), 3, 1);
396 foreach ($this->segments as $seg) {
397 if (strncmp($seg, 'GS'.$de, 3) == 0) {
398 $gs_ar = explode($de, $seg);
399 if (array_key_exists($gs_ar[1], $this->gstype_ar)) {
400 //$tp_tmp[] = $this->gstype_ar[$gs_ar[1]];
401 $tp_tmp[] = $gs_ar[1];
402 } else {
403 $tp_tmp[] = $gs_ar[1];
404 $this->message[] = 'edih_x12_type: unknown x12 type ' . text($gs_ar[1]);
408 } else {
409 $this->message[] = 'edih_x12_type: no content to determine x12 type';
410 return $tpstr;
413 // $f_text has content only if file contents supplied or in text property
414 if ($f_text) {
415 // use regular expression instead of strpos($f_text, $dt.'GS'.$de)
416 $pcrepattern = '/GS\\'.$de.'(?:HB|HS|HR|HI|HN|HP|FA|HC)\\'.$de.'/';
417 $pr = preg_match_all($pcrepattern, $f_text, $matches, PREG_OFFSET_CAPTURE);
419 if ($pr && count($matches)) {
420 foreach ($matches as $m) {
421 //$gspos1 = $m[0][1];
422 $gs_ar1 = explode($de, $m[0][0]);
423 if (array_key_exists($gs_ar1[1], $this->gstype_ar)) {
424 //$tp_tmp[] = $this->gstype_ar[$gs_ar1[1]];
425 $tp_tmp[] = $gs_ar1[1];
426 } else {
427 $tp_tmp[] = $gs_ar1[1];
428 $this->message[] = 'edih_x12_type: unknown x12 type ' . text($gs_ar1[1]);
431 } else {
432 $this->message[] = 'edih_x12_type: did not find GS segment ';
435 /* **** this replaced by preg_match_all() above ******
437 // scan GS segments
438 $gs_str = $dt.'GS'.$de;
439 $gs_pos = 1;
440 $gse_pos = 2;
441 while ($gs_pos) {
442 $gs_pos = strpos($f_text, $gs_str, $gs_pos);
443 if ($gs_pos) {
444 $gsterm = strpos($f_text, $dt, $gs_pos+1);
445 $gsseg = trim(substr($f_text, $gs_pos+1, $gsterm-$gs_pos-1));
446 //$gs_ar = explode($de, substr($f_text, $gs_pos+1, $gsterm-$gs_pos-1) );
447 $this->message[] = 'edih_x12_type: '.$gsseg.PHP_EOL;
448 $gs_ar = explode($de, $gsseg);
449 if ( array_key_exists($gs_ar[1], $this->gstype_ar) ) {
450 $tp_tmp[] = $this->gstype_ar[$gs_ar[1]];
451 } else {
452 $tp_tmp[] = $gs_ar[1];
453 $this->message[] = 'edih_x12_type: unknown x12 type '.$gs_ar[1];
455 $gs_pos = $gsterm + 1;
458 ******************* */
461 // x12 type information collected
462 if (count($tp_tmp)) {
463 $tp3 = array_values(array_unique($tp_tmp));
464 // mixed should not happen -- concatenated ISA envelopes of different types?
465 $tpstr = ( count($tp3) > 1 ) ? 'mixed|' . implode("|", $tp3) : $tp3[0];
466 //$this->message[] = 'edih_x12_type: ' . $tpstr;
467 } else {
468 $this->message[] = 'edih_x12_type: error in identifying type ';
469 return false;
472 return $tpstr;
477 * Extract x12 delimiters from the ISA segment
479 * There are obviously easier/faster ways of doing this, but we go character by character.
480 * The value returned is empty on error, otherwise:
481 * <pre>
482 * array('t'=>segment terminator, 'e'=>element delimiter,
483 * 's'=>sub-element delimiter, 'r'=>repetition delimiter)
484 * </pre>
486 * @param string $isa_str110 first n>=106 characters of x12 file
487 * @return array array or empty on error
489 public function edih_x12_delimiters($isa_str110 = '')
492 $delim_ar = array();
493 if (!$isa_str110 && $this->text) {
494 $isa_str = substr($this->text, 0, 106);
495 } else {
496 $isa_str = trim($isa_str110);
499 $isalen = strlen($isa_str);
500 if ($isalen >= 106) {
501 if (substr($isa_str, 0, 3) != 'ISA') {
502 // not the starting characters
503 $this->message[] = 'edih_x12_delimiters: text does not begin with ISA';
504 return $delim_ar;
507 /* Extract delimiters using the prescribed positions.
508 * -- problem is possibly mangled files
509 * $t_ar['e'] = substr($isa_str, 3, 1);
510 * $t_ar['r'] = substr($isa_str, 82, 1);
511 * $t_ar['s'] = substr($isa_str, 104, 1);
512 * $t_ar['t'] = substr($isa_str, 105, 1);
514 } else {
515 $this->message[] = 'edih_x12_delimiters: ISA string too short'.PHP_EOL;
516 return $delim_ar;
519 $s = '';
520 $delim_ct = 0;
521 $de = substr($isa_str, 3, 1); // ISA*
522 for ($i = 0; $i < $isalen; $i++) {
523 if ($isa_str[$i] == $de) {
524 // element count incremented at end of loop
525 // repetition separator in version 5010
526 if ($delim_ct == 11) {
527 $dr = substr($s, 1, 1);
530 if ($delim_ct == 12) {
531 if (strpos($s, '501') === false) {
532 $dr = '';
537 if ($delim_ct == 15) {
538 $ds = substr($isa_str, $i+1, 1);
539 $dt = substr($isa_str, $i+2, 1);
542 if ($delim_ct == 16) {
543 break;
546 $s = $isa_str[$i]; // $elem_delim;
547 $delim_ct++;
548 } else {
549 $s .= $isa_str[$i];
553 // there are 16 elements in ISA segment
554 if ($delim_ct < 16) {
555 // too few elements -- probably did not get delimiters
556 $this->message[] = "edih_x12_delimiters: too few elements in ISA string";
557 return $delim_ar;
561 $delim_ar = array('t'=>$dt, 'e'=>$de, 's'=>$ds, 'r'=>$dr);
563 return $delim_ar;
567 * Create a multidimensional array of edi envelope info from object segments.
568 * Useful for slicing and dicing. The ['ST'][$stky]['trace'] value is used only for 835
569 * or 999 type files and the ['ST'][$stky]['acct'][i] array will have multiple values
570 * likely only for 835, 271, and 277 types, because response from a payer will have
571 * multiple transactions in the ST-SE envelope while OpenEMR probably will place each
572 * transaction in its own ST-SE envelope for 270 and 837 types.
574 * The ['start'] and ['count'] values are for use in php function array_slice()
575 * The numeric keys of the segments array begin at 1 and the ['start'] value is one less
576 * than the actual key because array_slice() offset is zero-based.
578 * <pre>
579 * ['ISA'][$icn]=>['start']['count']['sender']['receiver']['icn']['gscount']['date']
580 * ['GS'][$gs_ct]=>['start']['count']['gsn']['icn']['sender']['date']['stcount']['type']
581 * ['ST'][$stky]=>['start']['count']['stn']['gsn']['icn']['type']['trace']['acct']
582 * ['ST'][$stky]['acct'][i]=>CLM01
583 * ['ST'][$stky]['bht03'][i]=>BHT03
584 * </pre>
586 * @return array array as shown above or empty on error
588 public function edih_x12_envelopes($file_text = '')
590 // produce an array of envelopes and positions
591 $env_ar = array();
592 $de = '';
593 if ($file_text) {
594 // presume need for file scan and delimiters
595 $vars = $this->edih_file_text($file_text, false, true, true);
596 $segment_ar = (isset($vars['segments']) ) ? $vars['segments'] : array();
597 $de = (isset($vars['delimiters']) ) ? $vars['delimiters']['e'] : '';
598 //$segment_ar = $this->edih_x12_segments($file_text);
599 if (empty($segment_ar) || !$de) {
600 $this->message[] = 'edih_x12_envelopes: invalid file text';
601 return $env_ar;
603 } elseif (count($this->segments)) {
604 $segment_ar = $this->segments;
605 if (isset($this->delimiters['e'])) {
606 $de = $this->delimiters['e'];
607 } else {
608 $de = (substr(reset($segment_ar), 0, 3) == 'ISA') ? substr(reset($segment_ar), 3, 1) : '';
610 } else {
611 $this->message[] = 'edih_x12_envelopes: no text or segments';
612 return $env_ar;
615 if (!$de) {
616 $this->message[] = 'edih_x12_envelopes: invalid delimiters';
617 return $env_ar;
621 // get the segment array bounds
622 $seg_first = (reset($segment_ar) !== false) ? key($segment_ar) : '1';
623 $seg_last = (end($segment_ar) !== false) ? key($segment_ar) : count($segment_ar) + $seg_first;
624 if (reset($segment_ar) === false) {
625 $this->message[] = 'edi_x12_envelopes: reset() error in segment array';
626 return $env_ar;
627 } else {
628 $seg_ct = $seg_last + 1;
631 // variables
632 $seg_txt = '';
633 $sn = '';
634 $st_type = '';
635 $st_ct = 0;
636 $isa_ct = 0;
637 $iea_ct = 0;
638 $gs_st_ct = 0;
639 $trnset_seg_ct = 0;
640 $st_segs_ct = 0;
641 $isa_segs_ct = 0;
642 $chk_trn = false;
643 $trncd = '2';
644 //$id278 = false;
645 $ta1_icn = '';
646 $seg_ar = array();
647 // the segment IDs we look for
648 $chk_segs = array('ISA', 'GS'.$de, 'TA1', 'ST'.$de, 'BHT', 'HL'.$de, 'TRN', 'CLP', 'CLM', 'SE'.$de, 'GE'.$de, 'IEA');
650 for ($i=$seg_first; $i<$seg_ct; $i++) {
651 // counters
652 $isa_segs_ct++;
653 $st_segs_ct++;
655 $seg_text = $segment_ar[$i];
656 $sn = substr($seg_text, 0, 4);
657 // skip over segments that are not envelope boundaries or identifiers
658 if (!in_array(substr($sn, 0, 3), $chk_segs)) {
659 continue;
662 // create the structure array
663 if (strncmp($sn, 'ISA'.$de, 4) == 0) {
664 $seg_ar = explode($de, $seg_text);
665 $icn = trim($seg_ar[13]);
667 $env_ar['ISA'][$icn]['start'] = strval($i-1);
668 $env_ar['ISA'][$icn]['sender'] = trim($seg_ar[6]);
669 $env_ar['ISA'][$icn]['receiver'] = trim($seg_ar[8]);
670 $env_ar['ISA'][$icn]['icn'] = $icn;
671 $env_ar['ISA'][$icn]['date'] = trim($seg_ar[9]); // YYMMDD
672 $env_ar['ISA'][$icn]['version'] = trim($seg_ar[12]);
674 $isa_segs_ct = 1;
675 $isa_ct++;
676 continue;
680 if (strncmp($sn, 'GS'.$de, 3) == 0) {
681 $seg_ar = explode($de, $seg_text);
682 $gs_start = strval($i-1);
683 $gsn = $seg_ar[6];
684 // GS06 could be used to id 997/999 response, if truly unique
685 // cannot index on $gsn due to concatenated ISA envelopes and non-unique
686 $gs_ct = isset($env_ar['GS']) ? count($env_ar['GS']) : 0;
688 $env_ar['GS'][$gs_ct]['start'] = $gs_start;
689 $env_ar['GS'][$gs_ct]['gsn'] = $gsn;
690 $env_ar['GS'][$gs_ct]['icn'] = $icn;
691 $env_ar['GS'][$gs_ct]['sender'] = trim($seg_ar[2]);
692 $env_ar['GS'][$gs_ct]['date'] = trim($seg_ar[4]);
693 $env_ar['GS'][$gs_ct]['srcid'] = '';
694 // to verify type of edi transaction
695 if (array_key_exists($seg_ar[1], $this->gstype_ar)) {
696 $gs_fid = $this->gstype_ar[$seg_ar[1]];
697 $env_ar['GS'][$gs_ct]['type'] = $seg_ar[1];
698 } else {
699 $gs_fid = 'NA';
700 $env_ar['GS'][$gs_ct]['type'] = 'NA';
701 $this->message[] = 'edih_x12_envelopes: Unknown GS type ' . text($seg_ar[1]);
704 continue;
707 // expect 999 TA1 before ST
708 if (strncmp($sn, 'TA1'.$de, 4) == 0) {
709 $seg_ar = explode($de, $seg_text);
710 if (isset($seg_ar[1]) && $seg_ar[1]) {
711 $ta1_icn = $seg_ar[1];
712 } else {
713 $this->message[] = 'edih_x12_envelopes: Error in TA1 segment response ICN';
716 //TA1*ISA13ICN*ISA09DATE*ISA10TIME*ACKCode*NoteCode~
717 continue;
721 if (strncmp($sn, 'ST'.$de, 3) == 0) {
722 $seg_ar = explode($de, $seg_text);
723 $stn = $seg_ar[2];
724 $st_type = $seg_ar[1];
725 $st_start = strval($i);
726 $st_segs_ct = 1;
727 $st_ct = isset($env_ar['ST']) ? count($env_ar['ST']) : 0;
729 $env_ar['ST'][$st_ct]['start'] = strval($i-1);
730 $env_ar['ST'][$st_ct]['count'] = '';
731 $env_ar['ST'][$st_ct]['stn'] = $seg_ar[2];
732 $env_ar['ST'][$st_ct]['gsn'] = $gsn;
733 $env_ar['ST'][$st_ct]['icn'] = $icn;
734 $env_ar['ST'][$st_ct]['type'] = $seg_ar[1];
735 $env_ar['ST'][$st_ct]['trace'] = '0';
736 $env_ar['ST'][$st_ct]['acct'] = array();
737 $env_ar['ST'][$st_ct]['bht03'] = array();
738 // GS file id FA can be 999 or 997
739 if ($gs_fid != $st_type && strpos($st_type, '99') === false) {
740 $this->message[] = "edih_x12_envelopes: ISA " . text($icn) . ", GS " . text($gsn . " " . $gs_fid) . " ST " . text($stn . " " . $st_type) . " type mismatch".PHP_EOL;
744 continue;
748 if (strpos('|270|271|276|277|278', $st_type)) {
750 if (strncmp($sn, 'BHT'.$de, 4) == 0) {
751 $seg_ar = explode($de, $seg_text);
752 if (isset($seg_ar[2])) {
753 $trncd = ($seg_ar[2] == '13') ? '1' : '2';
754 // 13 = request, otherwise assume response
755 } else {
756 $this->message[] = 'edih_x12_envelopes: missing BHT02 type element';
759 if (isset($seg_ar[3]) && $seg_ar[3]) {
760 $env_ar['ST'][$st_ct]['bht03'][] = $seg_ar[3];
761 } else {
762 $this->message[] = 'edih_x12_envelopes: missing BHT03 identifier';
766 if (strncmp($sn, 'HL'.$de, 3) == 0) {
767 $seg_ar = explode($de, $seg_text);
768 if (isset($seg_ar[3]) && $seg_ar[3]) {
769 $chk_trn = ( strpos('|22|23|PT', $seg_ar[3]) ) ? true : false;
770 } else {
771 $this->message[] = 'edih_x12_envelopes: missing HL03 level element';
774 continue;
777 if ($chk_trn && strncmp($sn, 'TRN'.$de, 4) == 0) {
778 $seg_ar = explode($de, $seg_text);
779 if (isset($seg_ar[1]) && $seg_ar[1] == $trncd) {
780 $env_ar['ST'][$st_ct]['acct'][] = (isset($seg_ar[2])) ? $seg_ar[2] : '';
781 $chk_trn = false;
782 } else {
783 $this->message[] = 'edih_x12_envelopes: missing TRN02 type identifier element';
786 continue;
791 if ($st_type == '835') {
792 if (strncmp($sn, 'TRN'.$de, 4) == 0) {
793 $seg_ar = explode($de, $seg_text);
794 if (!isset($seg_ar[2]) || !isset($seg_ar[3])) {
795 $this->message[] = 'error in 835 TRN segment ' . text($seg_text);
798 $env_ar['ST'][$st_ct]['trace'] = (isset($seg_ar[2])) ? $seg_ar[2] : "";
799 // to match OpenEMR billing parse file name
800 if (isset($seg_ar[4])) {
801 $env_ar['GS'][$gs_ct]['srcid'] = $seg_ar[4];
802 } else {
803 $env_ar['GS'][$gs_ct]['srcid'] = (isset($seg_ar[3])) ? $seg_ar[3] : "";
807 continue;
810 if (strncmp($sn, 'CLP'.$de, 4) == 0) {
811 $seg_ar = explode($de, $seg_text);
812 if (isset($seg_ar[1])) {
813 $env_ar['ST'][$st_ct]['acct'][] = $seg_ar[1];
814 } else {
815 $this->message[] = 'error in 835 CLP segment ' . text($seg_text);
818 continue;
823 if ($st_type == '837') {
824 if (strncmp($sn, 'BHT'.$de, 4) == 0) {
825 $seg_ar = explode($de, $seg_text);
826 if (isset($seg_ar[3]) && $seg_ar[3]) {
827 $env_ar['ST'][$st_ct]['bht'][] = $seg_ar[3];
828 } else {
829 $this->message[] = 'edih_x12_envelopes: missing BHT03 identifier';
834 if (strncmp($sn, 'CLM'.$de, 4) == 0) {
835 $seg_ar = explode($de, $seg_text);
836 if (isset($seg_ar[1])) {
837 $env_ar['ST'][$st_ct]['acct'][] = $seg_ar[1];
838 } else {
839 $this->message[] = 'error in 837 CLM segment ' . text($seg_text);
842 continue;
847 if (strncmp($sn, 'SE'.$de, 3) == 0) {
848 // make sure no lingering toggle
849 $id278 = false;
850 $chk_trn = false;
852 $seg_ar = explode($de, $seg_text);
853 $se_num = $seg_ar[2];
854 $env_ar['ST'][$st_ct]['count'] = strval($seg_ar[1]);
855 // 999 case: expect TA1 before ST, so capture batch icn here
856 if ($st_type == '999' || $st_type == '997') {
857 if (isset($ta1_icn) && strlen($ta1_icn)) {
858 $env_ar['ST'][$st_ct]['trace'] = $ta1_icn;
859 $ta1_icn = '';
863 // errors
864 if ($se_num != $stn) {
865 $this->message[] = 'edih_x12_envelopes: ST-SE number mismatch ' . text($stn) . ' ' . text($se_num) . ' in ISA ' . text($icn) . PHP_EOL;
868 if (intval($seg_ar[1]) != $st_segs_ct) {
869 $this->message[] = 'edih_x12_envelopes: ST-SE segment count mismatch ' . text($st_segs_ct) . ' ' . text($seg_ar[1]) . ' in ISA ' . text($icn) . PHP_EOL;
872 continue;
876 if (strncmp($sn, 'GE'.$de, 3) == 0) {
877 $seg_ar = explode($de, $seg_text);
878 $env_ar['GS'][$gs_ct]['count'] = $i - $gs_start - 1;
879 $env_ar['GS'][$gs_ct]['stcount'] = trim($seg_ar[1]); // ST count
880 $gs_st_ct += $seg_ar[1];
882 if ($seg_ar[2] != $env_ar['GS'][$gs_ct]['gsn']) {
883 $this->message[] = 'edih_x12_envelopes: GS-GE identifier mismatch'.PHP_EOL;
886 if ($gs_ct === 0 && ($seg_ar[1] != count($env_ar['ST']))) {
887 $this->message[] = 'edih_x12_envelopes: GS count of ST mismatch'.PHP_EOL;
888 } elseif ($gs_st_ct != count($env_ar['ST'])) {
889 $this->message[] = 'edih_x12_envelopes: GS count of ST mismatch'.PHP_EOL;
892 continue;
896 if (strncmp($sn, 'IEA'.$de, 4) == 0) {
897 $seg_ar = explode($de, $seg_text);
898 $env_ar['ISA'][$icn]['count'] = $isa_segs_ct;
899 $env_ar['ISA'][$icn]['gscount'] = $seg_ar[1];
900 $iea_ct++;
902 if (count($env_ar['GS']) != $seg_ar[1]) {
903 $this->message[] = 'edih_x12_envelopes: GS count mismatch in ISA ' . text($icn) . PHP_EOL;
904 $gsct = count($env_ar['GS']);
905 $this->message[] = 'GS group count: ' . text($gsct) . ' IEA01: ' . text($seg_ar[1]) . ' segment: ' . text($seg_text);
908 if ($env_ar['ISA'][$icn]['icn'] !== $seg_ar[2]) {
909 $this->message[] = 'edih_x12_envelopes: ISA-IEA identifier mismatch ISA ' . text($icn) . ' IEA ' . text($seg_ar[2]);
912 if ($iea_ct == $isa_ct) {
913 $trnset_seg_ct += $isa_segs_ct;
914 //if ( $i+1 != $trnset_seg_ct ) {
915 if ($i != $trnset_seg_ct) {
916 $this->message[] = 'edih_x12_envelopes: IEA segment count error ' . text($i) . ' : ' . text($trnset_seg_ct);
918 } else {
919 $this->message[] = 'edih_x12_envelopes: ISA-IEA count mismatch ISA ' . text($isa_ct) . ' IEA ' . text($iea_ct);
922 continue;
927 return $env_ar;
931 * Parse x12 file contents into array of segments.
933 * @uses edih_x12_delimiters()
934 * @uses edih_x12_scan()
936 * @param string $file_text
937 * @return array array['i'] = segment, or empty on error
939 public function edih_x12_segments($file_text = '')
941 $ar_seg = array();
942 // do verifications
943 if ($file_text) {
944 if (!$this->constructing) {
945 // need to validate file
946 $vars = $this->edih_file_text($file_text, false, true, false);
947 $f_str = $file_text;
948 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
949 } else {
950 $f_str = $file_text;
951 if (isset($this->delimiters['t'])) {
952 $dt = $this->delimiters['t'];
953 } else {
954 $delims = $this->edih_x12_delimiters(substr($f_str, 0, 126));
955 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
958 } elseif ($this->text) {
959 $f_str = $this->text;
960 if (isset($this->delimiters['t'])) {
961 $dt = $this->delimiters['t'];
962 } else {
963 $delims = $this->edih_x12_delimiters(substr($f_str, 0, 126));
964 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
966 } else {
967 $this->message[] = 'edih_x12_segments: no file text';
968 return $ar_seg;
971 // did we get the segment terminator?
972 if (!$dt) {
973 $this->message[] = 'edih_x12_segments: invalid delimiters';
974 return $ar_seg;
977 // OK, now initialize variables
978 $seg_pos = 0; // position where segment begins
979 $seg_end = 0;
980 $seg_ct = 0;
981 $moresegs = true;
982 // could test this against simple $segments = explode($dt, $f_str)
983 while ($moresegs) {
984 // extract each segment from the file text
985 $seg_end = strpos($f_str, $dt, $seg_pos);
986 $seg_text = substr($f_str, $seg_pos, $seg_end-$seg_pos);
987 $seg_pos = $seg_end + 1;
988 $moresegs = strpos($f_str, $dt, $seg_pos);
989 $seg_ct++;
990 // we trim in case there are line or carriage returns
991 $ar_seg[$seg_ct] = trim($seg_text);
995 return $ar_seg;
1000 * extract the segments representing a transaction for CLM01 pt-encounter number
1001 * note: there may be more than one in a file, all matching are returned
1002 * 27x transactions will have unique BHT03 that could be used as the claimid argument
1004 * return_array[i] => transaction segments array
1005 * return_array[i][j] => particular segment string
1007 * @param string $clm01 837 CLM01 or BHT03 from 277
1008 * @param string $stn ST number -- optional, limit search to that ST-SE envelope
1009 * @param string $filetext optional file contents
1010 * @return array multidimensional array of segments or empty on failure
1012 public function edih_x12_transaction($clm01, $stn = '', $filetext = '')
1015 $ret_ar = array();
1017 if (!$clm01) {
1018 $this->message[] = 'edih_x12_transaction: invalid argument';
1019 return $ret_ar;
1023 $de = '';
1024 $tp = '';
1025 $seg_ar = array();
1026 $env_ar = array();
1027 // select the data to search
1028 if ($filetext && !$this->constructing) {
1029 $vars = $this->edih_file_text($filetext, true, true, true);
1030 $tp = ( isset($vars['type']) ) ? $vars['type'] : $tp;
1031 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : $de;
1032 $seg_ar = ( isset($vars['segments']) ) ? $vars['segments'] : $seg_ar;
1033 //$env_ar = $vars['envelopes']; // probably faster without envelopes in this case
1034 } elseif (count($this->segments)) {
1035 // default created object
1036 $seg_ar = $this->segments;
1037 if (count($this->delimiters)) {
1038 $de = $this->delimiters['e'];
1039 } else {
1040 $de = (substr(reset($segment_ar), 0, 3) == 'ISA') ? substr(reset($segment_ar), 3, 1) : '';
1043 $tp = ($this->type) ? $this->type : $this->edih_x12_type();
1044 $env_ar = ( isset($this->envelopes['ST']) ) ? $this->envelopes : $env_ar;
1045 } elseif ($this->text) {
1046 // object with file text, but no processing
1047 $tp = $this->edih_x12_type();
1048 $seg_ar = ( $tp ) ? $this->edih_x12_segments() : $seg_ar;
1049 if (count($seg_ar)) {
1050 $de = substr(reset($seg_ar), 3, 1);
1052 } else {
1053 $this->message[] = 'edih_x12_transaction: invalid search data';
1054 return $ret_ar;
1057 if (!count($seg_ar)) {
1058 $this->message[] = 'edih_x12_transaction: invalid segments';
1059 return $ret_ar;
1062 if (!$de) {
1063 $this->message[] = 'edih_x12_transaction: invalid delimiters';
1064 return $ret_ar;
1067 //array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278',
1068 // 'HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
1069 if (substr($tp, 0, 5) == 'mixed') {
1070 $tp = substr($tp, -2);
1073 if (!strpos('|HB|271|HS|270|HR|276|HI|278|HN|277|HP|835|FA|999|HC|837', $tp)) {
1074 $this->message[] = 'edih_x12_transaction: wrong edi type for transaction search ' . text($tp);
1075 return $ret_ar;
1078 $idx = 0;
1079 $is_found = false;
1080 $slice = array();
1081 $srch_ar = array();
1082 $sl_idx = 0;
1083 // there may be several in same ST envelope with the same $clm01, esp. 835
1084 // we will get each set of relevant transaction segments in foreach() below
1085 if (count($env_ar)) {
1086 foreach ($env_ar['ST'] as $st) {
1087 if (strlen($stn) && $st['stn'] != $stn) {
1088 continue;
1091 if (isset($st['acct']) && count($st['acct'])) {
1092 $ky = array_search($clm01, $st['acct']);
1093 if ($ky !== false) {
1094 $srch_ar[$idx]['array'] = array_slice($seg_ar, $st['start'], $st['count'], true);
1095 $srch_ar[$idx]['start'] = $st['start'];
1096 $srch_ar[$idx]['type'] = $st['type'];
1097 $idx++;
1103 // if not identified in envelope search, use segments
1104 if (!count($srch_ar)) {
1105 $srch_ar[0]['array'] = $seg_ar;
1106 $srch_ar[0]['start'] = 0; // with array_slice() the index is absolute zero base
1107 $srch_ar[0]['type'] = $tp;
1110 // verify we have type
1111 if ($srch_ar[0]['type'] == 'NA' || !$srch_ar[0]['type']) {
1112 $this->edih_message('edih_x12_transaction(): invalid file type ' . text($srch_ar[0]['type']));
1113 return $ret_ar;
1116 // segments we check
1117 $test_id = array('TRN','CLM','CLP','ST'.$de,'BHT','REF','LX'.$de,'PLB','SE'.$de);
1119 foreach ($srch_ar as $srch) {
1120 $idx = $srch['start'] - 1; // align index to segments array offset
1121 $type = (string)$srch['type'];
1122 $is_found = false;
1123 $idval = '';
1124 $idlen = 1;
1126 foreach ($srch['array'] as $key => $seg) {
1127 $idx++;
1129 $test_str = substr($seg, 0, 3);
1130 if (!in_array($test_str, $test_id, true)) {
1131 continue;
1135 // the opening ST segment should be in each search array,
1136 // so type and search values can be determined here.
1137 if (strncmp($seg, 'ST'.$de, 3) == 0) {
1138 $stseg = explode($de, $seg);
1139 $type = ($type) ? $type : (string)$stseg[1];
1141 $idval = ( strpos('|HN|277|HB|271', $type) ) ? 'TRN'.$de.'2'.$de.$clm01 : '';
1142 $idval = ( strpos('|HR|276|HS|270', $type) ) ? 'TRN'.$de.'1'.$de.$clm01 : $idval;
1143 $idval = ( strpos('|HI|278', $type) ) ? 'REF'.$de.'EJ'.$de.$clm01 : $idval;
1144 $idval = ( strpos('|HC|837', $type) ) ? 'CLM'.$de.$clm01.$de : $idval;
1145 $idval = ( strpos('|HP|835', $type) ) ? 'CLP'.$de.$clm01.$de : $idval;
1146 $idlen = strlen($idval);
1148 continue;
1151 //array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278',
1152 // 'HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
1153 // these types use the BHT segment to begin transactions
1154 if (strpos('|HI|278|HN|277|HR|276|HB|271|HS|270|HC|837', $type)) {
1156 if (strncmp($seg, 'BHT'.$de, 4) === 0) {
1157 $bht_seg = explode($de, $seg);
1158 $bht_pos = $idx;
1159 //$bht_pos = $key;
1160 if ($is_found && isset($slice[$sl_idx]['start'])) {
1161 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1162 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1163 $is_found = false;
1164 $sl_idx++;
1165 } elseif (strcmp($clm01, $bht_seg[3]) === 0) {
1166 // matched by BHT03 identifier
1167 $is_found = true;
1168 $slice[$sl_idx]['start'] = $bht_pos;
1171 continue;
1175 if (strncmp($seg, $idval, $idlen) === 0) {
1176 // matched by clm01 identifier (idval)
1177 $is_found = true;
1178 $slice[$sl_idx]['start'] = $bht_pos;
1179 continue;
1184 if ($type == 'HP' || $type == '835') {
1185 if (strncmp($seg, 'CLP'.$de, 4) === 0) {
1186 if (strncmp($seg, $idval, $idlen) === 0) {
1187 if ($is_found && isset($slice[$sl_idx]['start'])) {
1188 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1189 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1190 $sl_idx++;
1193 $is_found = true;
1194 $slice[$sl_idx]['start'] = $idx;
1195 //$slice[$sl_idx]['start'] = $key;
1196 } else {
1197 if ($is_found && isset($slice[$sl_idx]['start'])) {
1198 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1199 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1200 $is_found = false;
1201 $sl_idx++;
1205 continue;
1208 // LX segment is often used to group claim payment information
1209 // we do not capture TS3 or TS2 segments in the transaction
1210 if (strncmp($seg, 'LX'.$de, 3) === 0) {
1211 if ($is_found && isset($slice[$sl_idx]['start'])) {
1212 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1213 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1214 $is_found = false;
1215 $sl_idx++;
1218 continue;
1221 // PLB segment is part of summary/trailer in 835
1222 // not part of the preceeding transaction
1223 if (strncmp($seg, 'PLB'.$de, 4) === 0) {
1224 if ($is_found && isset($slice[$sl_idx]['start'])) {
1225 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1226 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1227 $is_found = false;
1228 $sl_idx++;
1231 continue;
1235 // SE will always mark end of transaction segments
1236 if (strncmp($seg, 'SE'.$de, 3) === 0) {
1237 if ($is_found && isset($slice[$sl_idx]['start'])) {
1238 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1239 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1240 $is_found = false;
1241 $sl_idx++;
1244 } // end foreach($srch['array'] as $seg)
1245 } // end foreach($srch_ar as $srch)
1247 if (count($slice)) {
1248 foreach ($slice as $sl) {
1249 $ret_ar[] = array_slice($seg_ar, $sl['start'], $sl['count'], true);
1254 return $ret_ar;
1259 * get the segment(s) with a particular ID, such as CLP, NM1, etc.
1260 * return is array
1261 * array[i] => matching segment string
1263 * @param string $segmentID such as NM1, CLP, STC, etc.
1264 * @param string $srchStr optional string contained in segment
1265 * @param array $seg_array optional supplied array of segments to search
1266 * @return array
1268 public function edih_get_segment($segmentID, $srchStr = '', $seg_array = '')
1271 $ret_ar = array();
1272 $seg_ar = array();
1273 $segid = ( strlen($segmentID) ) ? trim($segmentID) : '';
1275 $srch = ( strlen($srchStr) ) ? $srchStr : '';
1278 if (!$segid) {
1279 $this->message[] = 'edih_get_segment(): missing segment ID';
1280 return $ret_ar;
1284 $de = ( isset($this->delimiters['e']) ) ? $this->delimiters['e'] : '';
1285 $dt = ( isset($this->delimiters['t']) ) ? $this->delimiters['t'] : '';
1287 // segment array from edih_x12_transaction() is two dimension
1288 if (is_array($seg_array) && count($seg_array)) {
1289 if (isset($seg_array[0]) && is_array($seg_array[0])) {
1290 foreach ($seg_array as $ar) {
1291 $seg_ar = array_merge($seg_ar, $ar);
1293 } else {
1294 $seg_ar = $seg_array;
1296 } elseif (count($this->segments)) {
1297 $seg_ar = $this->segments;
1298 } elseif ($this->text) {
1299 if (!$de) {
1300 $delims = $this->edih_x12_delimiters(substr($this->text, 0, 126));
1301 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
1302 $de = ( isset($delims['e']) ) ? $delims['e'] : '';
1305 if (!$de || !$dt) {
1306 $this->message[] = 'edih_get_segment() : unable to get delimiters';
1307 return $ret_ar;
1311 $segsrch = ($segid == 'ISA') ? $segid.$de : $dt.$segid.$de;
1312 $seg_pos = 1;
1313 $see_pos = 2;
1314 while ($seg_pos) {
1315 $seg_pos = strpos($this->text, $segsrch, $seg_pos);
1316 $see_pos = strpos($this->text, $dt, $seg_pos+1);
1317 if ($seg_pos) {
1318 $segstr = trim(substr($this->text, $seg_pos, $see_pos-$seg_pos), $dt);
1319 if ($srch) {
1320 if (strpos($segstr, $srch) !== false) {
1321 $ret_ar[] = $segstr;
1323 } else {
1324 $ret_ar[] = $segstr;
1327 $seg_pos = $see_pos+1;
1333 if (count($seg_ar)) {
1334 $cmplen = strlen($segid.$de);
1335 foreach ($seg_ar as $key => $seg) {
1336 if (strncmp($seg, $segid.$de, $cmplen) === 0) {
1337 if ($srch) {
1338 if (strpos($seg, $srch) !== false) {
1339 $ret_ar[$key] = $seg;
1341 } else {
1342 $ret_ar[$key] = $seg;
1346 } else {
1347 $this->message[] = 'edih_get_segment() : no segments or text content available';
1351 return $ret_ar;
1356 * Get a slice of the segments array
1357 * Supply an array with one or more of the following keys and values:
1359 * ['trace'] => trace value from 835(TRN02) or 999(TA101) x12 type
1360 * ['ISA13'] => ISA13
1361 * ['GS06'] => GS06 (sconsider also 'ISA13')
1362 * ['ST02'] => ST02 (condider also 'ISA13' and 'GS06')
1363 * ['keys'] => true to preserve segment numbering from original file
1365 * The return value will be an array of one or more segments.
1366 * The 'search' parameter results in one or more segments containing
1367 * the search string. The
1368 * @param array note: all element values except 'keys' are strings
1369 * @return array
1371 function edih_x12_slice($arg_array, $file_text = '')
1374 $ret_ar = array();
1375 $f_str = '';
1376 // see what we have
1377 if (!is_array($arg_array) || !count($arg_array)) {
1378 // debug
1379 $this->message[] = 'edih_x12_slice() invalid array argument';
1380 return $ret_ar;
1384 if ($file_text) {
1385 // need to validate file edih_file_text($file_text, $type=false, $delimiters=false, $segments=false)
1386 $vars = $this->edih_file_text($file_text, true, true, false);
1387 if (is_array($vars) && count($vars)) {
1388 $f_str = $file_text;
1389 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
1390 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : '';
1391 $ft = ( isset($vars['type']) ) ? $vars['type'] : '';
1392 //$seg_ar = ( isset($vars['segments']) ) ? $vars['segments'] : '';
1393 //$env_ar = $this->edih_x12_envelopes($f_str);
1394 } else {
1395 $this->message[] = 'edih_x12_slice() error processing file text';
1396 // debug
1397 //echo $this->edih_message().PHP_EOL;
1398 return $ret_ar;
1400 } elseif (count($this->segments) && count($this->envelopes) && count($this->delimiters)) {
1401 $seg_ar = $this->segments;
1402 $env_ar = $this->envelopes;
1403 $dt = $this->delimiters['t'];
1404 $de = $this->delimiters['e'];
1405 $ft = $this->type;
1406 } else {
1407 $this->message[] = 'edih_x12_slice() object missing needed properties';
1408 // debug
1409 //echo $this->edih_message().PHP_EOL;
1410 return $ret_ar;
1413 // initialize search variables
1414 $trace = '';
1415 $stn = '';
1416 $gsn = '';
1417 $icn = '';
1418 $prskeys = false;
1420 foreach ($arg_array as $key => $val) {
1421 switch ((string)$key) {
1422 case 'trace':
1423 $trace = (string)$val;
1424 break;
1425 case 'ST02':
1426 $stn = (string)$val;
1427 break;
1428 case 'GS06':
1429 $gsn = (string)$val;
1430 break;
1431 case 'ISA13':
1432 $icn = (string)$val;
1433 break;
1434 case 'keys':
1435 $prskeys = (bool)$val;
1436 break;
1441 if ($trace && strpos('|HP|FA', $ft) === false) {
1442 $this->message[] = 'edih_x12_slice() incorrect type [' . text($ft) . '] for trace';
1443 return $ret_ar;
1447 if ($f_str) {
1448 $srchstr = '';
1449 if ($icn) {
1450 $icnpos = strpos($f_str, $de.$icn.$de);
1451 if ($icnpos === false) {
1452 // $icn not found
1453 $this->message[] = 'edih_x12_slice() did not find ISA13 ' . text($icn);
1454 // debug
1455 //echo $this->edih_message().PHP_EOL;
1456 return $ret_ar;
1457 } elseif ($icnpos < 106) {
1458 $isapos = 0;
1459 } else {
1460 $isapos = strrpos($f_str, $dt.'ISA'.$de, ($icnpos - strlen($f_str))) + 1;
1463 $ieapos = strpos($f_str, $de.$icn.$dt, $isapos);
1464 $ieapos = strpos($f_str, $dt, $ieapos) + 1;
1465 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $isapos+2) + 1 : 0;
1467 $srchstr = substr($f_str, $isapos, $ieapos-$isapos);
1470 if ($gsn) {
1471 $srchstr = ($srchstr) ? $srchstr : $f_str;
1472 $gspos = strpos($srchstr, $de.$gsn.$de);
1473 if ($gspos === false) {
1474 // $gsn not found
1475 $this->message[] = 'edih_x12_slice() did not find GS06 ' . text($gsn);
1476 return $ret_ar;
1477 } else {
1478 $gspos = strrpos(substr($srchstr, 0, $gspos), $dt) + 1;
1481 $gepos = strpos($srchstr, $dt.'GE'.$dt, $gspos);
1482 $gepos = strpos($srchstr, $dt, $gepos+1) + 1;
1483 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $gspos+2) + 1 : 0;
1485 $srchstr = substr($srchstr, $gspos, $gepos-$gspos);
1488 if ($stn) {
1489 $srchstr = ($srchstr) ? $srchstr : $f_str;
1490 $sttp = $this->gstype_ar[$ft];
1491 $seg_st = $dt.'ST'.$de.$sttp.$de.$stn ;
1492 $seg_se = $dt.'SE'.$de;
1493 // $segpos = 1;
1494 $stpos = strpos($srchstr, $seg_st);
1495 if ($stpos === false) {
1496 // $stn not found
1497 $this->message[] = 'edih_x12_slice() did not find ST02 ' . text($stn);
1498 return $ret_ar;
1499 } else {
1500 $stpos = $stpos + 1;
1503 $sepos = strpos($srchstr, $seg_se, $stpos);
1504 $sepos = strpos($srchstr, $dt, $sepos+1);
1505 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $stpos+2) + 1 : 0;
1507 $srchstr = substr($srchstr, $stpos, $sepos-$stpos);
1510 if ($trace) {
1512 $trpos = strpos($f_str, $de.$trace);
1513 if ($trpos === false) {
1514 // $icn not found
1515 $this->message[] = 'edih_x12_slice() did not find trace ' . text($trace);
1516 return $ret_ar;
1519 $sttp = $this->gstype_ar[$ft];
1520 $seg_st = $dt.'ST'.$de.$sttp.$de;
1521 $stpos = strrpos($f_str, $seg_st, ($trpos - strlen($f_str)));
1522 $sepos = strpos($f_str, $dt.'SE'.$de, $stpos);
1523 $sepos = strpos($f_str, $dt, $sepos+1);
1525 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $st_pos+2) + 1 : 0;
1526 $srchstr = substr($f_str, $stpos+1, $sepos-$stpos);
1529 // if we have a match, the $srchstr should have the desired segments
1530 if ($trace || $icn || $gsn || $stn) {
1531 if ($srchstr) {
1532 $seg_ar = explode($dt, $srchstr);
1533 // to keep segment numbers same as original file
1534 foreach ($seg_ar as $seg) {
1535 $ret_ar[$segidx] = $seg;
1536 $segidx++;
1539 return $ret_ar;
1540 } else {
1541 $this->message[] = 'edih_x12_slice() error creating substring';
1542 return $ret_ar;
1546 // file_text not supplied, check for object values
1547 } elseif (!($seg_ar && $env_ar && $dt && $de && $ft)) {
1548 // debug
1549 $this->message[] = 'edih_x12_slice() error is processing file';
1550 return $ret_ar;
1553 // file_text not supplied, use object values
1554 if ($trace) {
1555 foreach ($env_ar['ST'] as $st) {
1556 if ($st['trace'] == $trace) {
1557 // have to add one to the count to capture the SE segment so html_str has data
1558 // when called from edih_835_payment_html function in edih_835_html.php 4-25-17 SMW
1559 $ret_ar = array_slice($seg_ar, $st['start'], $st['count']+1, $prskeys);
1560 break;
1563 } elseif ($icn && !($stn || $gsn)) {
1564 if (isset($env_ar['ISA'][$icn])) {
1565 $ret_ar = array_slice($seg_ar, $env_ar['ISA'][$icn]['start'], $env_ar['ISA'][$icn]['count'], $prskeys);
1567 } elseif ($gsn && !$stn) {
1568 foreach ($env_ar['GS'] as $gs) {
1569 if ($icn) {
1570 if (($gs['icn'] == $icn) && ($gs['gsn'] == $gsn)) {
1571 $ret_ar = array_slice($seg_ar, $gs['start'], $gs['count'], $prskeys);
1572 break;
1574 } else {
1575 if ($gs['gsn'] == $gsn) {
1576 $ret_ar = array_slice($seg_ar, $gs['start'], $gs['count'], $prskeys);
1577 break;
1581 } elseif ($stn) {
1582 // ;
1583 foreach ($env_ar['ST'] as $st) {
1585 if ($icn) {
1586 if ($gsn) {
1587 if ($st['icn'] == $icn && $st['gsn'] == $gsn && $st['stn'] == $stn) {
1588 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1589 break;
1591 } else {
1592 if ($st['icn'] == $icn && $st['stn'] == $stn) {
1593 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1594 break;
1597 } elseif ($gsn) {
1598 if ($st['gsn'] == $gsn && $st['stn'] == $stn) {
1599 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1600 break;
1602 } elseif ($st['stn'] == $stn) {
1604 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1605 break;
1608 } else {
1609 $this->message[] = 'edih_x12_slice() no file text or invalid array argument keys or values';
1613 if (!count($ret_ar)) {
1614 $this->message[] = 'edih_x12_slice() no match';
1617 return $ret_ar;
1620 // end class edih_x12_file