Eye module improvements with other minor improvements
[openemr.git] / library / edihistory / edih_997_error.php
blob74d2a8716aeb536e16a3f9d661b00add5343b4b7
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)
42 if (strlen($icn) == 13) {
43 $bticn = substr($icn, 0, 9);
44 $stn = substr($icn, -4);
45 } else {
46 $bticn = $icn;
49 if (is_numeric($filetype)) {
50 $ftp = 'f'.$filetype;
51 } else {
52 $ftp = $filetype;
56 $btfn = csv_file_by_controlnum($ftp, $bticn);
57 $bfullpath = ($btfn) ? csv_check_filepath($btfn, $ftp) : '';
59 return $bfullpath;
62 /**
63 * Extract information on rejected files or transactions
65 * @param object
66 * @return array
68 function edih_997_errdata($obj997)
71 $segments = $obj997->edih_segments();
72 $delims = $obj997->edih_delimiters();
73 $de = $delims['e'];
74 $ds = $delims['s'];
75 $dr = $delims['r'];
77 $dt = '~';
79 $diag = array();
80 $diag['err'] = array();
81 $iserr = false;
82 $batchfile = '';
83 $idx = -1;
85 foreach ($segments as $seg) {
86 $sar = array();
87 if (strncmp($seg, 'TA1'.$de, 4) == 0) {
88 $sar = explode($de, $seg);
90 $sub_icn = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
91 $subdate = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
92 $subtime = (isset($sar[3]) && $sar[3]) ? $sar[3] : '';
93 $ackcode = (isset($sar[4]) && $sar[4]) ? $sar[4] : '';
94 $acknote = (isset($sar[5]) && $sar[5]) ? $sar[5] : '';
97 continue;
100 if (strncmp($seg, 'AK1'.$de, 4) == 0) {
101 $sar = explode($de, $seg);
103 $fg_type = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
104 $fg_id = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
106 continue;
109 if (strncmp($seg, 'AK2'.$de, 4) == 0 || strncmp($seg, 'IK2'.$de, 4) == 0) {
110 $sar = explode($de, $seg);
112 $iserr = false;
114 $subtype = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
115 $substn = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
116 // AK2*837*0001
117 continue;
120 if (strncmp($seg, 'AK3'.$de, 4) == 0 || strncmp($seg, 'IK3'.$de, 4) == 0) {
121 $sar = explode($de, $seg);
122 //$idx = count($diag);
123 $idx++;
124 $iserr = true;
126 $diag['err'][$idx]['subtype'] = 'f'.$subtype;
127 $diag['err'][$idx]['substn'] = $substn;
129 $diag['err'][$idx]['ik3segid'] = (isset($sar[1])) ? $sar[1] : '';
130 $diag['err'][$idx]['ik3segpos'] = (isset($sar[2])) ? $sar[2] : '';
131 $diag['err'][$idx]['ik3loop'] = (isset($sar[3])) ? $sar[3] : '';
132 $diag['err'][$idx]['ik3code'] = (isset($sar[4])) ? $sar[4] : '';
134 continue;
137 if (strncmp($seg, 'CTX'.$de, 4) == 0) {
138 $sar = explode($de, $seg);
139 if (isset($sar[1]) && strpos($sar[1], 'TRIG')) {
140 // CTX*SITUATIONAL TRIGGER*
141 $diag['err'][$idx]['ctxid'] = (isset($sar[2])) ? $sar[2] : '';
142 $diag['err'][$idx]['ctxpos'] = (isset($sar[3])) ? $sar[3] : '';
143 $diag['err'][$idx]['ctxloop'] = (isset($sar[4])) ? $sar[4] : '';
144 $diag['err'][$idx]['ctxelem'] = (isset($sar[5])) ? $sar[5] : '';
145 // $sar[6] Reference in Segment
146 // Data Element Reference Number : Data Element Reference Number Composite
147 } else {
148 // business unit identifier
149 $diag['err'][$idx]['ctxacct'] = (isset($sar[2])) ? $sar[2] : '';
153 continue;
156 if (strncmp($seg, 'AK4'.$de, 4) == 0 || strncmp($seg, 'IK4'.$de, 4) == 0) {
157 $sar = explode($de, $seg);
158 $diag['err'][$idx]['ik401'] = (isset($sar[1])) ? $sar[1] : '';
159 $diag['err'][$idx]['ik402'] = (isset($sar[2])) ? $sar[2] : '';
160 $diag['err'][$idx]['ik403'] = (isset($sar[3])) ? $sar[3] : '';
161 $diag['err'][$idx]['ik404'] = (isset($sar[4])) ? $sar[4] : '';
163 continue;
166 if (strncmp($seg, 'AK5'.$de, 4) == 0 || strncmp($seg, 'IK5'.$de, 4) == 0) {
167 if ($iserr) {
168 $sar = explode($de, $seg);
169 $diag['err'][$idx]['ik501'] = (isset($sar[1])) ? $sar[1] : '';
170 $diag['err'][$idx]['ik502'] = (isset($sar[2])) ? $sar[2] : '';
171 $diag['err'][$idx]['ik503'] = (isset($sar[3])) ? $sar[3] : '';
172 $diag['err'][$idx]['ik504'] = (isset($sar[4])) ? $sar[4] : '';
173 $diag['err'][$idx]['ik505'] = (isset($sar[5])) ? $sar[5] : '';
175 $iserr = false;
179 continue;
182 if (strncmp($seg, 'AK9'.$de, 4) == 0) {
183 $diag['summary']['sub_icn'] = $sub_icn;
184 $diag['summary']['subtype'] = $subtype;
185 $diag['summary']['subdate'] = $subdate;
186 $diag['summary']['subtime'] = $subtime;
187 $diag['summary']['ackcode'] = $ackcode;
188 $diag['summary']['acknote'] = $acknote;
189 $diag['summary']['fg_type'] = $fg_type;
190 $diag['summary']['fg_id'] = $fg_id;
192 $sar = explode($de, $seg);
193 $diag['summary']['ak901'] = (isset($sar[1])) ? $sar[1] : ''; // AK901 A=Accepted R=Rejected.
194 $diag['summary']['ak902'] = (isset($sar[2])) ? $sar[2] : ''; // AK902 number of transaction sets
195 $diag['summary']['ak903'] = (isset($sar[3])) ? $sar[3] : ''; // AK903 number of transaction sets received by the translator.
196 $diag['summary']['ak904'] = (isset($sar[4])) ? $sar[4] : ''; // AK904 number of transaction sets accepted by the translator.
197 $diag['summary']['ak905'] = (isset($sar[5])) ? $sar[5] : ''; // codes
198 $diag['summary']['ak906'] = (isset($sar[6])) ? $sar[6] : '';
199 $diag['summary']['ak907'] = (isset($sar[7])) ? $sar[7] : '';
200 $diag['summary']['ak908'] = (isset($sar[8])) ? $sar[8] : '';
201 $diag['summary']['ak909'] = (isset($sar[9])) ? $sar[9] : '';
203 continue;
207 return $diag;
212 * Create an html report on rejected files or transactions
214 * @uses edih_997_ta1_code()
215 * @uses edih_997_code_text()
216 * @uses edih_rsp_st_match()
218 * @param object
219 * @return array
221 function edih_997_err_report($err_array)
224 if (!is_array($err_array) || !count($err_array)) {
225 $str_html = "Error: invalid argument for error report";
226 csv_edihist_log('edih_997_err_report: invalid function argument');
227 return $str_html;
231 $str_html = "";
232 $batchfile = "";
234 if (isset($err_array['summary'])) {
235 extract($err_array['summary'], EXTR_OVERWRITE);
237 $str_html .= "<p class='rpt997'>".PHP_EOL;
238 $str_html .= (isset($sub_icn)) ? "<em>Submitted ICN</em> $sub_icn" : "Submitted file unknown";
239 $str_html .= (isset($subdate)) ? " <em>Date</em> ".edih_format_date($subdate) : "";
240 $str_html .= (isset($subtime)) ? " <em>Time</em> $subtime<br />" : "<br />";
241 $str_html .= (isset($ackcode)) ? " TA1 $ackcode : ".edih_997_ta1_code($ackcode)." <br />" : "";
242 $str_html .= (isset($acknote)) ? " TA1 $acknote : ".edih_997_ta1_code($acknote)." <br />".PHP_EOL : "<br />".PHP_EOL;
243 if (isset($fg_type)) {
244 $fgtp = csv_file_type($fg_type);
245 $str_html .= " <em>Functional Group Type</em> $fg_type ($fgtp)";
246 $str_html .= (isset($fg_id)) ? " <em>GS06</em> $fg_id <br />".PHP_EOL : "<br />".PHP_EOL;
250 //$str_html .= "</p>".PHP_EOL;
252 $str_html .= (isset($ak901)) ? "999/997 $ak901 ".edih_997_code_text('ak501', $ak901)."<br />" : "";
253 $str_html .= (isset($ak902)) ? " Transactions: submitted $ak902" : " ";
254 $str_html .= (isset($ak903)) ? " received $ak903" : "";
255 $str_html .= (isset($ak904)) ? " accepted $ak904" : "";
256 $str_html .= (isset($ak905) && $ak905) ? "<br />$ak905 ".edih_997_code_text('ak502', $ak905)."<br />" : "";
257 $str_html .= (isset($ak906) && $ak906) ? $ak906." ".edih_997_code_text('ak502', $ak906)."<br />" : "";
258 $str_html .= (isset($ak907) && $ak907) ? $ak907." ".edih_997_code_text('ak502', $ak907)."<br />" : "";
259 $str_html .= (isset($ak908) && $ak908) ? $ak908." ".edih_997_code_text('ak502', $ak908)."<br />" : "";
260 $str_html .= (isset($ak909) && $ak909) ? $ak909." ".edih_997_code_text('ak502', $ak909)."<br />" : "";
262 $str_html .= "</p>".PHP_EOL;
266 foreach ($err_array['err'] as $k => $v) {
268 $ct = $k + 1;
269 $icn = (isset($sub_icn)) ? $sub_icn : '';
270 $stn = (isset($v['substn'])) ? $v['substn'] : '';
271 $rtp = (isset($v['subtype'])) ? $v['subtype'] : '';
273 $str_html .= "<p class='err997'>".PHP_EOL;
274 $str_html .= "Error $ct ";
275 $str_html .= ($stn) ? "<em>ST</em> $stn <br />" : "<br />";
277 if ($icn && $stn && $rtp) {
278 $trc = sprintf("%s%04d", $icn, $stn);
279 $srch = array('s_val'=>$trc, 's_col'=>4,'r_cols'=>'All');
280 // array('s_val'=>'0024', 's_col'=>9, 'r_cols'=>array(1, 2, 7)),
281 $trn_ar = csv_search_record($rtp, 'claim', $srch);
282 if (is_array($trn_ar) && count($trn_ar)) {
283 //'f837':array('PtName', 'SvcDate', 'CLM01', 'InsLevel', 'BHT03', 'FileName', 'Fee', 'PtPaid', 'Provider' );
284 //'f276':array('PtName', 'SvcDate', 'CLM01', 'ClaimID', 'BHT03', 'FileName', 'Payer', 'Trace'); break;
285 //'f270':array('PtName', 'ReqDate', 'Trace', 'InsBnft', 'BHT03', 'FileName', 'Payer'); break;
286 $pt_name = $trn_ar[0][0]; // $trn_ar['PtName'];
287 $clm01 = ($rtp == 'f837') ? $trn_ar[0][2] : $trn_ar[0][4]; // $trn_ar['CLM01'] : $trn_ar['BHT03'];
288 $svcdate = $trn_ar[0][1]; // ($rtp == 'f270') ? $trn_ar['ReqDate'] : $trn_ar['SvcDate'];
289 $btfn = $trn_ar[0][5]; // $trn_ar['FileName'];
290 $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;
291 } else {
292 $str_html .= "Unable to locate transaction <em>Trace</em> $trc <br />".PHP_EOL;
294 } else {
295 $str_html .= "Unable to trace, did not get all of icn, type, and st number <br />".PHP_EOL;
299 $str_html .= (isset($v['ctxacct'])) ? "<em>Transaction ID</em> ".$v['ctxacct'] : "";
300 $str_html .= (isset($v['ik3segid'])) ? " Segment <em>ID</em> ".$v['ik3segid'] : "";
301 $str_html .= (isset($v['ik3segpos'])) ? " <em>Position</em> ".$v['ik3segpos'] : "";
302 $str_html .= (isset($v['ik3loop'])) ? " <em>Loop</em> ".$v['ik3loop'] : "";
303 $str_html .= (isset($v['ik3code'])) ? "<br /> <em>Code</em> ".$v['ik3code']." ".edih_997_code_text('ak304', $v['ik3code'])."<br />" : "<br />";
305 $str_html .= (isset($v['ctxid'])) ? "Situational ".PHP_EOL."<em>Segment</em> ".$v['ctxid'] : "";
306 $str_html .= (isset($v['ctxpos'])) ? " <em>Position</em> ".$v['ctxpos'] : "";
307 $str_html .= (isset($v['ctxloop'])) ? " <em>Position</em> ".$v['ctxloop'] : "";
308 $str_html .= (isset($v['ctxelem'])) ? " <em>Element</em> ".$v['ctxelem']."<br />".PHP_EOL : PHP_EOL;
310 $str_html .= (isset($v['ik401'])) ? "Data Element <em>element</em> ".$v['ik401'] : "";
311 $str_html .= (isset($v['ik402'])) ? " <em>ref</em> ".$v['ik402'] : "";
312 $str_html .= (isset($v['ik404'])) ? " <em>data</em> ".$v['ik404'] : "";
313 $str_html .= (isset($v['ik403'])) ? "<br /> <em>code</em> ".$v['ik403']." ".edih_997_code_text('ak403', $v['ik403'])."<br />" : "<br />";
315 $str_html .= (isset($v['ik501']) && $v['ik501']) ? "<em>Status</em> ".$v['ik501']." ".edih_997_code_text('ak501', $v['ik501'])."<br />" : "";
316 $str_html .= (isset($v['ik502']) && $v['ik502']) ? " <em>code</em> ".$v['ik502']." ".edih_997_code_text('ak502', $v['ik502'])."<br />" : "";
317 $str_html .= (isset($v['ik503']) && $v['ik503']) ? " <em>code</em> ".$v['ik503']." ".edih_997_code_text('ak502', $v['ik503'])."<br />" : "";
318 $str_html .= (isset($v['ik504']) && $v['ik504']) ? " <em>code</em> ".$v['ik504']." ".edih_997_code_text('ak502', $v['ik504'])."<br />" : "";
319 $str_html .= (isset($v['ik505']) && $v['ik505']) ? " <em>code</em> ".$v['ik505']." ".edih_997_code_text('ak502', $v['ik505'])."<br />" : "";
321 $str_html .= "</p>".PHP_EOL;
324 return $str_html;
328 * Main function in this script
330 * @uses csv_check_x12_obj()
331 * @uses edih_997_errdata()
332 * @uses edih_997_err_report()
334 * @param string
335 * @return string
337 function edih_997_error($filepath)
340 $html_str = '';
342 $obj997 = csv_check_x12_obj($filepath, 'f997');
343 if ($obj997 && ('edih_x12_file' == get_class($obj997))) {
344 $data = edih_997_errdata($obj997);
345 $html_str .= edih_997_err_report($data);
346 } else {
347 $html_str .= "<p>Error: invalid file path</p>".PHP_EOL;
348 csv_edihist_log("edih_997_error: invalid file path $filepath");
351 return $html_str;