removing more undefined variables to clean up log
[openemr.git] / library / edi.inc
blob1a4c9f1a6110bf93688698ef70b99cc8e1ce78a6
1 <?php
2 // Copyright (C) 2010 MMF Systems, Inc>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // SEGMENT FUNCTION START 
11 // ISA Segment  - EDI-270 format 
13 function create_ISA($row,$X12info,$segTer,$compEleSep) {
15         $ISA     =      array();
17         $ISA[0] = "ISA";                                                        // Interchange Control Header Segment ID 
18         
19         $ISA[1] = "00";                                                         // Author Info Qualifier 
20         
21         $ISA[2] = str_pad("0000000",10," ");            // Author Information 
22         
23         $ISA[3] = "00";                                                         //   Security Information Qualifier
24                                                                                                 //   MEDI-CAL NOTE: For Leased-Line & Dial-Up use '01', 
25                                                                                                 //   for BATCH use '00'.
26                                                                                                 //   '00' No Security Information Present 
27                                                                                                 //   (No Meaningful Information in I04)
29         $ISA[4] = str_pad("0000000000",10," ");         // Security Information 
30         
31         $ISA[5] = str_pad("ZZ",2," ");                          // Interchange ID Qualifier
32         
33         $ISA[6] = str_pad($X12info[2],15," ");          // INTERCHANGE SENDER ID 
34         
35         $ISA[7] = str_pad("ZZ",2," ");                          // Interchange ID Qualifier 
36         
37         $ISA[8] = str_pad($X12info[3],15," ");          // INTERCHANGE RECEIVER ID  
38         
39         $ISA[9] = str_pad(date('ymd'),6," ");           // Interchange Date (YYMMDD) 
40         
41         $ISA[10] = str_pad(date('Hi'),4," ");           // Interchange Time (HHMM) 
42         
43         $ISA[11] = "U";                                                         // Interchange Control Standards Identifier 
44         
45         $ISA[12] = str_pad("00401",5," ");                      // Interchange Control Version Number 
46         
47         $ISA[13] = str_pad("000000001",9," ");          // INTERCHANGE CONTROL NUMBER   
48         
49         $ISA[14] = str_pad("1",1," ");                          // Acknowledgment Request [0= not requested, 1= requested]  
50         
51         $ISA[15] =  str_pad("P",1," ");                         // Usage Indicator [ P = Production Data, T = Test Data ]  
52         
53         $ISA['Created'] = implode('*', $ISA);           // Data Element Separator 
55         $ISA['Created'] = $ISA['Created'] ."*";
57         $ISA['Created'] = $ISA ['Created'] . $segTer . $compEleSep; 
58         
59         return trim($ISA['Created']);
60         
63 // GS Segment  - EDI-270 format 
65 function create_GS($row,$X12info,$segTer,$compEleSep) {
67         $GS        = array();
69         $GS[0] = "GS";                                          // Functional Group Header Segment ID 
70         
71         $GS[1] = "HS";                                          // Functional ID Code [ HS = Eligibility, Coverage or Benefit Inquiry (270) ] 
72         
73         $GS[2] =  $X12info[2];                          // Application Sender’s ID 
74         
75         $GS[3] =  $X12info[3];                          // Application Receiver’s ID 
76         
77         $GS[4] = date('Ymd');                           // Date [CCYYMMDD] 
78         
79         $GS[5] = date('His');                           // Time [HHMM] – Group Creation Time  
80         
81         $GS[6] = "000000002";                           // Group Control Number 
82         
83         $GS[7] = "X";                                   // Responsible Agency Code Accredited Standards Committee X12 ] 
84         
85         $GS[8] = "004010X092A1";                        // Version –Release / Industry[ Identifier Code Query 
87         $GS['Created'] = implode('*', $GS);             // Data Element Separator 
89         $GS['Created'] = $GS ['Created'] . $compEleSep; 
90          
91         return trim($GS['Created']);
92         
95 // ST Segment  - EDI-270 format 
97 function create_ST($row,$X12info,$segTer,$compEleSep) {
99         $ST        =    array();
101         $ST[0] = "ST";                                                          // Transaction Set Header Segment ID 
102         
103         $ST[1] = "270";                                                         // Transaction Set Identifier Code (Inquiry Request) 
104         
105         $ST[2] = "000000003";                                           // Transaction Set Control Number - Must match SE's 
106         
107         $ST['Created'] = implode('*', $ST);                     // Data Element Separator 
109         $ST['Created'] = $ST ['Created'] . $compEleSep; 
110          
111         return trim($ST['Created']);
112                         
115 // BHT Segment  - EDI-270 format 
117 function create_BHT($row,$X12info,$segTer,$compEleSep) {
119         $BHT    =       array();
120         
121         $BHT[0] = "BHT";                                                // Beginning of Hierarchical Transaction Segment ID 
123         $BHT[1] = "0022";                                               // Subscriber Structure Code   
125         $BHT[2] = "13";                                                 // Purpose Code - This is a Request   
127         $BHT[3] = "PROVTest600";                                //  Submitter Transaction Identifier  
128                                                                                         //This information is required by the information Receiver 
129                                                                                         //when using Real Time transactions. 
130                                                                                         //For BATCH this can be used for optional information.
132         $BHT[4] = str_pad(date('Ymd'),8," ");                   // Date Transaction Set Created 
133         
134         $BHT[5] = str_pad(date('His'),8," ");                   // Time Transaction Set Created 
136         $BHT['Created'] = implode('*', $BHT);                   // Data Element Separator 
138         $BHT['Created'] = $BHT ['Created'] . $compEleSep; 
139          
140         return trim($BHT['Created']);
141         
144 // HL Segment  - EDI-270 format 
146 function create_HL($row, $nHlCounter,$X12info,$segTer,$compEleSep) {
148         $HL             = array();
150         $HL[0]          = "HL";                 // Hierarchical Level Segment ID 
151         $HL_LEN[0]      =  2;
153         $HL[1] = $nHlCounter;           // Hierarchical ID No. 
154         
155         if($nHlCounter == 1)
156         { 
157                 $HL[2] = ""; 
158                 $HL[3] = 20;                    // Description: Identifies the payor, maintainer, or source of the information.
159                 $HL[4] = 1;                             // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure. 
160         }
161         else if($nHlCounter == 2)
162         {
163                 $HL[2] = 1;                             // Hierarchical Parent ID Number 
164                 $HL[3] = 21;                    // Hierarchical Level Code. '21' Information Receiver
165                 $HL[4] = 1;                             // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure. 
166         }
167         else
168         {
169                 $HL[2] = 2;
170                 $HL[3] = 22;                    // Hierarchical Level Code.'22' Subscriber 
171                 $HL[4] = 0;                             // 0 no Additional Subordinate in the Hierarchical Structure. 
172         }
173         
174         $HL['Created'] = implode('*', $HL);             // Data Element Separator 
176         $HL['Created'] = $HL ['Created'] . $compEleSep; 
177          
178         return trim($HL['Created']);
182 // NM1 Segment  - EDI-270 format 
184 function create_NM1($row,$nm1Cast,$X12info,$segTer,$compEleSep) {
186         $NM1            = array();
187         
188         $NM1[0]         = "NM1";                                        // Subscriber Name Segment ID 
189         
190         if($nm1Cast == 'PR')
191         {
192                 $NM1[1] = "PR";                                         // Entity ID Code - Payer [PR Payer] 
193                 $NM1[2] = "2";                                          // Entity Type - Non-Person 
194                 $NM1[3] = $row["payer_name"];           // Organizational Name 
195                 $NM1[4] = "";                                           // Data Element not required.
196                 $NM1[5] = "";                                           // Data Element not required.
197                 $NM1[6] = "";                                           // Data Element not required.
198                 $NM1[7] = "";                                           // Data Element not required.
199                 $NM1[8] = "46";                                         // 46 - Electronic Transmitter Identification Number (ETIN) 
200                 $NM1[9] = $X12info[3];                          // Application Sender’s ID 
201         }
202         else if($nm1Cast == '1P')
203         {
204                 $NM1[1] = "IP";                                         // Entity ID Code - Provider [1P Provider]
205                 $NM1[2] = "1";                                          // Entity Type - Person 
206                 $NM1[3] = $row['facility_name'];                        // Organizational Name 
207                 $NM1[4] = $row['provider_lname'];                       // Data Element not required.
208                 $NM1[5] = $row['provider_fname'];                       // Data Element not required.
209                 $NM1[6] = "";                                           // Data Element not required.
210                 $NM1[7] = "";                                           // Data Element not required.
211                 $NM1[8] = "XX";                                         
212                 $NM1[9] = $row['provider_npi'];         
213         }
214         else if($nm1Cast == 'IL')
215         {
216                 $NM1[1] = "IL";                                         // Insured or Subscriber 
217                 $NM1[2] = "1";                                          // Entity Type - Person 
218                 $NM1[3] = $row['lname'];                                // last Name    
219                 $NM1[4] = $row['fname'];                                // first Name   
220                 $NM1[5] = $row['mname'];                                // middle Name  
221                 $NM1[6] = "";                                           // data element 
222                 $NM1[7] = "";                                           // data element 
223                 $NM1[8] = "MI";                                         // Identification Code Qualifier 
224                 $NM1[9] = $row['subscriber_ss'];                        // Identification Code 
225         }
226         
227         $NM1['Created'] = implode('*', $NM1);                           // Data Element Separator 
229         $NM1['Created'] = $NM1['Created'] . $compEleSep; 
230          
231         return trim($NM1['Created']);
235 // REF Segment  - EDI-270 format 
237 function create_REF($row,$ref,$X12info,$segTer,$compEleSep) {
239         $REF    =       array();
241         $REF[0] = "REF";                                                // Subscriber Additional Identification 
243         if($ref == '1P')
244         {
245                 $REF[1] = "4A";                                         // Reference Identification Qualifier 
246                 $REF[2] = $row['provider_pin'];                         // Provider Pin. 
247         }
248         else
249         {
250                 $REF[1] = "EJ";                                         // 'EJ' for Patient Account Number 
251                 $REF[2] = $row['pid'];                                  // Patient Account No. 
252         }
253         $REF['Created'] = implode('*', $REF);                           // Data Element Separator 
255         $REF['Created'] = $REF['Created'] . $compEleSep; 
256          
257         return trim($REF['Created']);
258   
261 // TRN Segment - EDI-270 format 
263 function create_TRN($row,$tracno,$refiden,$X12info,$segTer,$compEleSep) {
265         $TRN    =       array();
267         $TRN[0] = "TRN";                                                // Subscriber Trace Number Segment ID 
269         $TRN[1] = "1";                                                  // Trace Type Code – Current Transaction Trace Numbers 
271         $TRN[2] = $tracno;                                              // Trace Number 
273         $TRN[3] = "9000000000";                                         // Originating Company ID – must be 10 positions in length 
275         $TRN[4] = $refiden;                                             // Additional Entity Identifier (i.e. Subdivision) 
277         $TRN['Created'] = implode('*', $TRN);                           // Data Element Separator 
279         $TRN['Created'] = $TRN['Created'] . $compEleSep; 
280          
281         return trim($TRN['Created']);
282   
285 // DMG Segment - EDI-270 format 
287 function create_DMG($row,$X12info,$segTer,$compEleSep) {
289         $DMG    =       array();
290         
291         $DMG[0] = "DMG";                                                        // Date or Time or Period Segment ID 
293         $DMG[1] = "D8";                                                         // Date Format Qualifier - (D8 means CCYYMMDD) 
295         $DMG[2] = $row['dob'];                                          // Subscriber's Birth date 
297         $DMG['Created'] = implode('*', $DMG);           // Data Element Separator 
299         $DMG['Created'] = $DMG['Created'] .  $compEleSep; 
300          
301         return trim($DMG['Created']);                   
304 // DTP Segment - EDI-270 format 
306 function create_DTP($row,$qual,$X12info,$segTer,$compEleSep) {
308         $DTP    =       array();
309         
310         $DTP[0] = "DTP";                                                // Date or Time or Period Segment ID 
311         
312         $DTP[1] = $qual;                                                // Qualifier - Date of Service 
313         
314         $DTP[2] = "D8";                                                 // Date Format Qualifier - (D8 means CCYYMMDD) 
315         
316         if($qual == '102'){
317                 $DTP[3] = $row['date'];                         // Date 
318         }else{
319                 $DTP[3] = $row['pc_eventDate'];         // Date of Service 
320         }
321         $DTP['Created'] = implode('*', $DTP);   // Data Element Separator 
323         $DTP['Created'] = $DTP['Created'] .  $compEleSep; 
324          
325         return trim($DTP['Created']);
328 // EQ Segment - EDI-270 format 
330 function create_EQ($row,$X12info,$segTer,$compEleSep) {
332         $EQ             =       array();
333         
334         $EQ[0]  = "EQ";                                                                 // Subscriber Eligibility or Benefit Inquiry Information 
335         
336         $EQ[1]  = "30";                                                                 // Service Type Code 
337         
338         $EQ['Created'] = implode('*', $EQ);                             // Data Element Separator 
340         $EQ['Created'] = $EQ['Created'] . $compEleSep; 
341          
342         return trim($EQ['Created']);
345 // SE Segment - EDI-270 format 
347 function create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep) {
349         $SE     =       array();
350         
351         $SE[0] = "SE";                                                          // Transaction Set Trailer Segment ID 
353         $SE[1] = $segmentcount;                                         // Segment Count 
355         $SE[2] = "000000003";                                           // Transaction Set Control Number - Must match ST's 
357         $SE['Created'] = implode('*', $SE);                     // Data Element Separator 
359         $SE['Created'] = $SE['Created'] . $compEleSep; 
360          
361         return trim($SE['Created']);
364 // GE Segment - EDI-270 format 
366 function create_GE($row,$X12info,$segTer,$compEleSep) {
368         $GE     =       array();
369         
370         $GE[0]  = "GE";                                                 // Functional Group Trailer Segment ID 
372         $GE[1]  = "1";                                                  // Number of included Transaction Sets 
374         $GE[2]  = "000000002";                                          // Group Control Number 
376         $GE['Created'] = implode('*', $GE);                             // Data Element Separator 
378         $GE['Created'] = $GE['Created'] . $compEleSep; 
379          
380         return trim($GE['Created']);
383 // IEA Segment - EDI-270 format 
385 function create_IEA($row,$X12info,$segTer,$compEleSep) {
387         $IEA    =       array();
388         
389         $IEA[0] = "IEA";                                                // Interchange Control Trailer Segment ID 
391         $IEA[1] = "1";                                                  // Number of included Functional Groups 
393         $IEA[2] = "000000001";                                          // Interchange Control Number 
395         $IEA['Created'] = implode('*', $IEA);
397         $IEA['Created'] = $IEA['Created'] .  $compEleSep; 
398          
399         return trim($IEA['Created']);
402 function translate_relationship($relationship) {
403         switch ($relationship) {
404                 case "spouse":
405                         return "01";
406                         break;
407                 case "child":
408                         return "19";
409                         break;
410                 case "self":
411                 default:
412                         return "S";
413         }
416 // EDI-270 Batch file Generation 
418 function print_elig($res,$X12info,$segTer,$compEleSep){
419                 
420         $i=1;
422         $PATEDI    = "";
424         // For Header Segment 
426         $nHlCounter = 1;
427         $rowCount       = 0;
428         $trcNo          = 1234501;
429         $refiden        = 5432101;
430         
431         while ($row = sqlFetchArray($res)) 
432         {
433                 
434                 if($nHlCounter == 1)
435                 {
436                         // create ISA 
437                         $PATEDI    = create_ISA($row,$X12info,$segTer,$compEleSep);
438                         
439                         // create GS 
440                         $PATEDI   .= create_GS($row,$X12info,$segTer,$compEleSep);
442                         // create ST 
443                         $PATEDI   .= create_ST($row,$X12info,$segTer,$compEleSep);
444                         
445                         // create BHT 
446                         $PATEDI   .= create_BHT($row,$X12info,$segTer,$compEleSep);
447                         
448                         // For Payer Segment 
449                                 
450                         $PATEDI  .= create_HL($row,1,$X12info,$segTer,$compEleSep);
451                         $PATEDI  .= create_NM1($row,'PR',$X12info,$segTer,$compEleSep);
453                         // For Provider Segment                                 
454                                         
455                         $PATEDI  .= create_HL($row,2,$X12info,$segTer,$compEleSep);
456                         $PATEDI  .= create_NM1($row,'1P',$X12info,$segTer,$compEleSep);
457                         $PATEDI  .= create_REF($row,'1P',$X12info,$segTer,$compEleSep);
459                         $nHlCounter = $nHlCounter + 2;  
460                         $segmentcount = 7; // segement counts - start from ST 
461                 }
463                 // For Subscriber Segment                               
464                 
465                 $PATEDI  .= create_HL($row,$nHlCounter,$X12info,$segTer,$compEleSep);
466                 $PATEDI  .= create_TRN($row,$trcNo,$refiden,$X12info,$segTer,$compEleSep);
467                 $PATEDI  .= create_NM1($row,'IL',$X12info,$segTer,$compEleSep);
468                 $PATEDI  .= create_REF($row,'IL',$X12info,$segTer,$compEleSep);
469                 $PATEDI  .= create_DMG($row,$X12info,$segTer,$compEleSep);
470                 
471                 //      $PATEDI  .= create_DTP($row,'102',$X12info,$segTer,$compEleSep);
472                 
473                 $PATEDI  .= create_DTP($row,'472',$X12info,$segTer,$compEleSep);
474                 $PATEDI  .= create_EQ($row,$X12info,$segTer,$compEleSep);
475                                                                 
476                 $segmentcount   = $segmentcount + 7;
477                 $nHlCounter     = $nHlCounter + 1;
478                 $rowCount       = $rowCount + 1;
479                 $trcNo          = $trcNo + 1;
480                 $refiden        = $refiden + 1;
481                 
483                 if($rowCount == sqlNumRows($res))
484                 {
485                         $segmentcount = $segmentcount + 1;
486                         $PATEDI   .= create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep);
487                         $PATEDI   .= create_GE($row,$X12info,$segTer,$compEleSep);
488                         $PATEDI   .= create_IEA($row,$X12info,$segTer,$compEleSep);
489                 }
490         }
492         echo $PATEDI;
495 // Report Generation 
497 function show_elig($res,$X12info,$segTer,$compEleSep){
498                 
499         $i=0;
500         echo "  <div id='report_results'>
501                         <table>
502                                 <thead>
503                                 
504                                         <th style='width:12%;'> ". htmlspecialchars( xl('Facility Name'), ENT_NOQUOTES) ."</th>
505                                         <th style='width:9%;' > ". htmlspecialchars( xl('Facility NPI'), ENT_NOQUOTES) ."</th>
506                                         <th style='width:15%;'> ". htmlspecialchars( xl('Insurance Comp'), ENT_NOQUOTES) ."</th>
507                                         <th style='width:8%;' > ". htmlspecialchars( xl('Policy No'), ENT_NOQUOTES) ."</th>
508                                         <th style='width:16%;'> ". htmlspecialchars( xl('Patient Name'), ENT_NOQUOTES) ."</th>
509                                         <th style='width:7%;' > ". htmlspecialchars( xl('DOB'), ENT_NOQUOTES) ."</th>
510                                         <th style='width:6%;' > ". htmlspecialchars( xl('Gender'), ENT_NOQUOTES) ."</th>
511                                         <th style='width:9%;' > ". htmlspecialchars( xl('SSN'), ENT_NOQUOTES) ."</th>
512                                         <th style='width:2%;' > &nbsp;                    </th>
513                                 </thead>
515                                 <tbody>
516                                         
517                 ";
519         while ($row = sqlFetchArray($res)) { 
520                 
521                                                 
522                 $i= $i+1;
524                 if($i%2 == 0){
525                         $background = '#FFF';
526                 }else{
527                         $background = '#FFF';
528                 }
530                 $elig     = array();
531                 $elig[0]  = $row['facility_name'];                              // Inquiring Provider Name  calendadr 
532                 $elig[1]  = $row['facility_npi'];                               // Inquiring Provider NPI 
533                 $elig[2]  = $row['payer_name'];                                 // Payer Name  our insurance co name
534                 $elig[3]  = $row['policy_number'];                              // Subscriber ID   
535                 $elig[4]  = $row['subscriber_lname'];                           // Subscriber Last Name 
536                 $elig[5]  = $row['subscriber_fname'];                           // Subscriber First Name 
537                 $elig[6]  = $row['subscriber_mname'];                           // Subscriber Middle Initial 
538                 $elig[7]  = $row['subscriber_dob'];                             // Subscriber Date of Birth 
539                 $elig[8]  = substr($row['subscriber_sex'], 0, 1);               // Subscriber Sex 
540                 $elig[9]  = $row['subscriber_ss'];                              // Subscriber SSN 
541                 $elig[10] = translate_relationship($row['subscriber_relationship']);    // Pt Relationship to insured 
542                 $elig[11] = $row['lname'];                                      // Dependent Last Name 
543                 $elig[12] = $row['fname'];                                      // Dependent First Name 
544                 $elig[13] = $row['mname'];                                      // Dependent Middle Initial 
545                 $elig[14] = $row['dob'];                                        // Dependent Date of Birth 
546                 $elig[15] = substr($row['sex'], 0, 1);                          // Dependent Sex 
547                 $elig[16] = $row['pc_eventDate'];                               // Date of service 
548                 $elig[17] = "30";                                               // Service Type 
549                 $elig[18] = $row['pubpid'];                                     // Patient Account Number pubpid  
551                 echo "  <tr id='PR".$i."_". htmlspecialchars( $row['policy_number'], ENT_QUOTES)."'>
552                                 <td class ='detail' style='width:12%;'>". htmlspecialchars( $row['facility_name'], ENT_NOQUOTES) ."</td>
553                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['facility_npi'], ENT_NOQUOTES) ."</td>
554                                 <td class ='detail' style='width:15%;'>". htmlspecialchars( $row['payer_name'], ENT_NOQUOTES) ."</td>
555                                 <td class ='detail' style='width:8%;'>".  htmlspecialchars( $row['policy_number'], ENT_NOQUOTES) ."</td>
556                                 <td class ='detail' style='width:16%;'>". htmlspecialchars( $row['subscriber_lname']." ".$row['subscriber_fname'], ENT_NOQUOTES) ."</td>
557                                 <td class ='detail' style='width:7%;'>".  htmlspecialchars( $row['subscriber_dob'], ENT_NOQUOTES) ."</td>
558                                 <td class ='detail' style='width:6%;'>".  htmlspecialchars( $row['subscriber_sex'], ENT_NOQUOTES) ."</td>
559                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['subscriber_ss'], ENT_NOQUOTES) ."</td>
560                                 <td class ='detail' style='width:2%;'>
561                                         <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) . "\")'>
562                                 </td>
563                         </tr>                   
564                 ";
566                                 
567                 unset($elig);
568         }
569         
570         if($i==0){
572                 echo "  <tr>
573                                 <td class='norecord' colspan=9>
574                                         <div style='padding:5px;font-family:arial;font-size:13px;text-align:center;'>". htmlspecialchars( xl('No records found'), ENT_NOQUOTES) . "</div>
575                                 </td>
576                         </tr>   ";
577         }
578                 echo "  </tbody>
579                         </table>";
582 // To Show Eligibility Verification data 
584 function show_eligibility_information($pid)
587         $query = "      SELECT          eligr.response_description as ResponseMessage, 
588                                         DATE_FORMAT(eligv.eligibility_check_date, '%d %M %Y') as VerificationDate, 
589                                         eligv.copay, eligv.deductible, eligv.deductiblemet, 
590                                         if(eligr.response_status = 'A','Active','Inactive') as Status, 
591                                         insd.pid, insc.name 
592                         FROM            eligibility_verification eligv 
593                         INNER JOIN      eligibility_response eligr on eligr.response_id = eligv.response_id 
594                         INNER JOIN      insurance_data insd on insd.id = eligv.insurance_id 
595                         INNER JOIN      insurance_companies insc on insc.id = insd.provider
596                         WHERE           insd.pid = ?
597                         AND                     eligr.response_status = 'A'
598                         AND                     eligv.eligibility_check_date = (SELECT max(eligibility_check_date) 
599                                                                                 FROM eligibility_verification 
600                                                                                 WHERE   insurance_id = eligv.insurance_id)";
601         $result         = sqlStatement($query, array($pid) );
602                 
603         $row = sqlFetchArray($result);
605         $showString .=  "<br><div class='text'>" .
606                         "<b>" .
607                         htmlspecialchars( xl('Insurance Provider'), ENT_NOQUOTES) . ":</b> " .
608                         (!empty($row['name']) ? htmlspecialchars( $row['name'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
609                         "<br>\n" .
610                         "<b>" .
611                         htmlspecialchars( xl('Status'), ENT_NOQUOTES) . ":</b> " .
612                         (!empty($row['ResponseMessage']) ? htmlspecialchars( $row['ResponseMessage'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) . 
613                         "<br>\n" .
614                         "<b>" .
615                         htmlspecialchars( xl('Last Verified On'), ENT_NOQUOTES) . ":</b> " .
616                         (!empty($row['VerificationDate']) ? htmlspecialchars( $row['VerificationDate'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
617                         "<br>" .
618                         "<b>" . htmlspecialchars( xl('Copay'), ENT_NOQUOTES) . ":</b> " .
619                         (!empty($row['copay']) ? htmlspecialchars( $row['copay'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
620                         "<br><b>" . htmlspecialchars( xl('Deductible'), ENT_NOQUOTES) . ":</b> " .
621                         (!empty($row['deductible']) ? htmlspecialchars( $row['deductible'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
622                         "<br><b>" . htmlspecialchars( xl('Deductible Met'), ENT_NOQUOTES) . ":</b> " .
623                         (!empty($row['deductiblemet']) ? ($row['deductiblemet'] == 'Y' ? htmlspecialchars( xl('Yes'), ENT_NOQUOTES) : htmlspecialchars( xl('No'), ENT_NOQUOTES)) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
624                         "</div>";
625         
626         echo $showString;
629 // For EDI 271 
632 // Function to save the values in eligibility_response table 
634 function eligibility_response_save($segmentVal,$vendorID)
637         $resCount = 0;
639         $query = "  SELECT   count(*) as RecordsCount   
640                                 FROM     eligibility_response 
641                                 WHERE    response_description = ? and 
642                                          response_vendor_id       = ?";
643                                                                                 
644         $resCount = sqlStatement($query, array($segmentVal, $vendorID) );
645         
646         
647         if(isset($resCount))
648         {
650                 $row            = sqlFetchArray($resCount);
652                 $resCount       = $row['RecordsCount'];
653                                 
654         }
656         if($resCount == 0)
657         {
659                 $query = "INSERT into eligibility_response SET  response_description =?,
660                                                                 response_vendor_id       = ?,
661                                                                 response_create_date = now(),
662                                                                 response_modify_date = now()";
663                 $res    = sqlStatement($query, array($segmentVal, $vendorID) );
664         }
668 // Function to save the values in eligibility_verification table
670 function eligibility_verification_save($segmentVal,$x12PartnerId,$patientId)
673         $resCount = 0;
675         // For fetching the response Id
677         $query = "  SELECT       response_id    
678                                 FROM     eligibility_response 
679                                 WHERE    response_description = ? and 
680                                                  response_vendor_id       = ?";
681                                                                                 
682         $resId  = sqlStatement($query, array($segmentVal, $x12PartnerId) );
683         
684         // For fetching the insuarace data Id 
686         $query = "  SELECT       id,copay       
687                                 FROM     insurance_data 
688                                 WHERE    type = 'primary' and
689                                                  pid = ?";
690                                                                                 
691         $insId  = sqlStatement($query, array($patientId) );
692         
693         if(isset($resId))
694         {
696                 $row            = sqlFetchArray($resId);
698                 $responseId     = $row['response_id'];
699                                 
700         }
701         if(isset($insId))
702         {
704                 $row            = sqlFetchArray($insId);
706                 $insuranceId    = $row['id'];
707                 $copay          = $row['copay'];
708                                 
709         }
711         if($resCount == 0)
712         {
714                 if(isset($insuranceId) && !empty($insuranceId)){
716                         //Set up the sql variable binding array (this prevents sql-injection attacks)
717                         $sqlBindArray = array();
718                         $query = "INSERT into eligibility_verification SET      response_id     = ? ,
719                                                                                 insurance_id    = ?,";
720                         array_push($sqlBindArray, $responseId, $insuranceId);   
722                         if(!empty($copay))
723                         {
724                                 $query .= "copay = ?,";
725                                 array_push($sqlBindArray, $copay);
726                         }
727                         $query .= "eligibility_check_date       = now(),
728                                    create_date                  = now()";
729                         $res    = sqlStatement($query, $sqlBindArray);
730                 }
731         }
734 // Function to fetch the Patient information - eligibility
736 function eligibility_information($insuranceId)
738         $insuranceId = 1;
740         $query = "      SELECT          *       
741                         FROM            eligibility_verification
742                         WHERE       insuranceid = ?";
743                                                                                 
744         $result         = sqlStatement($query, array($insuranceId) );
745         $row            = sqlFetchArray($result);
746         return $row;
749 // return array of X12 partners 
751 function getX12Partner() {
752         $rez = sqlStatement("select * from x12_partners");
753         for($iter=0; $row=sqlFetchArray($rez); $iter++)
754                 $returnval[$iter]=$row;
756         return $returnval;
759 // return array of provider usernames 
760 function getUsernames() {
761         $rez = sqlStatement("select distinct username, lname, fname,id from users " .
762                 "where authorized = 1 and username != ''");
763         for($iter=0; $row=sqlFetchArray($rez); $iter++)
764                 $returnval[$iter]=$row;
766         return $returnval;
769 // return formated array 
771 function arrFormated(&$item, $key){
772         $item = strstr($item, '_');
773         $item = substr($item,1,strlen($item)-1);
774         $item = "'".$item;
775 }