installation bug fix
[openemr.git] / library / edihistory / edih_997_error.php
blobab64759e4928cf6c46d14bce4964501a2f97f9ca
1 <?php
2 /*
3 * edih_997_error.php
4 *
5 * Copyright 2016 Kevin McCormick Longview, Texas
6 *
7 *
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 * @author Kevin McCormick
23 * @link: http://www.open-emr.org
24 * @package OpenEMR
25 * @subpackage ediHistory
28 // codes used in 997/999 files;
29 //require_once './codes/edih_997_codes.php';
31 /**
32 * Look up file name by control number
34 * @param string
35 * @param string
37 * @return string
39 function edih_997_sbmtfile($icn, $filetype) {
41 if ( strlen($icn) == 13 ) {
42 $bticn = substr($icn, 0, 9);
43 $stn = substr($icn, -4);
44 } else {
45 $bticn = $icn;
47 if (is_numeric($filetype)) {
48 $ftp = 'f'.$filetype;
49 } else {
50 $ftp = $filetype;
53 $btfn = csv_file_by_controlnum($ftp, $bticn);
54 $bfullpath = ($btfn) ? csv_check_filepath($btfn, $ftp) : '';
56 return $bfullpath;
59 /**
60 * Extract information on rejected files or transactions
62 * @param object
63 * @return array
65 function edih_997_errdata($obj997) {
67 $segments = $obj997->edih_segments();
68 $delims = $obj997->edih_delimiters();
69 $de = $delims['e'];
70 $ds = $delims['s'];
71 $dr = $delims['r'];
73 $dt = '~';
75 $diag = array();
76 $diag['err'] = array();
77 $iserr = false;
78 $batchfile = '';
79 $idx = -1;
81 foreach($segments as $seg) {
82 $sar = array();
83 if (strncmp($seg, 'TA1'.$de, 4) == 0) {
84 $sar = explode($de, $seg);
86 $sub_icn = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
87 $subdate = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
88 $subtime = (isset($sar[3]) && $sar[3]) ? $sar[3] : '';
89 $ackcode = (isset($sar[4]) && $sar[4]) ? $sar[4] : '';
90 $acknote = (isset($sar[5]) && $sar[5]) ? $sar[5] : '';
93 continue;
95 if (strncmp($seg, 'AK1'.$de, 4) == 0) {
96 $sar = explode($de, $seg);
98 $fg_type = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
99 $fg_id = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
101 continue;
104 if (strncmp($seg, 'AK2'.$de, 4) == 0 || strncmp($seg, 'IK2'.$de, 4) == 0 ) {
105 $sar = explode($de, $seg);
107 $iserr = false;
109 $subtype = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
110 $substn = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
111 // AK2*837*0001
112 continue;
114 if (strncmp($seg, 'AK3'.$de, 4) == 0 || strncmp($seg, 'IK3'.$de, 4) == 0 ) {
115 $sar = explode($de, $seg);
116 //$idx = count($diag);
117 $idx++;
118 $iserr = true;
120 $diag['err'][$idx]['subtype'] = 'f'.$subtype;
121 $diag['err'][$idx]['substn'] = $substn;
123 $diag['err'][$idx]['ik3segid'] = (isset($sar[1])) ? $sar[1] : '';
124 $diag['err'][$idx]['ik3segpos'] = (isset($sar[2])) ? $sar[2] : '';
125 $diag['err'][$idx]['ik3loop'] = (isset($sar[3])) ? $sar[3] : '';
126 $diag['err'][$idx]['ik3code'] = (isset($sar[4])) ? $sar[4] : '';
128 continue;
130 if (strncmp($seg, 'CTX'.$de, 4) == 0) {
131 $sar = explode($de, $seg);
132 if (isset($sar[1]) && strpos($sar[1],'TRIG')) {
133 // CTX*SITUATIONAL TRIGGER*
134 $diag['err'][$idx]['ctxid'] = (isset($sar[2])) ? $sar[2] : '';
135 $diag['err'][$idx]['ctxpos'] = (isset($sar[3])) ? $sar[3] : '';
136 $diag['err'][$idx]['ctxloop'] = (isset($sar[4])) ? $sar[4] : '';
137 $diag['err'][$idx]['ctxelem'] = (isset($sar[5])) ? $sar[5] : '';
138 // $sar[6] Reference in Segment
139 // Data Element Reference Number : Data Element Reference Number Composite
140 } else {
141 // business unit identifier
142 $diag['err'][$idx]['ctxacct'] = (isset($sar[2])) ? $sar[2] : '';
145 continue;
147 if (strncmp($seg, 'AK4'.$de, 4) == 0 || strncmp($seg, 'IK4'.$de, 4) == 0 ) {
148 $sar = explode($de, $seg);
149 $diag['err'][$idx]['ik401'] = (isset($sar[1])) ? $sar[1] : '';
150 $diag['err'][$idx]['ik402'] = (isset($sar[2])) ? $sar[2] : '';
151 $diag['err'][$idx]['ik403'] = (isset($sar[3])) ? $sar[3] : '';
152 $diag['err'][$idx]['ik404'] = (isset($sar[4])) ? $sar[4] : '';
154 continue;
156 if (strncmp($seg, 'AK5'.$de, 4) == 0 || strncmp($seg, 'IK5'.$de, 4) == 0 ) {
157 if ($iserr) {
158 $sar = explode($de, $seg);
159 $diag['err'][$idx]['ik501'] = (isset($sar[1])) ? $sar[1] : '';
160 $diag['err'][$idx]['ik502'] = (isset($sar[2])) ? $sar[2] : '';
161 $diag['err'][$idx]['ik503'] = (isset($sar[3])) ? $sar[3] : '';
162 $diag['err'][$idx]['ik504'] = (isset($sar[4])) ? $sar[4] : '';
163 $diag['err'][$idx]['ik505'] = (isset($sar[5])) ? $sar[5] : '';
165 $iserr = false;
168 continue;
170 if (strncmp($seg, 'AK9'.$de, 4) == 0) {
171 $diag['summary']['sub_icn'] = $sub_icn;
172 $diag['summary']['subtype'] = $subtype;
173 $diag['summary']['subdate'] = $subdate;
174 $diag['summary']['subtime'] = $subtime;
175 $diag['summary']['ackcode'] = $ackcode;
176 $diag['summary']['acknote'] = $acknote;
177 $diag['summary']['fg_type'] = $fg_type;
178 $diag['summary']['fg_id'] = $fg_id;
180 $sar = explode($de, $seg);
181 $diag['summary']['ak901'] = (isset($sar[1])) ? $sar[1] : ''; // AK901 A=Accepted R=Rejected.
182 $diag['summary']['ak902'] = (isset($sar[2])) ? $sar[2] : ''; // AK902 number of transaction sets
183 $diag['summary']['ak903'] = (isset($sar[3])) ? $sar[3] : ''; // AK903 number of transaction sets received by the translator.
184 $diag['summary']['ak904'] = (isset($sar[4])) ? $sar[4] : ''; // AK904 number of transaction sets accepted by the translator.
185 $diag['summary']['ak905'] = (isset($sar[5])) ? $sar[5] : ''; // codes
186 $diag['summary']['ak906'] = (isset($sar[6])) ? $sar[6] : '';
187 $diag['summary']['ak907'] = (isset($sar[7])) ? $sar[7] : '';
188 $diag['summary']['ak908'] = (isset($sar[8])) ? $sar[8] : '';
189 $diag['summary']['ak909'] = (isset($sar[9])) ? $sar[9] : '';
191 continue;
194 return $diag;
198 /**
199 * Create an html report on rejected files or transactions
201 * @uses edih_997_ta1_code()
202 * @uses edih_997_code_text()
203 * @uses edih_rsp_st_match()
205 * @param object
206 * @return array
208 function edih_997_err_report($err_array) {
210 if (!is_array($err_array) || !count($err_array)) {
211 $str_html = "Error: invalid argument for error report";
212 csv_edihist_log('edih_997_err_report: invalid function argument');
213 return $str_html;
216 $str_html = "";
217 $batchfile = "";
219 if (isset($err_array['summary'])) {
220 extract($err_array['summary'], EXTR_OVERWRITE);
222 $str_html .= "<p class='rpt997'>".PHP_EOL;
223 $str_html .= (isset($sub_icn)) ? "<em>Submitted ICN</em> $sub_icn" : "Submitted file unknown";
224 $str_html .= (isset($subdate)) ? " <em>Date</em> ".edih_format_date($subdate) : "";
225 $str_html .= (isset($subtime)) ? " <em>Time</em> $subtime<br />" : "<br />";
226 $str_html .= (isset($ackcode)) ? " TA1 $ackcode : ".edih_997_ta1_code($ackcode)." <br />" : "";
227 $str_html .= (isset($acknote)) ? " TA1 $acknote : ".edih_997_ta1_code($acknote)." <br />".PHP_EOL : "<br />".PHP_EOL;
228 if (isset($fg_type)) {
229 $fgtp = csv_file_type($fg_type);
230 $str_html .= " <em>Functional Group Type</em> $fg_type ($fgtp)";
231 $str_html .= (isset($fg_id)) ? " <em>GS06</em> $fg_id <br />".PHP_EOL : "<br />".PHP_EOL;
234 //$str_html .= "</p>".PHP_EOL;
236 $str_html .= (isset($ak901)) ? "999/997 $ak901 ".edih_997_code_text('ak501',$ak901)."<br />" : "";
237 $str_html .= (isset($ak902)) ? " Transactions: submitted $ak902" : " ";
238 $str_html .= (isset($ak903)) ? " received $ak903" : "";
239 $str_html .= (isset($ak904)) ? " accepted $ak904" : "";
240 $str_html .= (isset($ak905) && $ak905) ? "<br />$ak905 ".edih_997_code_text('ak502',$ak905)."<br />" : "";
241 $str_html .= (isset($ak906) && $ak906) ? $ak906." ".edih_997_code_text('ak502',$ak906)."<br />" : "";
242 $str_html .= (isset($ak907) && $ak907) ? $ak907." ".edih_997_code_text('ak502',$ak907)."<br />" : "";
243 $str_html .= (isset($ak908) && $ak908) ? $ak908." ".edih_997_code_text('ak502',$ak908)."<br />" : "";
244 $str_html .= (isset($ak909) && $ak909) ? $ak909." ".edih_997_code_text('ak502',$ak909)."<br />" : "";
246 $str_html .= "</p>".PHP_EOL;
249 foreach($err_array['err'] as $k=>$v) {
251 $ct = $k + 1;
252 $icn = (isset($sub_icn)) ? $sub_icn : '';
253 $stn = (isset($v['substn'])) ? $v['substn'] : '';
254 $rtp = (isset($v['subtype'])) ? $v['subtype'] : '';
256 $str_html .= "<p class='err997'>".PHP_EOL;
257 $str_html .= "Error $ct ";
258 $str_html .= ($stn) ? "<em>ST</em> $stn <br />" : "<br />";
260 if ($icn && $stn && $rtp) {
261 $trc = sprintf("%s%04d", $icn, $stn);
262 $srch = array('s_val'=>$trc, 's_col'=>4,'r_cols'=>'All');
263 // array('s_val'=>'0024', 's_col'=>9, 'r_cols'=>array(1, 2, 7)),
264 $trn_ar = csv_search_record($rtp, 'claim', $srch);
265 if (is_array($trn_ar) && count($trn_ar)) {
266 //'f837':array('PtName', 'SvcDate', 'CLM01', 'InsLevel', 'BHT03', 'FileName', 'Fee', 'PtPaid', 'Provider' );
267 //'f276':array('PtName', 'SvcDate', 'CLM01', 'ClaimID', 'BHT03', 'FileName', 'Payer', 'Trace'); break;
268 //'f270':array('PtName', 'ReqDate', 'Trace', 'InsBnft', 'BHT03', 'FileName', 'Payer'); break;
269 $pt_name = $trn_ar[0][0]; // $trn_ar['PtName'];
270 $clm01 = ($rtp == 'f837') ? $trn_ar[0][2] : $trn_ar[0][4]; // $trn_ar['CLM01'] : $trn_ar['BHT03'];
271 $svcdate = $trn_ar[0][1]; // ($rtp == 'f270') ? $trn_ar['ReqDate'] : $trn_ar['SvcDate'];
272 $btfn = $trn_ar[0][5]; // $trn_ar['FileName'];
273 $str_html .= "$pt_name $svcdate <em>Trace</em> <a class='rpt' href='edih_main.php?gtbl=claim&fname=$btfn&ftype=$rtp&pid=$clm01&fmt=seg'>$clm01</a> <br />".PHP_EOL;
274 } else {
275 $str_html .= "Unable to locate transaction <em>Trace</em> $trc <br />".PHP_EOL;
277 } else {
278 $str_html .= "Unable to trace, did not get all of icn, type, and st number <br />".PHP_EOL;
281 $str_html .= (isset($v['ctxacct'])) ? "<em>Transaction ID</em> ".$v['ctxacct'] : "";
282 $str_html .= (isset($v['ik3segid'])) ? " Segment <em>ID</em> ".$v['ik3segid'] : "";
283 $str_html .= (isset($v['ik3segpos'])) ? " <em>Position</em> ".$v['ik3segpos'] : "";
284 $str_html .= (isset($v['ik3loop'])) ? " <em>Loop</em> ".$v['ik3loop'] : "";
285 $str_html .= (isset($v['ik3code'])) ? "<br /> <em>Code</em> ".$v['ik3code']." ".edih_997_code_text('ak304',$v['ik3code'])."<br />" : "<br />";
287 $str_html .= (isset($v['ctxid'])) ? "Situational ".PHP_EOL."<em>Segment</em> ".$v['ctxid'] : "";
288 $str_html .= (isset($v['ctxpos'])) ? " <em>Position</em> ".$v['ctxpos'] : "";
289 $str_html .= (isset($v['ctxloop'])) ? " <em>Position</em> ".$v['ctxloop'] : "";
290 $str_html .= (isset($v['ctxelem'])) ? " <em>Element</em> ".$v['ctxelem']."<br />".PHP_EOL : PHP_EOL;
292 $str_html .= (isset($v['ik401'])) ? "Data Element <em>element</em> ".$v['ik401'] : "";
293 $str_html .= (isset($v['ik402'])) ? " <em>ref</em> ".$v['ik402'] : "";
294 $str_html .= (isset($v['ik404'])) ? " <em>data</em> ".$v['ik404'] : "";
295 $str_html .= (isset($v['ik403'])) ? "<br /> <em>code</em> ".$v['ik403']." ".edih_997_code_text('ak403',$v['ik403'])."<br />" : "<br />";
297 $str_html .= (isset($v['ik501']) && $v['ik501']) ? "<em>Status</em> ".$v['ik501']." ".edih_997_code_text('ak501',$v['ik501'])."<br />" : "";
298 $str_html .= (isset($v['ik502']) && $v['ik502']) ? " <em>code</em> ".$v['ik502']." ".edih_997_code_text('ak502',$v['ik502'])."<br />" : "";
299 $str_html .= (isset($v['ik503']) && $v['ik503']) ? " <em>code</em> ".$v['ik503']." ".edih_997_code_text('ak502',$v['ik503'])."<br />" : "";
300 $str_html .= (isset($v['ik504']) && $v['ik504']) ? " <em>code</em> ".$v['ik504']." ".edih_997_code_text('ak502',$v['ik504'])."<br />" : "";
301 $str_html .= (isset($v['ik505']) && $v['ik505']) ? " <em>code</em> ".$v['ik505']." ".edih_997_code_text('ak502',$v['ik505'])."<br />" : "";
303 $str_html .= "</p>".PHP_EOL;
305 return $str_html;
309 * Main function in this script
311 * @uses csv_check_x12_obj()
312 * @uses edih_997_errdata()
313 * @uses edih_997_err_report()
315 * @param string
316 * @return string
318 function edih_997_error($filepath) {
320 $html_str = '';
322 $obj997 = csv_check_x12_obj($filepath, 'f997');
323 if ($obj997 && ('edih_x12_file' == get_class($obj997))) {
324 $data = edih_997_errdata($obj997);
325 $html_str .= edih_997_err_report($data);
326 } else {
327 $html_str .= "<p>Error: invalid file path</p>".PHP_EOL;
328 csv_edihist_log("edih_997_error: invalid file path $filepath");
330 return $html_str;