/** 
 * Create an html report on rejected files or transactions
 *
 * @uses edih_997_ta1_code()
 * @uses edih_997_code_text()
 * @uses edih_rsp_st_match()
 * 
 * @param object
 * @return array
 */
function edih_997_err_report($err_array)
{
    //
    if (!is_array($err_array) || !count($err_array)) {
        $str_html = "Error: invalid argument for error report";
        csv_edihist_log('edih_997_err_report: invalid function argument');
        return $str_html;
    }
    //
    $str_html = "";
    $batchfile = "";
    //
    if (isset($err_array['summary'])) {
        extract($err_array['summary'], EXTR_OVERWRITE);
        //
        $str_html .= "<p class='rpt997'>" . PHP_EOL;
        $str_html .= isset($sub_icn) ? "<em>Submitted ICN</em> {$sub_icn}" : "Submitted file unknown";
        $str_html .= isset($subdate) ? " <em>Date</em> " . edih_format_date($subdate) : "";
        $str_html .= isset($subtime) ? " <em>Time</em> {$subtime}<br />" : "<br />";
        $str_html .= isset($ackcode) ? " TA1 {$ackcode} : " . edih_997_ta1_code($ackcode) . " <br />" : "";
        $str_html .= isset($acknote) ? " TA1 {$acknote} : " . edih_997_ta1_code($acknote) . " <br />" . PHP_EOL : "<br />" . PHP_EOL;
        if (isset($fg_type)) {
            $fgtp = csv_file_type($fg_type);
            $str_html .= " <em>Functional Group Type</em> {$fg_type} ({$fgtp})";
            $str_html .= isset($fg_id) ? " <em>GS06</em> {$fg_id} <br />" . PHP_EOL : "<br />" . PHP_EOL;
        }
        //
        //$str_html .= "</p>".PHP_EOL;
        //
        $str_html .= isset($ak901) ? "999/997 {$ak901} " . edih_997_code_text('ak501', $ak901) . "<br />" : "";
        $str_html .= isset($ak902) ? " Transactions: submitted {$ak902}" : " ";
        $str_html .= isset($ak903) ? " received {$ak903}" : "";
        $str_html .= isset($ak904) ? " accepted {$ak904}" : "";
        $str_html .= isset($ak905) && $ak905 ? "<br />{$ak905} " . edih_997_code_text('ak502', $ak905) . "<br />" : "";
        $str_html .= isset($ak906) && $ak906 ? $ak906 . " " . edih_997_code_text('ak502', $ak906) . "<br />" : "";
        $str_html .= isset($ak907) && $ak907 ? $ak907 . " " . edih_997_code_text('ak502', $ak907) . "<br />" : "";
        $str_html .= isset($ak908) && $ak908 ? $ak908 . " " . edih_997_code_text('ak502', $ak908) . "<br />" : "";
        $str_html .= isset($ak909) && $ak909 ? $ak909 . " " . edih_997_code_text('ak502', $ak909) . "<br />" : "";
        //
        $str_html .= "</p>" . PHP_EOL;
    }
    //
    foreach ($err_array['err'] as $k => $v) {
        //
        $ct = $k + 1;
        $icn = isset($sub_icn) ? $sub_icn : '';
        $stn = isset($v['substn']) ? $v['substn'] : '';
        $rtp = isset($v['subtype']) ? $v['subtype'] : '';
        //
        $str_html .= "<p class='err997'>" . PHP_EOL;
        $str_html .= "Error {$ct} ";
        $str_html .= $stn ? "<em>ST</em> {$stn} <br />" : "<br />";
        //
        if ($icn && $stn && $rtp) {
            $trc = sprintf("%s%04d", $icn, $stn);
            $srch = array('s_val' => $trc, 's_col' => 4, 'r_cols' => 'All');
            // array('s_val'=>'0024', 's_col'=>9, 'r_cols'=>array(1, 2, 7)),
            $trn_ar = csv_search_record($rtp, 'claim', $srch);
            if (is_array($trn_ar) && count($trn_ar)) {
                //'f837':array('PtName', 'SvcDate', 'CLM01', 'InsLevel', 'BHT03', 'FileName', 'Fee', 'PtPaid', 'Provider' );
                //'f276':array('PtName', 'SvcDate', 'CLM01', 'ClaimID', 'BHT03', 'FileName', 'Payer', 'Trace'); break;
                //'f270':array('PtName', 'ReqDate', 'Trace', 'InsBnft', 'BHT03', 'FileName', 'Payer'); break;
                $pt_name = $trn_ar[0][0];
                // $trn_ar['PtName'];
                $clm01 = $rtp == 'f837' ? $trn_ar[0][2] : $trn_ar[0][4];
                // $trn_ar['CLM01'] : $trn_ar['BHT03'];
                $svcdate = $trn_ar[0][1];
                // ($rtp == 'f270') ? $trn_ar['ReqDate'] : $trn_ar['SvcDate'];
                $btfn = $trn_ar[0][5];
                // $trn_ar['FileName'];
                $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;
            } else {
                $str_html .= "Unable to locate transaction  <em>Trace</em> {$trc} <br />" . PHP_EOL;
            }
        } else {
            $str_html .= "Unable to trace, did not get all of icn, type, and st number <br />" . PHP_EOL;
        }
        //
        $str_html .= isset($v['ctxacct']) ? "<em>Transaction ID</em> " . $v['ctxacct'] : "";
        $str_html .= isset($v['ik3segid']) ? " Segment <em>ID</em> " . $v['ik3segid'] : "";
        $str_html .= isset($v['ik3segpos']) ? " <em>Position</em> " . $v['ik3segpos'] : "";
        $str_html .= isset($v['ik3loop']) ? " <em>Loop</em> " . $v['ik3loop'] : "";
        $str_html .= isset($v['ik3code']) ? "<br /> <em>Code</em> " . $v['ik3code'] . " " . edih_997_code_text('ak304', $v['ik3code']) . "<br />" : "<br />";
        //
        $str_html .= isset($v['ctxid']) ? "Situational " . PHP_EOL . "<em>Segment</em> " . $v['ctxid'] : "";
        $str_html .= isset($v['ctxpos']) ? " <em>Position</em> " . $v['ctxpos'] : "";
        $str_html .= isset($v['ctxloop']) ? " <em>Position</em> " . $v['ctxloop'] : "";
        $str_html .= isset($v['ctxelem']) ? " <em>Element</em> " . $v['ctxelem'] . "<br />" . PHP_EOL : PHP_EOL;
        //
        $str_html .= isset($v['ik401']) ? "Data Element <em>element</em> " . $v['ik401'] : "";
        $str_html .= isset($v['ik402']) ? " <em>ref</em> " . $v['ik402'] : "";
        $str_html .= isset($v['ik404']) ? " <em>data</em> " . $v['ik404'] : "";
        $str_html .= isset($v['ik403']) ? "<br /> <em>code</em> " . $v['ik403'] . " " . edih_997_code_text('ak403', $v['ik403']) . "<br />" : "<br />";
        //
        $str_html .= isset($v['ik501']) && $v['ik501'] ? "<em>Status</em> " . $v['ik501'] . " " . edih_997_code_text('ak501', $v['ik501']) . "<br />" : "";
        $str_html .= isset($v['ik502']) && $v['ik502'] ? " <em>code</em> " . $v['ik502'] . " " . edih_997_code_text('ak502', $v['ik502']) . "<br />" : "";
        $str_html .= isset($v['ik503']) && $v['ik503'] ? " <em>code</em> " . $v['ik503'] . " " . edih_997_code_text('ak502', $v['ik503']) . "<br />" : "";
        $str_html .= isset($v['ik504']) && $v['ik504'] ? " <em>code</em> " . $v['ik504'] . " " . edih_997_code_text('ak502', $v['ik504']) . "<br />" : "";
        $str_html .= isset($v['ik505']) && $v['ik505'] ? " <em>code</em> " . $v['ik505'] . " " . edih_997_code_text('ak502', $v['ik505']) . "<br />" : "";
        //
        $str_html .= "</p>" . PHP_EOL;
    }
    return $str_html;
}
/**
 * create a display for an individual claim status response	
 * 
 * @uses csv_check_x12_obj()
 * @uses edih_271_transaction_html()
 * 
 * @param string  $filename the filename
 * @param string  $bht03 identifier from 837 CLM or BHT segment
 * 
 * @return string  either an error message or an html table 
 */
