Improved Code Sniffing (#928)
[openemr.git] / library / edi.inc
blobe0fff1d424164420071d86c088ab544fc97172b2
1 <?php
2 /*
3  * Inc file for the 270 / 271 creation and uploading
4  *
5  * This program creates the segments for the x12 270 eligibility file
6  * It also allows the reading and storing of the x12 271 file
7  *
8  * Copyright (C) 2016 Terry Hill <terry@lillysystems.com>
9  * Copyright (C) 2010 MMF Systems, Inc
10  *
11  * LICENSE: This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 3
14  * of the License, or (at your option) any later version.
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see http://opensource.org/licenses/gpl-license.php.
21  *
22  * @package OpenEMR
23  * @author Terry Hill <terry@lilysystems.com>
24  * @link http://www.open-emr.org
25  */
27 // SEGMENT FUNCTION START
29 // ISA Segment  - EDI-270 format
31 function create_ISA($row,$X12info,$segTer,$compEleSep)
34     $ISA     =  array();
36     $ISA[0] = "ISA";                            // Interchange Control Header Segment ID
38     $ISA[1] = "00";                                 // Author Info Qualifier
40     $ISA[2] = str_pad("0000000",10," ");        // Author Information
42     $ISA[3] = "00";                                 //   Security Information Qualifier
43                                                 //   MEDI-CAL NOTE: For Leased-Line & Dial-Up use '01',
44                                                 //   for BATCH use '00'.
45                                                 //   '00' No Security Information Present
46                                                 //   (No Meaningful Information in I04)
48     $ISA[4] = str_pad("0000000000",10," ");         // Security Information
50     $ISA[5] = str_pad("ZZ",2," ");              // Interchange ID Qualifier
52     $ISA[6] = str_pad($X12info[2],15," ");      // INTERCHANGE SENDER ID
54     $ISA[7] = str_pad("ZZ",2," ");              // Interchange ID Qualifier
56     $ISA[8] = str_pad($X12info[3],15," ");      // INTERCHANGE RECEIVER ID
58     $ISA[9] = str_pad(date('ymd'),6," ");       // Interchange Date (YYMMDD)
60     $ISA[10] = str_pad(date('Hi'),4," ");       // Interchange Time (HHMM)
62     $ISA[11] = "^";                                 // Interchange Control Standards Identifier
64     $ISA[12] = str_pad("00501",5," ");          // Interchange Control Version Number
66     $ISA[13] = str_pad("000000001",9," ");      // INTERCHANGE CONTROL NUMBER
68     $ISA[14] = str_pad("1",1," ");              // Acknowledgment Request [0= not requested, 1= requested]
70     $ISA[15] =  str_pad("P",1," ");                 // Usage Indicator [ P = Production Data, T = Test Data ]
72     $ISA['Created'] = implode('*', $ISA);       // Data Element Separator
74     $ISA['Created'] = $ISA['Created'] ."*";
76     $ISA['Created'] = $ISA ['Created'] . $compEleSep . $segTer;
78     return trim($ISA['Created']);
82 // GS Segment  - EDI-270 format
84 function create_GS($row,$X12info,$segTer,$compEleSep)
87     $GS    = array();
89     $GS[0] = "GS";                      // Functional Group Header Segment ID
91     $GS[1] = "HS";                      // Functional ID Code [ HS = Eligibility, Coverage or Benefit Inquiry (270) ]
93     $GS[2] =  $X12info[2];              // Application Sender's ID
95     $GS[3] =  $X12info[3];              // Application Receiver's ID
97     $GS[4] = date('Ymd');               // Date [CCYYMMDD]
99     $GS[5] = date('His');               // Time [HHMM] Group Creation Time
101     $GS[6] = "2";                       // Group Control Number No zeros for 5010
103     $GS[7] = "X";                   // Responsible Agency Code Accredited Standards Committee X12 ]
105     $GS[8] = "005010X279A1";            // Version Release / Industry[ Identifier Code Query 005010X279A1
107     $GS['Created'] = implode('*', $GS);         // Data Element Separator
109     $GS['Created'] = $GS ['Created'] . $segTer;  // change the information in the tag or change the tag
111     return trim($GS['Created']);
115 // ST Segment  - EDI-270 format
117 function create_ST($row,$X12info,$segTer,$compEleSep)
120     $ST    =    array();
122     $ST[0] = "ST";                              // Transaction Set Header Segment ID
124     $ST[1] = "270";                                 // Transaction Set Identifier Code (Inquiry Request)
126     $ST[2] = "000000003";                       // Transaction Set Control Number - Must match SE's
128     $ST[3] = "005010X279A1";                    // Standard 005010X279A1 in $ST[3]
130     $ST['Created'] = implode('*', $ST);             // Data Element Separator
132     $ST['Created'] = $ST ['Created'] . $segTer;
134     return trim($ST['Created']);
138 // BHT Segment  - EDI-270 format
140 function create_BHT($row,$X12info,$segTer,$compEleSep)
143     $BHT    =   array();
145     $BHT[0] = "BHT";                        // Beginning of Hierarchical Transaction Segment ID
147     $BHT[1] = "0022";                       // Subscriber Structure Code
149     $BHT[2] = "13";                             // Purpose Code - This is a Request
151     $BHT[3] = "PROVTest600";                //  Submitter Transaction Identifier
152                                             //This information is required by the information Receiver
153                                             //when using Real Time transactions.
154                                             //For BATCH this can be used for optional information.
156     $BHT[4] = str_pad(date('Ymd'),8," ");           // Date Transaction Set Created
158     $BHT[5] = str_pad(date('Hi'),4," ");            // Time Transaction Set Created no space after and 1300 is plenty
160     $BHT['Created'] = implode('*', $BHT);           // Data Element Separator
162     $BHT['Created'] = $BHT ['Created'] . $segTer;
164     return trim($BHT['Created']);
168 // HL Segment  - EDI-270 format
170 function create_HL($row, $nHlCounter,$X12info,$segTer,$compEleSep)
173     $HL         = array();
175     $HL[0]      = "HL";             // Hierarchical Level Segment ID
176     $HL_LEN[0]  =  2;
178     $HL[1] = $nHlCounter;       // Hierarchical ID No.
180     if($nHlCounter == 1)
181     {
182         $HL[2] = "";
183         $HL[3] = 20;            // Description: Identifies the payor, maintainer, or source of the information.
184         $HL[4] = 1;                 // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure.
185     }
186     else if($nHlCounter == 2)
187     {
188         $HL[2] = 1;                 // Hierarchical Parent ID Number
189         $HL[3] = 21;            // Hierarchical Level Code. '21' Information Receiver
190         $HL[4] = 1;                 // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure.
191     }
192     else
193     {
194         $HL[2] = 2;
195         $HL[3] = 22;            // Hierarchical Level Code.'22' Subscriber
196         $HL[4] = 0;                 // 0 no Additional Subordinate in the Hierarchical Structure.
197     }
199     $HL['Created'] = implode('*', $HL);         // Data Element Separator
201     $HL['Created'] = $HL ['Created'] . $segTer;
203     return trim($HL['Created']);
207 // NM1 Segment  - EDI-270 format
209 function create_NM1($row,$nm1Cast,$X12info,$segTer,$compEleSep)
212     $NM1        = array();
214     $NM1[0]         = "NM1";                    // Subscriber Name Segment ID
216     if($nm1Cast == 'PR')
217     {
218         $NM1[1] = "PR";                         // Entity ID Code - Payer [PR Payer]
219         $NM1[2] = "2";                      // Entity Type - Non-Person
220         $NM1[3] = $row["payer_name"];       // Organizational Name
221         $NM1[4] = "";                       // Data Element not required.
222         $NM1[5] = "";                       // Data Element not required.
223         $NM1[6] = "";                       // Data Element not required.
224         $NM1[7] = "";                       // Data Element not required.
225         $NM1[8] = "46";                         // 46 - Electronic Transmitter Identification Number (ETIN)
226         $NM1[9] = $X12info[3];              // Application Sender's ID
227     }
228     else if($nm1Cast == 'FA')
229     {
230         $NM1[1] = "FA";                         // Entity ID Code - Facility [FA Facility]
231         $NM1[2] = "2";                      // Entity Type - Non-Person
232         $NM1[3] = $row['facility_name'];            // Organizational Name
233         $NM1[4] = "";           // Data Element not required.
234         $NM1[5] = "";           // Data Element not required.
235         $NM1[6] = "";                       // Data Element not required.
236         $NM1[7] = "";                       // Data Element not required.
237         $NM1[8] = "FI";
238         $NM1[9] = $row['facility_npi'];
239     }
240     else if($nm1Cast == 'IL')
241     {
242         $NM1[1] = "IL";                         // Insured or Subscriber
243         $NM1[2] = "1";                      // Entity Type - Person
244         $NM1[3] = $row['lname'];                // last Name
245         $NM1[4] = $row['fname'];                // first Name
246         $NM1[5] = $row['mname'];                // middle Name
247         $NM1[6] = "";                       // data element
248         $NM1[7] = "";                       // data element
249         $NM1[8] = "MI";                         // Identification Code Qualifier
250         $NM1[9] = $row['subscriber_ss'];            // Identification Code
251     }
253     $NM1['Created'] = implode('*', $NM1);               // Data Element Separator
255     $NM1['Created'] = $NM1['Created'] . $segTer;
257     return trim($NM1['Created']);
261 // REF Segment  - EDI-270 format
263 function create_REF($row,$ref,$X12info,$segTer,$compEleSep)
266     $REF    =   array();
268     $REF[0] = "REF";                        // Subscriber Additional Identification    does not want this for anything
270     if($ref == '1P')
271     {
272         $REF[1] = "4A";                         // Reference Identification Qualifier
273         $REF[2] = $row['provider_pin'];                 // Provider Pin.
274     }
275     else
276     {
277         $REF[1] = "EJ";                         // 'EJ' for Patient Account Number     does not want this for patient
278         $REF[2] = $row['pid'];                  // Patient Account No.
279     }
280     $REF['Created'] = implode('*', $REF);               // Data Element Separator
282     $REF['Created'] = $REF['Created'] . $segTer;
284     return trim($REF['Created']);
288 // TRN Segment - EDI-270 format
290 function create_TRN($row,$tracno,$refiden,$X12info,$segTer,$compEleSep)
293     $TRN    =   array();
295     $TRN[0] = "TRN";                        // Subscriber Trace Number Segment ID
297     $TRN[1] = "1";                          // Trace Type Code ï¿½ Current Transaction Trace Numbers
299     $TRN[2] = $tracno;                      // Trace Number
301     $TRN[3] = "9000000000";                         // Originating Company ID ï¿½ must be 10 positions in length
303     $TRN[4] = $refiden;                         // Additional Entity Identifier (i.e. Subdivision)
305     $TRN['Created'] = implode('*', $TRN);               // Data Element Separator
307     $TRN['Created'] = $TRN['Created'] . $segTer;
309     return trim($TRN['Created']);
313 // DMG Segment - EDI-270 format
315 function create_DMG($row,$X12info,$segTer,$compEleSep)
318     $DMG    =   array();
320     $DMG[0] = "DMG";                            // Date or Time or Period Segment ID
322     $DMG[1] = "D8";                                 // Date Format Qualifier - (D8 means CCYYMMDD)
324     $DMG[2] = $row['dob'];                      // Subscriber's Birth date
326     $DMG['Created'] = implode('*', $DMG);       // Data Element Separator
328     $DMG['Created'] = $DMG['Created'] .  $segTer;
330     return trim($DMG['Created']);
333 // DTP Segment - EDI-270 format
335 function create_DTP($row,$qual,$X12info,$segTer,$compEleSep)
338     $DTP    =   array();
340     $DTP[0] = "DTP";                        // Date or Time or Period Segment ID
342     $DTP[1] = $qual;                        // Qualifier - Date of Service
344     $DTP[2] = "D8";                             // Date Format Qualifier - (D8 means CCYYMMDD)
346     if($qual == '102'){
347         $DTP[3] = $row['date'];                 // Date
348     }else{
349         $DTP[3] = $row['pc_eventDate'];         // Date of Service
350     }
351     $DTP['Created'] = implode('*', $DTP);   // Data Element Separator
353     $DTP['Created'] = $DTP['Created'] .  $segTer;
355     return trim($DTP['Created']);
358 // EQ Segment - EDI-270 format
360 function create_EQ($row,$X12info,$segTer,$compEleSep)
363     $EQ         =   array();
365     $EQ[0]  = "EQ";                                     // Subscriber Eligibility or Benefit Inquiry Information
367     $EQ[1]  = "30";                                     // Service Type Code
369     $EQ['Created'] = implode('*', $EQ);                 // Data Element Separator
371     $EQ['Created'] = $EQ['Created'] . $segTer;
373     return trim($EQ['Created']);
376 // SE Segment - EDI-270 format
378 function create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep)
381     $SE     =   array();
383     $SE[0] = "SE";                              // Transaction Set Trailer Segment ID
385     $SE[1] = $segmentcount;                         // Segment Count
387     $SE[2] = "000000003";                       // Transaction Set Control Number - Must match ST's
389     $SE['Created'] = implode('*', $SE);             // Data Element Separator
391     $SE['Created'] = $SE['Created'] . $segTer;
393     return trim($SE['Created']);
396 // GE Segment - EDI-270 format
398 function create_GE($row,$X12info,$segTer,$compEleSep)
401     $GE     =   array();
403     $GE[0]  = "GE";                             // Functional Group Trailer Segment ID
405     $GE[1]  = "1";                          // Number of included Transaction Sets
407     $GE[2]  = "2";                          // Group Control Number
409     $GE['Created'] = implode('*', $GE);                 // Data Element Separator
411     $GE['Created'] = $GE['Created'] . $segTer;
413     return trim($GE['Created']);
416 // IEA Segment - EDI-270 format
418 function create_IEA($row,$X12info,$segTer,$compEleSep)
421     $IEA    =   array();
423     $IEA[0] = "IEA";                        // Interchange Control Trailer Segment ID
425     $IEA[1] = "1";                          // Number of included Functional Groups
427     $IEA[2] = "000000001";                      // Interchange Control Number
429     $IEA['Created'] = implode('*', $IEA);
431     $IEA['Created'] = $IEA['Created'] .  $segTer;
433     return trim($IEA['Created']);
436 function translate_relationship($relationship)
438     switch ($relationship) {
439         case "spouse":
440             return "01";
441             break;
442         case "child":
443             return "19";
444             break;
445         case "self":
446         default:
447             return "S";
448     }
451 // EDI-270 Batch file Generation
453 function print_elig($res,$X12info,$segTer,$compEleSep)
456     $i=1;
458     $PATEDI    = "";
460     // For Header Segment
462     $nHlCounter = 1;
463     $rowCount   = 0;
464     $trcNo      = 1234501;
465     $refiden    = 5432101;
467     while ($row = sqlFetchArray($res))
468     {
470         if($nHlCounter == 1)
471         {
472             // create ISA
473             $PATEDI    = create_ISA($row,$X12info,$segTer,$compEleSep);
475             // create GS
476             $PATEDI   .= create_GS($row,$X12info,$segTer,$compEleSep);
478             // create ST
479             $PATEDI   .= create_ST($row,$X12info,$segTer,$compEleSep);
481             // create BHT
482             $PATEDI   .= create_BHT($row,$X12info,$segTer,$compEleSep);
484             // For Payer Segment
486             $PATEDI  .= create_HL($row,1,$X12info,$segTer,$compEleSep);
487             $PATEDI  .= create_NM1($row,'PR',$X12info,$segTer,$compEleSep);
489             // For Provider Segment
491             $PATEDI  .= create_HL($row,2,$X12info,$segTer,$compEleSep);
492             $PATEDI  .= create_NM1($row,'FA',$X12info,$segTer,$compEleSep);
494             $nHlCounter = $nHlCounter + 2;
495             $segmentcount = 7; // segement counts - start from ST
496         }
498         // For Subscriber Segment
500         $PATEDI  .= create_HL($row,$nHlCounter,$X12info,$segTer,$compEleSep);
501         $PATEDI  .= create_NM1($row,'IL',$X12info,$segTer,$compEleSep);
502         $PATEDI  .= create_DMG($row,$X12info,$segTer,$compEleSep);
504         $PATEDI  .= create_DTP($row,'291',$X12info,$segTer,$compEleSep);
505         $PATEDI  .= create_EQ($row,$X12info,$segTer,$compEleSep);
507         $segmentcount   = $segmentcount + 7;
508         $nHlCounter     = $nHlCounter + 1;
509         $rowCount   = $rowCount + 1;
510         $trcNo      = $trcNo + 1;
511         $refiden    = $refiden + 1;
514         if($rowCount == sqlNumRows($res))
515         {
516             $segmentcount = $segmentcount + 1;
517             $PATEDI   .= create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep);
518             $PATEDI   .= create_GE($row,$X12info,$segTer,$compEleSep);
519             $PATEDI   .= create_IEA($row,$X12info,$segTer,$compEleSep);
520         }
521     }
523     echo $PATEDI;
526 // Report Generation
528 function show_elig($res,$X12info,$segTer,$compEleSep)
531     $i=0;
532     echo "      <div id='report_results'>
533                         <table>
534                                 <thead>
536                                         <th style='width:12%;'> ". htmlspecialchars( xl('Facility Name'), ENT_NOQUOTES) ."</th>
537                                         <th style='width:9%;' > ". htmlspecialchars( xl('Facility NPI'), ENT_NOQUOTES) ."</th>
538                                         <th style='width:15%;'> ". htmlspecialchars( xl('Insurance Comp'), ENT_NOQUOTES) ."</th>
539                                         <th style='width:8%;' > ". htmlspecialchars( xl('Policy No'), ENT_NOQUOTES) ."</th>
540                                         <th style='width:16%;'> ". htmlspecialchars( xl('Patient Name'), ENT_NOQUOTES) ."</th>
541                                         <th style='width:7%;' > ". htmlspecialchars( xl('DOB'), ENT_NOQUOTES) ."</th>
542                                         <th style='width:6%;' > ". htmlspecialchars( xl('Gender'), ENT_NOQUOTES) ."</th>
543                                         <th style='width:9%;' > ". htmlspecialchars( xl('SSN'), ENT_NOQUOTES) ."</th>
544                                         <th style='width:2%;' > &nbsp;                    </th>
545                                 </thead>
547                                 <tbody>
549                 ";
551     while ($row = sqlFetchArray($res)) {
554         $i= $i+1;
556         if($i%2 == 0){
557             $background = '#FFF';
558         }else{
559             $background = '#FFF';
560         }
562         $elig     = array();
563         $elig[0]  = $row['facility_name'];              // Inquiring Provider Name  calendadr
564         $elig[1]  = $row['facility_npi'];               // Inquiring Provider NPI
565         $elig[2]  = $row['payer_name'];                     // Payer Name  our insurance co name
566         $elig[3]  = $row['policy_number'];              // Subscriber ID
567         $elig[4]  = $row['subscriber_lname'];               // Subscriber Last Name
568         $elig[5]  = $row['subscriber_fname'];               // Subscriber First Name
569         $elig[6]  = $row['subscriber_mname'];               // Subscriber Middle Initial
570         $elig[7]  = $row['subscriber_dob'];                 // Subscriber Date of Birth
571         $elig[8]  = substr($row['subscriber_sex'], 0, 1);       // Subscriber Sex
572         $elig[9]  = $row['subscriber_ss'];              // Subscriber SSN
573         $elig[10] = translate_relationship($row['subscriber_relationship']);    // Pt Relationship to insured
574         $elig[11] = $row['lname'];                  // Dependent Last Name
575         $elig[12] = $row['fname'];                  // Dependent First Name
576         $elig[13] = $row['mname'];                  // Dependent Middle Initial
577         $elig[14] = $row['dob'];                    // Dependent Date of Birth
578         $elig[15] = substr($row['sex'], 0, 1);              // Dependent Sex
579         $elig[16] = $row['pc_eventDate'];               // Date of service
580         $elig[17] = "30";                       // Service Type
581         $elig[18] = $row['pubpid'];                     // Patient Account Number pubpid
583         echo "  <tr id='PR".$i."_". htmlspecialchars( $row['policy_number'], ENT_QUOTES)."'>
584                                 <td class ='detail' style='width:12%;'>". htmlspecialchars( $row['facility_name'], ENT_NOQUOTES) ."</td>
585                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['facility_npi'], ENT_NOQUOTES) ."</td>
586                                 <td class ='detail' style='width:15%;'>". htmlspecialchars( $row['payer_name'], ENT_NOQUOTES) ."</td>
587                                 <td class ='detail' style='width:8%;'>".  htmlspecialchars( $row['policy_number'], ENT_NOQUOTES) ."</td>
588                                 <td class ='detail' style='width:16%;'>". htmlspecialchars( $row['subscriber_lname']." ".$row['subscriber_fname'], ENT_NOQUOTES) ."</td>
589                                 <td class ='detail' style='width:7%;'>".  htmlspecialchars( $row['subscriber_dob'], ENT_NOQUOTES) ."</td>
590                                 <td class ='detail' style='width:6%;'>".  htmlspecialchars( $row['subscriber_sex'], ENT_NOQUOTES) ."</td>
591                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['subscriber_ss'], ENT_NOQUOTES) ."</td>
592                                 <td class ='detail' style='width:2%;'>
593                                         <img src='../../images/deleteBtn.png' title=' .htmlspecialchars( xl('Delete Row'), ENT_QUOTES) . ' style='cursor:pointer;cursor:hand;' onclick='deletetherow(\"" . $i."_". htmlspecialchars( $row['policy_number'], ENT_QUOTES) . "\")'>
594                                 </td>
595                         </tr>
596                 ";
599         unset($elig);
600     }
602     if($i==0){
604         echo "  <tr>
605                                 <td class='norecord' colspan=9>
606                                         <div style='padding:5px;font-family:arial;font-size:13px;text-align:center;'>". htmlspecialchars( xl('No records found'), ENT_NOQUOTES) . "</div>
607                                 </td>
608                         </tr>   ";
609     }
610         echo "  </tbody>
611                         </table>";
614 // To Show Eligibility Verification data
616 function show_eligibility_information($pid)
619     $query = "  SELECT          eligr.response_description as ResponseMessage,
620                                         DATE_FORMAT(eligv.eligibility_check_date, '%d %M %Y') as VerificationDate,
621                                         eligv.copay, eligv.deductible, eligv.deductiblemet,
622                                         if(eligr.response_status = 'A','Active','Inactive') as Status,
623                                         insd.pid, insc.name
624                         FROM            eligibility_verification eligv
625                         INNER JOIN      eligibility_response eligr on eligr.response_id = eligv.response_id
626                         INNER JOIN      insurance_data insd on insd.id = eligv.insurance_id
627                         INNER JOIN      insurance_companies insc on insc.id = insd.provider
628                         WHERE           insd.pid = ?
629                         AND                     eligr.response_status = 'A'
630                         AND                     eligv.eligibility_check_date = (SELECT max(eligibility_check_date)
631                                                                                 FROM eligibility_verification
632                                                                                 WHERE   insurance_id = eligv.insurance_id)";
633     $result         = sqlStatement($query, array($pid) );
635     $row = sqlFetchArray($result);
637         $showString="";
638     $showString .=  "<br><div class='text'>" .
639             "<b>" .
640             htmlspecialchars( xl('Insurance Provider'), ENT_NOQUOTES) . ":</b> " .
641             (!empty($row['name']) ? htmlspecialchars( $row['name'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
642             "<br>\n" .
643             "<b>" .
644             htmlspecialchars( xl('Status'), ENT_NOQUOTES) . ":</b> " .
645             (!empty($row['ResponseMessage']) ? htmlspecialchars( $row['ResponseMessage'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
646             "<br>\n" .
647             "<b>" .
648             htmlspecialchars( xl('Last Verified On'), ENT_NOQUOTES) . ":</b> " .
649             (!empty($row['VerificationDate']) ? htmlspecialchars( $row['VerificationDate'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
650             "<br>" .
651             "<b>" . htmlspecialchars( xl('Copay'), ENT_NOQUOTES) . ":</b> " .
652             (!empty($row['copay']) ? htmlspecialchars( $row['copay'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
653             "<br><b>" . htmlspecialchars( xl('Deductible'), ENT_NOQUOTES) . ":</b> " .
654             (!empty($row['deductible']) ? htmlspecialchars( $row['deductible'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
655             "<br><b>" . htmlspecialchars( xl('Deductible Met'), ENT_NOQUOTES) . ":</b> " .
656             (!empty($row['deductiblemet']) ? ($row['deductiblemet'] == 'Y' ? htmlspecialchars( xl('Yes'), ENT_NOQUOTES) : htmlspecialchars( xl('No'), ENT_NOQUOTES)) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
657             "</div>";
659     echo $showString;
662 // For EDI 271
665 // Function to save the values in eligibility_response table
667 function eligibility_response_save($segmentVal,$vendorID)
670     $resCount = 0;
672     $query = "  SELECT   count(*) as RecordsCount
673                                 FROM     eligibility_response
674                                 WHERE    response_description = ? and
675                                          response_vendor_id       = ?";
677     $resCount = sqlStatement($query, array($segmentVal, $vendorID) );
680     if(isset($resCount))
681     {
683         $row        = sqlFetchArray($resCount);
685         $resCount   = $row['RecordsCount'];
687     }
689     if($resCount == 0)
690     {
692         $query = "INSERT into eligibility_response SET  response_description =?,
693                                                                 response_vendor_id       = ?,
694                                                                 response_create_date = now(),
695                                                                 response_modify_date = now()";
696         $res    = sqlStatement($query, array($segmentVal, $vendorID) );
697     }
701 // Function to save the values in eligibility_verification table
703 function eligibility_verification_save($segmentVal,$x12PartnerId,$patientId)
706     $resCount = 0;
708     // For fetching the response Id
710     $query = "  SELECT   response_id
711                                 FROM     eligibility_response
712                                 WHERE    response_description = ? and
713                                                  response_vendor_id       = ?";
715     $resId  = sqlStatement($query, array($segmentVal, $x12PartnerId) );
717     // For fetching the insuarace data Id
719     $query = "  SELECT   id,copay
720                                 FROM     insurance_data
721                                 WHERE    type = 'primary' and
722                                                  pid = ?";
724     $insId  = sqlStatement($query, array($patientId) );
726     if(isset($resId))
727     {
729         $row        = sqlFetchArray($resId);
731         $responseId     = $row['response_id'];
733     }
734     if(isset($insId))
735     {
737         $row        = sqlFetchArray($insId);
739         $insuranceId    = $row['id'];
740         $copay      = $row['copay'];
742     }
744     if($resCount == 0)
745     {
747         if(isset($insuranceId) && !empty($insuranceId)){
749             //Set up the sql variable binding array (this prevents sql-injection attacks)
750             $sqlBindArray = array();
751             $query = "INSERT into eligibility_verification SET  response_id     = ? ,
752                                                                                 insurance_id    = ?,";
753             array_push($sqlBindArray, $responseId, $insuranceId);
755             if(!empty($copay))
756             {
757                 $query .= "copay = ?,";
758                 array_push($sqlBindArray, $copay);
759             }
760             $query .= "eligibility_check_date   = now(),
761                                    create_date                  = now()";
762             $res    = sqlStatement($query, $sqlBindArray);
763         }
764     }
767 // Function to fetch the Patient information - eligibility
769 function eligibility_information($insuranceId)
771     $insuranceId = 1;
773     $query = "  SELECT          *
774                         FROM            eligibility_verification
775                         WHERE       insuranceid = ?";
777     $result         = sqlStatement($query, array($insuranceId) );
778     $row        = sqlFetchArray($result);
779     return $row;
782 // return array of X12 partners
784 function getX12Partner()
786     $rez = sqlStatement("select * from x12_partners");
787     for($iter=0; $row=sqlFetchArray($rez); $iter++)
788         $returnval[$iter]=$row;
790     return $returnval;
793 // return array of provider usernames
794 function getUsernames()
796     $rez = sqlStatement("select distinct username, lname, fname,id from users " .
797         "where authorized = 1 and username != ''");
798     for($iter=0; $row=sqlFetchArray($rez); $iter++)
799         $returnval[$iter]=$row;
801     return $returnval;
804 // return formated array
806 function arrFormated(&$item, $key)
808     $item = strstr($item, '_');
809     $item = substr($item,1,strlen($item)-1);
810     $item = "'".$item;