dev ldap fixes (#3914)
[openemr.git] / library / edihistory / edih_997_error.php
blobe485a18223c37cf7aa31793f914ee368321c8dff
1 <?php
3 /*
4 * edih_997_error.php
6 * Copyright 2016 Kevin McCormick Longview, Texas
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; version 3 or later. You should have
17 * received a copy of the GNU General Public License along with this program;
18 * if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * <http://opensource.org/licenses/gpl-license.php>
23 * @author Kevin McCormick
24 * @link: https://www.open-emr.org
25 * @package OpenEMR
26 * @subpackage ediHistory
29 use OpenEMR\Common\Csrf\CsrfUtils;
31 // codes used in 997/999 files;
32 //require_once './codes/edih_997_codes.php';
34 /**
35 * Look up file name by control number
37 * @param string
38 * @param string
40 * @return string
42 function edih_997_sbmtfile($icn, $filetype)
45 if (strlen($icn) == 13) {
46 $bticn = substr($icn, 0, 9);
47 $stn = substr($icn, -4);
48 } else {
49 $bticn = $icn;
52 if (is_numeric($filetype)) {
53 $ftp = 'f' . $filetype;
54 } else {
55 $ftp = $filetype;
59 $btfn = csv_file_by_controlnum($ftp, $bticn);
60 $bfullpath = ($btfn) ? csv_check_filepath($btfn, $ftp) : '';
62 return $bfullpath;
65 /**
66 * Extract information on rejected files or transactions
68 * @param object
69 * @return array
71 function edih_997_errdata($obj997)
74 $segments = $obj997->edih_segments();
75 $delims = $obj997->edih_delimiters();
76 $de = $delims['e'];
77 $ds = $delims['s'];
78 $dr = $delims['r'];
80 $dt = '~';
82 $diag = array();
83 $diag['err'] = array();
84 $iserr = false;
85 $batchfile = '';
86 $idx = -1;
88 foreach ($segments as $seg) {
89 $sar = array();
90 if (strncmp($seg, 'TA1' . $de, 4) == 0) {
91 $sar = explode($de, $seg);
93 $sub_icn = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
94 $subdate = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
95 $subtime = (isset($sar[3]) && $sar[3]) ? $sar[3] : '';
96 $ackcode = (isset($sar[4]) && $sar[4]) ? $sar[4] : '';
97 $acknote = (isset($sar[5]) && $sar[5]) ? $sar[5] : '';
100 continue;
103 if (strncmp($seg, 'AK1' . $de, 4) == 0) {
104 $sar = explode($de, $seg);
106 $fg_type = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
107 $fg_id = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
109 continue;
112 if (strncmp($seg, 'AK2' . $de, 4) == 0 || strncmp($seg, 'IK2' . $de, 4) == 0) {
113 $sar = explode($de, $seg);
115 $iserr = false;
117 $subtype = (isset($sar[1]) && $sar[1]) ? $sar[1] : '';
118 $substn = (isset($sar[2]) && $sar[2]) ? $sar[2] : '';
119 // AK2*837*0001
120 continue;
123 if (strncmp($seg, 'AK3' . $de, 4) == 0 || strncmp($seg, 'IK3' . $de, 4) == 0) {
124 $sar = explode($de, $seg);
125 //$idx = count($diag);
126 $idx++;
127 $iserr = true;
129 $diag['err'][$idx]['subtype'] = 'f' . $subtype;
130 $diag['err'][$idx]['substn'] = $substn;
132 $diag['err'][$idx]['ik3segid'] = (isset($sar[1])) ? $sar[1] : '';
133 $diag['err'][$idx]['ik3segpos'] = (isset($sar[2])) ? $sar[2] : '';
134 $diag['err'][$idx]['ik3loop'] = (isset($sar[3])) ? $sar[3] : '';
135 $diag['err'][$idx]['ik3code'] = (isset($sar[4])) ? $sar[4] : '';
137 continue;
140 if (strncmp($seg, 'CTX' . $de, 4) == 0) {
141 $sar = explode($de, $seg);
142 if (isset($sar[1]) && strpos($sar[1], 'TRIG')) {
143 // CTX*SITUATIONAL TRIGGER*
144 $diag['err'][$idx]['ctxid'] = (isset($sar[2])) ? $sar[2] : '';
145 $diag['err'][$idx]['ctxpos'] = (isset($sar[3])) ? $sar[3] : '';
146 $diag['err'][$idx]['ctxloop'] = (isset($sar[4])) ? $sar[4] : '';
147 $diag['err'][$idx]['ctxelem'] = (isset($sar[5])) ? $sar[5] : '';
148 // $sar[6] Reference in Segment
149 // Data Element Reference Number : Data Element Reference Number Composite
150 } else {
151 // business unit identifier
152 $diag['err'][$idx]['ctxacct'] = (isset($sar[2])) ? $sar[2] : '';
156 continue;
159 if (strncmp($seg, 'AK4' . $de, 4) == 0 || strncmp($seg, 'IK4' . $de, 4) == 0) {
160 $sar = explode($de, $seg);
161 $diag['err'][$idx]['ik401'] = (isset($sar[1])) ? $sar[1] : '';
162 $diag['err'][$idx]['ik402'] = (isset($sar[2])) ? $sar[2] : '';
163 $diag['err'][$idx]['ik403'] = (isset($sar[3])) ? $sar[3] : '';
164 $diag['err'][$idx]['ik404'] = (isset($sar[4])) ? $sar[4] : '';
166 continue;
169 if (strncmp($seg, 'AK5' . $de, 4) == 0 || strncmp($seg, 'IK5' . $de, 4) == 0) {
170 if ($iserr) {
171 $sar = explode($de, $seg);
172 $diag['err'][$idx]['ik501'] = (isset($sar[1])) ? $sar[1] : '';
173 $diag['err'][$idx]['ik502'] = (isset($sar[2])) ? $sar[2] : '';
174 $diag['err'][$idx]['ik503'] = (isset($sar[3])) ? $sar[3] : '';
175 $diag['err'][$idx]['ik504'] = (isset($sar[4])) ? $sar[4] : '';
176 $diag['err'][$idx]['ik505'] = (isset($sar[5])) ? $sar[5] : '';
178 $iserr = false;
182 continue;
185 if (strncmp($seg, 'AK9' . $de, 4) == 0) {
186 $diag['summary']['sub_icn'] = $sub_icn;
187 $diag['summary']['subtype'] = $subtype;
188 $diag['summary']['subdate'] = $subdate;
189 $diag['summary']['subtime'] = $subtime;
190 $diag['summary']['ackcode'] = $ackcode;
191 $diag['summary']['acknote'] = $acknote;
192 $diag['summary']['fg_type'] = $fg_type;
193 $diag['summary']['fg_id'] = $fg_id;
195 $sar = explode($de, $seg);
196 $diag['summary']['ak901'] = (isset($sar[1])) ? $sar[1] : ''; // AK901 A=Accepted R=Rejected.
197 $diag['summary']['ak902'] = (isset($sar[2])) ? $sar[2] : ''; // AK902 number of transaction sets
198 $diag['summary']['ak903'] = (isset($sar[3])) ? $sar[3] : ''; // AK903 number of transaction sets received by the translator.
199 $diag['summary']['ak904'] = (isset($sar[4])) ? $sar[4] : ''; // AK904 number of transaction sets accepted by the translator.
200 $diag['summary']['ak905'] = (isset($sar[5])) ? $sar[5] : ''; // codes
201 $diag['summary']['ak906'] = (isset($sar[6])) ? $sar[6] : '';
202 $diag['summary']['ak907'] = (isset($sar[7])) ? $sar[7] : '';
203 $diag['summary']['ak908'] = (isset($sar[8])) ? $sar[8] : '';
204 $diag['summary']['ak909'] = (isset($sar[9])) ? $sar[9] : '';
206 continue;
210 return $diag;
215 * Create an html report on rejected files or transactions
217 * @uses edih_997_ta1_code()
218 * @uses edih_997_code_text()
219 * @uses edih_rsp_st_match()
221 * @param object
222 * @return array
224 function edih_997_err_report($err_array)
227 if (!is_array($err_array) || !count($err_array)) {
228 $str_html = "Error: invalid argument for error report";
229 csv_edihist_log('edih_997_err_report: invalid function argument');
230 return $str_html;
234 $str_html = "";
235 $batchfile = "";
237 if (isset($err_array['summary'])) {
238 extract($err_array['summary'], EXTR_OVERWRITE);
240 $str_html .= "<p class='rpt997'>" . PHP_EOL;
241 $str_html .= (isset($sub_icn)) ? "<em>Submitted ICN</em>" . text($sub_icn) : "Submitted file unknown";
242 $str_html .= (isset($subdate)) ? " <em>Date</em> " . text(edih_format_date($subdate)) : "";
243 $str_html .= (isset($subtime)) ? " <em>Time</em> " . text($subtime) . "<br />" : "<br />";
244 $str_html .= (isset($ackcode)) ? " TA1 $ackcode : " . text(edih_997_ta1_code($ackcode)) . " <br />" : "";
245 $str_html .= (isset($acknote)) ? " TA1 $acknote : " . text(edih_997_ta1_code($acknote)) . " <br />" . PHP_EOL : "<br />" . PHP_EOL;
246 if (isset($fg_type)) {
247 $fgtp = csv_file_type($fg_type);
248 $str_html .= " <em>Functional Group Type</em> " . text($fg_type) . " (" . text($fgtp) . ")";
249 $str_html .= (isset($fg_id)) ? " <em>GS06</em> " . text($fg_id) . " <br />" . PHP_EOL : "<br />" . PHP_EOL;
253 //$str_html .= "</p>".PHP_EOL;
255 $str_html .= (isset($ak901)) ? "999/997 $ak901 " . text(edih_997_code_text('ak501', $ak901)) . "<br />" : "";
256 $str_html .= (isset($ak902)) ? " Transactions: submitted $ak902" : " ";
257 $str_html .= (isset($ak903)) ? " received $ak903" : "";
258 $str_html .= (isset($ak904)) ? " accepted $ak904" : "";
259 $str_html .= (isset($ak905) && $ak905) ? "<br />$ak905 " . text(edih_997_code_text('ak502', $ak905)) . "<br />" : "";
260 $str_html .= (isset($ak906) && $ak906) ? $ak906 . " " . text(edih_997_code_text('ak502', $ak906)) . "<br />" : "";
261 $str_html .= (isset($ak907) && $ak907) ? $ak907 . " " . text(edih_997_code_text('ak502', $ak907)) . "<br />" : "";
262 $str_html .= (isset($ak908) && $ak908) ? $ak908 . " " . text(edih_997_code_text('ak502', $ak908)) . "<br />" : "";
263 $str_html .= (isset($ak909) && $ak909) ? $ak909 . " " . text(edih_997_code_text('ak502', $ak909)) . "<br />" : "";
265 $str_html .= "</p>" . PHP_EOL;
269 foreach ($err_array['err'] as $k => $v) {
271 $ct = $k + 1;
272 $icn = (isset($sub_icn)) ? $sub_icn : '';
273 $stn = (isset($v['substn'])) ? $v['substn'] : '';
274 $rtp = (isset($v['subtype'])) ? $v['subtype'] : '';
276 $str_html .= "<p class='err997'>" . PHP_EOL;
277 $str_html .= "Error " . text($ct) . " ";
278 $str_html .= ($stn) ? "<em>ST</em> " . text($stn) . " <br />" : "<br />";
280 if ($icn && $stn && $rtp) {
281 $trc = sprintf("%s%04d", $icn, $stn);
282 $srch = array('s_val' => $trc, 's_col' => 4,'r_cols' => 'All');
283 // array('s_val'=>'0024', 's_col'=>9, 'r_cols'=>array(1, 2, 7)),
284 $trn_ar = csv_search_record($rtp, 'claim', $srch);
285 if (is_array($trn_ar) && count($trn_ar)) {
286 //'f837':array('PtName', 'SvcDate', 'CLM01', 'InsLevel', 'BHT03', 'FileName', 'Fee', 'PtPaid', 'Provider' );
287 //'f276':array('PtName', 'SvcDate', 'CLM01', 'ClaimID', 'BHT03', 'FileName', 'Payer', 'Trace'); break;
288 //'f270':array('PtName', 'ReqDate', 'Trace', 'InsBnft', 'BHT03', 'FileName', 'Payer'); break;
289 $pt_name = $trn_ar[0][0]; // $trn_ar['PtName'];
290 $clm01 = ($rtp == 'f837') ? $trn_ar[0][2] : $trn_ar[0][4]; // $trn_ar['CLM01'] : $trn_ar['BHT03'];
291 $svcdate = $trn_ar[0][1]; // ($rtp == 'f270') ? $trn_ar['ReqDate'] : $trn_ar['SvcDate'];
292 $btfn = $trn_ar[0][5]; // $trn_ar['FileName'];
293 $str_html .= text($pt_name) . " " . text($svcdate) . " <em>Trace</em> <a class='rpt' href='edih_main.php?gtbl=claim&fname=" . attr_url($btfn) . "&ftype=" . attr_url($rtp) . "&pid=" . attr_url($clm01) . "&fmt=seg&csrf_token_form=" . attr_url(CsrfUtils::collectCsrfToken()) . "'>" . text($clm01) . "</a> <br />" . PHP_EOL;
294 } else {
295 $str_html .= "Unable to locate transaction <em>Trace</em> " . text($trc) . " <br />" . PHP_EOL;
297 } else {
298 $str_html .= "Unable to trace, did not get all of icn, type, and st number <br />" . PHP_EOL;
302 $str_html .= (isset($v['ctxacct'])) ? "<em>Transaction ID</em> " . text($v['ctxacct']) : "";
303 $str_html .= (isset($v['ik3segid'])) ? " Segment <em>ID</em> " . text($v['ik3segid']) : "";
304 $str_html .= (isset($v['ik3segpos'])) ? " <em>Position</em> " . text($v['ik3segpos']) : "";
305 $str_html .= (isset($v['ik3loop'])) ? " <em>Loop</em> " . text($v['ik3loop']) : "";
306 $str_html .= (isset($v['ik3code'])) ? "<br /> <em>Code</em> " . text($v['ik3code']) . " " . text(edih_997_code_text('ak304', $v['ik3code'])) . "<br />" : "<br />";
308 $str_html .= (isset($v['ctxid'])) ? "Situational " . PHP_EOL . "<em>Segment</em> " . text($v['ctxid']) : "";
309 $str_html .= (isset($v['ctxpos'])) ? " <em>Position</em> " . text($v['ctxpos']) : "";
310 $str_html .= (isset($v['ctxloop'])) ? " <em>Position</em> " . text($v['ctxloop']) : "";
311 $str_html .= (isset($v['ctxelem'])) ? " <em>Element</em> " . text($v['ctxelem']) . "<br />" . PHP_EOL : PHP_EOL;
313 $str_html .= (isset($v['ik401'])) ? "Data Element <em>element</em> " . text($v['ik401']) : "";
314 $str_html .= (isset($v['ik402'])) ? " <em>ref</em> " . text($v['ik402']) : "";
315 $str_html .= (isset($v['ik404'])) ? " <em>data</em> " . text($v['ik404']) : "";
316 $str_html .= (isset($v['ik403'])) ? "<br /> <em>code</em> " . text($v['ik403']) . " " . text(edih_997_code_text('ak403', $v['ik403'])) . "<br />" : "<br />";
318 $str_html .= (isset($v['ik501']) && $v['ik501']) ? "<em>Status</em> " . text($v['ik501']) . " " . text(edih_997_code_text('ak501', $v['ik501'])) . "<br />" : "";
319 $str_html .= (isset($v['ik502']) && $v['ik502']) ? " <em>code</em> " . text($v['ik502']) . " " . text(edih_997_code_text('ak502', $v['ik502'])) . "<br />" : "";
320 $str_html .= (isset($v['ik503']) && $v['ik503']) ? " <em>code</em> " . text($v['ik503']) . " " . text(edih_997_code_text('ak502', $v['ik503'])) . "<br />" : "";
321 $str_html .= (isset($v['ik504']) && $v['ik504']) ? " <em>code</em> " . text($v['ik504']) . " " . text(edih_997_code_text('ak502', $v['ik504'])) . "<br />" : "";
322 $str_html .= (isset($v['ik505']) && $v['ik505']) ? " <em>code</em> " . text($v['ik505']) . " " . text(edih_997_code_text('ak502', $v['ik505'])) . "<br />" : "";
324 $str_html .= "</p>" . PHP_EOL;
327 return $str_html;
331 * Main function in this script
333 * @uses csv_check_x12_obj()
334 * @uses edih_997_errdata()
335 * @uses edih_997_err_report()
337 * @param string
338 * @return string
340 function edih_997_error($filepath)
343 $html_str = '';
345 $obj997 = csv_check_x12_obj($filepath, 'f997');
346 if ($obj997 && ('edih_x12_file' == get_class($obj997))) {
347 $data = edih_997_errdata($obj997);
348 $html_str .= edih_997_err_report($data);
349 } else {
350 $html_str .= "<p>Error: invalid file path</p>" . PHP_EOL;
351 csv_edihist_log("edih_997_error: invalid file path $filepath");
354 return $html_str;