forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibr_277_read.php
1922 lines (1848 loc) · 74.7 KB
/
ibr_277_read.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ibr_277_read.php
*
* read x12 277 claim status responses, especially the 277CA variety
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 or later.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. You should have received
* a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
* <http://opensource.org/licenses/gpl-license.php>
*
* @author Kevin McCormick
* @link: http://www.open-emr.org
* @package OpenEMR
* @subpackage ediHistory
*/
// /**
// * a security measure to prevent direct web access to this file
// */
// if (!defined('SITE_IN')) die('Direct access not allowed!');
/**
* get the segments or array_slice parameters for ISA...IEA in a 277 file
*
* @todo adapt this to general purpose x12 section getter
*
* @uses csv_verify_file()
* @uses csv_x12_segments()
* @param string $filename name of file
* @param string $isa13 the ISA control number
* @param bool $slice_params whether to return array_slice() parameters or the segments
* @return array
*/
function ibr_277_isa_block($filename, $isa13, $slice_params=false) {
//
$isa_str = '';
$srchval = strval($isa13);
// get segments
$x12seg = csv_x12_segments($filename, 'f277', false);
if ( !$x12seg || ! isset($x12seg['segments']) ) {
$isa_str .= "ibr_277_isa_block: failed to get segments for ".basename($filename).PHP_EOL;
csv_edihist_log("ibr_277_isa_block: failed to get segments for $filename");
return $isa_str;
}
$elem_d = $x12seg['delimiters']['e'];
$isa277 = array();
$isa_slice = array();
$isa_pos = 0;
$idx = -1;
foreach($x12seg['segments'] as $segstr) {
$idx++;
if (substr($segstr, 0, 4) == 'ISA'.$elem_d) {
$seg = explode($elem_d, $segstr);
$isfound = (strval($seg[13]) == $srchval);
$isa_pos = $idx;
}
//
if ($isfound && !$slice_params) { $isa277[] = $segstr; }
//
if (substr($segstr, 0, 4) == 'IEA'.$elem_d) {
$seg = explode($elem_d, $segstr);
if (strval($seg[2]) == $srchval) {
$isa_slice['start'] = $isa_pos;
$isa_slice['count'] = $idx - $isa_pos + 1;
//$isfound = false;
break;
}
}
}
//
if ( !count($isa277) && !count($isa_slice) ) {
$isa_str .= "ibr_277_isa_block: did not find $isa13 in " . basename($filename).PHP_EOL;
csv_edihist_log("ibr_277_isa_block: did not find $isa13 in $filename");
return $isa_str;
}
//
if ($slice_params) {
return $isa_slice;
} else {
return $isa277;
}
}
/**
* Selected values from $ar277['claim'] array for claims_277.csv
*
* these are for initial batch return files, basically saying the claims are accepted
* for "adjudication" or rejected due to some error or omission
* close match to Availity ebr|ibr files, which are derived from these
* This uses an edit to the OpenEMR billing_process.php script to create
* a unique BHT03 id number, since that is how the claims are identified
*
* @uses ibr_batch_by_ctln()
* @param array $ar_277 the array from {@see ibr_277_parse()}
* @return array
*/
function ibr_277_csv_claim_data($ar_277_clm) {
//
// the $ar_277_vals [ $ar277['claim'] ] array:
// <pre>
// $arRSP[$isa_ct]['key']
// ['BHT'] ['isa_id']['gs06']['iea02']
//
// $arRSP["claim"][$isa_ct]=>["ISA13"]["GS04"]["GS06"]["IEA02"]
// =>['BHT']['bht_ct']['key']
// =>['ENV"]=>['BHT01']['BHT02']['BHT03']['BHT04']
// ['BHT06']["FILE"]["ISA13"]["ST02"]["SE01"]
// =>['A']=>['NM101']["NM102"]["NM103"]["NM104"]["NM105"]
// ["NM107"]["NM108"]["NM109"]["TRN01"]["TRN02"]
// ["DTP03050"]["DTP03009"]
// =>['B']=>['NM101']["NM102"]["NM103"]["NM104"]["NM105"]
// ["NM107"]["NM108"]["NM109"]["TRN01"]["TRN02"]
// ["STC011"]["STC012"]["STC013"]["STC014"]
// ["STC02"]["STC03"]["STC04"]["QTY01"]["QTY02"]
// ["AMT01"]["AMT02"]
// =>['C']=>['NM101']["NM102"]["NM103"]["NM104"]["NM105"]
// ["NM107"]["NM108"]["NM109"]["TRN01"]["TRN02"]
// ["REFTJ"]
// =>['D']=>['NM101']["NM102"]["NM103"]["NM104"]["NM105"]
// ["NM107"]["NM108"]["NM109"]["TRN01"]["TRN02"]
// ["REF1K"]["REFD9"]["DTP03FROM"]["DTP03TO"]
// =>["STC"][i]=>["STC011"]["STC012"]["STC013"]
// ["STC014"]["STC02"]["STC03"]["STC04"]
// </pre>
if (!is_array($ar_277_clm) || count($ar_277_clm) == 0 ) {
return FALSE;
}
//
$codes277 = new status_code_arrays();
//
$csv_ar = array();
//
foreach($ar_277_clm['BHT'] as $bht) {
$status = ""; $message = ""; $bfl = ""; $ctlnum = ""; $stnum = "";
// this is the BHT03 value from the batch file
$bt_bht03 = strval($bht['B']['TRN02']);
//
$st_277 = $bht['ENV']['ISA13'].'_'.$bht['ENV']['ST02']; // this is to find the response later
$file_277 = $bht['ENV']['FILE'];
$payer_name = isset($bht['A']['NM103'] ) ? $bht['A']['NM103'] : "Unknown Payer";
// if no claim_id, but ['TRN02'], find the pid-encounter with batch file and st02
if ( isset($bht['D']['REF1K']) ) {
$claim_id = $bht['D']['REF1K'];
} elseif (isset($bht['D']['REFD9'])) {
$claim_id = $bht['D']['REFD9'];
} elseif (isset($bht['D']['REFEA'])) {
$claim_id = $bht['D']['REFEA'];
} elseif (isset($bht['D']['REFBLT'])) {
$claim_id = $bht['D']['REFBLT'];
} elseif (isset($bht['D']['REFEJ'])) {
$claim_id = $bht['D']['REFEJ'];
} elseif (isset($bht['D']['REFXZ'])) {
$claim_id = $bht['D']['REFXZ'];
} elseif (isset($bht['D']['REFVV'])) {
$claim_id = $bht['D']['REFVV'];
} else {
$claim_id = "NF";
}
$pt_name = $bht['D']['NM103'] . ", " . $bht['D']['NM104'];
$date = $bht['D']['DTP03FROM'];
$pid = $bht['D']['TRN02']; //$pidenc[0]
foreach ($bht['D']['STC'] as $stc) {
//
$status = $stc['STC011'];
//
if ($stc['STC011'] == "A1" ) {
$status = "A1 Ack";
} elseif ($stc['STC011'] == "A2" ) {
$status = "A2 Acpt";
} elseif ($stc['STC011'] == "A3" ) {
$status = "A3 Rej";
} elseif ($stc['STC011'] == "A4" ) {
$status = "A4 Nfnd";
} elseif ($stc['STC011'] == "A5" ) {
$status = "A5 Split";
} elseif ($stc['STC011'] == "A6" ) {
$status = "A6 Rej";
} elseif ($stc['STC011'] == "A7" ) {
$status = "A7 Rej";
} elseif ($stc['STC011'] == "A8" ) {
$status = "A8 Rej";
} elseif ($stc['STC011'] == "DO" ) {
$status = "DO Fail";
}
if ($stc['STC03'] == "15") { $status .= " Resubmit"; }
//
// error message expected in STC12, but not always given
if (isset($stc['STC12'])) {
$message .= trim($stc['STC12']) . " ";
} elseif (strpos("|A1|A2|A5|", $stc['STC011']) === FALSE) {
$message .= 'Reject: ';
if (isset($stc['STC012'])) {
$cd = $codes277->get_STC_Status_Code( $stc['STC012'] );
$message .= $cd[0] . ' ' . $cd[1];
}
if (isset($stc['STC013'])) {
$cd = $codes277->get_STC_Entity_Code( $stc['STC013'] );
$message .= ' | ' . $cd[1];
}
}
}
// revised csv layout
//['f277']['claim'] = array('PtName', 'SvcDate', 'clm01', 'Status', 'st_277', 'File_277', 'payer_name', 'claim_id', 'bht03_837');
$csv_ar[] = array('pt_name'=>$pt_name, 'date'=>$date, 'pid'=>$pid, 'status'=>$status,
'st_277'=>$st_277, 'file_277'=>$file_277, 'payer_name'=>$payer_name,
'claim_id'=>$claim_id, 'bht03_837'=>$bt_bht03, 'message'=>$message);
}// end foreach ($ar_277_vals as $isa)
//
return $csv_ar;
}
/**
* write data to csv file
*
* @deprecated
* @uses csv_write_record()
* @param array $ar_claim_data the data array, either file data or claim data
* @param string $type either claim or file, claim if omitted
* @return int character count from fputcsv()
*/
function ibr_277_write_csv($ar_data, $type = "claim") {
//
$ct = ($type == "claim") ? 'claim' : 'file';
$rslt = csv_write_record($ar_data, "f277", $ct);
return $rslt;
}
/**
* create an html string for a table to display in a web page
*
* @param $ar_data array produced by function ibr_ebr_data_ar
* @param $err_only boolean ignore claim information if no 3e subarray is in the claim data
* @return string
*/
function ibr_277_html ($ar_data, $err_only=FALSE) {
//$ar_hd = $ar_data['head'];
//$ar_cd = $ar_data['claims'];
$idx = 0;
$idf = 0;
$has3 = FALSE;
$hasclm = FALSE;
$clm_html = "";
$str_html = "";
//
$dtl = ($err_only) ? "Errors only" : "All included claims";
//$ar_col_hdr = array('mtime', 'directory', 'file_name', 'text_name', 'claim_ct', 'reject');
// the table heading for files'mtime', 'directory', 'file_name', 'claim_ct', 'amt_accpt','reject', 'amt_rej'
$f_hdg = "<table cols=6 class=\"f277\">
<caption>277 Files Summary {$dtl} </caption>
<thead>
<tr>
<th>Date</th><th>277 File</th><th>Claims</th><th>Amt Total</th><th>Rejects</th><th>Amt Rej</th>
</tr>
</thead>
<tbody>";
//
// the details table heading'pt_name''date''pid''status''st_277''claim_id''payer_name''message'
$clm_hdg = "<table cols=6 class=\"f277\">
<thead>
<tr>
<th>Patient Name</th><th>Date</th><th>PtCtln</th><th>Status</th><th>Payer Name</th><th>Claim No</th>
</tr>
<tr>
<th colspan=6 align=left>Message</th>
</tr>
</thead>
<tbody>";
//
// start with the table heading
$str_html .= $f_hdg;
//
foreach ($ar_data as $ardt) {
// alternate colors
$bgf = ($idf % 2 == 1 ) ? 'fodd' : 'feven';
$idf++;
//
// if any individual claims detail is to be output
// a claims table was inserted, so put the files heading in
if ($hasclm) { $str_html .= $f_hdg; }
// reset variables for whether there are claims
$clm_html = "";
$has3 = FALSE;
$hasclm = FALSE;
//
if (isset($ardt['file'])) {
// 'filetime''filename''isa13_277''claim_ct''amt_accpt''claim_rct''amt_rej'
//
$str_html .= "
<tr class=\"{$bgf}\">
<td>{$ardt['file']['filetime']};</td>
<td><a href=\"edi_history_main.php?fvkey={$ardt['file']['filename']}\" target=\"_blank\">{$ardt['file']['filename']}</a></td>
<td>{$ardt['file']['claim_ct']}</td>
<td>{$ardt['file']['amt_accpt']}</td>
<td>{$ardt['file']['claim_rct']}</td>
<td>{$ardt['file']['amt_rej']}</td>
</tr>";
}
if (isset($ardt['claim']) ) {
foreach ($ardt['claim'] as $val) {
if ($err_only && $val['message'] == "" ) { continue; }
$bgc = ($idx % 2 == 1 ) ? 'odd' : 'even';
$hasclm = TRUE;
$clm_html .= "<tr class=\"{$bgc}\">
<td>{$val['pt_name']}</td>
<td>{$val['date']}</td>
<td><a class='btclm' target='_blank' href='edi_history_main.php?fvbatch={$val['bht03_837']}&btpid={$val['pid']}'>{$val['pid']}</td>
<td><a class='clmstatus' target='_blank' href='edi_history_main.php?rspfile={$val['file_277']}&pidenc={$val['pid']}&rspstnum={$val['st_277']}'>{$val['status']}</td>
<td>{$val['payer_name']}</td>
<td>{$val['claim_id']}</td>
</tr>
<tr class=\"{$bgc}\">
<td colspan = 7>{$val['message']}</td>
</tr>";
$idx++;
}
}
// if there were any claims detailed
if ( $hasclm && strlen($clm_html) ) {
$str_html .= $clm_hdg.$clm_html.PHP_EOL."</tbody></table>".PHP_EOL;
}
}
// finish the table
$str_html .= "</tbody></table>";
//
return $str_html;
}
/**
* Parse x12 277 file into array
*
* The x12 277 claim status response file contains many fields that are useful for
* different purposes, so there is a lot of surplus information depending on your
* reason for viewing the file. This function is really based on 277CA files,
* but it should mostly work with plain 277 files, (if there was a 276 generator)
*
* <pre>
* Return array has keys 'file' and 'claim'
* The 'claim' array is detailed here:
* $arRSP[$isa_ct]['key']
* ['BHT'] ['isa_id']['gs06']['iea02']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['key']
* ['ENV"] ['A'] ['B'] ['C'] ['D']['BHT01']['BHT02']['BHT03']['BHT04']['BHT06']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['ENV"]
* ['ISA13'] ['ST02'] ['SE01'] ['FILE']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['A'] (sender -- insurance company or clearinghouse)
* ['NM103']['NM109']['TRN02']['DTP03050']['DTP03009']['PER01']['PER02']['PER03']['PER04']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['B'] (receiver -- practice or biller)
* ['STC011']['STC012']['STC013']['STC014']
* ['STC01']['STC02']['STC03']['STC04']['STC05']['STC06']['STC07']['STC08'] ['STC09']
* ['STC101']['STC102']['STC103']['STC104']
* ['STC111']['STC112']['STC113']['STC114']
* ['QTY01']['QTY02']['AMT01']['AMT02']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['C'] (provider -- practice or individual)
* ['NM103']['NM104']['NM105']['NM107']['NM108']['NM109']['TRN01'] ['TRN02']
* ['REF01']['REF02']['QTY01']['QTY02']['AMT01']['AMT02']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['D'] (patient -- individual)
* ['NM102']['NM103']['NM104']['NM105']['NM107']['NM108']['NM109']['TRN01']['TRN02']
* ['REF1K']['REFD9']['REFEA']['REFBLT']['REFEJ']['REFXZ']['REFVV']
* ['DTP03FROM'] ['DTP03TO']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['D']['STC'][stccount]['key']
* ['STC011']['STC012']['STC013']['STC014']['STC011']['STC012']
* ['STC02']['STC03']['STC04']['STC05']['STC06']['STC07']['STC08']['STC09']
* ['STC101']['STC102']['STC103']['STC104']
* ['STC111']['STC112']['STC113']['STC114']
*
* $arRSP[$isa_ct]['BHT']['bht_ct']['D']['SVC'][svccount]['key']
* ['SVC011']['SVC012']['SVC013']['SVC014']['SVC015']['SVC016']['SVC017']
* ['SVC02']['SVC03']['SVC04']['SVC05']['SVC06']['SVC07']
* $arRSP[$isa_ct]['BHT']['bht_ct']['D']['SVC'][svccount]['STC'][stccount]['key']
* ['STC011']['STC012']['STC013']['STC014']
* ['STC02']['STC03']['STC04']['STC05']['STC06']['STC07']['STC08']['STC09']
* ['STC101']['STC102']['STC103']['STC104']
* ['STC111']['STC112']['STC113']['STC114']
* ['STC12']
* </pre>
*
* @todo refactor array design -- does not account well enough for x12 controls or references
* @param array $ar_segments -- from ibr_277_process_new and csv_record_include.php
* @return array
*/
function ibr_277_parse($ar_segments) {
//
// read each segment and parse for desired data
//
// $ar_vals['hl_prv']
// ['prv_name']['prv_id']
// $ar_vals['hl_prv']['clm_ct']
//
// ISA*00
// GS*HN
// - ST*277
// 0085 expect - BHT*0085
// HL*1 NM1*PR TRN*1 DTP*050 DTP*009 // HL03 NM103 NM109
// HL*2 NM1*41 TRN*2 STC*A1 QTY*90*1 AMT*YU //
// HL*3 NM1*85 TRN*1 REF*TJ QTY*QA*1 AMT*YU
// HL*4 NM1*QC TRN*2 STC*A1 REF*1K REF*D9 DTP*472
// - repeat BHT series
// - SE*27
// GE*26
// IEA*1
//
if (is_array($ar_segments) && count($ar_segments['segments']) ) {
$fdir = dirname($ar_segments['path']);
$fname = basename($ar_segments['path']);
$fmtime = date('Ymd', filemtime($ar_segments['path']) );
//
$ar_277_segments = $ar_segments['segments'];
//
$elem_d = $ar_segments['delimiters']['e'];
$rep_d = $ar_segments['delimiters']['r'];
$sub_d = $ar_segments['delimiters']['s'];
} else {
csv_edihist_log("ibr_277_parse: error invalid segments array");
return FALSE;
}
//
//$arRSP = array();
$ar277 = array();
//
$bct = -1;
$ict = -1;
$st_ct = 0;
$svc_ct = 0;
$clm_ct = 0;
$rej_ct = 0;
$amt_accpt = 0;
$amt_rej = 0;
$hl_code = "";
$loopid = "0";
//
foreach($ar_277_segments as $segline) {
// explode the segment into an array of elements
//
$seg = explode($elem_d, $segline);
// set counters, loops, etc. here
// count segments to verify ST--SE blocks
// $st_seg_ct is set to 1 in "ST"
$st_seg_ct = isset($st_seg_ct) ? $st_seg_ct+1 : 0;
//
if ($seg[0] == "ISA") {
// x12-277 files may have multiple ISA--IEA segment blocks
$ict++;
//
$isa13 = $seg[13];
$fmtime = '20'.strval($seg[9]);
//
// reset the ST count and BHT count
$st_ct = 0;
$st_seg_ct = 0;
$bct = -1;
//
$loopid = "0";
// paranoia check
if ($rep_d != $seg[11] ) {
$rep_d = $seg[11];
}
if ($sub_d != $seg[16] ) {
$sub_d = $seg[16];
}
//
continue;
}
if ($seg[0] == "IEA") {
//$ar277[$ict]['claim']['IEA02'] = $seg[2];
if ($seg[2] != $isa13) {
echo "ibr_277_read: ISA Mismatch IEA {$seg[2]} vs ISA $isa13 <br />" . PHP_EOL;
}
// consider indexing file array on ISA -- will increase csv table x2 or x3
//$ar_col_hdr = array('mtime', 'directory', 'file_name', 'claim_ct', 'amt_accpt','reject', 'amt_rej');
$ar277[$ict]['file']['filetime'] = $fmtime;
$ar277[$ict]['file']['filename'] = $fname;
$ar277[$ict]['file']['isa13_277'] = $isa13;
$ar277[$ict]['file']['claim_ct'] = $clm_ct;
$ar277[$ict]['file']['amt_accpt'] = $amt_accpt;
$ar277[$ict]['file']['claim_rct'] = $rej_ct;
$ar277[$ict]['file']['amt_rej'] = $amt_rej;
//
$amt_accpt = 0; $amt_rej = 0; $clm_ct = 0; $rej_ct =0;
$isa13 = ''; $fmtime = '';
continue;
}
if ($seg[0] == "GS") {
$fmtime = strval($seg[4]);
$gs04 = strval($seg[4]); // File date
$gs06 = strval($seg[6]); // Group Control Number
//
continue;
}
if ($seg[0] == "ST") {
//
$st01 = $seg[1]; //R 277 Transaction Set Identifier Code
$st02 = $seg[2]; //R Transaction Set Control Number
$st03 = $seg[3]; //R 005010X214 Implementation Convention Reference
//
$st_seg_ct = 1; // the ST segment is included in the segment count
$st_ct++;
$stc_ct = 0; // STC segments (status) at claim level may repear
//
continue;
}
if ($seg[0] == "SE") {
// check segment count
$se01 = $seg[1];
$se02 = $seg[2];
if ($se01 != $st_seg_ct) {
csv_edihist_log ("ibr_277_read: SE segment count mismatch $se01 $st_seg_ct $fname<br />");
}
if ($se02 != $st02) {
csv_edihist_log ("ibr_277_read: SE ST id mismatch $se02 $st02 $fname<br />");
}
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['SE01'] = $se01;
//
continue;
}
if ($seg[0] == "BHT") {
// There may be many BHT's Begin Hierarchical Transaction
$bct++;
// define the HL structure of the transaction set
// 0010 Information Source, Information Receiver, Provider of Service, Subscriber, Dependent
// 0085 (Guess ?? ) Information Source, Information Receiver, Provider of Service, Patient
//['BHT02']['BHT03']['BHT04']['BHT06']
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['BHT01'] = $seg[1]; //R 0010 0085 Hierarchical Structure Code
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['BHT02'] = $seg[2]; //R 08 (status) TRANSACTION SET PURPOSE CODE
// issue with the Originator reference is that it apparently is created by Availity
// and is not our batch file control number
// relating back to our claims will require the encounter number (2000D TRN03)
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['BHT03'] = $seg[3]; //R Originater REFERENCE IDENTIFICATION
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['BHT04'] = $seg[4]; //R CCYYMMDD Transaction Set Creation Date
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['BHT06'] = $seg[6]; //R DG CH TH Transaction Type Code
//['ISA13'] ['ST02'] ['SE01'] ['FILE']
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['FILE'] = $fname; //basename($fp);
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['ISA13'] = $isa13;
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['GS04'] = $gs04;
$ar277[$ict]['claim']['BHT'][$bct]['ENV']['ST02'] = $st02;
//
$hlhaschild = '';
$hlchildof = '';
$hlischild = '';
$hlcount = '';
$hlparent = '';
//
$clm_ct++; // treat each BHT as a claim detail
//
$ky = '';
//
continue;
}
if ($seg[0] == "HL") {
// loop 2000A, 2000B, 2000C, 2000D ??
// set the $ky variable acording to HL level
// HL identified loops can repeat -- that case is not handled
// the values for the last instance only will be recorded in the array
if ($seg[3] == "20") {
// HL*1**20*1~ HL*1 NM1*PR TRN*1 DTP*050 DTP*009
$loopid = "2000A";
$ky = 'A';
} elseif ($seg[3] == "21") {
// HL*2*1*21*1~ NM1*41 TRN*2 STC QTY AMT
$loopid = "2000B";
$ky = 'B';
} elseif ($seg[3] == "19") {
// HL*3*2*19*1~ NM1*85 TRN*1 REF QTY AMT
$loopid = "2000C";
$ky = 'C';
} elseif ($seg[3] == "PT" || $seg[3] == "22") {
// HL*4*3*PT~ patient NM1*QC TRN*2 STC REF REF DTP
// HL*4*3*22*1~ subscriber NM1*IL TRN*2 STC REF REF DTP
$loopid = "2000D";
$ky = 'D';
//
$lp2100D = false;
$lp2200D = false;
$lp2220D = false; // used to indicate SVC segment in D or E
} elseif ($seg[3] == "23") {
$loopid = "2000E";
$ky = 'E';
//
$lp2100E = false;
$lp2200E = false;
$lp2220E = false;
}
// a preliminary scheme for testing HL parent/child relationship
$hlhaschild = (isset($seg[4]) || $seg[4] != '0')? $seg[4] : '0';
$hlchildof = $seg[2];
$hlischild = (!$hlchildof) ? false : (($hlchildof == $hlparent) ? true : false);
$hlparent = ($hlischild) ? $seg[2] : $seg[1];
$hlcount = $seg[1];
//////////////// end parent/child identification
//
$hl_code = $seg[3];
// reset stc segment counter
$stc_ct = -1;
//
continue;
}
// testing
if ($loopid == "2000A" && $ky != 'A') { echo "HL loop mismatch, $hl_code $loopid $ky $segline $fname<br />" . PHP_EOL; }
if ($loopid == "2000B" && $ky != 'B') { echo "HL loop mismatch, $hl_code $loopid $ky $segline $fname<br />" . PHP_EOL; }
if ($loopid == "2000C" && $ky != 'C') { echo "HL loop mismatch, $hl_code $loopid $ky $segline $fname<br />" . PHP_EOL; }
if ($loopid == "2000D" && $ky != 'D') { echo "HL loop mismatch, $hl_code $loopid $ky $segline $fname<br />" . PHP_EOL; }
if ($loopid == "2000E" && $ky != 'E') { echo "HL loop mismatch, $hl_code $loopid $ky $segline $fname<br />" . PHP_EOL; }
//
if ($seg[0] == "NM1") {
// this segment may repeat (rare), but only last NM1 is stored in array
//
// if ($loopid == "2000A") { $loopid = "2100A" } // NM1*PR payer NM1*AY clearinghouse repeat=1
// if ($loopid == "2000B") { $loopid = "2100B" } // NM1*41 submitter NM1*AY clearinghouse repeat=1
// if ($loopid == "2000C") { $loopid = "2100C" } // NM1*1P provider repeat=1
// if ($loopid == "2000D") { $loopid = "2100D" } // NM1*PT patient NM1*IL subscriber/insured NM1*QC repeat=1
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM101'] = $seg[1];
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM102'] = $seg[2];
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM103'] = $seg[3]; // Last name or company name
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM104'] = $seg[4]; // first name or blank
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM105'] = $seg[5]; // middle name or blank
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM107'] = $seg[7]; // name suffix
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM108'] = $seg[8]; // PI|XV|XX|46|FI|SV|24|II|MI Identification Code Qualifier
$ar277[$ict]['claim']['BHT'][$bct][$ky]['NM109'] = $seg[9]; // taxID, NPI, payerID, memberID
//
continue;
}
if ($seg[0] == "TRN") {
// if ($loopid == "2100B") { $loopid = "2200B" } // TRN02 is BHT03 ['B']['TRN02'] (277CA <> 837; 277 <> 276)
// if ($loopid == "2100C") { $loopid = "2200C" }
// if ($loopid == "2100D") { $loopid = "2200D" } // TRN02 is pid-encounter ['D']['TRN02']
$ar277[$ict]['claim']['BHT'][$bct][$ky]['TRN01'] = $seg[1];
$ar277[$ict]['claim']['BHT'][$bct][$ky]['TRN02'] = $seg[2];
//
continue;
}
if ($seg[0] == "DTP") {
if ($seg[1] == "050") { $ar277[$ict]['claim']['BHT'][$bct][$ky]['DTP03050'] = $seg[3]; }
if ($seg[1] == "009") { $ar277[$ict]['claim']['BHT'][$bct][$ky]['DTP03009'] = $seg[3]; }
if ($seg[1] == "472") {
if ($seg[2] == "RD8") {
// service dates
$sp = strpos($seg[3], "-");
$ar277[$ict]['claim']['BHT'][$bct][$ky]['DTP03FROM'] = ($sp) ? substr($seg[3], 0, $sp) : $seg[3];
$ar277[$ict]['claim']['BHT'][$bct][$ky]['DTP03TO'] = ($sp) ? substr($seg[3], $sp+1) : $seg[3];
}
if ($seg[2] == "D8") {
$ar277[$ict]['claim']['BHT'][$bct]['D']['DTP03FROM'] = $seg[3];
}
}
//
continue;
}
if ($seg[0] == "PER") {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['PER01'] = $seg[1]; //R IC Contact Function Code R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['PER02'] = $seg[2]; //S Payer Contact Name
$ar277[$ict]['claim']['BHT'][$bct][$ky]['PER03'] = $seg[3]; //R ED, EM, TE, FX Communication Number Qualifier
$ar277[$ict]['claim']['BHT'][$bct][$ky]['PER04'] = $seg[4]; //R Communication Number
//
continue;
}
// take quantity and amount from 2000B loop since that probably sums to totals for claims in ISA envelope
if ($seg[0] == "QTY" && $loopid == "2000B") {
$ar277[$ict]['claim']['BHT'][$bct]['B']['QTY01'] = $seg[1]; // QTY TOTAL ACCEPTED QUANTITY "90" 2200B QTY TOTAL REJECTED QUANTITY "AA" 2200B
$ar277[$ict]['claim']['BHT'][$bct]['B']['QTY02'] = $seg[2]; // count of accepted or rejected items
// for 277CA -- QTY segment not in 277 segment list
if ($seg[1] == '90') {
$clm_ct += $seg[2];
} else {
$rej_ct += $seg[2];
}
continue;
}
if ($seg[0] == "AMT" && $loopid == "2000B") {
$ar277[$ict]['claim']['BHT'][$bct]['B']['AMT01'] = $seg[1]; // AMT TOTAL ACCEPTED AMOUNT "YU" 2200B AMT TOTAL REJECTED AMOUNT "YY"
$ar277[$ict]['claim']['BHT'][$bct]['B']['AMT02'] = $seg[2]; // quantity, i.e. dollars, accepted or rejected
// for 277CA -- AMT segment not in 277 segment list
if ($seg[1] == 'YU') {
$amt_accpt += $seg[2];
} else {
$amt_rej += $seg[2];
}
//
continue;
}
if ($seg[0] == "STC" && !$lp2220D ) {
// increment stc count, it is reset to -1 at each HL segment (ky change)
$stc_ct++;
//
if ( strpos($seg[1], $sub_d) ) {
$sp = strpos($seg[1], $sub_d);
$stc01 = explode($sub_d, $seg[1]); // A1:20 is expected here
if ( is_array($stc01) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC011'] = isset($stc01[0]) ? $stc01[0] : ""; //STC01-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC012'] = isset($stc01[1]) ? $stc01[1] : ""; //STC01-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC013'] = isset($stc01[2]) ? $stc01[2] : ""; //STC01-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC014'] = isset($stc01[3]) ? $stc01[3] : ""; //STC01-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC01'] = isset($seg[1]) ? $seg[1] : "";
}
}
//
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC02'] = isset($seg[2]) ? $seg[2] : ""; //STC02 Status Information Effective Date DT 08/08/12 R CCYYMMDD
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC03'] = isset($seg[3]) ? $seg[3] : ""; //STC03 Action Code ID 01/02/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC04'] = isset($seg[4]) ? $seg[4] : ""; //STC04 Monetary Amount R 01/18/12 N/U
// no segments beyond STC04 are expected in loop 2200B STC
if ( !isset($seg[5]) ) { continue; }
//
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC05'] = isset($seg[5]) ? $seg[5] : ""; //STC05 Monetary Amount R 01/18/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC06'] = isset($seg[6]) ? $seg[6] : ""; //STC06 Date DT 08/08/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC07'] = isset($seg[7]) ? $seg[7] : ""; //STC07 Payment Method Code ID 03/03/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC08'] = isset($seg[8]) ? $seg[8] : ""; //STC08 Date DT 08/08/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC09'] = isset($seg[9]) ? $seg[9] : ""; //STC09 Check Number AN 01/16/12 N/U
//
if ( !isset($seg[10]) ) { continue; }
//STC10 HEALTH CARE CLAIM STATUS S
if ( isset($seg[10]) ) {
if ( strpos($seg[10], $sub_d) ) {
$stc10 = explode($sub_d, $seg[10]);
if ( is_array($stc01) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC101'] = isset($stc10[0]) ? $stc10[0] : ""; //STC10-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC102'] = isset($stc10[1]) ? $stc10[1] : ""; //STC10-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC103'] = isset($stc10[2]) ? $stc10[2] : ""; //STC10-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC104'] = isset($stc10[3]) ? $stc10[3] : ""; //STC10-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC10'] = $seg[10];
}
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC10'] = isset($seg[10]) ? $seg[10] : "";
}
}
//
//STC11 HEALTH CARE CLAIM STATUS
if ( isset($seg[11]) ) {
if ( strpos($seg[11], $sub_d) ) {
$stc11 = explode($sub_d, $seg[10]);
if ( is_array($stc11) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC111'] = isset($stc11[0]) ? $stc11[0] : ""; //STC11-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC112'] = isset($stc11[1]) ? $stc11[1] : ""; //STC11-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC113'] = isset($stc11[2]) ? $stc11[2] : ""; //STC11-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC114'] = isset($stc11[3]) ? $stc11[3] : ""; //STC11-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
// sub-element detected, but no array -- unexpected
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC11'] = $seg[11];
}
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC11'] = $seg[11];
}
}
//STC12 Free-Form Message Text AN 01/01/64 N/U
if ( isset($seg[12]) ) { $ar277[$ict]['claim']['BHT'][$bct][$ky]['STC'][$stc_ct]['STC12'] = $seg[12]; }
//
}
if ($seg[0] == "REF") {
if ($ky == "C") {
if ($seg[1] == "TJ") { $ar277[$ict]['claim']['BHT'][$bct][$ky]['REFTJ'] = $seg[2]; }
} elseif ($ky == "D") {
// ref, 1K, EJ, D9 will be expected
if ($seg[1] == "1K") {
// REF*1K*<<subscriber number >>~REF02 Payer Claim Control Number AN 1-50 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REF1K'] = $seg[2];
} elseif ($seg[1] == "D9") {
// REF*D9*NA~ clearinghouse ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFD9'] = $seg[2];
} elseif ($seg[1] == "EA") {
// record ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFEA'] = $seg[2];
} elseif ($seg[1] == "BLT") {
// institutional ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFBLT'] = $seg[2];
} elseif ($seg[1] == "EJ") {
// control ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFEJ'] = $seg[2];
} elseif ($seg[1] == "XZ") {
// prescripton ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFXZ'] = $seg[2];
} elseif ($seg[1] == "VV") {
// voucher ID
$ar277[$ict]['claim']['BHT'][$bct][$ky]['REFVV'] = $seg[2];
}
} else {
$refkey = 'REF'.$seg[1];
$ar277[$ict]['claim']['BHT'][$bct][$ky][$refkey] = $seg[1] . ":" . $seg[2]; // qualifier:value
}
//
continue;
}
if ($seg[0] == "SVC") {
// loop 2220D
// set another loop id
//$lp2200D = FALSE;
$lp2220D = TRUE;
//
// SVC SERVICE LINE INFORMATION 1 S 2220D >1
$sbr_svc01 = $seg[1]; // SVC01 COMPOSITE MEDICAL PROCEDURE INDENTIFIER R
$svc01 = explode($sub_d, $seg[1]);
// AD, ER, HC, HP, IV, N4, NU, WK
if ( is_array($stc01) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC011'] = isset($svc01[0]) ? $svc01[0] : ""; //SVC01-1 Product/Service ID Qualifier ID 02/02/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC012'] = isset($svc01[1]) ? $svc01[1] : ""; //SVC01-2 Service Identification Code AN 01/01/48 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC013'] = isset($svc01[2]) ? $svc01[2] : ""; //SVC01-3 Procedure Modifier AN 02/02/12 S
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC014'] = isset($svc01[3]) ? $svc01[3] : ""; //SVC01-4 Procedure Modifier AN 02/02/12 S
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC015'] = isset($svc01[4]) ? $svc01[4] : ""; //SVC01-5 Procedure Modifier AN 02/02/12 S
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC016'] = isset($svc01[5]) ? $svc01[5] : ""; //SVC01-6 Procedure Modifier AN 02/02/12 S
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC017'] = isset($svc01[6]) ? $svc01[6] : ""; //SVC01-7 Description AN 01/01/80 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC01'] = isset($seg[1]) ? $seg[1] : "";
}
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC02'] = isset($seg[2]) ? $seg[2] : ""; //SVC02 Line Item Charge Amount S9(7)V99 R 01/18/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC03'] = isset($seg[2]) ? $seg[3] : ""; //SVC03 Line Item Payment Amount S9(7)V99 R 01/18/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC04'] = isset($seg[2]) ? $seg[4] : ""; //SVC04 Revenue Code AN 01/01/48 S
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC05'] = isset($seg[2]) ? $seg[5] : ""; //SVC05 Quantity R 01/15/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC06'] = isset($seg[2]) ? $seg[6] : ""; //SVC06 COMPOSITE MEDICAL PROCEDURE INDENTIFIER N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['SVC07'] = isset($seg[2]) ? $seg[7] : ""; //SVC07 Units of Service Count S9(3)V9 R 01/15/12 S
//
$svc_ct++;
continue;
}
if ($seg[0] == "STC" && $lp2220D ) {
// loop 2220D
// this is an STC segment following a SVC segment -- status for a particular service
//STC*A1:20*20120217*WQ*65~
if (!isset($ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'])) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'] = array();
}
$svcstc_ct = count($ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC']);
if ( strpos($seg[1], $sub_d) ) {
$sp = strpos($sub_d, $seg[1]);
$stcsvc01 = explode($sub_d, $seg[1]); // 2200D STC01-1 Health Care Claim Status Category Code
if ( is_array($stc01) ) { // "A2" Accept , "A3" Reject, or “R3” Warning 1/30
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC011'] = isset($stc01[0]) ? $stcsvc01[0] : ""; //STC01-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC012'] = isset($stc01[1]) ? $stcsvc01[1] : ""; //STC01-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC013'] = isset($stc01[1]) ? $stcsvc01[1] : ""; //STC01-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC014'] = isset($stc01[1]) ? $stcsvc01[1] : ""; //STC01-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC011'] = isset($seg[1]) ? $seg[1] : "";
}
}
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC02'] = isset($seg[2]) ? $seg[2] : ""; //STC02 Status Information Effective Date DT 08/08/12 R CCYYMMDD
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC03'] = isset($seg[3]) ? $seg[3] : ""; //STC03 Action Code ID 01/02/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC04'] = isset($seg[4]) ? $seg[4] : ""; //STC04 Monetary Amount R 01/18/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC05'] = isset($seg[5]) ? $seg[5] : ""; //STC05 Monetary Amount R 01/18/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC06'] = isset($seg[6]) ? $seg[6] : ""; //STC06 Date DT 08/08/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC07'] = isset($seg[7]) ? $seg[7] : ""; //STC07 Payment Method Code ID 03/03/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC08'] = isset($seg[8]) ? $seg[8] : ""; //STC08 Date DT 08/08/12 N/U
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC09'] = isset($seg[9]) ? $seg[9] : ""; //STC09 Check Number AN 01/16/12 N/U
//STC10 HEALTH CARE CLAIM STATUS S
if ( isset($seg[10]) && strpos($seg[10], $sub_d) ) {
$stc10 = explode($sub_d, $seg[10]);
if ( is_array($stc01) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC101'] = isset($stc10[0]) ? $stc10[0] : ""; //STC10-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC102'] = isset($stc10[1]) ? $stc10[1] : ""; //STC10-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC103'] = isset($stc10[2]) ? $stc10[2] : ""; //STC10-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC104'] = isset($stc10[3]) ? $stc10[3] : ""; //STC10-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC'][$svcstc_ct]['STC101'] = isset($seg[10]) ? $seg[10] : "";
}
}
//
//STC11 HEALTH CARE CLAIM STATUS S
if ( isset($seg[11]) && strpos($seg[11], $sub_d) ) {
$stc11 = explode($sub_d, $seg[10]);
if ( is_array($stc11) ) {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC111'] = isset($stc11[0]) ? $stc11[0] : ""; //STC11-1 Health Care Claim Status Category Code AN 01/30/12 R D0, E
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC112'] = isset($stc11[1]) ? $stc11[1] : ""; //STC11-2 Health Care Claim Status Code AN 01/30/12 R
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC113'] = isset($stc11[2]) ? $stc11[2] : ""; //STC11-3 Entity Identifier Code ID 02/03/12 S 1P
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC114'] = isset($stc11[3]) ? $stc11[3] : ""; //STC11-4 Code List Qualifier Code ID 01/03/12 N/U
} else {
$ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC111'] = isset($seg[11]) ? $seg[11] : "";
}
}
//STC12 Free-Form Message Text AN 01/01/64 N/U
if ( isset($seg[12]) ) { $ar277[$ict]['claim']['BHT'][$bct][$ky]['SVC'][$svc_ct]['STC12'] = $seg[12]; }
//
if( $loopid == "2000D" || $loopid == "2000E") { $svc_ct++; }
//
continue;
//
} // end if ($seg[0] == "STC" && $lp2220D ) in loop 2200D
//
} // end foreach ( )
//
return $ar277;
}
/**
* Process new files for csv table and html output
*
* @uses csv_newfile_list()
* @uses csv_parameters()
* @uses csv_x12_segments()
* @uses ibr_277_parse()
* @uses ibr_277_csv_claim_data()
* @uses csv_write_record()
* @param bool $html_out -- whether to return html output
* @param bool $err_only -- only list claims with errors in output
* @param array $files_ar -- list of new files from upload script
* @return string
*/
function ibr_277_process_new($files_ar = NULL, $html_out = FALSE, $err_only = TRUE) {
//
if ( $files_ar === NULL || !is_array($files_ar) || count($files_ar) == 0) {
$ar_files = csv_newfile_list("f277");
} else {
$ar_files = $files_ar;
//$need_dir = FALSE;
}
//
if ( count($ar_files) == 0 ) {
if($html_out) {
$html_str .= "<p>ibr_277_process_new: no new f277 files <br />";
return $html_str;
}
}
// we have some new ones, verify and get complete path
foreach($ar_files as $fbt) {
$fp = csv_verify_file($fbt, 'f277', false);
if ($fp) { $ar_277files[] = $fp; }
}
//
if (!is_array($ar_277files) || count($ar_277files) == 0 ) {
$html_str = "ibr_277_process_new: no new f277 files found <br />" . PHP_EOL;
return $html_str;
} else {
$f277count = count($ar_277files);
}
// OK, we have some new files
$html_str = "";
$idx = 0;
$chr_c = 0;
$chr_f = 0;
//
foreach ($ar_277files as $file_277) {
// since the newfiles routine is updated, the need_dir test is not necessary
//$path_277 = ($need_dir) ? $dir.DIRECTORY_SEPARATOR.$file_277 : $file_277;
$path_277 = $file_277;
//
$ar_277seg = csv_x12_segments($path_277, "f277", FALSE);
//
if (is_array($ar_277seg) && count($ar_277seg['segments']) ) {
$ar_277_vals = ibr_277_parse($ar_277seg);
if (!$ar_277_vals) {
$html_str .= "failed to get segments for $file_277 <br />" .PHP_EOL;
continue;
}
} else {
$html_str .= "failed to get segments for $file_277 <br />" .PHP_EOL;
continue;
}
// since main array is indexed on isa segment and there may be more
// than one per file, the 'file' csv table has a row for each ISA segment
// and the 'claim' csv table has a row for each BHT segment
foreach($ar_277_vals as $isa) {
$ar_csvf = $isa['file'];
$ar_csvc = ibr_277_csv_claim_data($isa['claim']);
//
if ($html_out) {
$ar_h[$idx]['file'] = $ar_csvf;
$ar_h[$idx]['claim'] = $ar_csvc;
$idx++;