function edih_271_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if ($filename) {
        $obj271 = csv_check_x12_obj($filename, 'f271');
        if ('edih_x12_file' == get_class($obj271)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_271_transaction_html($obj271, $bht03);
            } else {
                // file contents
                $env_ar = $obj271->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_271_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj271->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        }
                        if ($gs['gsn'] == $st['gsn']) {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        $html_str .= "<h3>{$bht} Benefit Eligibility</h3>" . PHP_EOL;
                        $html_str .= "<div id='ac_{$bht}'>" . PHP_EOL;
                        //
                        $html_str .= edih_271_transaction_html($obj271, $bht);
                        //
                        $html_str .= "</div>" . PHP_EOL;
                    }
                    $html_str .= "</div>" . PHP_EOL;
                }
            }
        } else {
            $html_str .= "<p>edih_271_html: file parse error</p>" . PHP_EOL;
        }
    } else {
        csv_edihist_log("edih_271_html: error in file arguments");
        $html_str .= "Error: invalid file name<br />" . PHP_EOL;
        return $html_str;
    }
    //
    return $html_str;
}
/**
 * create a display for an individual authorization response	
 * 
 * @uses csv_check_x12_obj()
 * @uses edih_278_transaction_html
 * 
 * @param string  $filename the filename
 * @param string  $bht03 identifier from 837 CLM or27 BHT segment
 * 
 * @return string  either an error message or an html table
 */
function edih_278_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if (!$filename) {
        csv_edihist_log("edih_278_html: called with no file arguments");
        $html_str .= "Error, no file given<br />" . PHP_EOL;
        return $html_str;
    } else {
        $obj278 = csv_check_x12_obj($filename, 'f278');
        if ($obj278 && 'edih_x12_file' == get_class($obj278)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_278_transaction_html($obj278, $bht03);
            } else {
                // file contents
                $env_ar = $obj278->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_278_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj278->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        }
                        if ($gs['gsn'] == $st['gsn']) {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        $html_str .= "<h3>{$bht} Services Review</h3>" . PHP_EOL;
                        $html_str .= "<div id='ac_{$bht}'>" . PHP_EOL;
                        //
                        $html_str .= edih_278_transaction_html($obj278, $bht);
                        //
                        $html_str .= "</div>" . PHP_EOL;
                    }
                    $html_str .= "</div>" . PHP_EOL;
                }
            }
        } else {
            csv_edihist_log("edih_278_html: error in retreiving file object");
            $html_str .= "<p>x12 278 file parse error</p>" . PHP_EOL;
        }
    }
    //
    return $html_str;
}
/**
 * create a display for an individual claim status response	
 *
 * @uses csv_check_x12_obj()
 * @uses edih_277_transaction_html()
 * 
 * @param string  $filename the filename
 * @param string  $clm01 identifier from 837 CLM of BHT segment
 * 
 * @return string  either an error message or a table with the information from the response
 */
