EDI history module, version 2.(take 3)
[openemr.git] / library / edihistory / edih_x12file_class.php
blobf677fddf4b313d29ad45d0a2270aeabd5883aa4b
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 {
61 // properties
62 private $filepath = '';
63 private $filename = '';
64 private $type = '';
65 private $version = '';
66 private $text = '';
67 private $length = 0;
68 private $valid = false;
69 private $isx12 = false;
70 private $hasGS = false;
71 private $hasST = false;
72 private $message = array();
73 private $delimiters = array();
74 private $segments = array();
75 private $envelopes = array();
77 private $constructing = false;
79 private $gstype_ar = array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HN'=>'277',
80 'HI'=>'278', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
82 function __construct($file_path='', $mk_segs=true, $text=false ) {
84 if ($file_path === '') { return true; }
86 if ( is_file($file_path) && is_readable($file_path) ) {
87 $this->filepath = trim($file_path);
88 $this->filename = basename($this->filepath);
89 $f_text = file_get_contents($this->filepath);
91 $testval = ($f_text) ? $this->edih_x12_scan($f_text) : '';
92 $this->valid = ( strpos($testval, 'v') ) ? true : false;
93 $this->isx12 = ( strpos($testval, 'i') ) ? true : false;
94 $this->hasGS = ( strpos($testval, 'g') ) ? true : false;
95 $this->hasST = ( strpos($testval, 's') ) ? true : false;
97 if ($this->valid) {
98 $this->constructing = true;
99 $this->text = ($text) ? $f_text : '';
100 $this->length = ($f_text) ? strlen($f_text) : 0;
101 if ($this->isx12) {
102 $this->delimiters = $this->edih_x12_delimiters( substr($f_text, 0, 126) );
103 $this->version = substr($f_text, 84, 5);
104 if ($mk_segs) {
105 $this->segments = $this->edih_x12_segments($f_text);
106 if ( is_array($this->segments) && count($this->segments) ) {
107 $this->envelopes = $this->edih_x12_envelopes();
108 $this->type = $this->edih_x12_type();
109 } else {
110 $this->message[] = 'edih_x12_file: error in creating segment array ' . $this->filename . PHP_EOL;
112 } else {
113 // read file contents to try and determine x12 type
114 $this->type = $this->edih_x12_type($f_text);
118 } else {
119 // invalid file path
120 $this->message[] = 'edih_x12_file: invalid file path ' . $file_path;
122 $this->constructing = false;
123 return $this->valid;
127 * function to support empty object and '_x12_' functions called with supplied file text
129 * @param string $file_text
130 * @param bool return x12 type
131 * @param bool return delimiters
132 * @param bool return segments
133 * @return array array['filetext'] and maybe ['type'] ['$delimiters'] ['segments']
135 private function edih_file_text($file_text, $type=false, $delimiters=false, $segments=false) {
137 $ret_ar = array();
138 if ( !$file_text || is_string($file_text) == false ) {
139 $this->message[] = 'edih_file_text(): invalid argument';
140 return $ret_ar;
142 // do verifications
143 $v = $this->edih_x12_scan($file_text);
144 if ( !strpos($v, 's') ) {
145 $this->message[] = 'edih_file_text(): failed scan of file text ('.$v.')';
146 return $ret_ar;
149 $this->constructing = true;
151 if ($type) { $ret_ar['type'] = $this->edih_x12_type($file_text); }
152 if ($delimiters) { $ret_ar['delimiters'] = $this->edih_x12_delimiters( substr($file_text, 0, 126) ); }
153 if ($segments) { $ret_ar['segments'] = $this->edih_x12_segments($file_text); }
155 $this->constructing = false;
157 return $ret_ar;
161 * functions to return properties
163 public function classname() { return get_class($this); }
164 public function edih_filepath() { return $this->filepath; }
165 public function edih_filename() { return $this->filename; }
166 public function edih_type() { return $this->type; }
167 public function edih_version() { return $this->version; }
168 public function edih_text() { return $this->text; }
169 public function edih_length() { return $this->length; }
170 public function edih_valid() { return $this->valid; }
171 public function edih_isx12() { return $this->isx12; }
172 public function edih_hasGS() { return $this->hasGS; }
173 public function edih_hasST() { return $this->hasST; }
174 public function edih_delimiters() { return $this->delimiters; }
175 public function edih_segments() { return $this->segments; }
176 public function edih_envelopes() { return $this->envelopes; }
179 * message statements regarding object or from functions
180 * formatted as html
182 * @return string
184 public function edih_message() {
185 $str_html = '<p>' . PHP_EOL;
186 if ( count($this->message) ) {
187 foreach($this->message as $msg) {
188 $str_html .= $msg . '<br />' . PHP_EOL;
190 $str_html .= PHP_EOL . '</p>' . PHP_EOL;
191 } else {
192 $str_html = '';
194 return $str_html;
199 * Numeric type of x12 HC file associated with GS01 code
201 * @param string $gs01
202 * @return string|bool
204 public function edih_gs_type($gs01) {
205 $tpky = strtoupper($gs01);
206 return ( isset($this->gstype_ar[$tpky]) ) ? $this->gstype_ar[$tpky] : false;
210 * Use PHP FileInfo to check mime type and then scan for unwanted characters
211 * check for Non-basic ASCII character and <%, <asp:, <?, ${, #!, <scr (any other evil script indicators?)
212 * basically allows A-Z a-z 0-9 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ and newline carriage_return
213 * This function accepts the following mime-type: text/plain; charset=us-ascii
215 * The return string can be 'ovigs' ov - valid, igs - ISA GS ST
217 * @param string $filetext the file contents
218 * @return string zero length on failure
220 public function edih_x12_scan($filetext) {
221 $hasval = '';
222 $flen = ( $filetext && is_string($filetext) ) ? strlen($filetext) : 0;
223 if ( !$flen ) {
224 $this->message[] = 'edih_x12_scan: zero length or invalid file text';
225 return $hasval;
227 $de = '';
228 $dt = '';
229 // use finfo php class
230 if ( class_exists('finfo') ) {
231 $finfo = new finfo(FILEINFO_MIME);
232 $mimeinfo = $finfo->buffer($filetext);
233 if ( strncmp($mimeinfo, 'text/plain; charset=us-ascii', 28) !== 0 ) {
234 $this->message[] = 'edih_x12_scan: '.$this->filename.' : invalid mime info: <br />'.$mimeinfo;
236 return $hasval;
240 if (preg_match('/[^\x20-\x7E\x0A\x0D]|(<\?)|(<%)|(<asp)|(#!)|(\$\{)|(<scr)|(script:)/is', $filetext, $matches, PREG_OFFSET_CAPTURE)) {
242 $this->message[] = 'edih_x12_scan: suspect characters in file '.$this->filename.'<br />'.
243 ' character: '.$matches[0][0].' position: '. $matches[0][1];
245 return $hasval;
247 $hasval = 'ov'; // valid
248 // check for required segments ISA GS ST; assume segment terminator is last character
249 if (substr($filetext, 0, 3) === 'ISA') {
250 $hasval = 'ovi';
251 $de = substr($filetext, 3, 1);
252 $dt = substr($filetext, -1);
253 if ( strpos($filetext, $dt.'GS'.$de, 0) ) { $hasval = 'ovig'; }
254 if ( strpos($filetext, $dt.'ST'.$de, 0) ) { $hasval = 'ovigs'; }
256 return $hasval;
260 * read the GS segments in file contents to determine x12 type, or, if the
261 * object was created with a file path and envelopes, from the GS envelope array
263 * @param string $file_text optional contents of an x12 file
264 * @return string the x12 type, e.g. 837, 835, 277, 999, etc.
266 public function edih_x12_type($file_text='') {
267 $tpstr = '';
268 $tp_tmp = array();
269 $f_text = '';
270 $delims = array();
271 $delimarg = '';
272 $dt = ( isset($this->delimiters['t']) ) ? $this->delimiters['t'] : '';
273 $de = ( isset($this->delimiters['e']) ) ? $this->delimiters['e'] : '';
275 if ($file_text) {
276 // For when '_x12_' function is called with file contents as argument
277 if (!$this->constructing) {
278 $vars = $this->edih_file_text($file_text, false, true, false);
279 $f_text = $file_text;
280 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
281 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : '';
282 } elseif ( $this->text ) {
283 // called in initial construction, delimiters already created if x12 file
284 $f_text =& $this->text;
285 if ( !$dt ) {
286 $this->message[] = 'edih_x12_type: not x12 file';
287 return $tpstr;
289 } else {
290 // called after file scan, but no segment array exists
291 $f_text =& $file_text;
292 if ( !$dt ) {
293 $delims = $this->edih_x12_delimiters( substr($f_text, 0, 126) );
294 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
295 $de = ( isset($delims['e']) ) ? $delims['e'] : '';
298 if (!$f_text) {
299 $this->message[] = 'edih_x12_type: failed scan of file content';
300 return $tpstr;
302 } elseif ( isset($this->envelopes['GS']) ) {
303 // No argument, so if envelopes exist, take values from there
304 foreach($this->envelopes['GS'] as $gs) {
305 $tp_tmp[] = $gs['type'];
307 } elseif ( count($this->segments) ) {
308 // No argument and no envelopes, so scan segments
309 if (!$de) { $de = substr(reset($this->segments), 3, 1); }
310 foreach( $this->segments as $seg ) {
311 if ( strncmp($seg, 'GS'.$de, 3) == 0) {
312 $gs_ar = explode($de, $seg);
313 if ( array_key_exists($gs_ar[1], $this->gstype_ar) ) {
314 //$tp_tmp[] = $this->gstype_ar[$gs_ar[1]];
315 $tp_tmp[] = $gs_ar[1];
316 } else {
317 $tp_tmp[] = $gs_ar[1];
318 $this->message[] = 'edih_x12_type: unknown x12 type '.$gs_ar[1];
322 } else {
323 $this->message[] = 'edih_x12_type: no content to determine x12 type';
324 return $tpstr;
326 // $f_text has content only if file contents supplied or in text property
327 if ( $f_text ) {
328 // use regular expression instead of strpos($f_text, $dt.'GS'.$de)
329 $pcrepattern = '/GS\\'.$de.'(?:HB|HS|HR|HI|HN|HP|FA|HC)\\'.$de.'/';
330 $pr = preg_match_all($pcrepattern, $f_text, $matches, PREG_OFFSET_CAPTURE);
332 if ($pr && count($matches)) {
333 foreach($matches as $m) {
334 //$gspos1 = $m[0][1];
335 $gs_ar1 = explode($de, $m[0][0]);
336 if ( array_key_exists($gs_ar1[1], $this->gstype_ar) ) {
337 //$tp_tmp[] = $this->gstype_ar[$gs_ar1[1]];
338 $tp_tmp[] = $gs_ar1[1];
339 } else {
340 $tp_tmp[] = $gs_ar1[1];
341 $this->message[] = 'edih_x12_type: unknown x12 type '.$gs_ar1[1];
344 } else {
345 $this->message[] = 'edih_x12_type: did not find GS segment ';
347 /* **** this replaced by preg_match_all() above ******
349 // scan GS segments
350 $gs_str = $dt.'GS'.$de;
351 $gs_pos = 1;
352 $gse_pos = 2;
353 while ($gs_pos) {
354 $gs_pos = strpos($f_text, $gs_str, $gs_pos);
355 if ($gs_pos) {
356 $gsterm = strpos($f_text, $dt, $gs_pos+1);
357 $gsseg = trim(substr($f_text, $gs_pos+1, $gsterm-$gs_pos-1));
358 //$gs_ar = explode($de, substr($f_text, $gs_pos+1, $gsterm-$gs_pos-1) );
359 $this->message[] = 'edih_x12_type: '.$gsseg.PHP_EOL;
360 $gs_ar = explode($de, $gsseg);
361 if ( array_key_exists($gs_ar[1], $this->gstype_ar) ) {
362 $tp_tmp[] = $this->gstype_ar[$gs_ar[1]];
363 } else {
364 $tp_tmp[] = $gs_ar[1];
365 $this->message[] = 'edih_x12_type: unknown x12 type '.$gs_ar[1];
367 $gs_pos = $gsterm + 1;
370 ******************* */
372 // x12 type information collected
373 if ( count($tp_tmp) ) {
374 $tp3 = array_values( array_unique($tp_tmp) );
375 // mixed should not happen -- concatenated ISA envelopes of different types?
376 $tpstr = ( count($tp3) > 1 ) ? 'mixed|' . implode("|", $tp3) : $tp3[0];
377 //$this->message[] = 'edih_x12_type: ' . $tpstr;
378 } else {
379 $this->message[] = 'edih_x12_type: error in identifying type ';
380 return false;
382 return $tpstr;
387 * Extract x12 delimiters from the ISA segment
389 * There are obviously easier/faster ways of doing this, but we go character by character.
390 * The value returned is empty on error, otherwise:
391 * <pre>
392 * array('t'=>segment terminator, 'e'=>element delimiter,
393 * 's'=>sub-element delimiter, 'r'=>repetition delimiter)
394 * </pre>
396 * @param string $isa_str110 first n>=106 characters of x12 file
397 * @return array array or empty on error
399 public function edih_x12_delimiters($isa_str110='') {
401 $delim_ar = array();
402 if (!$isa_str110 && $this->text) {
403 $isa_str = substr($this->text, 0, 106);
404 } else {
405 $isa_str = trim($isa_str110);
407 $isalen = strlen($isa_str);
408 if ($isalen >= 106) {
409 if (substr($isa_str, 0, 3) != 'ISA') {
410 // not the starting characters
411 $this->message[] = 'edih_x12_delimiters: text does not begin with ISA';
412 return $delim_ar;
414 /* Extract delimiters using the prescribed positions.
415 * -- problem is possibly mangled files
416 * $t_ar['e'] = substr($isa_str, 3, 1);
417 * $t_ar['r'] = substr($isa_str, 82, 1);
418 * $t_ar['s'] = substr($isa_str, 104, 1);
419 * $t_ar['t'] = substr($isa_str, 105, 1);
422 } else {
423 $this->message[] = 'edih_x12_delimiters: ISA string too short'.PHP_EOL;
424 return $delim_ar;
426 $s = '';
427 $delim_ct = 0;
428 $de = substr($isa_str, 3, 1); // ISA*
429 for ($i = 0; $i < $isalen; $i++) {
430 if ($isa_str[$i] == $de) {
431 // element count incremented at end of loop
432 // repetition separator in version 5010
433 if ($delim_ct == 11) { $dr = substr($s, 1, 1); }
434 if ($delim_ct == 12) {
435 if ( strpos($s, '501') === false ) { $dr = ''; }
438 if ($delim_ct == 15) {
439 $ds = substr($isa_str, $i+1, 1);
440 $dt = substr($isa_str, $i+2, 1);
442 if ($delim_ct == 16) { break; }
443 $s = $isa_str[$i]; // $elem_delim;
444 $delim_ct++;
445 } else {
446 $s .= $isa_str[$i];
449 // there are 16 elements in ISA segment
450 if ($delim_ct < 16) {
451 // too few elements -- probably did not get delimiters
452 $this->message[] = "edih_x12_delimiters: too few elements in ISA string";
453 return $delim_ar;
456 $delim_ar = array('t'=>$dt, 'e'=>$de, 's'=>$ds, 'r'=>$dr);
458 return $delim_ar;
462 * Create a multidimensional array of edi envelope info from object segments.
463 * Useful for slicing and dicing. The ['ST'][$stky]['trace'] value is used only for 835
464 * or 999 type files and the ['ST'][$stky]['acct'][i] array will have multiple values
465 * likely only for 835, 271, and 277 types, because response from a payer will have
466 * multiple transactions in the ST-SE envelope while OpenEMR probably will place each
467 * transaction in its own ST-SE envelope for 270 and 837 types.
469 * The ['start'] and ['count'] values are for use in php function array_slice()
470 * The numeric keys of the segments array begin at 1 and the ['start'] value is one less
471 * than the actual key because array_slice() offset is zero-based.
473 * <pre>
474 * ['ISA'][$icn]=>['start']['count']['sender']['receiver']['icn']['gscount']['date']
475 * ['GS'][$gs_ct]=>['start']['count']['gsn']['icn']['sender']['date']['stcount']['type']
476 * ['ST'][$stky]=>['start']['count']['stn']['gsn']['icn']['type']['trace']['acct']
477 * ['ST'][$stky]['acct'][i]=>CLM01
478 * ['ST'][$stky]['bht03'][i]=>BHT03
479 * </pre>
481 * @return array array as shown above or empty on error
483 public function edih_x12_envelopes($file_text='') {
484 // produce an array of envelopes and positions
485 $env_ar = array();
486 $de = '';
487 if ($file_text) {
488 // presume need for file scan and delimiters
489 $vars = $this->edih_file_text($file_text, false, true, true);
490 $segment_ar = (isset($vars['segments']) ) ? $vars['segments'] : array();
491 $de = (isset($vars['delimiters']) ) ? $vars['delimiters']['e'] : '';
492 //$segment_ar = $this->edih_x12_segments($file_text);
493 if ( empty($segment_ar) || !$de ) {
494 $this->message[] = 'edih_x12_envelopes: invalid file text';
495 return $env_ar;
497 } elseif ( count($this->segments) ) {
498 $segment_ar = $this->segments;
499 if (isset($this->delimiters['e'])) {
500 $de = $this->delimiters['e'];
501 } else {
502 $de = (substr(reset($segment_ar), 0, 3) == 'ISA') ? substr(reset($segment_ar), 3, 1) : '';
504 } else {
505 $this->message[] = 'edih_x12_envelopes: no text or segments';
506 return $env_ar;
508 if (!$de) {
509 $this->message[] = 'edih_x12_envelopes: invalid delimiters';
510 return $env_ar;
513 // get the segment array bounds
514 $seg_first = (reset($segment_ar) !== false) ? key($segment_ar) : '1';
515 $seg_last = (end($segment_ar) !== false) ? key($segment_ar) : count($segment_ar) + $seg_first;
516 if (reset($segment_ar) === false) {
517 $this->message[] = 'edi_x12_envelopes: reset() error in segment array';
518 return $env_ar;
519 } else {
520 $seg_ct = $seg_last + 1;
522 // variables
523 $seg_txt = '';
524 $sn = '';
525 $st_type = '';
526 $st_ct = 0;
527 $isa_ct = 0;
528 $iea_ct = 0;
529 $gs_st_ct = 0;
530 $trnset_seg_ct = 0;
531 $st_segs_ct = 0;
532 $isa_segs_ct = 0;
533 $chk_trn = false;
534 $trncd = '2';
535 //$id278 = false;
536 $ta1_icn = '';
537 $seg_ar = array();
538 // the segment IDs we look for
539 $chk_segs = array('ISA', 'GS'.$de, 'TA1', 'ST'.$de, 'BHT', 'HL'.$de, 'TRN', 'CLP', 'CLM', 'SE'.$de, 'GE'.$de, 'IEA');
541 for ($i=$seg_first; $i<$seg_ct; $i++) {
542 // counters
543 $isa_segs_ct++;
544 $st_segs_ct++;
546 $seg_text = $segment_ar[$i];
547 $sn = substr($seg_text, 0, 4);
548 // skip over segments that are not envelope boundaries or identifiers
549 if ( !in_array(substr($sn, 0, 3), $chk_segs) ) { continue; }
550 // create the structure array
551 if ( strncmp($sn, 'ISA'.$de, 4) == 0 ) {
552 $seg_ar = explode($de, $seg_text);
553 $icn = trim($seg_ar[13]);
555 $env_ar['ISA'][$icn]['start'] = strval($i-1);
556 $env_ar['ISA'][$icn]['sender'] = trim($seg_ar[6]);
557 $env_ar['ISA'][$icn]['receiver'] = trim($seg_ar[8]);
558 $env_ar['ISA'][$icn]['icn'] = $icn;
559 $env_ar['ISA'][$icn]['date'] = trim($seg_ar[9]); // YYMMDD
560 $env_ar['ISA'][$icn]['version'] = trim($seg_ar[12]);
562 $isa_segs_ct = 1;
563 $isa_ct++;
564 continue;
567 if ( strncmp($sn, 'GS'.$de, 3) == 0) {
568 $seg_ar = explode($de, $seg_text);
569 $gs_start = strval($i-1);
570 $gsn = $seg_ar[6];
571 // GS06 could be used to id 997/999 response, if truly unique
572 // cannot index on $gsn due to concatenated ISA envelopes and non-unique
573 $gs_ct = isset($env_ar['GS']) ? count($env_ar['GS']) : 0;
575 $env_ar['GS'][$gs_ct]['start'] = $gs_start;
576 $env_ar['GS'][$gs_ct]['gsn'] = $gsn;
577 $env_ar['GS'][$gs_ct]['icn'] = $icn;
578 $env_ar['GS'][$gs_ct]['sender'] = trim($seg_ar[2]);
579 $env_ar['GS'][$gs_ct]['date'] = trim($seg_ar[4]);
580 $env_ar['GS'][$gs_ct]['srcid'] = '';
581 // to verify type of edi transaction
582 if ( array_key_exists($seg_ar[1], $this->gstype_ar) ) {
583 $gs_fid = $this->gstype_ar[$seg_ar[1]];
584 $env_ar['GS'][$gs_ct]['type'] = $seg_ar[1];
585 } else {
586 $gs_fid = 'NA';
587 $env_ar['GS'][$gs_ct]['type'] = 'NA';
588 $this->message[] = 'edih_x12_envelopes: Unknown GS type '.$seg_ar[1];
590 continue;
592 // expect 999 TA1 before ST
593 if ( strncmp($sn, 'TA1'.$de, 4) == 0) {
594 $seg_ar = explode($de, $seg_text);
595 if ( isset($seg_ar[1]) && $seg_ar[1] ) {
596 $ta1_icn = $seg_ar[1];
597 } else {
598 $this->message[] = 'edih_x12_envelopes: Error in TA1 segment response ICN';
600 //TA1*ISA13ICN*ISA09DATE*ISA10TIME*ACKCode*NoteCode~
601 continue;
604 if ( strncmp($sn, 'ST'.$de, 3) == 0 ) {
605 $seg_ar = explode($de, $seg_text);
606 $stn = $seg_ar[2];
607 $st_type = $seg_ar[1];
608 $st_start = strval($i);
609 $st_segs_ct = 1;
610 $st_ct = isset($env_ar['ST']) ? count($env_ar['ST']) : 0;
612 $env_ar['ST'][$st_ct]['start'] = strval($i-1);
613 $env_ar['ST'][$st_ct]['count'] = '';
614 $env_ar['ST'][$st_ct]['stn'] = $seg_ar[2];
615 $env_ar['ST'][$st_ct]['gsn'] = $gsn;
616 $env_ar['ST'][$st_ct]['icn'] = $icn;
617 $env_ar['ST'][$st_ct]['type'] = $seg_ar[1];
618 $env_ar['ST'][$st_ct]['trace'] = '0';
619 $env_ar['ST'][$st_ct]['acct'] = array();
620 $env_ar['ST'][$st_ct]['bht03'] = array();
621 // GS file id FA can be 999 or 997
622 if ( $gs_fid != $st_type && strpos($st_type, '99') === false ) {
623 $this->message[] = "edih_x12_envelopes: ISA $icn, GS $gsn $gs_fid ST $stn $st_type type mismatch".PHP_EOL;
626 continue;
629 if ( strpos('|270|271|276|277|278', $st_type) ) {
631 if ( strncmp($sn, 'BHT'.$de, 4) == 0) {
632 $seg_ar = explode($de, $seg_text);
633 if ( isset($seg_ar[2]) ) {
634 $trncd = ($seg_ar[2] == '13') ? '1' : '2';
635 // 13 = request, otherwise assume response
636 } else {
637 $this->message[] = 'edih_x12_envelopes: missing BHT02 type element';
639 if ( isset($seg_ar[3]) && $seg_ar[3]) {
640 $env_ar['ST'][$st_ct]['bht03'][] = $seg_ar[3];
641 } else {
642 $this->message[] = 'edih_x12_envelopes: missing BHT03 identifier';
645 if (strncmp($sn, 'HL'.$de, 3) == 0 ) {
646 $seg_ar = explode($de, $seg_text);
647 if (isset($seg_ar[3]) && $seg_ar[3]) {
648 $chk_trn = ( strpos('|22|23|PT', $seg_ar[3]) ) ? true : false;
649 } else {
650 $this->message[] = 'edih_x12_envelopes: missing HL03 level element';
652 continue;
654 if ($chk_trn && strncmp($sn, 'TRN'.$de, 4) == 0) {
655 $seg_ar = explode($de, $seg_text);
656 if (isset($seg_ar[1]) && $seg_ar[1] == $trncd) {
657 $env_ar['ST'][$st_ct]['acct'][] = (isset($seg_ar[2])) ? $seg_ar[2] : '';;
658 $chk_trn = false;
659 } else {
660 $this->message[] = 'edih_x12_envelopes: missing TRN02 type identifier element';
662 continue;
667 if ($st_type == '835') {
668 if ( strncmp($sn, 'TRN'.$de, 4) == 0) {
669 $seg_ar = explode($de, $seg_text);
670 if (!isset($seg_ar[2]) || !isset($seg_ar[3]) ) {
671 $this->message[] = 'error in 835 TRN segment '.$seg_text;
673 $env_ar['ST'][$st_ct]['trace'] = (isset($seg_ar[2])) ? $seg_ar[2] : "";
674 // to match OpenEMR billing parse file name
675 if (isset($seg_ar[4])) {
676 $env_ar['GS'][$gs_ct]['srcid'] = $seg_ar[4];
677 } else {
678 $env_ar['GS'][$gs_ct]['srcid'] = (isset($seg_ar[3])) ? $seg_ar[3] : "";
681 continue;
683 if ( strncmp($sn, 'CLP'.$de, 4) == 0) {
684 $seg_ar = explode($de, $seg_text);
685 if ( isset($seg_ar[1]) ) {
686 $env_ar['ST'][$st_ct]['acct'][] = $seg_ar[1];
687 } else {
688 $this->message[] = 'error in 835 CLP segment '.$seg_text;
690 continue;
694 if ($st_type == '837') {
695 if ( strncmp($sn, 'BHT'.$de, 4) == 0) {
696 $seg_ar = explode($de, $seg_text);
697 if ( isset($seg_ar[3]) && $seg_ar[3]) {
698 $env_ar['ST'][$st_ct]['bht'][] = $seg_ar[3];
699 } else {
700 $this->message[] = 'edih_x12_envelopes: missing BHT03 identifier';
704 if ( strncmp($sn, 'CLM'.$de, 4) == 0) {
705 $seg_ar = explode($de, $seg_text);
706 if ( isset($seg_ar[1]) ) {
707 $env_ar['ST'][$st_ct]['acct'][] = $seg_ar[1];
708 } else {
709 $this->message[] = 'error in 837 CLM segment '.$seg_text;
711 continue;
715 if ( strncmp($sn, 'SE'.$de, 3) == 0 ) {
716 // make sure no lingering toggle
717 $id278 = false;
718 $chk_trn = false;
720 $seg_ar = explode($de, $seg_text);
721 $se_num = $seg_ar[2];
722 $env_ar['ST'][$st_ct]['count'] = strval($seg_ar[1]);
723 // 999 case: expect TA1 before ST, so capture batch icn here
724 if ($st_type == '999' || $st_type == '997') {
725 if ( isset($ta1_icn) && strlen($ta1_icn) ) {
726 $env_ar['ST'][$st_ct]['trace'] = $ta1_icn;
727 $ta1_icn = '';
730 // errors
731 if ($se_num != $stn) {
732 $this->message[] = 'edih_x12_envelopes: ST-SE number mismatch '.$stn.' '. $se_num.' in ISA '.$icn.PHP_EOL;
734 if (intval($seg_ar[1]) != $st_segs_ct) {
735 $this->message[] = 'edih_x12_envelopes: ST-SE segment count mismatch '.$st_segs_ct.' '.$seg_ar[1].' in ISA '.$icn.PHP_EOL;
737 continue;
740 if ( strncmp($sn, 'GE'.$de, 3) == 0 ) {
741 $seg_ar = explode($de, $seg_text);
742 $env_ar['GS'][$gs_ct]['count'] = $i - $gs_start - 1;
743 $env_ar['GS'][$gs_ct]['stcount'] = trim($seg_ar[1]); // ST count
744 $gs_st_ct += $seg_ar[1];
746 if ($seg_ar[2] != $env_ar['GS'][$gs_ct]['gsn']) {
747 $this->message[] = 'edih_x12_envelopes: GS-GE identifier mismatch'.PHP_EOL;
749 if ( $gs_ct === 0 && ($seg_ar[1] != count($env_ar['ST'])) ) {
750 $this->message[] = 'edih_x12_envelopes: GS count of ST mismatch'.PHP_EOL;
751 } elseif ($gs_st_ct != count($env_ar['ST']) ) {
752 $this->message[] = 'edih_x12_envelopes: GS count of ST mismatch'.PHP_EOL;
754 continue;
757 if ( strncmp($sn, 'IEA'.$de, 4) == 0 ) {
758 $seg_ar = explode($de, $seg_text);
759 $env_ar['ISA'][$icn]['count'] = $isa_segs_ct;
760 $env_ar['ISA'][$icn]['gscount'] = $seg_ar[1];
761 $iea_ct++;
763 if ( count( $env_ar['GS'] ) != $seg_ar[1] ) {
764 $this->message[] = 'edih_x12_envelopes: GS count mismatch in ISA '.$icn.PHP_EOL;
765 $gsct = count( $env_ar['GS'] );
766 $this->message[] = 'GS group count: '.$gsct.' IEA01: '.$seg_ar[1].' segment: '.$seg_text;
768 if ($env_ar['ISA'][$icn]['icn'] !== $seg_ar[2]) {
769 $this->message[] = 'edih_x12_envelopes: ISA-IEA identifier mismatch ISA '.$icn.' IEA '.$seg_ar[2];
771 if ($iea_ct == $isa_ct) {
772 $trnset_seg_ct += $isa_segs_ct;
773 //if ( $i+1 != $trnset_seg_ct ) {
774 if ( $i != $trnset_seg_ct ) {
775 $this->message[] = 'edih_x12_envelopes: IEA segment count error '.($i).' : '.$trnset_seg_ct;
777 } else {
778 $this->message[] = 'edih_x12_envelopes: ISA-IEA count mismatch ISA '.$isa_ct.' IEA '.$iea_ct;
780 continue;
784 return $env_ar;
788 * Parse x12 file contents into array of segments.
790 * @uses edih_x12_delimiters()
791 * @uses edih_x12_scan()
793 * @param string $file_text
794 * @return array array['i'] = segment, or empty on error
796 public function edih_x12_segments($file_text='') {
797 $ar_seg = array();
798 // do verifications
799 if ( $file_text ) {
800 if (!$this->constructing) {
801 // need to validate file
802 $vars = $this->edih_file_text($file_text, false, true, false);
803 $f_str = $file_text;
804 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
805 } else {
806 $f_str = $file_text;
807 if ( isset($this->delimiters['t']) ) {
808 $dt = $this->delimiters['t'];
809 } else {
810 $delims = $this->edih_x12_delimiters( substr($f_str, 0, 126) );
811 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
814 } elseif ( $this->text ) {
815 $f_str = $this->text;
816 if ( isset($this->delimiters['t']) ) {
817 $dt = $this->delimiters['t'];
818 } else {
819 $delims = $this->edih_x12_delimiters( substr($f_str, 0, 126) );
820 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
822 } else {
823 $this->message[] = 'edih_x12_segments: no file text';
824 return $ar_seg;
826 // did we get the segment terminator?
827 if ( !$dt ) {
828 $this->message[] = 'edih_x12_segments: invalid delimiters';
829 return $ar_seg;
831 // OK, now initialize variables
832 $seg_pos = 0; // position where segment begins
833 $seg_end = 0;
834 $seg_ct = 0;
835 $moresegs = true;
836 // could test this against simple $segments = explode($dt, $f_str)
837 while ($moresegs) {
838 // extract each segment from the file text
839 $seg_end = strpos($f_str, $dt, $seg_pos);
840 $seg_text = substr($f_str, $seg_pos, $seg_end-$seg_pos);
841 $seg_pos = $seg_end + 1;
842 $moresegs = strpos($f_str, $dt, $seg_pos);
843 $seg_ct++;
844 // we trim in case there are line or carriage returns$seg_ct
845 $ar_seg[$seg_ct] = trim($seg_text);
848 return $ar_seg;
853 * extract the segments representing a transaction for CLM01 pt-encounter number
854 * note: there may be more than one in a file, all matching are returned
855 * 27x transactions will have unique BHT03 that could be used as the claimid argument
857 * return_array[i] => transaction segments array
858 * return_array[i][j] => particular segment string
860 * @param string $clm01 837 CLM01 or BHT03 from 277
861 * @param string $stn ST number -- optional, limit search to that ST-SE envelope
862 * @param string $filetext optional file contents
863 * @return array multidimensional array of segments or empty on failure
865 public function edih_x12_transaction($clm01, $stn='', $filetext='') {
867 $ret_ar = array();
869 if (!$clm01) {
870 $this->message[] = 'edih_x12_transaction: invalid argument';
871 return $ret_ar;
874 $de = '';
875 $tp = '';
876 $seg_ar = array();
877 $env_ar = array();
878 // select the data to search
879 if ( $filetext && !$this->constructing) {
880 $vars = $this->edih_file_text($filetext, true, true, true);
881 $tp = ( isset($vars['type']) ) ? $vars['type'] : $tp;
882 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : $de;
883 $seg_ar = ( isset($vars['segments']) ) ? $vars['segments'] : $seg_ar;
884 //$env_ar = $vars['envelopes']; // probably faster without envelopes in this case
885 } elseif ( count($this->segments) ) {
886 // default created object
887 $seg_ar = $this->segments;
888 if (count($this->delimiters)) {
889 $de = $this->delimiters['e'];
890 } else {
891 $de = (substr(reset($segment_ar), 0, 3) == 'ISA') ? substr(reset($segment_ar), 3, 1) : '';
893 $tp = ($this->type) ? $this->type : $this->edih_x12_type();
894 $env_ar = ( isset($this->envelopes['ST']) ) ? $this->envelopes : $env_ar;
895 } elseif ($this->text) {
896 // object with file text, but no processing
897 $tp = $this->edih_x12_type();
898 $seg_ar = ( $tp ) ? $this->edih_x12_segments() : $seg_ar;
899 if ( count($seg_ar) ) {
900 $de = substr(reset($seg_ar), 3, 1);
902 } else {
903 $this->message[] = 'edih_x12_transaction: invalid search data';
904 return $ret_ar;
907 if ( !count($seg_ar) ) {
908 $this->message[] = 'edih_x12_transaction: invalid segments';
909 return $ret_ar;
911 if (!$de) {
912 $this->message[] = 'edih_x12_transaction: invalid delimiters';
913 return $ret_ar;
915 //array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278',
916 // 'HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
917 if ( substr($tp, 0, 5) == 'mixed' ) { $tp = substr($tp, -2); }
918 if ( !strpos('|HB|271|HS|270|HR|276|HI|278|HN|277|HP|835|FA|999|HC|837', $tp) ) {
919 $this->message[] = 'edih_x12_transaction: wrong edi type for transaction search '.$tp;
920 return $ret_ar;
922 $idx = 0;
923 $is_found = false;
924 $slice = array();
925 $srch_ar = array();
926 $sl_idx = 0;
927 // there may be several in same ST envelope with the same $clm01, esp. 835
928 // we will get each set of relevant transaction segments in foreach() below
929 if ( count($env_ar) ) {
930 foreach($env_ar['ST'] as $st) {
931 if ( strlen($stn) && $st['stn'] != $stn ) { continue; }
932 if ( isset($st['acct']) && count($st['acct']) ) {
933 $ky = array_search($clm01, $st['acct']);
934 if ($ky !== false) {
935 $srch_ar[$idx]['array'] = array_slice($seg_ar, $st['start'], $st['count'], true);
936 $srch_ar[$idx]['start'] = $st['start'];
937 $srch_ar[$idx]['type'] = $st['type'];
938 $idx++;
943 // if not identified in envelope search, use segments
944 if ( !count($srch_ar) ) {
945 $srch_ar[0]['array'] = $seg_ar;
946 $srch_ar[0]['start'] = 0; // with array_slice() the index is absolute zero base
947 $srch_ar[0]['type'] = $tp;
949 // verify we have type
950 if ($srch_ar[0]['type'] == 'NA' || !$srch_ar[0]['type']) {
951 $this->edih_message('edih_x12_transaction(): invalid file type '.$srch_ar[0]['type']);
952 return $ret_ar;
954 // segments we check
955 $test_id = array('TRN','CLM','CLP','ST'.$de,'BHT','REF','LX'.$de,'PLB','SE'.$de);
957 foreach($srch_ar as $srch) {
958 $idx = $srch['start'] - 1; // align index to segments array offset
959 $type = (string)$srch['type'];
960 $is_found = false;
961 $idval = '';
962 $idlen = 1;
964 foreach($srch['array'] as $key=>$seg) {
965 $idx++;
967 $test_str = substr($seg, 0, 3);
968 if ( !in_array($test_str, $test_id, true) ) { continue; }
970 // the opening ST segment should be in each search array,
971 // so type and search values can be determined here.
972 if ( strncmp($seg, 'ST'.$de, 3) == 0) {
973 $stseg = explode($de, $seg);
974 $type = ($type) ? $type : (string)$stseg[1];
976 $idval = ( strpos('|HN|277|HB|271', $type) ) ? 'TRN'.$de.'2'.$de.$clm01 : '';
977 $idval = ( strpos('|HR|276|HS|270', $type) ) ? 'TRN'.$de.'1'.$de.$clm01 : $idval;
978 $idval = ( strpos('|HI|278', $type) ) ? 'REF'.$de.'EJ'.$de.$clm01 : $idval;
979 $idval = ( strpos('|HC|837', $type) ) ? 'CLM'.$de.$clm01.$de : $idval;
980 $idval = ( strpos('|HP|835', $type) ) ? 'CLP'.$de.$clm01.$de : $idval;
981 $idlen = strlen($idval);
983 continue;
985 //array('HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278',
986 // 'HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
987 // these types use the BHT segment to begin transactions
988 if ( strpos('|HI|278|HN|277|HR|276|HB|271|HS|270|HC|837', $type) ) {
990 if ( strncmp($seg, 'BHT'.$de, 4) === 0 ) {
991 $bht_seg = explode($de, $seg);
992 $bht_pos = $idx;
993 //$bht_pos = $key;
994 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
995 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
996 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
997 $is_found = false;
998 $sl_idx++;
999 } elseif ( strcmp($clm01, $bht_seg[3]) === 0 ) {
1000 // matched by BHT03 identifier
1001 $is_found = true;
1002 $slice[$sl_idx]['start'] = $bht_pos;
1004 continue;
1007 if (strncmp($seg, $idval, $idlen) === 0 ) {
1008 // matched by clm01 identifier (idval)
1009 $is_found = true;
1010 $slice[$sl_idx]['start'] = $bht_pos;
1011 continue;
1015 if ( $type == 'HP' || $type == '835') {
1016 if ( strncmp($seg, 'CLP'.$de, 4) === 0 ) {
1017 if ( strncmp($seg, $idval, $idlen) === 0 ) {
1018 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
1019 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1020 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1021 $sl_idx++;
1023 $is_found = true;
1024 $slice[$sl_idx]['start'] = $idx;
1025 //$slice[$sl_idx]['start'] = $key;
1026 } else {
1027 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
1028 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1029 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1030 $is_found = false;
1031 $sl_idx++;
1034 continue;
1036 // LX segment is often used to group claim payment information
1037 // we do not capture TS3 or TS2 segments in the transaction
1038 if ( strncmp($seg, 'LX'.$de, 3) === 0 ) {
1039 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
1040 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1041 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1042 $is_found = false;
1043 $sl_idx++;
1045 continue;
1047 // PLB segment is part of summary/trailer in 835
1048 // not part of the preceeding transaction
1049 if ( strncmp($seg, 'PLB'.$de, 4) === 0 ) {
1050 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
1051 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1052 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1053 $is_found = false;
1054 $sl_idx++;
1056 continue;
1059 // SE will always mark end of transaction segments
1060 if ( strncmp($seg, 'SE'.$de, 3) === 0 ) {
1061 if ( $is_found && isset($slice[$sl_idx]['start']) ) {
1062 $slice[$sl_idx]['count'] = $idx - $slice[$sl_idx]['start'];
1063 //$slice[$sl_idx]['count'] = $key - $slice[$sl_idx]['start'];
1064 $is_found = false;
1065 $sl_idx++;
1068 } // end foreach($srch['array'] as $seg)
1069 } // end foreach($srch_ar as $srch)
1071 if ( count($slice) ) {
1072 foreach($slice as $sl) {
1073 $ret_ar[] = array_slice($seg_ar, $sl['start'], $sl['count'], true);
1077 return $ret_ar;
1082 * get the segment(s) with a particular ID, such as CLP, NM1, etc.
1083 * return is array
1084 * array[i] => matching segment string
1086 * @param string $segmentID such as NM1, CLP, STC, etc.
1087 * @param string $srchStr optional string contained in segment
1088 * @param array $seg_array optional supplied array of segments to search
1089 * @return array
1091 public function edih_get_segment($segmentID, $srchStr='', $seg_array='') {
1093 $ret_ar = array();
1094 $seg_ar = array();
1095 $segid = ( strlen($segmentID) ) ? trim($segmentID) : '';
1097 $srch = ( strlen($srchStr) ) ? $srchStr : '';
1100 if ( !$segid ) {
1101 $this->message[] = 'edih_get_segment(): missing segment ID';
1102 return $ret_ar;
1105 $de = ( isset($this->delimiters['e']) ) ? $this->delimiters['e'] : '';
1106 $dt = ( isset($this->delimiters['t']) ) ? $this->delimiters['t'] : '';
1108 // segment array from edih_x12_transaction() is two dimension
1109 if ( is_array($seg_array) && count($seg_array) ) {
1110 if ( isset($seg_array[0]) && is_array($seg_array[0]) ) {
1111 foreach($seg_array as $ar) { $seg_ar = array_merge($seg_ar, $ar); }
1112 } else {
1113 $seg_ar = $seg_array;
1115 } elseif ( count($this->segments) ) {
1116 $seg_ar = $this->segments;
1117 } elseif ( $this->text ) {
1118 if ( !$de ) {
1119 $delims = $this->edih_x12_delimiters( substr($this->text, 0, 126) );
1120 $dt = ( isset($delims['t']) ) ? $delims['t'] : '';
1121 $de = ( isset($delims['e']) ) ? $delims['e'] : '';
1123 if ( !$de || !$dt ) {
1124 $this->message[] = 'edih_get_segment() : unable to get delimiters';
1125 return $ret_ar;
1128 $segsrch = ($segid == 'ISA') ? $segid.$de : $dt.$segid.$de;
1129 $seg_pos = 1;
1130 $see_pos = 2;
1131 while ($seg_pos) {
1132 $seg_pos = strpos($this->text, $segsrch, $seg_pos);
1133 $see_pos = strpos($this->text, $dt, $seg_pos+1);
1134 if ($seg_pos) {
1135 $segstr = trim(substr($this->text, $seg_pos, $see_pos-$seg_pos), $dt);
1136 if ($srch) {
1137 if ( strpos($segstr, $srch) !== false ) {
1138 $ret_ar[] = $segstr;
1140 } else {
1141 $ret_ar[] = $segstr;
1143 $seg_pos = $see_pos+1;
1148 if ( count($seg_ar) ) {
1149 $cmplen = strlen($segid.$de);
1150 foreach($seg_ar as $key=>$seg) {
1151 if (strncmp($seg, $segid.$de, $cmplen) === 0 ) {
1152 if ($srch) {
1153 if ( strpos($seg, $srch) !== false ) {
1154 $ret_ar[$key] = $seg;
1156 } else {
1157 $ret_ar[$key] = $seg;
1161 } else {
1162 $this->message[] = 'edih_get_segment() : no segments or text content available';
1165 return $ret_ar;
1170 * Get a slice of the segments array
1171 * Supply an array with one or more of the following keys and values:
1173 * ['trace'] => trace value from 835(TRN02) or 999(TA101) x12 type
1174 * ['ISA13'] => ISA13
1175 * ['GS06'] => GS06 (sconsider also 'ISA13')
1176 * ['ST02'] => ST02 (condider also 'ISA13' and 'GS06')
1177 * ['keys'] => true to preserve segment numbering from original file
1179 * The return value will be an array of one or more segments.
1180 * The 'search' parameter results in one or more segments containing
1181 * the search string. The
1182 * @param array note: all element values except 'keys' are strings
1183 * @return array
1185 function edih_x12_slice($arg_array, $file_text='') {
1187 $ret_ar = array();
1188 $f_str = '';
1189 // see what we have
1190 if ( !is_array($arg_array) || !count($arg_array) ) {
1191 // debug
1192 $this->message[] = 'edih_x12_slice() invalid array argument';
1193 return $ret_ar;
1196 if ( $file_text ) {
1197 // need to validate file edih_file_text($file_text, $type=false, $delimiters=false, $segments=false)
1198 $vars = $this->edih_file_text($file_text, true, true, false);
1199 if ( is_array($vars) && count($vars) ) {
1200 $f_str = $file_text;
1201 $dt = ( isset($vars['delimiters']['t']) ) ? $vars['delimiters']['t'] : '';
1202 $de = ( isset($vars['delimiters']['e']) ) ? $vars['delimiters']['e'] : '';
1203 $ft = ( isset($vars['type']) ) ? $vars['type'] : '';
1204 //$seg_ar = ( isset($vars['segments']) ) ? $vars['segments'] : '';
1205 //$env_ar = $this->edih_x12_envelopes($f_str);
1206 } else {
1207 $this->message[] = 'edih_x12_slice() error processing file text';
1208 // debug
1209 //echo $this->edih_message().PHP_EOL;
1210 return $ret_ar;
1212 } elseif ( count($this->segments) && count($this->envelopes) && count($this->delimiters) ) {
1213 $seg_ar = $this->segments;
1214 $env_ar = $this->envelopes;
1215 $dt = $this->delimiters['t'];
1216 $de = $this->delimiters['e'];
1217 $ft = $this->type;
1218 } else {
1219 $this->message[] = 'edih_x12_slice() object missing needed properties';
1220 // debug
1221 //echo $this->edih_message().PHP_EOL;
1222 return $ret_ar;
1224 // initialize search variables
1225 $trace = '';
1226 $stn = '';
1227 $gsn = '';
1228 $icn = '';
1229 $prskeys = false;
1231 foreach($arg_array as $key=>$val) {
1232 switch((string)$key) {
1233 case 'trace': $trace = (string)$val; break;
1234 case 'ST02': $stn = (string)$val; break;
1235 case 'GS06': $gsn = (string)$val; break;
1236 case 'ISA13': $icn = (string)$val; break;
1237 case 'keys': $prskeys = (bool)$val; break;
1241 if ($trace && strpos('|HP|FA', $ft) === false) {
1242 $this->message[] = 'edih_x12_slice() incorrect type ['.$ft.'] for trace';
1243 return $ret_ar;
1246 if ($f_str) {
1247 $srchstr = '';
1248 if ($icn) {
1249 $icnpos = strpos($f_str, $de.$icn.$de);
1250 if ($icnpos === false) {
1251 // $icn not found
1252 $this->message[] = 'edih_x12_slice() did not find ISA13 '.$icn;
1253 // debug
1254 //echo $this->edih_message().PHP_EOL;
1255 return $ret_ar;
1256 } elseif ($icnpos < 106) {
1257 $isapos = 0;
1258 } else {
1259 $isapos = strrpos($f_str, $dt.'ISA'.$de, ($icnpos - strlen($f_str)) ) + 1;
1261 $ieapos = strpos($f_str, $de.$icn.$dt, $isapos);
1262 $ieapos = strpos($f_str, $dt, $ieapos) + 1;
1263 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $isapos+2) + 1 : 0;
1265 $srchstr = substr($f_str, $isapos, $ieapos-$isapos);
1267 if ($gsn) {
1268 $srchstr = ($srchstr) ? $srchstr : $f_str;
1269 $gspos = strpos($srchstr, $de.$gsn.$de);
1270 if ($gspos === false) {
1271 // $gsn not found
1272 $this->message[] = 'edih_x12_slice() did not find GS06 '.$gsn;
1273 return $ret_ar;
1274 } else {
1275 $gspos = strrpos(substr($srchstr, 0, $gspos), $dt) + 1;
1277 $gepos = strpos($srchstr, $dt.'GE'.$dt, $gspos);
1278 $gepos = strpos($srchstr, $dt, $gepos+1) + 1;
1279 $segidx = ($prskeys) ? substr_count ($f_str, $dt, 0, $gspos+2) + 1 : 0;
1281 $srchstr = substr($srchstr, $gspos, $gepos-$gspos);
1283 if ($stn) {
1284 $srchstr = ($srchstr) ? $srchstr : $f_str;
1285 $sttp = $this->gstype_ar[$ft];
1286 $seg_st = $dt.'ST'.$de.$sttp.$de.$stn ;
1287 $seg_se = $dt.'SE'.$de;
1288 // $segpos = 1;
1289 $stpos = strpos($srchstr, $seg_st);
1290 if ($stpos === false) {
1291 // $stn not found
1292 $this->message[] = 'edih_x12_slice() did not find ST02 '.$stn;
1293 return $ret_ar;
1294 } else {
1295 $stpos = $stpos + 1;
1297 $sepos = strpos($srchstr, $seg_se, $stpos);
1298 $sepos = strpos($srchstr, $dt, $sepos+1);
1299 $segidx = ($prskeys) ? substr_count ($f_str, $dt, 0, $stpos+2) + 1 : 0;
1301 $srchstr = substr($srchstr, $stpos, $sepos-$stpos);
1303 if ($trace) {
1305 $trpos = strpos($f_str, $de.$trace);
1306 if ($trpos === false) {
1307 // $icn not found
1308 $this->message[] = 'edih_x12_slice() did not find trace '.$trace;
1309 return $ret_ar;
1311 $sttp = $this->gstype_ar[$ft];
1312 $seg_st = $dt.'ST'.$de.$sttp.$de;
1313 $stpos = strrpos($f_str, $seg_st, ($trpos - strlen($f_str)) );
1314 $sepos = strpos($f_str, $dt.'SE'.$de, $stpos);
1315 $sepos = strpos($f_str, $dt, $sepos+1);
1317 $segidx = ($prskeys) ? substr_count($f_str, $dt, 0, $st_pos+2) + 1 : 0;
1318 $srchstr = substr($f_str, $stpos+1, $sepos-$stpos);
1320 // if we have a match, the $srchstr should have the desired segments
1321 if ($trace || $icn || $gsn || $stn) {
1322 if ($srchstr) {
1323 $seg_ar = explode($dt, $srchstr);
1324 // to keep segment numbers same as original file
1325 foreach($seg_ar as $seg) {
1326 $ret_ar[$segidx] = $seg;
1327 $segidx++;
1329 return $ret_ar;
1330 } else {
1331 $this->message[] = 'edih_x12_slice() error creating substring';
1332 return $ret_ar;
1335 // file_text not supplied, check for object values
1336 } elseif (!($seg_ar && $env_ar && $dt && $de && $ft) ) {
1337 // debug
1338 $this->message[] = 'edih_x12_slice() error is processing file';
1339 return $ret_ar;
1341 // file_text not supplied, use object values
1342 if ($trace) {
1343 foreach($env_ar['ST'] as $st) {
1344 if ($st['trace'] == $trace) {
1345 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1346 break;
1349 } elseif ($icn && !($stn || $gsn) ) {
1350 if ( isset($env_ar['ISA'][$icn]) ) {
1351 $ret_ar = array_slice($seg_ar, $env_ar['ISA'][$icn]['start'], $env_ar['ISA'][$icn]['count'], $prskeys);
1353 } elseif ($gsn && !$stn) {
1354 foreach($env_ar['GS'] as $gs) {
1355 if ($icn) {
1356 if ( ($gs['icn'] == $icn) && ($gs['gsn'] == $gsn) ) {
1357 $ret_ar = array_slice($seg_ar, $gs['start'], $gs['count'], $prskeys);
1358 break;
1360 } else {
1361 if ( $gs['gsn'] == $gsn ) {
1362 $ret_ar = array_slice($seg_ar, $gs['start'], $gs['count'], $prskeys);
1363 break;
1367 } elseif ($stn) {
1368 // ;
1369 foreach($env_ar['ST'] as $st) {
1371 if ($icn) {
1372 if ($gsn) {
1373 if ($st['icn'] == $icn && $st['gsn'] == $gsn && $st['stn'] == $stn) {
1374 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1375 break;
1377 } else {
1378 if ($st['icn'] == $icn && $st['stn'] == $stn) {
1379 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1380 break;
1383 } elseif ($gsn) {
1384 if ($st['gsn'] == $gsn && $st['stn'] == $stn) {
1385 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1386 break;
1388 } elseif ($st['stn'] == $stn) {
1390 $ret_ar = array_slice($seg_ar, $st['start'], $st['count'], $prskeys);
1391 break;
1394 } else {
1395 $this->message[] = 'edih_x12_slice() no file text or invalid array argument keys or values';
1398 if ( !count($ret_ar) ) {
1399 $this->message[] = 'edih_x12_slice() no match';
1401 return $ret_ar;
1404 // end class edih_x12_file