Billing invoice email broken (#6712)
[openemr.git] / library / edihistory / edih_835_html.php
blobc6fc6a9553b80857c839683d5cbe4fe07beb09b9
1 <?php
3 /*
4 * new_edih_835_html.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Kevin McCormick Longview, Texas
9 * @author Stephen Waite <stephen.waite@cmsvt.com>
10 * @copyright Copyright (c) 2016 Kevin McCormick Longview, Texas
11 * @copyright Copyright (c) 2021 Stephen Waite <stephen.waite@cmsvt.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 // lookup codes
17 //require_once("$srcdir/edihistory/codes/edih_835_code_class.php");
18 //require_once("$srcdir/edihistory/codes/edih_271_code_class.php");
20 /**
21 * callback to round floats to 2 digit precision
23 * @param float
24 * @param string
25 * @return float
27 function edih_round_cb(&$v, $k)
29 $v = round((int)$v, 2);
31 /**
32 * Create summary html string for an x12 835 claim payment
34 * @param array
35 * @param object
36 * @param object
37 * @param array
38 * @param string
40 * @return string
42 function edih_835_clp_summary($trans_array, $codes27x, $codes835, $delimiters, $fname = '')
44 // NM1 CPL
45 $str_html = "";
46 if (is_array($trans_array) && count($trans_array)) {
47 if (csv_singlerecord_test($trans_array)) {
48 $clp_ar = array();
49 $clp_ar[] = $trans_array;
50 } else {
51 $clp_ar = $trans_array;
53 } else {
54 csv_edihist_log("edih_835_transaction_html: Did not get transaction segments");
55 $str_html .= "<p>Did not get transaction segments</p>" . PHP_EOL;
56 return $str_html;
59 $de = (isset($delimiters['e'])) ? $delimiters['e'] : "";
60 $ds = (isset($delimiters['s'])) ? $delimiters['s'] : "";
61 $dr = (isset($delimiters['r'])) ? $delimiters['r'] : "";
63 if (!$de || !$ds) {
64 csv_edihist_log("edih_835_transaction_html: Did not get delimiters");
65 $str_html .= "<p>Did not get delimiters</p>" . PHP_EOL;
66 return $str_html;
70 $fn = ($fname) ? trim($fname) : "";
72 // get the code objects right
73 $cd835 = $cd27x = '';
74 if ('edih_835_codes' == get_class($codes835)) {
75 $cd835 = $codes835;
76 } elseif ('edih_835_codes' == get_class($codes27x)) {
77 $cd835 = $codes27x;
80 if ('edih_271_codes' == get_class($codes27x)) {
81 $cd27x = $codes27x;
82 } elseif ('edih_271_codes' == get_class($codes835)) {
83 $cd27x = $codes835;
86 if (!$cd835 || !$cd27x) {
87 csv_edihist_log('edih_835_payment_html: invalid code class argument');
88 $str_html .= "<p>invalid code class argument</p>" . PHP_EOL;
89 return $str_html;
93 $tblid = "";
94 $capstr = "";
95 $mia_str = "";
97 $hdr_html = "<tr><th>Reference</th><th colspan=2>Information</th><th colspan=2>" . text($fn) . "</th></tr>" . PHP_EOL;
98 $hdr_html .= "</thead>" . PHP_EOL . "<tbody>" . PHP_EOL;
99 $clp_html = "";
100 $svc_html = "";
101 $sbr_html = "";
102 $chksegs = array('CLP', 'NM1', 'AMT', 'QTY');
103 foreach ($trans_array as $trans) {
104 $capstr = "Summary ";
105 $loopid = 'NA';
106 foreach ($trans as $seg) {
108 $test_str = substr($seg, 0, 3);
109 if ($test_str == 'SVC') {
110 break;
113 if (!in_array($test_str, $chksegs, true)) {
114 continue;
118 if (strncmp('CLP' . $de, $seg, 4) === 0) {
119 $sar = explode($de, $seg);
120 $loopid = '2100';
121 $cls = 'clp';
123 $clp09ar = array('1' => 'Original', '7' => 'Replacement', '8' => 'Void');
125 $clp01 = $clp02 = $clp03 = $clp04 = $clp05 = $clp06 = $clp07 = '';
126 $clp08 = $clp09 = $clp11 = $clp12 = $clp13 = $capstr = $tblid = '';
127 foreach ($sar as $k => $v) {
128 switch ((int)$k) {
129 case 0:
130 break;
131 case 1:
132 $clp01 = $v;
133 $capstr = $v;
134 $tblid = $v;
135 break; // Pt ID CLM01
136 case 2:
137 $clp02 = $cd835->get_835_code('CLAIM_STATUS', $v);
138 break;
139 case 3:
140 $clp03 = ($v) ? "<em>Fee:</em> " . text(edih_format_money($v)) : "0";
141 break;
142 case 4:
143 $clp04 = ($v) ? "<em>Pmt:</em> " . text(edih_format_money($v)) : "0";
144 break;
145 case 5:
146 $clp05 = ($v) ? "<em>PtRsp:</em> " . text(edih_format_money($v)) : "0";
147 break;
148 case 7:
149 $clp07 = ($v) ? "<em>PR Ref:</em> " . text($v) : "";
150 break;
151 case 8:
152 $clp08 = ($v) ? "<em>Location</em> " . text($cd27x->get_271_code('POS', $v)) : '';
153 break;
154 case 9:
155 $clp09 = ($v && isset($clp09ar[$v])) ? "<em>Freq</em> " . text($clp09ar[$v]) : text($v);
160 $clp_html .= "<tr class='" . attr($cls) . "'><td><em>PtID:</em> " . text($clp01) . "</td><td colspan=3><em>Status</em> " . text($clp02) . " <em>" . text($clp06) . "</em></td></tr>" . PHP_EOL;
161 $clp_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>$clp03 $clp04 $clp05 $clp07 </td></tr>" . PHP_EOL;
162 $clp_html .= ($clp08 || $clp09) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>$clp08 $clp09</td></tr>" . PHP_EOL : "";
164 continue;
168 if ($loopid == '2100') {
169 if (strncmp('AMT' . $de, $seg, 4) === 0) {
170 // Payment information
171 $sar = explode($de, $seg);
173 $amt01 = (isset($sar[1]) && $sar[1]) ? $cd835->get_835_code('AMT', $sar[1]) : "";
174 $amt02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : "";
176 $clp_html .= ($amt01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($amt01 . " " . $amt02) . "</td></tr>" . PHP_EOL : "";
178 continue;
182 if (strncmp('QTY' . $de, $seg, 4) === 0) {
183 // Payment information
184 $sar = explode($de, $seg);
186 $qty01 = (isset($sar[1]) && $sar[1]) ? $cd835->get_835_code('AMT', $sar[1]) : "";
187 $qty02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : "";
189 if ($loopid == '2100') {
190 $clp_html .= ($qty01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($qty01 . " " . $qty02) . "</td></tr>" . PHP_EOL : "";
194 continue;
197 if (strncmp('NM1' . $de, $seg, 4) === 0) {
198 $sar = explode($de, $seg);
200 $descr = (isset($sar[1]) && $sar[1]) ? $cd27x->get_271_code('NM101', $sar[1]) : "";
202 $name = (isset($sar[3]) && $sar[3]) ? $sar[3] : "";
203 $name .= (isset($sar[7]) && $sar[7]) ? " {$sar[7]}" : "";
204 $name .= (isset($sar[4]) && $sar[4]) ? ", {$sar[4]}" : "";
205 $name .= (isset($sar[5]) && $sar[5]) ? " {$sar[5]}" : "";
206 $name .= (isset($sar[7]) && $sar[7]) ? " {$sar[7]}" : "";
208 $nm108 = (isset($sar[8]) && $sar[8]) ? $cd27x->get_271_code('NM108', $sar[8]) : "";
209 $nm109 = (isset($sar[9]) && $sar[9]) ? $sar[9] : "";
210 // complete table caption
211 if (isset($sar[1]) && $sar[1] == "QC") {
212 $capstr .= " $name";
216 if ($nm108) {
217 $sbr_html .= "<tr class='sbr'><td><em>" . text($descr) . "</em></td><td colspan=3>" . text($name) . " <em>" . text($nm108) . "</em> " . text($nm109) . "</td></tr>" . PHP_EOL;
218 } else {
219 $sbr_html .= "<tr class='sbr'><td><em>" . text($descr) . "</em></td><td colspan=3>" . text($name) . " </td></tr>" . PHP_EOL;
223 $descr = $name = $nm108 = $nm109 = '';
224 continue;
227 if (strncmp('CAS' . $de, $seg, 4) === 0) {
228 $sar = explode($de, $seg);
229 $cas_str = '';
230 // claim adjustment group; expect CAS segment for each adjustment group
231 foreach ($sar as $k => $v) {
232 switch ((int)$k) {
233 case 0:
234 break;
235 case 1:
236 $cas_str .= "$v " . $cd835->get_835_code('CAS_GROUP', $v);
237 break;
238 case 2:
239 $cas_str .= ($v) ? " $v" : "";
240 break;
241 case 3:
242 $cas_str .= ($v) ? " " . edih_format_money($v) : "";
243 break;
244 case 4:
245 $cas_str .= ($v) ? "x$v" : "";
246 break;
247 case 5:
248 $cas_str .= ($v) ? " $v" : "";
249 break;
250 case 6:
251 $cas_str .= ($v) ? " " . edih_format_money($v) : "";
252 break;
253 case 7:
254 $cas_str .= ($v) ? "x$v" : "";
255 break;
256 case 8:
257 $cas_str .= ($v) ? " $v" : "";
258 break;
259 case 9:
260 $cas_str .= ($v) ? " " . edih_format_money($v) : "";
261 break;
262 case 10:
263 $cas_str .= ($v) ? "x$v" : "";
264 break;
265 default:
266 $cas_str .= " *";
270 $clp_html .= ($cas_str) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($cas_str) . "</td></tr>" . PHP_EOL : "";
272 continue;
278 $str_html .= "<table name='" . attr($tblid) . "' class='h835c' columns=4><caption>" . text($capstr) . "</caption>" . PHP_EOL . "<thead>" . PHP_EOL;
279 $str_html .= $hdr_html;
280 $str_html .= $sbr_html;
281 $str_html .= $clp_html;
282 $str_html .= "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
286 return $str_html;
292 * Create html string for an x12 835 claim payment
294 * @param array
295 * @param object
296 * @param object
297 * @param array
298 * @param string
300 * @return string
302 function edih_835_transaction_html($trans_array, $codes27x, $codes835, $delimiters, $fname = '')
305 $str_html = "";
306 if (is_array($trans_array) && count($trans_array)) {
307 if (csv_singlerecord_test($trans_array)) {
308 $clp_ar = array();
309 $clp_ar[] = $trans_array;
310 } else {
311 $clp_ar = $trans_array;
313 } else {
314 csv_edihist_log("edih_835_transaction_html: Did not get transaction segments");
315 $str_html .= "<p>Did not get transaction segments</p>" . PHP_EOL;
316 return $str_html;
319 $de = (isset($delimiters['e'])) ? $delimiters['e'] : "";
320 $ds = (isset($delimiters['s'])) ? $delimiters['s'] : "";
321 $dr = (isset($delimiters['r'])) ? $delimiters['r'] : "";
323 if (!$de || !$ds) {
324 csv_edihist_log("edih_835_transaction_html: Did not get delimiters");
325 $str_html .= "<p>Did not get delimiters</p>" . PHP_EOL;
326 return $str_html;
330 $fn = ($fname) ? trim($fname) : "";
332 // get the code objects right
333 $cd835 = $cd27x = '';
334 if ('edih_835_codes' == get_class($codes835)) {
335 $cd835 = $codes835;
336 } elseif ('edih_835_codes' == get_class($codes27x)) {
337 $cd835 = $codes27x;
340 if ('edih_271_codes' == get_class($codes27x)) {
341 $cd27x = $codes27x;
342 } elseif ('edih_271_codes' == get_class($codes835)) {
343 $cd27x = $codes835;
346 if (!$cd835 || !$cd27x) {
347 csv_edihist_log('edih_835_payment_html: invalid code class argument');
348 $str_html .= "<p>invalid code class argument</p>" . PHP_EOL;
349 return $str_html;
353 $str_html = "";
355 $tblid = "";
356 $capstr = "";
357 $mia_str = "";
359 $hdr_html = "<tr><th>Reference</th><th colspan=3>Information &nbsp;" . text($fn) . "</th></tr>" . PHP_EOL;
360 $hdr_html .= "</thead>" . PHP_EOL . "<tbody>" . PHP_EOL;
361 $clp_html = "";
362 $svc_html = "";
363 $sbr_html = "";
364 $moa_html = "";
366 foreach ($clp_ar as $trans) {
367 $lq_ar = array();
368 $cas_ar = array();
369 $moa_ar = array();
370 $rarc_str = "";
371 $clp_html = "";
372 $svc_html = "";
373 $sbr_html = "";
374 $moa_html = "";
375 foreach ($trans as $seg) {
377 if (strncmp('REF' . $de, $seg, 4) === 0) {
378 $sar = explode($de, $seg);
380 if (isset($sar[1]) && $sar[1]) {
381 if ($sar[1] == 'LU') {
382 $ref01 = 'Location';
383 $ref02 = (isset($sar[2])) ? $cd27x->get_271_code('POS', $sar[2]) : '';
384 } else {
385 // entity ID code
386 $ref01 = (isset($sar[1])) ? $cd27x->get_271_code('REF', $sar[1]) : '';
387 // entity ID
388 $ref02 = (isset($sar[2])) ? $sar[2] : '';
392 if ($loopid == '2100') {
393 $clp_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($ref01) . "</em> " . text($ref02) . "</td></tr>" . PHP_EOL;
394 } elseif ($loopid == '2110') {
395 $svc_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($ref01) . "</em> " . text($ref02) . "</td></tr>" . PHP_EOL;
400 continue;
404 if (strncmp('DTM' . $de, $seg, 4) === 0) {
405 $sar = explode($de, $seg);
406 // DTM in 835 use DTP codes from 271 codes
407 $dtm01 = (isset($sar[1])) ? $cd27x->get_271_code('DTP', $sar[1]) : ''; // date qualifier
408 $dtm02 = (isset($sar[2])) ? edih_format_date($sar[2]) : ''; // production date
409 $dtm05 = (isset($sar[5])) ? $sar[5] : '';
410 $dtm06 = (isset($sar[6])) ? edih_format_date($sar[2]) : '';
412 //if ( $elem02 == 'D8' && $elem03) {
413 //$dtmar = edih_format_date($elem03);
414 //} elseif ( $elem02 == 'RD8' && $elem03) {
415 //$dtmar = edih_format_date( substr($elem03, 0, 8) );
416 //$dtmar .= ' - '.edih_format_date( substr($elem03, -8) );
419 if ($loopid == '2100') {
420 $clp_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($dtm01) . "</em> " . text($dtm02) . "</td></tr>" . PHP_EOL;
421 } elseif ($loopid == '2110') {
422 $svc_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($dtm01) . "</em> " . text($dtm02) . "</td></tr>" . PHP_EOL;
426 continue;
430 if (strncmp('PER' . $de, $seg, 4) === 0) {
431 $sar = explode($de, $seg);
433 $per01_ar = array('CX' => 'Claims Dept','BL' => 'Technical Dept','IC' => 'Website');
434 $per01 = $per02 = $per03 = $per04 = $per05 = $per06 = $per07 = $per08 = '';
435 foreach ($sar as $k => $v) {
436 switch ((int)$k) {
437 case 0:
438 break;
439 case 1:
440 $per01 = (isset($per01_ar[$v])) ? $per01_ar[$v] : $v;
441 break;
442 case 2:
443 $per02 = $v;
444 break;
445 case 3:
446 $per03 = $v;
447 break;
448 case 4:
449 $per04 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
450 break;
451 case 5:
452 $per05 = $v;
453 break;
454 case 6:
455 $per06 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
456 break;
457 case 7:
458 $per07 = $v;
459 break;
460 case 8:
461 $per08 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
466 if ($loopid == '2100') {
467 $clp_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per01 . " " . $per02 . " " . $per03 . " " . $per04) . " </td></tr>" . PHP_EOL;
468 $clp_html .= ($per05 || $per07) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per05 . " " . $per06 . " " . $per07 . " " . $per08) . "</td></tr>" . PHP_EOL : "";
472 continue;
476 if (strncmp('CLP' . $de, $seg, 4) === 0) {
477 $sar = explode($de, $seg);
478 $loopid = '2100';
479 $cls = 'clp';
481 $clp01 = $clp02 = $clp03 = $clp04 = $clp05 = $clp06 = $clp07 = $clp08 = $clp09 = $clp11 = $clp12 = $clp13 = $capstr = '';
483 $clp01 = (isset($sar[1]) && $sar[1]) ? $sar[1] : ''; // Pt ID CLM01
484 $clp02 = (isset($sar[2]) && $sar[2]) ? $cd835->get_835_code('CLAIM_STATUS', $sar[2]) : ''; // status code
485 $clp03 = (isset($sar[3]) && $sar[3]) ? edih_format_money($sar[3]) : '0'; // fee amont
486 $clp04 = (isset($sar[4]) && $sar[4]) ? edih_format_money($sar[4]) : '0'; // paid amount
487 $clp05 = (isset($sar[5]) && $sar[5]) ? edih_format_money($sar[5]) : '0'; // pt responsibility amont
488 $clp06 = (isset($sar[6]) && $sar[6]) ? $cd835->get_835_code('CLP06', $sar[6]) : ''; // filing indicator code
489 $clp07 = (isset($sar[7]) && $sar[7]) ? $sar[7] : ''; // Payer reference ID
490 $clp08 = (isset($sar[8]) && $sar[8]) ? "<em>Location</em> " . text($cd27x->get_271_code('POS', $sar[8])) : ''; // Faciliy code place of service
491 // frequency type code 1 original 7 replacement 8 void
492 $clp09ar = array('1' => 'original', '7' => 'replacement', '8' => 'void');
493 if (isset($sar[9]) && array_key_exists($sar[9], $clp09ar)) { // claim frequency code
494 $clp09 = "<em>Freq</em> " . text($clp09ar[$sar[9]]);
495 } else {
496 $clp09 = (isset($sar[9]) && $sar[9]) ? "<em>Freq</em> " . text($sar[9]) : "";
499 // DRG code not expected
500 $clp11 = (isset($sar[11]) && $sar[11]) ? "<em>DRG Code</em> " . text($sar[11]) : '';
501 // DRG weight
502 $clp12 = (isset($sar[12]) && $sar[12]) ? "<em>DRG Weight</em> " . text($sar[12]) : '';
503 // DRG percentage
504 $clp13 = (isset($sar[13]) && $sar[13]) ? "<em>Dischg Frctn</em> " . text(edih_format_percent($sar[13])) : '';
506 // table caption PtID PtName
507 $capstr .= $clp01;
508 $tblid = $clp01;
510 $clp_html .= "<tr class='" . attr($cls) . "'><td><em>Pt ID</em> " . text($clp01) . "</td><td colspan=3><em>Status</em> " . text($clp02) . " <em>" . text($clp06) . "</em></td></tr>" . PHP_EOL;
511 $clp_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Fee</em> " . text($clp03) . " <em>Pmt</em> " . text($clp04) . " <em>PtRsp</em> " . text($clp05) . " <em>PR Ref</em> " . text($clp07) . " </td></tr>" . PHP_EOL;
512 $clp_html .= ($clp08 || $clp09) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>$clp08 $clp09</td></tr>" . PHP_EOL : "";
513 $clp_html .= ($clp11 || $clp12 || $clp13) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>$clp11 $clp12 $clp13</td></tr>" . PHP_EOL : "";
515 continue;
519 if (strncmp('CAS' . $de, $seg, 4) === 0) {
520 $sar = explode($de, $seg);
521 // claim adjustments
522 $cls = ($loopid == '2100') ? 'clp' : 'svc';
523 // claim adjustment group; expect CAS segment for each adjustment group
524 if (isset($sar[1]) && $sar[1]) {
525 $cas_ar[$loopid][$sar[1]] = array_chunk(array_slice($sar, 2), 3);
526 // debug
527 //echo '== array_chunk'.PHP_EOL;
528 //var_dump( $cas_ar ).PHP_EOL;
532 continue;
537 if (strncmp('NM1' . $de, $seg, 4) === 0) {
538 $sar = explode($de, $seg);
539 $nm1_str = "";
541 if (isset($sar[1]) && $sar[1]) {
542 if (strpos('|IL|QC|72', $sar[1])) {
543 $cls = 'sbr';
544 } else {
545 $cls = 'clp';
548 $descr = $cd27x->get_271_code('NM101', $sar[1]);
549 } else {
550 $cls = 'clp';
551 $descr = '';
555 $name = (isset($sar[3]) && $sar[3]) ? $sar[3] : "";
556 $name .= (isset($sar[7]) && $sar[7]) ? " {$sar[7]}" : "";
557 $name .= (isset($sar[4]) && $sar[4]) ? ", {$sar[4]}" : "";
558 $name .= (isset($sar[5]) && $sar[5]) ? " {$sar[5]}" : "";
559 $name .= (isset($sar[6]) && $sar[6]) ? " {$sar[6]}" : "";
561 $nm108 = (isset($sar[8]) && $sar[8]) ? $cd27x->get_271_code('NM108', $sar[8]) : "";
562 $nm109 = (isset($sar[9]) && $sar[9]) ? $sar[9] : "";
563 // complete table caption
564 if (isset($sar[1]) && $sar[1] == "QC") {
565 $capstr .= " $name";
570 if ($nm108) {
571 $nm1_str .= "<tr class='" . attr($cls) . "'><td><em>" . text($descr) . "</em></td><td colspan=3>" . text($name) . " <em>" . text($nm108) . "</em> " . text($nm109) . "</td></tr>" . PHP_EOL;
572 } else {
573 $nm1_str .= "<tr class='" . attr($cls) . "'><td><em>" . text($descr) . "</em></td><td colspan=3>" . text($name) . " </td></tr>" . PHP_EOL;
576 if ($loopid == '2100') {
577 $clp_html .= $nm1_str;
578 } elseif ($loopid == '2110') {
579 $svc_html .= $nm1_str;
583 $descr = $name = $nm108 = $nm109 = '';
584 continue;
588 if (strncmp('MIA' . $de, $seg, 4) === 0) {
589 // Inpatient Adjudication information
590 $sar = explode($de, $seg);
591 // <tr class='mia'><td>&gt;</td><td> </td></tr>".PHP_EOL;
592 $tr1 = "<tr class='mia'><td>&gt;</td><td colspan=3>";
593 $tr2 = "</td></tr>" . PHP_EOL;
595 $mia_str .= (isset($sar[1]) && $sar[1]) ? $tr1 . "Covered Days or Visits: " . text($sar[1]) . $tr2 : ""; // days or visits
596 $mia_str .= (isset($sar[2]) && $sar[2]) ? $tr1 . "PPS Operating Outlier Amt: " . text(edih_format_money($sar[2])) . $tr2 : "";
597 $mia_str .= (isset($sar[3]) && $sar[3]) ? $tr1 . "Lifetime Psychiatric Days: " . text($sar[3]) . $tr2 : "";
598 $mia_str .= (isset($sar[4]) && $sar[4]) ? $tr1 . "Claim DRG Amt: " . text(edih_format_money($sar[4])) . $tr2 : "";
599 $mia_str .= (isset($sar[5]) && $sar[5]) ? "<tr class='mia'><td>" . text($sar[5]) . "</td><td colspan=3>" . text($cd835->get_835_code('RARC', $sar[5])) . $tr2 : "";
600 $mia_str .= (isset($sar[6]) && $sar[6]) ? $tr1 . "Claim DSH Amt: " . text(edih_format_money($sar[6])) . $tr2 : "";
601 $mia_str .= (isset($sar[7]) && $sar[7]) ? $tr1 . "Claim MSP Pass Thru Amt: " . text(edih_format_money($sar[7])) . $tr2 : "";
602 $mia_str .= (isset($sar[8]) && $sar[8]) ? $tr1 . "Claim PPS Capital Amt: " . text(edih_format_money($sar[8])) . $tr2 : "";
603 $mia_str .= (isset($sar[9]) && $sar[9]) ? $tr1 . "PPS Capital FSP DRG Amt: " . text(edih_format_money($sar[9])) . $tr2 : "";
604 $mia_str .= (isset($sar[10]) && $sar[10]) ? $tr1 . "PPS Capital HSP DRG Amt: " . text(edih_format_money($sar[10])) . $tr2 : "";
605 $mia_str .= (isset($sar[11]) && $sar[11]) ? $tr1 . "PPS Capital DSH DRG Amt: " . text(edih_format_money($sar[11])) . $tr2 : "";
606 $mia_str .= (isset($sar[12]) && $sar[12]) ? $tr1 . "Old Capital Amt: " . text(edih_format_money($sar[12])) . $tr2 : "";
607 $mia_str .= (isset($sar[13]) && $sar[13]) ? $tr1 . "PPS Capital Ind Med Edu Amt: " . text(edih_format_money($sar[13])) . $tr2 : "";
608 $mia_str .= (isset($sar[14]) && $sar[14]) ? $tr1 . "PPS Oper HSP Spec DRG Amt: " . text(edih_format_money($sar[14])) . $tr2 : "";
609 $mia_str .= (isset($sar[15]) && $sar[15]) ? $tr1 . "Cost Report Day Count: " . text($sar[15]) . $tr2 : "";
610 $mia_str .= (isset($sar[16]) && $sar[16]) ? $tr1 . "PPS Oper FSP Spec DRG Amt: " . text(edih_format_money($sar[16])) . $tr2 : "";
611 $mia_str .= (isset($sar[17]) && $sar[17]) ? $tr1 . "Claim PPS Outlier Amt: " . text(edih_format_money($sar[17])) . $tr2 : "";
612 $mia_str .= (isset($sar[18]) && $sar[18]) ? $tr1 . "Claim Indirect Teaching: " . text(edih_format_money($sar[18])) . $tr2 : "";
613 $mia_str .= (isset($sar[19]) && $sar[19]) ? $tr1 . "Non Pay Prof Component Amt: " . text(edih_format_money($sar[19])) . $tr2 : "";
614 $mia_str .= (isset($sar[20]) && $sar[20]) ? "<tr class='mia'><td>" . text($sar[20]) . "</td><td colspan=3>" . text($cd835->get_835_code('RARC', $sar[20])) . $tr2 : "";
615 $mia_str .= (isset($sar[21]) && $sar[21]) ? "<tr class='mia'><td>" . text($sar[21]) . "</td><td colspan=3>" . text($cd835->get_835_code('RARC', $sar[21])) . $tr2 : "";
616 $mia_str .= (isset($sar[22]) && $sar[22]) ? "<tr class='mia'><td>" . text($sar[22]) . "</td><td colspan=3>" . text($cd835->get_835_code('RARC', $sar[22])) . $tr2 : "";
618 continue;
622 if (strncmp('MOA' . $de, $seg, 4) === 0) {
623 // Inpatient Adjudication information
624 $sar = explode($de, $seg);
626 $moa_str = 'Claim Level Remarks: ';
627 foreach ($sar as $k => $v) {
628 switch ((int)$k) {
629 case 0:
630 break;
631 case 1:
632 $moa_str .= ($v) ? 'Reimbursement Rate: ' . edih_format_percent($v) : '';
633 break;
634 case 2:
635 $moa_str .= ($v) ? 'Allowed Amt: ' . edih_format_money($v) : '';
636 break;
637 case 8:
638 $moa_str .= ($v) ? 'ESRD Amt: ' . edih_format_money($v) : '';
639 break;
640 case 9:
641 $moa_str .= ($v) ? 'Non-Pay Prof Cmpnt: ' . edih_format_money($v) : '';
642 break;
643 default:
644 // case 3, 4, 5, 6, 7 are remark codes
645 $moa_str .= ($v) ? ' ' . $v : '';
646 $moa_ar[] = ($v) ? $v : '';
651 $clp_html .= ($moa_str) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($moa_str) . "</td></tr>" . PHP_EOL : "";
653 continue;
657 if (strncmp('AMT' . $de, $seg, 4) === 0) {
658 // Payment information
659 $sar = explode($de, $seg);
661 $amt01 = (isset($sar[1]) && $sar[1]) ? $cd835->get_835_code('AMT', $sar[1]) : "";
662 $amt02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : "";
664 if ($loopid == '2100') {
665 $clp_html .= ($amt01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($amt01 . " " . $amt02) . "</td></tr>" . PHP_EOL : "";
666 } elseif ($loopid == '2110') {
667 $svc_html .= ($amt01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($amt01 . " " . $amt02) . "</td></tr>" . PHP_EOL : "";
671 continue;
675 if (strncmp('QTY' . $de, $seg, 4) === 0) {
676 // Payment information
677 $sar = explode($de, $seg);
679 $qty01 = (isset($sar[1]) && $sar[1]) ? $cd835->get_835_code('AMT', $sar[1]) : "";
680 $qty02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : "";
682 if ($loopid == '2100') {
683 $clp_html .= ($qty01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($qty01 . " " . $qty02) . "</td></tr>" . PHP_EOL : "";
684 } elseif ($loopid == '2110') {
685 $svc_html .= ($qty01) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($qty01 . " " . $qty02) . "</td></tr>" . PHP_EOL : "";
689 continue;
693 if (strncmp('SVC' . $de, $seg, 4) === 0) {
695 $sar = explode($de, $seg);
696 $loopid = '2110';
697 $cls = 'svc';
698 $rarc_str = ''; // used in LQ segment stanza
699 // composite procedure code source:code:modifier:modifier
700 $svc01 = '';
701 if (isset($sar[1]) && $sar[1]) {
702 // construct a code source code modifier string
703 if (strpos($sar[1], $ds)) {
704 $scda = explode($ds, $sar[1]);
705 reset($scda);
706 foreach ($scda as $key => $val) {
707 if ($key == 0 && $val) {
708 $svc01 = $cd27x->get_271_code('EB13', $val);
709 } else {
710 $svc01 .= ":" . $val;
713 } else {
714 $svc01 = $sar[1];
719 $svc02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : ""; // billed amount
720 $svc03 = (isset($sar[3]) && $sar[3]) ? edih_format_money($sar[3]) : ""; // paid amount
721 $svc04 = (isset($sar[4]) && $sar[4]) ? "<em>NUBC</em> " . $sar[4] : ""; // NUBC revenue code
722 $svc05 = (isset($sar[5]) && $sar[5]) ? "<em>Units</em> " . $sar[5] : ""; // quantity
724 $svc06 = '';
725 if (isset($sar[6]) && $sar[6]) {
726 // construct a code source code modifier string
727 if (strpos($sar[6], $ds)) {
728 $scda = explode($ds, $sar[6]);
729 reset($scda);
730 foreach ($scda as $key => $val) {
731 if ($key == 0 && $val) {
732 $svc06 = $cd27x->get_271_code('EB13', $val) . " ";
733 } else {
734 $svc06 .= ":" . $val;
737 } else {
738 $svc06 = $sar[6];
742 $svc07 = (isset($sar[7]) && $sar[7]) ? $sar[7] : ""; // original unis of service
744 $svc_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Service</em> " . text($svc01) . " <em>Fee</em> " . text($svc02) . " <em>Pmt</em> " . text($svc03 . " " . $svc05 . " " . $svc04) . "</td></tr>" . PHP_EOL;
745 $svc_html .= ($svc06) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Submitted Svc</em> " . text($svc06) . " <em>Units</em> " . text($svc07) . "</td></tr>" . PHP_EOL : "";
747 continue;
751 if (strncmp('LQ' . $de, $seg, 3) === 0) {
752 $sar = explode($de, $seg);
753 // Health Care Remark Codes
754 $lq01 = (isset($sar[1]) && $sar[1]) ? $sar[1] : "";
755 if (isset($sar[2])) {
756 $lq02 = ($lq01 == 'HE') ? $sar[2] : "";
757 //$lq02 = $cd835->get_835_code('RARC', $sar[2]);
758 $rarc_str .= ($rarc_str) ? ' ' . $sar[2] : '<em>Service Remarks</em> ' . text($sar[2]);
759 $lq_ar[] = $sar[2];
760 } else {
761 $lq02 = "";
764 //$lq02 = (isset($sar[2]) && $sar[2] && $lq01 == 'HE') ? $cd835->get_835_code('RARC', $sar[2]) : "";
766 // $svc_html .= ($rarc_str) ? "<tr class='$cls'><td>&gt;</td><td colspan=3>$rarc_str</td></tr>".PHP_EOL : "";
768 continue;
772 } // end foreach trans as seg
773 // assemble the html table at end of the inside foreach loop
775 $str_html .= "<table name='" . attr($tblid) . "' class='h835c' columns=4><caption>" . text($capstr) . "</caption>" . PHP_EOL . "<thead>" . PHP_EOL;
776 $str_html .= $hdr_html;
777 $str_html .= $sbr_html;
778 $str_html .= $clp_html;
779 $str_html .= ($mia_str) ? $mia_str : '';
780 $str_html .= $svc_html;
781 $str_html .= ($rarc_str) ? "<tr class='svc'><td>&gt;</td><td colspan=3>$rarc_str</td></tr>" . PHP_EOL : "";
782 if (count($cas_ar)) {
783 foreach ($cas_ar as $key => $cas) {
784 if (!is_array($cas) && !count($cas)) {
785 continue;
788 if ($key == '2100' && count($cas)) {
789 $cls = 'remc';
790 $str_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Claim Level Adjustments</em></td></tr>" . PHP_EOL;
791 } else {
792 $cls = 'rems';
793 $str_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Service Level Adjustments</em></td></tr>" . PHP_EOL;
796 $cg = '';
797 foreach ($cas as $ky => $trp) {
798 //echo '==== cas_ar unwind cas as ky trp '.$ky.PHP_EOL;
799 //var_dump ($trp).PHP_EOL;
801 if (!is_array($trp) && !count($trp)) {
802 continue;
805 $cg = $cd835->get_835_code('CAS_GROUP', $ky);
806 foreach ($trp as $tr) {
807 // debug
808 //echo '==== cas_ar unwind trp as tr '.PHP_EOL;
809 //var_dump ($tr).PHP_EOL;
811 $cd = $cr = $ca = $cq = '';
812 foreach ($tr as $k => $c) {
813 //echo '==== cas_ar unwind tr as k c '.$k.PHP_EOL;
814 //var_dump ($c).PHP_EOL;
816 switch ((int)$k) {
817 case 0:
818 $cd = $c;
819 $cr = $cd835->get_835_code('CARC', $c);
820 break;
821 case 1:
822 $ca = ($c) ? edih_format_money($c) : "";
823 break;
824 case 2:
825 $cq = ($c) ? $c : "";
831 $str_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($ky . " " . $cg . " " . $cd . " " . $ca . " " . $cq) . "</td></tr>" . PHP_EOL;
832 $str_html .= "<tr class='" . attr($cls) . "'><td style='text-align: center;'>" . text($ky . " " . $cd) . "</td><td colspan=3>" . text($cr) . "</td></tr>" . PHP_EOL;
837 if (count($moa_ar)) {
838 $cls = 'remc';
839 $str_html .= "<tr class='" . attr($cls) . "'><td colspan=4><em>Remarks</em></td></tr>" . PHP_EOL;
840 foreach ($moa_ar as $moa) {
841 $moar = $cd835->get_835_code('RARC', $moa);
842 $str_html .= "<tr class='" . attr($cls) . "'><td style='text-align: center;'>" . text($moa) . "</td><td colspan=3>" . text($moar) . "</td></tr>" . PHP_EOL;
846 if (count($lq_ar)) {
847 $cls = 'mia';
848 $str_html .= ($rarc_str) ? "<tr class='" . attr($cls) . "'><td colspan=4>$rarc_str</td></tr>" . PHP_EOL : "";
849 foreach ($lq_ar as $lq) {
850 $lqr = $cd835->get_835_code('RARC', $lq);
851 $str_html .= "<tr class='" . attr($cls) . "'><td style='text-align: center;'>$lq</td><td colspan=3>" . text($lqr) . "</td></tr>" . PHP_EOL;
855 // bottom border
856 $str_html .= "<tr class='remc'><td colspan=4>&nbsp;</td></tr>" . PHP_EOL;
857 // end tags for table
858 $str_html .= "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
862 return $str_html;
868 * Create an HTML rendition of the 835 check payment transaction.
871 * @param array
872 * @param object
873 * @param object
874 * @param array
875 * @param string
877 * @return string HTML table
879 function edih_835_payment_html($segments, $codes27x, $codes835, $delimiters, $fname = '')
882 $str_html = '';
883 $pid = $chk = '';
884 if (is_array($segments) && count($segments)) {
885 $trans_ar = $segments;
886 } else {
887 csv_edihist_log("edih_835_payment_html: invalid segments argument");
888 $str_html .= "<p>invalid segments argument</p>" . PHP_EOL;
889 return $str_html;
892 if (is_array($delimiters) && count($delimiters)) {
893 $de = $delimiters['e'];
894 $ds = $delimiters['s'];
895 $dr = $delimiters['r'];
896 } else {
897 csv_edihist_log("edih_835_payment_html: invalid delimiters argument");
898 $str_html .= "<p>invalid delimiters argument</p>" . PHP_EOL;
899 return $str_html;
903 $fn = ($fname) ? trim($fname) : "";
905 // get the code objects right
906 $cd835 = $cd27x = '';
907 if ('edih_835_codes' == get_class($codes835)) {
908 $cd835 = $codes835;
909 } elseif ('edih_835_codes' == get_class($codes27x)) {
910 $cd835 = $codes27x;
913 if ('edih_271_codes' == get_class($codes27x)) {
914 $cd27x = $codes27x;
915 } elseif ('edih_271_codes' == get_class($codes835)) {
916 $cd27x = $codes835;
919 if (!$cd835 || !$cd27x) {
920 csv_edihist_log('edih_835_payment_html: invalid code class argument');
921 $str_html .= "<p>invalid code class argument</p>" . PHP_EOL;
922 return $str_html;
926 // collect all strings into this variable
927 $str_html = "";
929 $hdr_html = "<thead>" . PHP_EOL;
930 $hdr_html .= "<tr><th>Reference</th><th colspan=2>Information</th><th colspan=2>" . text($fn) . "</th></tr>" . PHP_EOL;
931 $hdr_html .= "</thead>" . PHP_EOL . "<tbody>" . PHP_EOL;
932 $pmt_html = "";
933 $src_html = "";
934 $rcv_html = "";
935 $lx_html = "";
936 $clp_html = "";
937 $trl_html = "";
939 $acctng = array('pmt' => 0,'fee' => 0,'clmpmt' => 0,'clmadj' => 0, 'ptrsp' => 0, 'svcptrsp' => 0, 'svcfee' => 0,'svcadj' => 0,'plbadj' => 0);
941 foreach ($trans_ar as $trans) {
942 $clpsegs = array();
943 $lx_ar = array();
944 $clp_ct = 0;
945 $lx_ct = 0;
946 $loop = '';
947 $lxkey = '';
948 $capstr = "Remittance ";
949 $tblid = "";
951 foreach ($trans as $seg) {
953 if (strncmp('ST' . $de, $seg, 3) === 0) {
954 $loopid = 'header';
955 continue;
959 if (strncmp('BPR' . $de, $seg, 4) === 0) {
960 $loopid = 'header';
961 $cls = 'pmt';
963 $acctng = array('pmt' => 0, 'fee' => 0, 'clmpmt' => 0, 'clmadj' => 0, 'ptrsp' => 0,
964 'svcptrsp' => 0, 'svcfee' => 0, 'svcpmt' => 0, 'svcadj' => 0, 'plbadj' => 0);
966 $sar = explode($de, $seg);
967 $bpr01 = (isset($sar[1]) && $sar[1]) ? $cd835->get_835_code('BPR01', $sar[1]) : ''; // handling code
968 $bpr02 = (isset($sar[2]) && $sar[2]) ? edih_format_money($sar[2]) : ''; // full payment amount
969 $bpr03 = (isset($sar[3]) && $sar[3] == 'D' ) ? 'Debit' : 'Credit'; // credit or debit flag
970 $bpr04 = (isset($sar[4]) && $sar[4]) ? $sar[4] : ''; // payment method ACH|CHK|NON
971 $bpr05 = (isset($sar[5]) && $sar[5]) ? $sar[5] : ''; // payment format code CCP|CTX
972 $bpr06 = (isset($sar[6]) && $sar[6]) ? $sar[6] : ''; // DFI ID qualifier
973 $bpr07 = (isset($sar[7]) && $sar[7]) ? $sar[7] : ''; // bank ID
974 $bpr08 = (isset($sar[8]) && $sar[8]) ? $sar[8] : ''; // account no. qualifier DA
975 $bpr09 = (isset($sar[9]) && $sar[9]) ? $sar[9] : ''; // sender account number
976 $bpr10 = (isset($sar[10]) && $sar[10]) ? $sar[10] : ''; // originating company ID
977 $bpr11 = (isset($sar[11]) && $sar[11]) ? $sar[11] : ''; // originating company supplemental ID
978 $bpr12 = (isset($sar[12]) && $sar[12]) ? $sar[12] : ''; // deposit acount ID
979 $bpr13 = (isset($sar[13]) && $sar[13]) ? $sar[13] : ''; // deposit bank ID
980 $bpr14 = (isset($sar[14]) && $sar[14]) ? $sar[14] : ''; // account type DA deposit SG savings
981 $bpr15 = (isset($sar[15]) && $sar[15]) ? $sar[15] : ''; // account number
982 $bpr16 = (isset($sar[16]) && $sar[16]) ? edih_format_date($sar[16]) : ''; // check or payment date
984 if ($bpr04 == 'NON') {
985 $pmt_html .= "<tr class='" . attr($cls) . "'><td>" . text($bpr16) . "</td><td>" . text($bpr03 . " " . $bpr04) . "</td><td colspan=2>Non Payment</td></tr>" . PHP_EOL;
986 } else {
987 $pmt_html .= "<tr class='" . attr($cls) . "'><td>" . text($bpr16) . "</td><td>" . text($bpr03 . " " . $bpr04) . "</td><td colspan=2>" . text($bpr02) . " to " . text($bpr13 . " " . $bpr12 . " " . $bpr14) . "</td></tr>" . PHP_EOL;
990 if (strpos('|ACH|BOP|FWT', $bpr04)) {
991 $pmt_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($bpr05) . " from: " . text($bpr07 . " " . $bpr09 . " " . $bpr10) . "</td></tr>" . PHP_EOL;
994 $pmt_html .= ($bpr11) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Pmt No.</em> " . text($bpr11 . " " . $bpr01) . "</td></tr>" . PHP_EOL : "";
995 $acctng['pmt'] = (isset($sar[2]) && $sar[2]) ? (float)$sar[2] : "";
997 continue;
1001 if (strncmp('TRN' . $de, $seg, 4) === 0) {
1002 $sar = explode($de, $seg);
1004 $trn01 = (isset($sar[1]) && $sar[1]) ? $sar[1] : ''; // trace type code
1005 $trn02 = (isset($sar[2]) && $sar[2]) ? $sar[2] : ''; // trace number (= BPR11)
1006 $trn03 = (isset($sar[3]) && $sar[3]) ? $sar[3] : ''; // originator ID
1007 $trn04 = (isset($sar[4]) && $sar[4]) ? $sar[4] : ''; // originator supplemental ID
1008 if ($trn03[0] == '1') {
1009 $trn03 = substr($trn03, 1);
1010 } // originator ID is '1' prepended to EIN or TIN
1011 // the html ID for the table
1012 $tblid = ($trn02) ? $trn02 : "";
1013 $capstr .= ($trn02) ? "Check No: " . $trn02 : "Payment Listing";
1015 $pmt_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Trace</em> " . text($trn02) . " <em>by</em> " . text($trn03 . " " . $trn04) . "</td></tr>" . PHP_EOL;
1017 continue;
1021 if (strncmp('CUR' . $de, $seg, 4) === 0) {
1022 $sar = explode($de, $seg);
1024 $cur01 = (isset($sar[1])) ? $sar[1] : ''; // entity ID code
1025 $cur02 = (isset($sar[2])) ? $sar[2] : ''; // currency code
1027 $pmt_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>Trace</em> " . text($cur02) . " by " . text($cur03 . " " . $cur04) . "</td></tr>" . PHP_EOL;
1029 continue;
1033 if (strncmp('REF' . $de, $seg, 4) === 0) {
1034 $sar = explode($de, $seg);
1036 $ref01 = (isset($sar[1])) ? $cd27x->get_271_code('REF', $sar[1]) : ''; // entity ID code
1037 $ref02 = (isset($sar[2])) ? $sar[2] : ''; // entity ID
1039 if ($loopid == 'header') {
1040 // should not be present for payee receiver
1041 $pmt_html .= "<tr class='" . text($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($ref01) . "</em> " . text($ref02) . "</td></tr>" . PHP_EOL;
1042 } elseif ($loopid == '1000A') {
1043 // source
1044 $src_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($ref01) . "</em> " . text($ref02) . "</td></tr>" . PHP_EOL;
1045 } elseif ($loopid == '1000B') {
1046 // receiver
1047 $rcv_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($ref01) . "</em> " . text($ref02) . "</td></tr>" . PHP_EOL;
1048 } elseif ($loopid == '2100') {
1050 $clpsegs[] = $seg;
1051 } elseif ($loopid == '2110') {
1053 $clpsegs[] = $seg;
1057 continue;
1060 if (strncmp('DTM' . $de, $seg, 4) === 0) {
1061 $sar = explode($de, $seg);
1062 // DTM in 835 use DTP codes from 271 codes
1063 $dtm01 = (isset($sar[1])) ? $cd27x->get_271_code('DTP', $sar[1]) : ''; // date qualifier
1064 $dtm02 = (isset($sar[2])) ? edih_format_date($sar[2]) : ''; // production date
1065 $dtm05 = (isset($sar[5])) ? $sar[5] : '';
1066 $dtm06 = (isset($sar[6])) ? edih_format_date($sar[2]) : '';
1068 //if ( $elem02 == 'D8' && $elem03) {
1069 //$dtmar = edih_format_date($elem03);
1070 //} elseif ( $elem02 == 'RD8' && $elem03) {
1071 //$dtmar = edih_format_date( substr($elem03, 0, 8) );
1072 //$dtmar .= ' - '.edih_format_date( substr($elem03, -8) );
1076 if ($loopid == 'header') {
1077 // should not be present for payee or receiver
1078 $pmt_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3><em>" . text($dtm01) . "</em> " . text($dtm02) . "</td></tr>" . PHP_EOL;
1079 } elseif ($loopid == '2100') {
1080 $clpsegs[] = $seg;
1081 } elseif ($loopid == '2110') {
1082 $clpsegs[] = $seg;
1086 continue;
1090 if (strncmp('N1' . $de, $seg, 3) === 0) {
1091 $sar = explode($de, $seg);
1093 $n101 = (isset($sar[1])) ? $cd27x->get_271_code('NM101', $sar[1]) : ''; // entity ID code
1094 $n102 = (isset($sar[2])) ? $sar[2] : ''; // name
1095 $n103 = (isset($sar[3])) ? $cd27x->get_271_code('NM108', $sar[3]) : ''; // entity ID type code
1096 $n104 = (isset($sar[4])) ? $sar[4] : '';
1098 if ($loopid == 'header') {
1099 $loopid = '1000A';
1100 $cls = 'src';
1101 $src_html .= "<tr class='" . attr($cls) . "'><td><em>" . text($n101) . "</em></td><td colspan=3>" . text($n102) . " <em>" . text($n103) . "</em> " . text($n104) . "</td></tr>" . PHP_EOL;
1102 } elseif ($loopid == '1000A') {
1103 $loopid = '1000B';
1104 $cls = 'rcv';
1105 $rcv_html .= "<tr class='" . attr($cls) . "'><td><em>" . text($n101) . "</em></td><td colspan=3>" . text($n102) . " <em>" . text($n103) . "</em> " . text($n104) . "</td></tr>" . PHP_EOL;
1109 continue;
1113 if (strncmp('N3' . $de, $seg, 3) === 0) {
1114 $sar = explode($de, $seg);
1116 $n301 = (isset($sar[1])) ? $sar[1] : ''; // address
1117 $n302 = (isset($sar[2])) ? $sar[2] : ''; // address line 2
1119 if ($loopid == '1000A') {
1120 $src_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n301 . " " . $n302) . "</td></tr>" . PHP_EOL;
1121 } elseif ($loopid == '1000B') {
1122 $rcv_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n301 . " " . $n302) . "</td></tr>" . PHP_EOL;
1126 continue;
1130 if (strncmp('N4' . $de, $seg, 3) === 0) {
1131 $sar = explode($de, $seg);
1133 $n401 = (isset($sar[1])) ? $sar[1] : ''; // city
1134 $n402 = (isset($sar[2])) ? $sar[2] : ''; // state
1135 $n403 = (isset($sar[3])) ? $sar[3] : ''; // Postal
1136 $n404 = (isset($sar[4])) ? $sar[4] : ''; // Country
1137 $n407 = (isset($sar[7])) ? $sar[7] : ''; // Country subdivision
1139 if ($loopid == '1000A') {
1140 $src_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n401 . " " . $n402 . " " . $n403) . "</td></tr>" . PHP_EOL;
1141 $src_html .= ($n404 || $n407) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n404 . " " . $n405) . "</td></tr>" . PHP_EOL : "";
1142 } elseif ($loopid == '1000B') {
1143 $rcv_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n401 . " " . $n402 . " " . $n403) . "</td></tr>" . PHP_EOL;
1144 $rcv_html .= ($n404 || $n407) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($n404 . " " . $n405) . "</td></tr>" . PHP_EOL : "";
1148 continue;
1152 if (strncmp('PER' . $de, $seg, 4) === 0) {
1153 if ($loopid == '2100' || $loopid == '2100') {
1154 // loop 2100 only
1155 $clpsegs[] = $seg;
1156 continue;
1159 $sar = explode($de, $seg);
1160 $per01_ar = array('CX' => 'Claims Dept','BL' => 'Technical Dept','IC' => 'Website');
1161 $per01 = $per02 = $per03 = $per04 = $per05 = $per06 = $per07 = $per08 = '';
1162 foreach ($sar as $k => $v) {
1163 switch ((int)$k) {
1164 case 0:
1165 break;
1166 case 1:
1167 $per01 = (isset($per01_ar[$v])) ? $per01_ar[$v] : $v;
1168 break;
1169 case 2:
1170 $per02 = $v;
1171 break;
1172 case 3:
1173 $per03 = $v;
1174 break;
1175 case 4:
1176 $per04 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
1177 break;
1178 case 5:
1179 $per05 = $v;
1180 break;
1181 case 6:
1182 $per06 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
1183 break;
1184 case 7:
1185 $per07 = $v;
1186 break;
1187 case 8:
1188 $per08 = ($per03 == 'TE') ? edih_format_telephone($v) : $v;
1193 if ($loopid == '1000A') {
1194 $src_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per01 . " " . $per02 . " " . $per03 . " " . $per04) . " </td></tr>" . PHP_EOL;
1195 $src_html .= ($per05 || $per07) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per05 . " " . $per06 . " " . $per07 . " " . $per08) . "</td></tr>" . PHP_EOL : "";
1196 } elseif ($loopid == '1000B') {
1197 $rcv_html .= "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per01 . " " . $per02 . " " . $per03 . " " . $per04) . " </td></tr>" . PHP_EOL;
1198 $rcv_html .= ($per05 || $per07) ? "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>" . text($per05 . " " . $per06 . " " . $per07 . " " . $per08) . "</td></tr>" . PHP_EOL : "";
1202 continue;
1206 if (strncmp('RDM' . $de, $seg, 4) === 0) {
1207 // remittance delivery method
1208 // loop 1000B -- add to pmt information
1209 $sar = explode($de, $seg);
1211 $rdm01 = (isset($sar[1])) ? $sar[1] : '';
1212 if ($sar[1] == 'BM') {
1213 $rdm01 = 'By mail';
1214 } elseif ($sar[1] == 'EM') {
1215 $rdm01 = 'By e-mail';
1216 } elseif ($sar[1] == 'FT') {
1217 $rdm01 = 'By file transfer';
1218 } elseif ($sar[1] == 'OL') {
1219 $rdm01 = 'By online';
1222 $rdm02 = (isset($sar[2])) ? $sar[2] : ''; // name
1223 $rdm03 = (isset($sar[3])) ? $sar[3] : ''; // number
1225 $pmt_html .= "<tr class='" . attr($cls) . "'><td>" . text($rdm01) . "</td><td colspan=3>" . text($rdm02 . " " . $rdm03) . "</td></tr>" . PHP_EOL;
1227 continue;
1231 if (strncmp('LX' . $de, $seg, 3) === 0) {
1232 // LX can end loop 1000B or a claim grouping
1233 if ($loopid == '1000B') {
1234 // finish off pmt, src, and rcv
1235 $rcv_html .= "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
1236 } elseif ($loopid == '2110') {
1237 if ($lxkey && array_key_exists($lxkey, $lx_ar)) {
1238 // LX claim grouping -- cannot predict detail
1239 // LX can follow loop 2110
1240 if (count($clpsegs)) {
1241 $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
1242 $clpsegs = array();
1245 $nlx_html = ($lx_html) ? "<table name='lx_" . attr($lxkey) . "' class='h835c' columns=4>" . PHP_EOL . "<tbody>" . PHP_EOL . $lx_html . PHP_EOL : "";
1246 $lx_ar[$lxkey]['lx'] = $nlx_html;
1247 $lx_ar[$lxkey]['clp'] = $clp_html;
1248 $lx_html = "";
1249 $clp_html = "";
1250 $clpsegs = array();
1254 $sar = explode($de, $seg);
1255 $lxkey = (isset($sar[1]) && $sar[1]) ? $sar[1] : ''; // identify a grouping for claim info
1256 $lx_ar[$lxkey] = array();
1258 $loopid = '2000';
1259 $cls = 'lx';
1260 //$lx_ct = count($lx_ar);
1261 $lx_html .= ($lxkey) ? "<tr class='" . attr($cls) . "'><td colspan=4><em>Claim Group</em> " . text($lxkey) . "</td></tr>" . PHP_EOL : "";
1262 continue;
1266 if (strncmp('TS3' . $de, $seg, 4) === 0) {
1267 $sar = explode($de, $seg);
1268 // this looks like a medicare part A or hospital remittance segment
1269 // segment TS2 gives DRG totals -- not read in this sequence. If you need it, code it
1270 $loopid = '2000';
1271 $ts301 = (isset($sar[1]) && $sar[1]) ? $sar[1] : ''; // Provider ID
1272 $ts302 = (isset($sar[2]) && $sar[2]) ? $cd27x->get_271_code('POS', $sar[2]) : ''; // Facility Code (place of service)
1273 $ts303 = (isset($sar[3]) && $sar[3]) ? edih_format_date($sar[3]) : ''; // date - last day of provider fiscal year
1274 $ts304 = (isset($sar[4]) && $sar[4]) ? $sar[4] : ''; // quantity
1275 $ts305 = (isset($sar[5]) && $sar[5]) ? edih_format_money($sar[5]) : ''; // monetary amount
1277 $lx_html .= "<tr class='" . attr($cls) . "'><td><em>Prv</em> " . text($ts301) . "</td><td colspan=3>" . text($ts302) . " <em>Count</em> " . text($ts304) . " <em>Amount</em> " . text($ts305) . "</td></tr>" . PHP_EOL;
1279 // Medicare Part A
1280 $tr1 = "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>";
1281 $tr2 = "</td></tr>" . PHP_EOL;
1283 $lx_html .= (isset($sar[13]) && $sar[13]) ? $tr1 . "Total MSP Payer Amt: " . text(edih_format_money($sar[13])) . $tr2 . PHP_EOL : "";
1284 $lx_html .= (isset($sar[15]) && $sar[15]) ? $tr1 . "Total Non-Lab Chrg Amt: " . text(edih_format_money($sar[15])) . $tr2 . PHP_EOL : "";
1285 $lx_html .= (isset($sar[17]) && $sar[17]) ? $tr1 . "Total HCPCS Rpt Chrg Amt: " . text(edih_format_money($sar[17])) . $tr2 . PHP_EOL : "";
1286 $lx_html .= (isset($sar[18]) && $sar[18]) ? $tr1 . "Total HCPCS Payable Amt: " . text(edih_format_money($sar[18])) . $tr2 . PHP_EOL : "";
1287 $lx_html .= (isset($sar[20]) && $sar[20]) ? $tr1 . "Total Prof Cmpnt Amt: " . text(edih_format_money($sar[20])) . $tr2 . PHP_EOL : "";
1288 $lx_html .= (isset($sar[21]) && $sar[21]) ? $tr1 . "Total MSP Pt Liab Met Amt: " . text(edih_format_money($sar[21])) . $tr2 . PHP_EOL : "";
1289 $lx_html .= (isset($sar[22]) && $sar[22]) ? $tr1 . "Total MSP Pt Reimb Amt: " . text(edih_format_money($sar[22])) . $tr2 . PHP_EOL : "";
1290 $lx_html .= (isset($sar[23]) && $sar[23]) ? $tr1 . "Total PIP Claim Count: " . text($sar[23]) . $tr2 . PHP_EOL : "";
1291 $lx_html .= (isset($sar[24]) && $sar[24]) ? $tr1 . "Total PIP Claim Count: " . text(edih_format_money($sar[24])) . $tr2 . PHP_EOL : "";
1293 continue;
1297 if (strncmp('TS2' . $de, $seg, 4) === 0) {
1298 csv_edihist_log("edih_835_transaction_html: segment TS2 present in $fn");
1299 // Medicare Part A
1300 $tr1 = "<tr class='" . attr($cls) . "'><td>&gt;</td><td colspan=3>";
1301 $tr2 = "</td></tr>" . PHP_EOL;
1303 $lx_html .= (isset($sar[1]) && $sar[1]) ? $tr1 . "Total DRG Amt: " . text(edih_format_money($sar[1])) . $tr2 : "";
1304 $lx_html .= (isset($sar[2]) && $sar[2]) ? $tr1 . "Total Fed Specific Amt: " . text(edih_format_money($sar[2])) . $tr2 : "";
1305 $lx_html .= (isset($sar[3]) && $sar[3]) ? $tr1 . "Total Hosp Specific Amt: " . text(edih_format_money($sar[3])) . $tr2 : "";
1306 $lx_html .= (isset($sar[4]) && $sar[4]) ? $tr1 . "Total DSP Share Amt: " . text(edih_format_money($sar[4])) . $tr2 : "";
1307 $lx_html .= (isset($sar[5]) && $sar[5]) ? $tr1 . "Total Capital Amt: " . text(edih_format_money($sar[5])) . $tr2 : "";
1308 $lx_html .= (isset($sar[6]) && $sar[6]) ? $tr1 . "Total Ind Med Edu Amt: " . text(edih_format_money($sar[6])) . $tr2 : "";
1309 $lx_html .= (isset($sar[7]) && $sar[7]) ? $tr1 . "Total Outlier Day Amt: " . text(edih_format_money($sar[7])) . $tr2 : "";
1310 $lx_html .= (isset($sar[8]) && $sar[8]) ? $tr1 . "Total Day Outlier Day Amt: " . text(edih_format_money($sar[8])) . $tr2 : "";
1311 $lx_html .= (isset($sar[9]) && $sar[9]) ? $tr1 . "Total Cost Outlier Day Amt: " . text(edih_format_money($sar[9])) . $tr2 : "";
1312 $lx_html .= (isset($sar[10]) && $sar[10]) ? $tr1 . "Avg DRG Length of Stay: " . text($sar[10]) . $tr2 : "";
1313 $lx_html .= (isset($sar[11]) && $sar[11]) ? $tr1 . "Total Discharge Count: " . text($sar[11]) . $tr2 : "";
1314 $lx_html .= (isset($sar[12]) && $sar[12]) ? $tr1 . "Total Cost Rpt Day Count: " . text($sar[12]) . $tr2 : "";
1315 $lx_html .= (isset($sar[13]) && $sar[13]) ? $tr1 . "Total Covered Day Count: " . text($sar[13]) . $tr2 : "";
1316 $lx_html .= (isset($sar[14]) && $sar[14]) ? $tr1 . "Total Non Covered Day Count: " . text($sar[14]) . $tr2 : "";
1317 $lx_html .= (isset($sar[15]) && $sar[15]) ? $tr1 . "Total MSP Pass-Thru Amt: " . text(edih_format_money($sar[15])) . $tr2 : "";
1318 $lx_html .= (isset($sar[16]) && $sar[16]) ? $tr1 . "Avg DRG Weight: " . text($sar[16]) . $tr2 : "";
1319 $lx_html .= (isset($sar[17]) && $sar[17]) ? $tr1 . "Total PPS Capital FSP DRG Amt: " . text(edih_format_money($sar[17])) . $tr2 : "";
1320 $lx_html .= (isset($sar[18]) && $sar[18]) ? $tr1 . "Total PPS Capital FSP HSP Amt: " . text(edih_format_money($sar[18])) . $tr2 : "";
1321 $lx_html .= (isset($sar[19]) && $sar[19]) ? $tr1 . "Total PPS DSH DRG Amt: " . text(edih_format_money($sar[19])) . $tr2 : "";
1323 continue;
1326 if (strncmp('PLB' . $de, $seg, 4) === 0) {
1327 // can signal end of claim transaction
1328 $loopid = 'summary';
1329 $cls = 'pmt';
1330 //if (count($clpsegs)) {
1331 //$clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
1332 //$clpsegs = array();
1335 $sar = explode($de, $seg);
1336 // provider ID and fiscal year end date
1337 $plb01 = (isset($sar[1]) && $sar[1]) ? $sar[1] : "";
1338 $plb02 = (isset($sar[2]) && $sar[2]) ? edih_format_date($sar[2]) : "";
1340 $pmt_html .= "<tr class='" . attr($cls) . "'><td><em>Provider</em></td><td colspan=3>" . text($plb01 . " " . $plb02) . "</td></tr>" . PHP_EOL;
1342 $plbar = array_slice($sar, 2);
1343 $plbar = array_chunk($plbar, 2);
1344 // reason code and amount
1345 foreach ($plbar as $plb) {
1346 foreach ($plb as $k => $p) {
1347 // PLB 3, 5, 7, 9, 11, 13
1348 // composite element 'code:reference'
1349 if ($k == 0) {
1350 if ($p && strpos($p, $ds)) {
1351 $plb_rc = substr($p, 0, strpos($p, $ds)); // code
1352 $plb_tr = substr($p, strpos($p, $ds) + 1); // reference (case #)?
1353 } else {
1354 $plb_rc = ($p) ? $p : "";
1355 $plb_tr = "";
1358 $plb_rt = ($plb_rc) ? $cd835->get_835_code('PLB', $plb_rc) : "";
1359 } else {
1360 // PLB 4, 6, 8, 10, 12, 14
1361 // monetary amount
1362 $plb_amt = ($p) ? edih_format_money($p) : "";
1363 $acctng['plbadj'] += ($p) ? (float)$p : 0;
1367 $pmt_html .= "<tr class='" . attr($cls) . "'><td>" . text($plb_tr) . "</td><td colspan=3>" . text($plb_rc . " " . $plb_rt . " " . $plb_amt) . "</td></tr>" . PHP_EOL;
1371 continue;
1375 if (strncmp('SE' . $de, $seg, 3) === 0) {
1376 // end of payment transaction, so create the html page
1377 $loopid = 'trailer';
1378 $cls = 'pmt';
1379 // include our accounting totals
1380 if (is_array($acctng) && count($acctng)) {
1381 array_walk($acctng, 'edih_round_cb');
1382 $bal = ($acctng['fee'] == ($acctng['pmt'] + $acctng['clmadj'] + $acctng['svcadj'] + $acctng['svcptrsp'] + $acctng['plbadj']) ) ? "Balanced" : "Not Balanced";
1383 $acct_str = text($bal) . ": <em>Fee</em> " . text($acctng['fee']) . " <em>Pmt</em> " . text($acctng['pmt']) . " ";
1384 $acct_str .= "<em>ClpAdj</em> " . text($acctng['clmadj']) . " <em>SvcAdj</em> " . text($acctng['svcadj']) . " ";
1385 $acct_str .= "<em>PtRsp</em> " . text($acctng['ptrsp']) . " (<em>svcPtRsp</em> " . text($acctng['svcptrsp']) . ") <em>PlbAdj</em> " . text($acctng['plbadj']) . " ";
1387 $pmt_html .= "<tr class='" . attr($cls) . "'><td colspan=4>$acct_str</td></tr>" . PHP_EOL;
1391 // create the html page
1392 $str_html .= "<table id=" . attr($tblid) . " class='h835' columns=4><caption>" . text($capstr) . "</caption>" . PHP_EOL;
1393 $str_html .= $hdr_html;
1394 if ($pmt_html) {
1395 $str_html .= $pmt_html;
1396 $pmt_html = "";
1399 if ($src_html) {
1400 $str_html .= $src_html;
1401 $src_html = "";
1404 if ($rcv_html) {
1405 $str_html .= $rcv_html;
1406 $rcv_html = "";
1410 if (count($lx_ar)) {
1411 // claim segments are in lx array
1412 // make sure we have current collection
1413 if ($lxkey && array_key_exists($lxkey, $lx_ar)) {
1414 if (count($clpsegs)) {
1415 $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
1416 $clpsegs = array();
1419 // note: table ending in CLP if stanza
1420 $nlx_html = "<table name='lx_" . attr($lxkey) . "' class='h835c' columns=4>" . PHP_EOL . "<tbody>" . PHP_EOL . $lx_html . PHP_EOL;
1421 $lx_ar[$lxkey]['lx'] = $nlx_html;
1422 $lx_ar[$lxkey]['clp'] = $clp_html;
1423 $lx_html = "";
1424 $clp_html = "";
1425 $clpsegs = array();
1428 // append segments to html
1429 foreach ($lx_ar as $key => $val) {
1430 $str_html .= $val['lx'];
1431 $str_html .= $val['clp'];
1433 } elseif ($lx_html) {
1434 $str_html .= $lx_html;
1435 $lx_html = "";
1439 if (count($clpsegs)) {
1440 // would be captured in LX and lx array
1441 $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
1442 $clpsegs = array();
1445 if ($clp_html) {
1446 $str_html .= $clp_html;
1447 $clp_html = "";
1450 if ($trl_html) {
1451 $str_html .= $trl_html;
1452 $trl_html = "";
1455 //$str_html .= "</tbody>".PHP_EOL."</table>".PHP_EOL;
1457 continue;
1460 if (strncmp('CLP' . $de, $seg, 4) === 0) {
1461 if ($loopid == '1000B') {
1462 // end of 1000B (receiver) loop
1463 $rcv_html .= ($clp_ct) ? "" : "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
1464 } elseif ($loopid == '2000') {
1465 // end of LX header (LX TS3 TS2 claim grouping)
1466 $lx_html .= ($clp_ct) ? "" : "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
1469 $loopid = '2100';
1470 //array('pmt'=>0, 'clmpmt'=>0, 'clmadj'=0, 'prvadj'=>0, 'ptrsp'=>0,'lx'=>array());
1471 $sar = explode($de, $seg);
1472 $acctng['fee'] += (isset($sar[3]) && $sar[3]) ? (float)$sar[3] : 0;
1473 $acctng['clmpmt'] += (isset($sar[4]) && $sar[4]) ? (float)$sar[4] : 0;
1474 $acctng['ptrsp'] += (isset($sar[5]) && $sar[5]) ? (float)$sar[5] : 0;
1476 if (count($clpsegs)) {
1477 $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
1480 $clpsegs = array();
1481 $clpsegs[] = $seg;
1482 $clp_ct++;
1483 continue;
1486 if (strncmp('SVC' . $de, $seg, 4) === 0) {
1487 $loopid = '2110';
1488 $sar = explode($de, $seg);
1489 $pmtm = $pmts = 1;
1490 foreach ($sar as $k => $v) {
1491 if ($k == 2) {
1492 $svcfee = ($v) ? (float)$v : 0;
1493 } elseif ($k == 3) {
1494 $svcpmt = ($v) ? (float)$v : 0;
1495 } elseif ($k == 5) {
1496 $pmtm = ($v) ? (int)$v : 1;
1497 } elseif ($k == 7) {
1498 $pmts = ($v) ? (int)$v : 1;
1502 $acctng['svcfee'] += $svcfee * $pmts;
1503 $acctng['svcpmt'] += $svcpmt * $pmtm;
1505 $clpsegs[] = $seg;
1506 continue;
1509 if (strncmp('CAS' . $de, $seg, 4) === 0) {
1510 $sar = explode($de, $seg);
1511 // category
1512 $ctg = (isset($sar[1]) && $sar[1]) ? $sar[1] : 'CO';
1513 // slice sar array to get triplet elements
1514 // chunk into triplets
1515 $sar1 = array_slice($sar, 2);
1516 $sar1 = array_chunk($sar1, 3);
1518 foreach ($sar1 as $cas) {
1519 $cav = 0;
1520 $cq = '';
1521 foreach ($cas as $k => $v) {
1522 if ($k == 1) {
1523 // monetary amount elem 3, 6, 9, 12, 15, 18
1524 $cav = ($v) ? $v : 0;
1525 } elseif ($k == 2) {
1526 // quantity elem 4, 7, 10, 13, 16, 19
1527 $cq = ($v) ? $v : "";
1528 if ($cq && strcmp($cq, '1') > 0) {
1529 $cav = $cav * $cq;
1534 if ($ctg == 'PR') {
1535 $acctng['svcptrsp'] += ($cav) ? (float)$cav : 0;
1536 } else {
1537 $acctky = ($loopid == '2100') ? 'clmadj' : 'svcadj';
1538 $acctng[$acctky] += ($cav) ? (float)$cav : 0;
1542 $clpsegs[] = $seg;
1543 continue;
1546 // uncaught segments should be routed by this
1547 if ($loopid == '2100' || $loopid == '2110') {
1548 $clpsegs[] = $seg;
1549 continue;
1551 } // end foreach(trans as seg)
1552 } // end foreach(trans_ar as trans)
1554 return $str_html;
1559 * create a display for an 835 claim payment file or transaction
1561 * @uses csv_check_x12_obj()
1563 * @param string $filename the filename
1564 * @param string TRN02 identifier from 835 check ir EFT
1565 * @param string CLM01 identifier from 837 CLM
1567 * @return string error message or a table with file information
1569 function edih_835_html($filename, $trace = '', $clm01 = '', $summary = false)
1572 $html_str = '';
1574 if (trim($filename)) {
1575 $obj835 = csv_check_x12_obj($filename, 'f835');
1576 if ($obj835 && 'edih_x12_file' == get_class($obj835)) {
1577 $fn = $obj835->edih_filename();
1578 $delims = $obj835->edih_delimiters();
1579 $env_ar = $obj835->edih_x12_envelopes();
1581 $de = (isset($delims['e'])) ? $delims['e'] : '';
1582 $ds = (isset($delims['s'])) ? $delims['s'] : '';
1583 $dr = (isset($delims['r'])) ? $delims['r'] : '';
1584 // $dr is not used, but just in case
1585 } else {
1586 $html_str .= "<p>edih_835_html: invalid file name</p>" . PHP_EOL;
1587 return $html_str;
1589 } else {
1590 $html_str .= "Error in file name or file parsing <br />" . PHP_EOL;
1591 csv_edihist_log("edih_835_html: error in parsing file $filename");
1592 return $html_str;
1595 if ($de && $ds) {
1596 // note $dr, repetition separator, is not always available
1597 $cd27x = new edih_271_codes($ds, $dr);
1598 $cd835 = new edih_835_codes($ds, $dr);
1599 } else {
1600 csv_edihist_log("edih_835_html: Did not get delimiters");
1601 $html_str .= "<p>Did not get delimiters for " . text($fn) . "</p>" . PHP_EOL;
1602 return $html_str;
1606 // if given, one only of trace or clm01
1607 $pid = $chk = '';
1608 if ($clm01) {
1609 $pid = trim((string)$clm01);
1610 } elseif ($trace) {
1611 $chk = trim((string)$trace);
1615 if ($pid) {
1616 $clp_ar = $obj835->edih_x12_transaction($pid);
1617 // $clp_ar is array[i][j]
1618 if (count($clp_ar)) {
1619 if ($summary) {
1620 $html_str .= edih_835_clp_summary($clp_ar, $cd27x, $cd835, $delims, $fn);
1621 } else {
1622 // create a table for this transaction for jquery-ui dialog
1623 $html_str .= edih_835_transaction_html($clp_ar, $cd27x, $cd835, $delims, $fn);
1625 } else {
1626 csv_edihist_log("edih_835_html: Did not find PtID $pid in $fn");
1627 $html_str .= "<p>Did not find PtID " . text($pid) . " in " . text($fn) . "</p>" . PHP_EOL;
1628 return $html_str;
1630 } elseif ($chk) {
1631 // check detail
1632 if (isset($env_ar['ST']) && count($env_ar['ST'])) {
1633 $trans_ar = array();
1634 foreach ($env_ar['ST'] as $st) {
1635 if ($st['trace'] != $chk) {
1636 continue;
1639 $trans_ar[] = $obj835->edih_x12_slice(array('trace' => $chk));
1641 } else {
1642 csv_edihist_log("edih_835_transaction_html: Did not get envelopes information for $fn");
1643 $html_str .= "<p>Did not get envelopes information for " . text($fn) . "</p>" . PHP_EOL;
1644 return $html_str;
1647 if (is_array($trans_ar) && count($trans_ar)) {
1648 // $trans_ar is a slice, array[i]
1649 $html_str .= edih_835_payment_html($trans_ar, $cd27x, $cd835, $delims, $fn);
1650 } else {
1651 csv_edihist_log("edih_835_transaction_html: Did not find trace $chk in $fn");
1652 $html_str .= "<p>Did not find trace " . text($chk) . " in " . text($fn) . "</p>" . PHP_EOL;
1653 return $html_str;
1655 } else {
1656 // entire file
1657 if (isset($env_ar['ST']) && count($env_ar['ST'])) {
1658 $trans_ar = array();
1659 foreach ($env_ar['ST'] as $st) {
1660 $trans_ar[] = $obj835->edih_x12_slice(array('trace' => $st['trace']));
1662 } else {
1663 csv_edihist_log("edih_835_transaction_html: Did not envelopes information for $fn");
1664 $html_str .= "<p>Did not get envelopes information for " . text($fn) . "</p>" . PHP_EOL;
1665 return $html_str;
1668 if (is_array($trans_ar) && count($trans_ar)) {
1669 // $trans_ar is a slice, array[i]
1670 $html_str .= edih_835_payment_html($trans_ar, $cd27x, $cd835, $delims, $fn);
1671 } else {
1672 csv_edihist_log("edih_835_transaction_html: Did not get ST envelopes for $fn");
1673 $html_str .= "<p>Did not get ST envelopes for " . text($fn) . "</p>" . PHP_EOL;
1674 return $html_str;
1679 return $html_str;