function edih_277_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if ($filename) {
        $fn = $filename;
    } else {
        csv_edihist_log("edih_277_html: called with no file arguments");
        $html_str .= "Error, no file given<br />" . PHP_EOL;
        return $html_str;
    }
    if ($fn) {
        $obj277 = csv_check_x12_obj($fn, 'f277');
        if ($obj277 && 'edih_x12_file' == get_class($obj277)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_277_transaction_html($obj277, $bht03);
            } else {
                // file contents
                $env_ar = $obj277->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_277_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj277->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        } else {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        //$html_str .= "<h3>$bht Claim Status <em>Date</em> $gs_date <em>Source</em> $gs_sender</h3>".PHP_EOL;
                        //$html_str .= "<div id='ac_$bht'>".PHP_EOL;
                        //
                        $html_str .= edih_277_transaction_html($obj277, $bht, true);
                        //
                        //$html_str .= "</div>".PHP_EOL;
                    }
                }
                $html_str .= "</div>" . PHP_EOL;
            }
        } else {
            $html_str .= "<p>{$filename} : file parse error</p>" . PHP_EOL;
        }
    } else {
        $html_str .= "Error with file name or file parsing <br />" . PHP_EOL;
        csv_edihist_log("edih_277_html: error in retreiving file object");
        return $html_str;
    }
    //
    return $html_str;
}
/**
 * Create an HTML rendition of the 835 check payment transaction.
 * 
 *
 * @param array
 * @param object
 * @param object
 * @param array
 * @param string
 * 
 * @return string     HTML table 	
 */
function edih_835_payment_html($segments, $codes27x, $codes835, $delimiters, $fname = '')
{
    //
    $str_html = '';
    $pid = $chk = '';
    if (is_array($segments) && count($segments)) {
        $trans_ar = $segments;
    } else {
        csv_edihist_log("edih_835_payment_html: invalid segments argument");
        $str_html .= "<p>invalid segments argument</p>" . PHP_EOL;
        return $str_html;
    }
    if (is_array($delimiters) && count($delimiters)) {
        $de = $delimiters['e'];
        $ds = $delimiters['s'];
        $dr = $delimiters['r'];
    } else {
        csv_edihist_log("edih_835_payment_html: invalid delimiters argument");
        $str_html .= "<p>invalid delimiters argument</p>" . PHP_EOL;
        return $str_html;
    }
    //
    $fn = $fname ? trim($fname) : "";
    //
    // get the code objects right
    $cd835 = $cd27x = '';
    if ('edih_835_codes' == get_class($codes835)) {
        $cd835 = $codes835;
    } elseif ('edih_835_codes' == get_class($codes27x)) {
        $cd835 = $codes27x;
    }
    if ('edih_271_codes' == get_class($codes27x)) {
        $cd27x = $codes27x;
    } elseif ('edih_271_codes' == get_class($codes835)) {
        $cd27x = $codes835;
    }
    if (!$cd835 || !$cd27x) {
        csv_edihist_log('edih_835_payment_html: invalid code class argument');
        $str_html .= "<p>invalid code class argument</p>" . PHP_EOL;
        return $str_html;
    }
    //
    // collect all strings into this variable
    $str_html = "";
    //
    $hdr_html = "<thead>" . PHP_EOL;
    $hdr_html .= "<tr><th>Reference</th><th colspan=2>Information</th><th colspan=2>{$fn}</th></tr>" . PHP_EOL;
    $hdr_html .= "</thead>" . PHP_EOL . "<tbody>" . PHP_EOL;
    $pmt_html = "";
    $src_html = "";
    $rcv_html = "";
    $lx_html = "";
    $clp_html = "";
    $trl_html = "";
    //
    $acctng = array('pmt' => 0, 'fee' => 0, 'clmpmt' => 0, 'clmadj' => 0, 'ptrsp' => 0, 'svcptrsp' => 0, 'svcfee' => 0, 'svcadj' => 0, 'plbadj' => 0);
    //
    foreach ($trans_ar as $trans) {
        $clpsegs = array();
        $lx_ar = array();
        $clp_ct = 0;
        $lx_ct = 0;
        $loop = '';
        $lxkey = '';
        $capstr = "Remittance ";
        $tblid = "";
        //
        foreach ($trans as $seg) {
            //
            if (strncmp('ST' . $de, $seg, 3) === 0) {
                $loopid = 'header';
                continue;
            }
            //
            if (strncmp('BPR' . $de, $seg, 4) === 0) {
                $loopid = 'header';
                $cls = 'pmt';
                //
                $acctng = array('pmt' => 0, 'fee' => 0, 'clmpmt' => 0, 'clmadj' => 0, 'ptrsp' => 0, 'svcptrsp' => 0, 'svcfee' => 0, 'svcpmt' => 0, 'svcadj' => 0, 'plbadj' => 0);
                //
                $sar = explode($de, $seg);
                $bpr01 = isset($sar[1]) && $sar[1] ? $cd835->get_835_code('BPR01', $sar[1]) : '';
                // handling code
                $bpr02 = isset($sar[2]) && $sar[2] ? edih_format_money($sar[2]) : '';
                // full payment amount
                $bpr03 = isset($sar[3]) && $sar[3] == 'D' ? 'Debit' : 'Credit';
                // credit or debit flag
                $bpr04 = isset($sar[4]) && $sar[4] ? $sar[4] : '';
                // payment method ACH|CHK|NON
                $bpr05 = isset($sar[5]) && $sar[5] ? $sar[5] : '';
                // payment format code CCP|CTX
                $bpr06 = isset($sar[6]) && $sar[6] ? $sar[6] : '';
                // DFI ID qualifier
                $bpr07 = isset($sar[7]) && $sar[7] ? $sar[7] : '';
                // bank ID
                $bpr08 = isset($sar[8]) && $sar[8] ? $sar[8] : '';
                // account no. qualifier DA
                $bpr09 = isset($sar[9]) && $sar[9] ? $sar[9] : '';
                // sender account number
                $bpr10 = isset($sar[10]) && $sar[10] ? $sar[10] : '';
                // originating company ID
                $bpr11 = isset($sar[11]) && $sar[11] ? $sar[11] : '';
                // originating company supplemental ID
                $bpr12 = isset($sar[12]) && $sar[12] ? $sar[12] : '';
                // deposit acount ID
                $bpr13 = isset($sar[13]) && $sar[13] ? $sar[13] : '';
                // deposit bank ID
                $bpr14 = isset($sar[14]) && $sar[14] ? $sar[14] : '';
                // account type DA deposit SG savings
                $bpr15 = isset($sar[15]) && $sar[15] ? $sar[15] : '';
                // account number
                $bpr16 = isset($sar[16]) && $sar[16] ? edih_format_date($sar[16]) : '';
                // check or payment date
                //
                if ($bpr04 == 'NON') {
                    $pmt_html .= "<tr class='{$cls}'><td>{$bpr16}</td><td>{$bpr03} {$bpr04}</td><td colspan=2>Non Payment</td></tr>" . PHP_EOL;
                } else {
                    $pmt_html .= "<tr class='{$cls}'><td>{$bpr16}</td><td>{$bpr03} {$bpr04}</td><td colspan=2>{$bpr02} to {$bpr13} {$bpr12} {$bpr14}</td></tr>" . PHP_EOL;
                }
                if (strpos('|ACH|BOP|FWT', $bpr04)) {
                    $pmt_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$bpr05} from: {$bpr07} {$bpr09} {$bpr10}</td></tr>" . PHP_EOL;
                }
                $pmt_html .= $bpr11 ? "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>Pmt No.</em> {$bpr11} {$bpr01}</td></tr>" . PHP_EOL : "";
                $acctng['pmt'] = isset($sar[2]) && $sar[2] ? (double) $sar[2] : "";
                //
                continue;
            }
            //
            if (strncmp('TRN' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                //
                $trn01 = isset($sar[1]) && $sar[1] ? $sar[1] : '';
                // trace type code
                $trn02 = isset($sar[2]) && $sar[2] ? $sar[2] : '';
                // trace number (= BPR11)
                $trn03 = isset($sar[3]) && $sar[3] ? $sar[3] : '';
                // originator ID
                $trn04 = isset($sar[4]) && $sar[4] ? $sar[4] : '';
                // originator supplemental ID
                if ($trn03[0] == '1') {
                    $trn03 = substr($trn03, 1);
                }
                // originator ID is '1' prepended to EIN or TIN
                // the html ID for the table
                $tblid = $trn02 ? $trn02 : "";
                $capstr .= $trn02 ? "Check No: " . $trn02 : "Payment Listing";
                //
                $pmt_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>Trace</em> {$trn02} <em>by</em> {$trn03} {$trn04}</td></tr>" . PHP_EOL;
                //
                continue;
            }
            //
            if (strncmp('CUR' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                //
                $cur01 = isset($sar[1]) ? $sar[1] : '';
                // entity ID code
                $cur02 = isset($sar[2]) ? $sar[2] : '';
                // currency code
                //
                $pmt_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>Trace</em> {$cur02} by {$cur03} {$cur04}</td></tr>" . PHP_EOL;
                //
                continue;
            }
            //
            if (strncmp('REF' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                //
                $ref01 = isset($sar[1]) ? $cd27x->get_271_code('REF', $sar[1]) : '';
                // entity ID code
                $ref02 = isset($sar[2]) ? $sar[2] : '';
                // entity ID
                //
                if ($loopid == 'header') {
                    // should not be present for payee receiver
                    $pmt_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>{$ref01}</em> {$ref02}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '1000A') {
                    // source
                    $src_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>{$ref01}</em> {$ref02}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '1000B') {
                    // receiver
                    $rcv_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>{$ref01}</em> {$ref02}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '2100') {
                    //
                    $clpsegs[] = $seg;
                } elseif ($loopid == '2110') {
                    //
                    $clpsegs[] = $seg;
                }
                //
                continue;
            }
            if (strncmp('DTM' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                // DTM in 835 use DTP codes from 271 codes
                $dtm01 = isset($sar[1]) ? $cd27x->get_271_code('DTP', $sar[1]) : '';
                // date qualifier
                $dtm02 = isset($sar[2]) ? edih_format_date($sar[2]) : '';
                // production date
                $dtm05 = isset($sar[5]) ? $sar[5] : '';
                $dtm06 = isset($sar[6]) ? edih_format_date($sar[2]) : '';
                //
                //if ( $elem02 == 'D8' && $elem03) {
                //$dtmar = edih_format_date($elem03);
                //} elseif ( $elem02 == 'RD8' && $elem03) {
                //$dtmar = edih_format_date( substr($elem03, 0, 8) );
                //$dtmar .= ' - '.edih_format_date( substr($elem03, -8) );
                //}
                //}
                //
                if ($loopid == 'header') {
                    // should not be present for payee or receiver
                    $pmt_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3><em>{$dtm01}</em> {$dtm02}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '2100') {
                    $clpsegs[] = $seg;
                } elseif ($loopid == '2110') {
                    $clpsegs[] = $seg;
                }
                //
                continue;
            }
            //
            if (strncmp('N1' . $de, $seg, 3) === 0) {
                $sar = explode($de, $seg);
                //
                $n101 = isset($sar[1]) ? $cd27x->get_271_code('NM101', $sar[1]) : '';
                // entity ID code
                $n102 = isset($sar[2]) ? $sar[2] : '';
                // name
                $n103 = isset($sar[3]) ? $cd27x->get_271_code('NM108', $sar[3]) : '';
                // entity ID type code
                $n104 = isset($sar[4]) ? $sar[4] : '';
                //
                if ($loopid == 'header') {
                    $loopid = '1000A';
                    $cls = 'src';
                    $src_html .= "<tr class='{$cls}'><td><em>{$n101}</em></td><td colspan=3>{$n102} <em>{$n103}</em> {$n104}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '1000A') {
                    $loopid = '1000B';
                    $cls = 'rcv';
                    $rcv_html .= "<tr class='{$cls}'><td><em>{$n101}</em></td><td colspan=3>{$n102} <em>{$n103}</em> {$n104}</td></tr>" . PHP_EOL;
                }
                //
                continue;
            }
            //
            if (strncmp('N3' . $de, $seg, 3) === 0) {
                $sar = explode($de, $seg);
                //
                $n301 = isset($sar[1]) ? $sar[1] : '';
                // address
                $n302 = isset($sar[2]) ? $sar[2] : '';
                // address line 2
                //
                if ($loopid == '1000A') {
                    $src_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n301} {$n302}</td></tr>" . PHP_EOL;
                } elseif ($loopid == '1000B') {
                    $rcv_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n301} {$n302}</td></tr>" . PHP_EOL;
                }
                //
                continue;
            }
            //
            if (strncmp('N4' . $de, $seg, 3) === 0) {
                $sar = explode($de, $seg);
                //
                $n401 = isset($sar[1]) ? $sar[1] : '';
                // city
                $n402 = isset($sar[2]) ? $sar[2] : '';
                // state
                $n403 = isset($sar[3]) ? $sar[3] : '';
                // Postal
                $n404 = isset($sar[4]) ? $sar[4] : '';
                // Country
                $n407 = isset($sar[7]) ? $sar[7] : '';
                // Country subdivision
                //
                if ($loopid == '1000A') {
                    $src_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n401} {$n402} {$n403}</td></tr>" . PHP_EOL;
                    $src_html .= $n404 || $n407 ? "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n404} {$n405}</td></tr>" . PHP_EOL : "";
                } elseif ($loopid == '1000B') {
                    $rcv_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n401} {$n402} {$n403}</td></tr>" . PHP_EOL;
                    $rcv_html .= $n404 || $n407 ? "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$n404} {$n405}</td></tr>" . PHP_EOL : "";
                }
                //
                continue;
            }
            //
            if (strncmp('PER' . $de, $seg, 4) === 0) {
                if ($loopid == '2100' || $loopid == '2100') {
                    // loop 2100 only
                    $clpsegs[] = $seg;
                    continue;
                }
                $sar = explode($de, $seg);
                $per01_ar = array('CX' => 'Claims Dept', 'BL' => 'Technical Dept', 'IC' => 'Website');
                $per01 = $per02 = $per03 = $per04 = $per05 = $per06 = $per07 = $per08 = '';
                foreach ($sar as $k => $v) {
                    switch ((int) $k) {
                        case 0:
                            break;
                        case 1:
                            $per01 = isset($per01_ar[$v]) ? $per01_ar[$v] : $v;
                            break;
                        case 2:
                            $per02 = $v;
                            break;
                        case 3:
                            $per03 = $v;
                            break;
                        case 4:
                            $per04 = $per03 == 'TE' ? edih_format_telephone($v) : $v;
                            break;
                        case 5:
                            $per05 = $v;
                            break;
                        case 6:
                            $per06 = $per03 == 'TE' ? edih_format_telephone($v) : $v;
                            break;
                        case 7:
                            $per07 = $v;
                            break;
                        case 8:
                            $per08 = $per03 == 'TE' ? edih_format_telephone($v) : $v;
                    }
                }
                //
                if ($loopid == '1000A') {
                    $src_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$per01} {$per02} {$per03} {$per04} </td></tr>" . PHP_EOL;
                    $src_html .= $per05 || $per07 ? "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$per05} {$per06} {$per07} {$per08}</td></tr>" . PHP_EOL : "";
                } elseif ($loopid == '1000B') {
                    $rcv_html .= "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$per01} {$per02} {$per03} {$per04} </td></tr>" . PHP_EOL;
                    $rcv_html .= $per05 || $per07 ? "<tr class='{$cls}'><td>&gt;</td><td colspan=3>{$per05} {$per06} {$per07} {$per08}</td></tr>" . PHP_EOL : "";
                }
                //
                continue;
            }
            //
            if (strncmp('RDM' . $de, $seg, 4) === 0) {
                // remittance delivery method
                // loop 1000B -- add to pmt information
                $sar = explode($de, $seg);
                //
                $rdm01 = isset($sar[1]) ? $sar[1] : '';
                if ($sar[1] == 'BM') {
                    $rdm01 = 'By mail';
                } elseif ($sar[1] == 'EM') {
                    $rdm01 = 'By e-mail';
                } elseif ($sar[1] == 'FT') {
                    $rdm01 = 'By file transfer';
                } elseif ($sar[1] == 'OL') {
                    $rdm01 = 'By online';
                }
                $rdm02 = isset($sar[2]) ? $sar[2] : '';
                // name
                $rdm03 = isset($sar[3]) ? $sar[3] : '';
                // number
                //
                $pmt_html .= "<tr class='{$cls}'><td>{$rdm01}</td><td colspan=3>{$rdm02} {$rdm03}</td></tr>" . PHP_EOL;
                //
                continue;
            }
            //
            if (strncmp('LX' . $de, $seg, 3) === 0) {
                // LX can end loop 1000B or a claim grouping
                if ($loopid == '1000B') {
                    // finish off pmt, src, and rcv
                    $rcv_html .= "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
                } elseif ($loopid == '2110') {
                    if ($lxkey && array_key_exists($lxkey, $lx_ar)) {
                        // LX claim grouping -- cannot predict detail
                        // LX can follow loop 2110
                        if (count($clpsegs)) {
                            $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
                            $clpsegs = array();
                        }
                        $nlx_html = $lx_html ? "<table name='lx_{$lxkey}' class='h835c' columns=4>" . PHP_EOL . "<tbody>" . PHP_EOL . $lx_html . PHP_EOL : "";
                        $lx_ar[$lxkey]['lx'] = $nlx_html;
                        $lx_ar[$lxkey]['clp'] = $clp_html;
                        $lx_html = "";
                        $clp_html = "";
                        $clpsegs = array();
                    }
                }
                $sar = explode($de, $seg);
                $lxkey = isset($sar[1]) && $sar[1] ? $sar[1] : '';
                // identify a grouping for claim info
                $lx_ar[$lxkey] = array();
                //
                $loopid = '2000';
                $cls = 'lx';
                //$lx_ct = count($lx_ar);
                $lx_html .= $lxkey ? "<tr class='{$cls}'><td colspan=4><em>Claim Group</em> {$lxkey}</td></tr>" . PHP_EOL : "";
                continue;
            }
            //
            if (strncmp('TS3' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                // this looks like a medicare part A or hospital remittance segment
                // segment TS2 gives DRG totals -- not read in this sequence. If you need it, code it
                $loopid = '2000';
                $ts301 = isset($sar[1]) && $sar[1] ? $sar[1] : '';
                // Provider ID
                $ts302 = isset($sar[2]) && $sar[2] ? $cd27x->get_271_code('POS', $sar[2]) : '';
                // Facility Code (place of service)
                $ts303 = isset($sar[3]) && $sar[3] ? edih_format_date($sar[3]) : '';
                // date - last day of provider fiscal year
                $ts304 = isset($sar[4]) && $sar[4] ? $sar[4] : '';
                // quantity
                $ts305 = isset($sar[5]) && $sar[5] ? edih_format_money($sar[5]) : '';
                // monetary amount
                //
                $lx_html .= "<tr class='{$cls}'><td><em>Prv</em> {$ts301}</td><td colspan=3>{$ts302} <em>Count</em> {$ts304} <em>Amount</em> {$ts305}</td></tr>" . PHP_EOL;
                //
                // Medicare Part A
                $tr1 = "<tr class='{$cls}'><td>&gt;</td><td colspan=3>";
                $tr2 = "</td></tr>" . PHP_EOL;
                //
                $lx_html .= isset($sar[13]) && $sar[13] ? $tr1 . "Total MSP Payer Amt: " . edih_format_money($sar[13]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[15]) && $sar[15] ? $tr1 . "Total Non-Lab Chrg Amt: " . edih_format_money($sar[15]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[17]) && $sar[17] ? $tr1 . "Total HCPCS Rpt Chrg Amt: " . edih_format_money($sar[17]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[18]) && $sar[18] ? $tr1 . "Total HCPCS Payable Amt: " . edih_format_money($sar[18]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[20]) && $sar[20] ? $tr1 . "Total Prof Cmpnt Amt: " . edih_format_money($sar[20]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[21]) && $sar[21] ? $tr1 . "Total MSP Pt Liab Met Amt: " . edih_format_money($sar[21]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[22]) && $sar[22] ? $tr1 . "Total MSP Pt Reimb Amt: " . edih_format_money($sar[22]) . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[23]) && $sar[23] ? $tr1 . "Total PIP Claim Count: " . $sar[23] . $tr2 . PHP_EOL : "";
                $lx_html .= isset($sar[24]) && $sar[24] ? $tr1 . "Total PIP Claim Count: " . edih_format_money($sar[24]) . $tr2 . PHP_EOL : "";
                //
                continue;
            }
            //
            if (strncmp('TS2' . $de, $seg, 4) === 0) {
                csv_edihist_log("edih_835_transaction_html: segment TS2 present in {$fn}");
                // Medicare Part A
                $tr1 = "<tr class='{$cls}'><td>&gt;</td><td colspan=3>";
                $tr2 = "</td></tr>" . PHP_EOL;
                //
                $lx_html .= isset($sar[1]) && $sar[1] ? $tr1 . "Total DRG Amt: " . edih_format_money($sar[1]) . $tr2 : "";
                $lx_html .= isset($sar[2]) && $sar[2] ? $tr1 . "Total Fed Specific Amt: " . edih_format_money($sar[2]) . $tr2 : "";
                $lx_html .= isset($sar[3]) && $sar[3] ? $tr1 . "Total Hosp Specific Amt: " . edih_format_money($sar[3]) . $tr2 : "";
                $lx_html .= isset($sar[4]) && $sar[4] ? $tr1 . "Total DSP Share Amt: " . edih_format_money($sar[4]) . $tr2 : "";
                $lx_html .= isset($sar[5]) && $sar[5] ? $tr1 . "Total Capital Amt: " . edih_format_money($sar[5]) . $tr2 : "";
                $lx_html .= isset($sar[6]) && $sar[6] ? $tr1 . "Total Ind Med Edu Amt: " . edih_format_money($sar[6]) . $tr2 : "";
                $lx_html .= isset($sar[7]) && $sar[7] ? $tr1 . "Total Outlier Day Amt: " . edih_format_money($sar[7]) . $tr2 : "";
                $lx_html .= isset($sar[8]) && $sar[8] ? $tr1 . "Total Day Outlier Day Amt: " . edih_format_money($sar[8]) . $tr2 : "";
                $lx_html .= isset($sar[9]) && $sar[9] ? $tr1 . "Total Cost Outlier Day Amt: " . edih_format_money($sar[9]) . $tr2 : "";
                $lx_html .= isset($sar[10]) && $sar[10] ? $tr1 . "Avg DRG Length of Stay: " . $sar[10] . $tr2 : "";
                $lx_html .= isset($sar[11]) && $sar[11] ? $tr1 . "Total Discharge Count: " . $sar[11] . $tr2 : "";
                $lx_html .= isset($sar[12]) && $sar[12] ? $tr1 . "Total Cost Rpt Day Count: " . $sar[12] . $tr2 : "";
                $lx_html .= isset($sar[13]) && $sar[13] ? $tr1 . "Total Covered Day Count: " . $sar[13] . $tr2 : "";
                $lx_html .= isset($sar[14]) && $sar[14] ? $tr1 . "Total Non Covered Day Count: " . $sar[14] . $tr2 : "";
                $lx_html .= isset($sar[15]) && $sar[15] ? $tr1 . "Total MSP Pass-Thru Amt: " . edih_format_money($sar[15]) . $tr2 : "";
                $lx_html .= isset($sar[16]) && $sar[16] ? $tr1 . "Avg DRG Weight: " . $sar[16] . $tr2 : "";
                $lx_html .= isset($sar[17]) && $sar[17] ? $tr1 . "Total PPS Capital FSP DRG Amt: " . edih_format_money($sar[17]) . $tr2 : "";
                $lx_html .= isset($sar[18]) && $sar[18] ? $tr1 . "Total PPS Capital FSP HSP Amt: " . edih_format_money($sar[18]) . $tr2 : "";
                $lx_html .= isset($sar[19]) && $sar[19] ? $tr1 . "Total PPS DSH DRG Amt: " . edih_format_money($sar[19]) . $tr2 : "";
                //
                continue;
            }
            if (strncmp('PLB' . $de, $seg, 4) === 0) {
                // can signal end of claim transaction
                $loopid = 'summary';
                $cls = 'pmt';
                //if (count($clpsegs)) {
                //$clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
                //$clpsegs = array();
                //}
                //
                $sar = explode($de, $seg);
                // provider ID and fiscal year end date
                $plb01 = isset($sar[1]) && $sar[1] ? $sar[1] : "";
                $plb02 = isset($sar[2]) && $sar[2] ? edih_format_date($sar[2]) : "";
                //
                $pmt_html .= "<tr class='{$cls}'><td><em>Provider</em></td><td colspan=3>{$plb01} {$plb02}</td></tr>" . PHP_EOL;
                //
                $plbar = array_slice($sar, 2);
                $plbar = array_chunk($plbar, 2);
                // reason code and amount
                foreach ($plbar as $plb) {
                    foreach ($plb as $k => $p) {
                        // PLB 3, 5, 7, 9, 11, 13
                        // composite element 'code:reference'
                        if ($k == 0) {
                            if ($p && strpos($p, $ds)) {
                                $plb_rc = substr($p, 0, strpos($p, $ds));
                                // code
                                $plb_tr = substr($p, strpos($p, $ds) + 1);
                                // reference (case #)?
                            } else {
                                $plb_rc = $p ? $p : "";
                                $plb_tr = "";
                            }
                            $plb_rt = $plb_rc ? $cd835->get_835_code('PLB', $plb_rc) : "";
                        } else {
                            // PLB 4, 6, 8, 10, 12, 14
                            // monetary amount
                            $plb_amt = $p ? edih_format_money($p) : "";
                            $acctng['plbadj'] += $p ? (double) $p : 0;
                        }
                    }
                    $pmt_html .= "<tr class='{$cls}'><td>{$plb_tr}</td><td colspan=3>{$plb_rc} {$plb_rt} {$plb_amt}</td></tr>" . PHP_EOL;
                }
                //
                continue;
            }
            //
            if (strncmp('SE' . $de, $seg, 3) === 0) {
                // end of payment transaction, so create the html page
                $loopid = 'trailer';
                $cls = 'pmt';
                // include our accounting totals
                if (is_array($acctng) && count($acctng)) {
                    array_walk($acctng, 'edih_round_cb');
                    $bal = $acctng['fee'] == $acctng['pmt'] + $acctng['clmadj'] + $acctng['svcadj'] + $acctng['svcptrsp'] + $acctng['plbadj'] ? "Balanced" : "Not Balanced";
                    $acct_str = "{$bal}: <em>Fee</em> {$acctng['fee']} <em>Pmt</em> {$acctng['pmt']} ";
                    $acct_str .= "<em>ClpAdj</em> {$acctng['clmadj']} <em>SvcAdj</em> {$acctng['svcadj']} ";
                    $acct_str .= "<em>PtRsp</em> {$acctng['ptrsp']} (<em>svcPtRsp</em> {$acctng['svcptrsp']}) <em>PlbAdj</em> {$acctng['plbadj']} ";
                    //
                    $pmt_html .= "<tr class='{$cls}'><td colspan=4>{$acct_str}</td></tr>" . PHP_EOL;
                }
                //
                // create the html page
                $str_html .= "<table id={$tblid} class='h835' columns=4><caption>{$capstr}</caption>" . PHP_EOL;
                $str_html .= $hdr_html;
                if ($pmt_html) {
                    $str_html .= $pmt_html;
                    $pmt_html = "";
                }
                if ($src_html) {
                    $str_html .= $src_html;
                    $src_html = "";
                }
                if ($rcv_html) {
                    $str_html .= $rcv_html;
                    $rcv_html = "";
                }
                //
                if (count($lx_ar)) {
                    // claim segments are in lx array
                    // make sure we have current collection
                    if ($lxkey && array_key_exists($lxkey, $lx_ar)) {
                        if (count($clpsegs)) {
                            $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
                            $clpsegs = array();
                        }
                        // note: table ending in CLP if stanza
                        $nlx_html = "<table name='lx_{$lxkey}' class='h835c' columns=4>" . PHP_EOL . "<tbody>" . PHP_EOL . $lx_html . PHP_EOL;
                        $lx_ar[$lxkey]['lx'] = $nlx_html;
                        $lx_ar[$lxkey]['clp'] = $clp_html;
                        $lx_html = "";
                        $clp_html = "";
                        $clpsegs = array();
                    }
                    // append segments to html
                    foreach ($lx_ar as $key => $val) {
                        $str_html .= $val['lx'];
                        $str_html .= $val['clp'];
                    }
                } elseif ($lx_html) {
                    $str_html .= $lx_html;
                    $lx_html = "";
                }
                //
                if (count($clpsegs)) {
                    // would be captured in LX and lx array
                    $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
                    $clpsegs = array();
                }
                if ($clp_html) {
                    $str_html .= $clp_html;
                    $clp_html = "";
                }
                if ($trl_html) {
                    $str_html .= $trl_html;
                    $trl_html = "";
                }
                //$str_html .= "</tbody>".PHP_EOL."</table>".PHP_EOL;
                //
                continue;
            }
            if (strncmp('CLP' . $de, $seg, 4) === 0) {
                if ($loopid == '1000B') {
                    // end of 1000B (receiver) loop
                    $rcv_html .= $clp_ct ? "" : "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
                } elseif ($loopid == '2000') {
                    // end of LX header (LX TS3 TS2 claim grouping)
                    $lx_html .= $clp_ct ? "" : "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
                }
                $loopid = '2100';
                //array('pmt'=>0, 'clmpmt'=>0, 'clmadj'=0, 'prvadj'=>0, 'ptrsp'=>0,'lx'=>array());
                $sar = explode($de, $seg);
                $acctng['fee'] += isset($sar[3]) && $sar[3] ? (double) $sar[3] : 0;
                $acctng['clmpmt'] += isset($sar[4]) && $sar[4] ? (double) $sar[4] : 0;
                $acctng['ptrsp'] += isset($sar[5]) && $sar[5] ? (double) $sar[5] : 0;
                //
                if (count($clpsegs)) {
                    $clp_html .= edih_835_transaction_html($clpsegs, $codes27x, $codes835, $delimiters);
                }
                $clpsegs = array();
                $clpsegs[] = $seg;
                $clp_ct++;
                continue;
            }
            if (strncmp('SVC' . $de, $seg, 4) === 0) {
                $loopid = '2110';
                $sar = explode($de, $seg);
                $pmtm = $pmts = 1;
                foreach ($sar as $k => $v) {
                    if ($k == 2) {
                        $svcfee = $v ? (double) $v : 0;
                    } elseif ($k == 3) {
                        $svcpmt = $v ? (double) $v : 0;
                    } elseif ($k == 5) {
                        $pmtm = $v ? (int) $v : 1;
                    } elseif ($k == 7) {
                        $pmts = $v ? (int) $v : 1;
                    }
                }
                $acctng['svcfee'] += $svcfee * $pmts;
                $acctng['svcpmt'] += $svcpmt * $pmtm;
                //
                $clpsegs[] = $seg;
                continue;
            }
            if (strncmp('CAS' . $de, $seg, 4) === 0) {
                $sar = explode($de, $seg);
                // category
                $ctg = isset($sar[1]) && $sar[1] ? $sar[1] : 'CO';
                // slice sar array to get triplet elements
                // chunk into triplets
                $sar1 = array_slice($sar, 2);
                $sar1 = array_chunk($sar1, 3);
                //
                foreach ($sar1 as $cas) {
                    $cav = 0;
                    $cq = '';
                    foreach ($cas as $k => $v) {
                        if ($k == 1) {
                            // monetary amount elem 3, 6, 9, 12, 15, 18
                            $cav = $v ? $v : 0;
                        } elseif ($k == 2) {
                            // quantity elem 4, 7, 10, 13, 16, 19
                            $cq = $v ? $v : "";
                            if ($cq && strcmp($cq, '1') > 0) {
                                $cav = $cav * $cq;
                            }
                        }
                    }
                    if ($ctg == 'PR') {
                        $acctng['svcptrsp'] += $cav ? (double) $cav : 0;
                    } else {
                        $acctky = $loopid == '2100' ? 'clmadj' : 'svcadj';
                        $acctng[$acctky] += $cav ? (double) $cav : 0;
                    }
                }
                $clpsegs[] = $seg;
                continue;
            }
            // uncaught segments should be routed by this
            if ($loopid == '2100' || $loopid == '2110') {
                $clpsegs[] = $seg;
                continue;
            }
        }
        // end foreach(trans as seg)
    }
    // end foreach(trans_ar as trans)
    //
    return $str_html;
}