Merge pull request #1169 from matrix-israel/AE/fixRegressionConditions
[openemr.git] / library / edi.inc
blob02c6175c80432e4be952aaf62f9fd6c0b05408df
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']);
81 // GS Segment  - EDI-270 format
83 function create_GS($row, $X12info, $segTer, $compEleSep)
86     $GS    = array();
88     $GS[0] = "GS";                      // Functional Group Header Segment ID
90     $GS[1] = "HS";                      // Functional ID Code [ HS = Eligibility, Coverage or Benefit Inquiry (270) ]
92     $GS[2] =  $X12info[2];              // Application Sender's ID
94     $GS[3] =  $X12info[3];              // Application Receiver's ID
96     $GS[4] = date('Ymd');               // Date [CCYYMMDD]
98     $GS[5] = date('His');               // Time [HHMM] Group Creation Time
100     $GS[6] = "2";                       // Group Control Number No zeros for 5010
102     $GS[7] = "X";                   // Responsible Agency Code Accredited Standards Committee X12 ]
104     $GS[8] = "005010X279A1";            // Version Release / Industry[ Identifier Code Query 005010X279A1
106     $GS['Created'] = implode('*', $GS);         // Data Element Separator
108     $GS['Created'] = $GS ['Created'] . $segTer;  // change the information in the tag or change the tag
110     return trim($GS['Created']);
113 // ST Segment  - EDI-270 format
115 function create_ST($row, $X12info, $segTer, $compEleSep)
118     $ST    =    array();
120     $ST[0] = "ST";                              // Transaction Set Header Segment ID
122     $ST[1] = "270";                                 // Transaction Set Identifier Code (Inquiry Request)
124     $ST[2] = "000000003";                       // Transaction Set Control Number - Must match SE's
126     $ST[3] = "005010X279A1";                    // Standard 005010X279A1 in $ST[3]
128     $ST['Created'] = implode('*', $ST);             // Data Element Separator
130     $ST['Created'] = $ST ['Created'] . $segTer;
132     return trim($ST['Created']);
135 // BHT Segment  - EDI-270 format
137 function create_BHT($row, $X12info, $segTer, $compEleSep)
140     $BHT    =   array();
142     $BHT[0] = "BHT";                        // Beginning of Hierarchical Transaction Segment ID
144     $BHT[1] = "0022";                       // Subscriber Structure Code
146     $BHT[2] = "13";                             // Purpose Code - This is a Request
148     $BHT[3] = "PROVTest600";                //  Submitter Transaction Identifier
149                                             //This information is required by the information Receiver
150                                             //when using Real Time transactions.
151                                             //For BATCH this can be used for optional information.
153     $BHT[4] = str_pad(date('Ymd'), 8, " ");           // Date Transaction Set Created
155     $BHT[5] = str_pad(date('Hi'), 4, " ");            // Time Transaction Set Created no space after and 1300 is plenty
157     $BHT['Created'] = implode('*', $BHT);           // Data Element Separator
159     $BHT['Created'] = $BHT ['Created'] . $segTer;
161     return trim($BHT['Created']);
164 // HL Segment  - EDI-270 format
166 function create_HL($row, $nHlCounter, $X12info, $segTer, $compEleSep)
169     $HL         = array();
171     $HL[0]      = "HL";             // Hierarchical Level Segment ID
172     $HL_LEN[0]  =  2;
174     $HL[1] = $nHlCounter;       // Hierarchical ID No.
176     if ($nHlCounter == 1) {
177         $HL[2] = "";
178         $HL[3] = 20;            // Description: Identifies the payor, maintainer, or source of the information.
179         $HL[4] = 1;                 // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure.
180     } else if ($nHlCounter == 2) {
181         $HL[2] = 1;                 // Hierarchical Parent ID Number
182         $HL[3] = 21;            // Hierarchical Level Code. '21' Information Receiver
183         $HL[4] = 1;                 // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure.
184     } else {
185         $HL[2] = 2;
186         $HL[3] = 22;            // Hierarchical Level Code.'22' Subscriber
187         $HL[4] = 0;                 // 0 no Additional Subordinate in the Hierarchical Structure.
188     }
190     $HL['Created'] = implode('*', $HL);         // Data Element Separator
192     $HL['Created'] = $HL ['Created'] . $segTer;
194     return trim($HL['Created']);
197 // NM1 Segment  - EDI-270 format
199 function create_NM1($row, $nm1Cast, $X12info, $segTer, $compEleSep)
202     $NM1        = array();
204     $NM1[0]         = "NM1";                    // Subscriber Name Segment ID
206     if ($nm1Cast == 'PR') {
207         $NM1[1] = "PR";                         // Entity ID Code - Payer [PR Payer]
208         $NM1[2] = "2";                      // Entity Type - Non-Person
209         $NM1[3] = $row["payer_name"];       // Organizational Name
210         $NM1[4] = "";                       // Data Element not required.
211         $NM1[5] = "";                       // Data Element not required.
212         $NM1[6] = "";                       // Data Element not required.
213         $NM1[7] = "";                       // Data Element not required.
214         $NM1[8] = "46";                         // 46 - Electronic Transmitter Identification Number (ETIN)
215         $NM1[9] = $X12info[3];              // Application Sender's ID
216     } else if ($nm1Cast == 'FA') {
217         $NM1[1] = "FA";                         // Entity ID Code - Facility [FA Facility]
218         $NM1[2] = "2";                      // Entity Type - Non-Person
219         $NM1[3] = $row['facility_name'];            // Organizational Name
220         $NM1[4] = "";           // Data Element not required.
221         $NM1[5] = "";           // Data Element not required.
222         $NM1[6] = "";                       // Data Element not required.
223         $NM1[7] = "";                       // Data Element not required.
224         $NM1[8] = "FI";
225         $NM1[9] = $row['facility_npi'];
226     } else if ($nm1Cast == 'IL') {
227         $NM1[1] = "IL";                         // Insured or Subscriber
228         $NM1[2] = "1";                      // Entity Type - Person
229         $NM1[3] = $row['lname'];                // last Name
230         $NM1[4] = $row['fname'];                // first Name
231         $NM1[5] = $row['mname'];                // middle Name
232         $NM1[6] = "";                       // data element
233         $NM1[7] = "";                       // data element
234         $NM1[8] = "MI";                         // Identification Code Qualifier
235         $NM1[9] = $row['subscriber_ss'];            // Identification Code
236     }
238     $NM1['Created'] = implode('*', $NM1);               // Data Element Separator
240     $NM1['Created'] = $NM1['Created'] . $segTer;
242     return trim($NM1['Created']);
245 // REF Segment  - EDI-270 format
247 function create_REF($row, $ref, $X12info, $segTer, $compEleSep)
250     $REF    =   array();
252     $REF[0] = "REF";                        // Subscriber Additional Identification    does not want this for anything
254     if ($ref == '1P') {
255         $REF[1] = "4A";                         // Reference Identification Qualifier
256         $REF[2] = $row['provider_pin'];                 // Provider Pin.
257     } else {
258         $REF[1] = "EJ";                         // 'EJ' for Patient Account Number     does not want this for patient
259         $REF[2] = $row['pid'];                  // Patient Account No.
260     }
262     $REF['Created'] = implode('*', $REF);               // Data Element Separator
264     $REF['Created'] = $REF['Created'] . $segTer;
266     return trim($REF['Created']);
269 // TRN Segment - EDI-270 format
271 function create_TRN($row, $tracno, $refiden, $X12info, $segTer, $compEleSep)
274     $TRN    =   array();
276     $TRN[0] = "TRN";                        // Subscriber Trace Number Segment ID
278     $TRN[1] = "1";                          // Trace Type Code ï¿½ Current Transaction Trace Numbers
280     $TRN[2] = $tracno;                      // Trace Number
282     $TRN[3] = "9000000000";                         // Originating Company ID ï¿½ must be 10 positions in length
284     $TRN[4] = $refiden;                         // Additional Entity Identifier (i.e. Subdivision)
286     $TRN['Created'] = implode('*', $TRN);               // Data Element Separator
288     $TRN['Created'] = $TRN['Created'] . $segTer;
290     return trim($TRN['Created']);
293 // DMG Segment - EDI-270 format
295 function create_DMG($row, $X12info, $segTer, $compEleSep)
298     $DMG    =   array();
300     $DMG[0] = "DMG";                            // Date or Time or Period Segment ID
302     $DMG[1] = "D8";                                 // Date Format Qualifier - (D8 means CCYYMMDD)
304     $DMG[2] = $row['dob'];                      // Subscriber's Birth date
306     $DMG['Created'] = implode('*', $DMG);       // Data Element Separator
308     $DMG['Created'] = $DMG['Created'] .  $segTer;
310     return trim($DMG['Created']);
313 // DTP Segment - EDI-270 format
315 function create_DTP($row, $qual, $X12info, $segTer, $compEleSep)
318     $DTP    =   array();
320     $DTP[0] = "DTP";                        // Date or Time or Period Segment ID
322     $DTP[1] = $qual;                        // Qualifier - Date of Service
324     $DTP[2] = "D8";                             // Date Format Qualifier - (D8 means CCYYMMDD)
326     if ($qual == '102') {
327         $DTP[3] = $row['date'];                 // Date
328     } else {
329         $DTP[3] = $row['pc_eventDate'];         // Date of Service
330     }
332     $DTP['Created'] = implode('*', $DTP);   // Data Element Separator
334     $DTP['Created'] = $DTP['Created'] .  $segTer;
336     return trim($DTP['Created']);
339 // EQ Segment - EDI-270 format
341 function create_EQ($row, $X12info, $segTer, $compEleSep)
344     $EQ         =   array();
346     $EQ[0]  = "EQ";                                     // Subscriber Eligibility or Benefit Inquiry Information
348     $EQ[1]  = "30";                                     // Service Type Code
350     $EQ['Created'] = implode('*', $EQ);                 // Data Element Separator
352     $EQ['Created'] = $EQ['Created'] . $segTer;
354     return trim($EQ['Created']);
357 // SE Segment - EDI-270 format
359 function create_SE($row, $segmentcount, $X12info, $segTer, $compEleSep)
362     $SE     =   array();
364     $SE[0] = "SE";                              // Transaction Set Trailer Segment ID
366     $SE[1] = $segmentcount;                         // Segment Count
368     $SE[2] = "000000003";                       // Transaction Set Control Number - Must match ST's
370     $SE['Created'] = implode('*', $SE);             // Data Element Separator
372     $SE['Created'] = $SE['Created'] . $segTer;
374     return trim($SE['Created']);
377 // GE Segment - EDI-270 format
379 function create_GE($row, $X12info, $segTer, $compEleSep)
382     $GE     =   array();
384     $GE[0]  = "GE";                             // Functional Group Trailer Segment ID
386     $GE[1]  = "1";                          // Number of included Transaction Sets
388     $GE[2]  = "2";                          // Group Control Number
390     $GE['Created'] = implode('*', $GE);                 // Data Element Separator
392     $GE['Created'] = $GE['Created'] . $segTer;
394     return trim($GE['Created']);
397 // IEA Segment - EDI-270 format
399 function create_IEA($row, $X12info, $segTer, $compEleSep)
402     $IEA    =   array();
404     $IEA[0] = "IEA";                        // Interchange Control Trailer Segment ID
406     $IEA[1] = "1";                          // Number of included Functional Groups
408     $IEA[2] = "000000001";                      // Interchange Control Number
410     $IEA['Created'] = implode('*', $IEA);
412     $IEA['Created'] = $IEA['Created'] .  $segTer;
414     return trim($IEA['Created']);
417 function translate_relationship($relationship)
419     switch ($relationship) {
420         case "spouse":
421             return "01";
422             break;
423         case "child":
424             return "19";
425             break;
426         case "self":
427         default:
428             return "S";
429     }
432 // EDI-270 Batch file Generation
434 function print_elig($res, $X12info, $segTer, $compEleSep)
437     $i=1;
439     $PATEDI    = "";
441     // For Header Segment
443     $nHlCounter = 1;
444     $rowCount   = 0;
445     $trcNo      = 1234501;
446     $refiden    = 5432101;
448     while ($row = sqlFetchArray($res)) {
449         if ($nHlCounter == 1) {
450             // create ISA
451             $PATEDI    = create_ISA($row, $X12info, $segTer, $compEleSep);
453             // create GS
454             $PATEDI   .= create_GS($row, $X12info, $segTer, $compEleSep);
456             // create ST
457             $PATEDI   .= create_ST($row, $X12info, $segTer, $compEleSep);
459             // create BHT
460             $PATEDI   .= create_BHT($row, $X12info, $segTer, $compEleSep);
462             // For Payer Segment
464             $PATEDI  .= create_HL($row, 1, $X12info, $segTer, $compEleSep);
465             $PATEDI  .= create_NM1($row, 'PR', $X12info, $segTer, $compEleSep);
467             // For Provider Segment
469             $PATEDI  .= create_HL($row, 2, $X12info, $segTer, $compEleSep);
470             $PATEDI  .= create_NM1($row, 'FA', $X12info, $segTer, $compEleSep);
472             $nHlCounter = $nHlCounter + 2;
473             $segmentcount = 7; // segement counts - start from ST
474         }
476         // For Subscriber Segment
478         $PATEDI  .= create_HL($row, $nHlCounter, $X12info, $segTer, $compEleSep);
479         $PATEDI  .= create_NM1($row, 'IL', $X12info, $segTer, $compEleSep);
480         $PATEDI  .= create_DMG($row, $X12info, $segTer, $compEleSep);
482         $PATEDI  .= create_DTP($row, '291', $X12info, $segTer, $compEleSep);
483         $PATEDI  .= create_EQ($row, $X12info, $segTer, $compEleSep);
485         $segmentcount   = $segmentcount + 7;
486         $nHlCounter     = $nHlCounter + 1;
487         $rowCount   = $rowCount + 1;
488         $trcNo      = $trcNo + 1;
489         $refiden    = $refiden + 1;
492         if ($rowCount == sqlNumRows($res)) {
493             $segmentcount = $segmentcount + 1;
494             $PATEDI   .= create_SE($row, $segmentcount, $X12info, $segTer, $compEleSep);
495             $PATEDI   .= create_GE($row, $X12info, $segTer, $compEleSep);
496             $PATEDI   .= create_IEA($row, $X12info, $segTer, $compEleSep);
497         }
498     }
500     echo $PATEDI;
503 // Report Generation
505 function show_elig($res, $X12info, $segTer, $compEleSep)
508     $i=0;
509     echo "      <div id='report_results'>
510                         <table>
511                                 <thead>
513                                         <th style='width:12%;'> ". htmlspecialchars(xl('Facility Name'), ENT_NOQUOTES) ."</th>
514                                         <th style='width:9%;' > ". htmlspecialchars(xl('Facility NPI'), ENT_NOQUOTES) ."</th>
515                                         <th style='width:15%;'> ". htmlspecialchars(xl('Insurance Comp'), ENT_NOQUOTES) ."</th>
516                                         <th style='width:8%;' > ". htmlspecialchars(xl('Policy No'), ENT_NOQUOTES) ."</th>
517                                         <th style='width:16%;'> ". htmlspecialchars(xl('Patient Name'), ENT_NOQUOTES) ."</th>
518                                         <th style='width:7%;' > ". htmlspecialchars(xl('DOB'), ENT_NOQUOTES) ."</th>
519                                         <th style='width:6%;' > ". htmlspecialchars(xl('Gender'), ENT_NOQUOTES) ."</th>
520                                         <th style='width:9%;' > ". htmlspecialchars(xl('SSN'), ENT_NOQUOTES) ."</th>
521                                         <th style='width:2%;' > &nbsp;                    </th>
522                                 </thead>
524                                 <tbody>
526                 ";
528     while ($row = sqlFetchArray($res)) {
529         $i= $i+1;
531         if ($i%2 == 0) {
532             $background = '#FFF';
533         } else {
534             $background = '#FFF';
535         }
537         $elig     = array();
538         $elig[0]  = $row['facility_name'];              // Inquiring Provider Name  calendadr
539         $elig[1]  = $row['facility_npi'];               // Inquiring Provider NPI
540         $elig[2]  = $row['payer_name'];                     // Payer Name  our insurance co name
541         $elig[3]  = $row['policy_number'];              // Subscriber ID
542         $elig[4]  = $row['subscriber_lname'];               // Subscriber Last Name
543         $elig[5]  = $row['subscriber_fname'];               // Subscriber First Name
544         $elig[6]  = $row['subscriber_mname'];               // Subscriber Middle Initial
545         $elig[7]  = $row['subscriber_dob'];                 // Subscriber Date of Birth
546         $elig[8]  = substr($row['subscriber_sex'], 0, 1);       // Subscriber Sex
547         $elig[9]  = $row['subscriber_ss'];              // Subscriber SSN
548         $elig[10] = translate_relationship($row['subscriber_relationship']);    // Pt Relationship to insured
549         $elig[11] = $row['lname'];                  // Dependent Last Name
550         $elig[12] = $row['fname'];                  // Dependent First Name
551         $elig[13] = $row['mname'];                  // Dependent Middle Initial
552         $elig[14] = $row['dob'];                    // Dependent Date of Birth
553         $elig[15] = substr($row['sex'], 0, 1);              // Dependent Sex
554         $elig[16] = $row['pc_eventDate'];               // Date of service
555         $elig[17] = "30";                       // Service Type
556         $elig[18] = $row['pubpid'];                     // Patient Account Number pubpid
558         echo "  <tr id='PR".$i."_". htmlspecialchars($row['policy_number'], ENT_QUOTES)."'>
559                                 <td class ='detail' style='width:12%;'>". htmlspecialchars($row['facility_name'], ENT_NOQUOTES) ."</td>
560                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars($row['facility_npi'], ENT_NOQUOTES) ."</td>
561                                 <td class ='detail' style='width:15%;'>". htmlspecialchars($row['payer_name'], ENT_NOQUOTES) ."</td>
562                                 <td class ='detail' style='width:8%;'>".  htmlspecialchars($row['policy_number'], ENT_NOQUOTES) ."</td>
563                                 <td class ='detail' style='width:16%;'>". htmlspecialchars($row['subscriber_lname']." ".$row['subscriber_fname'], ENT_NOQUOTES) ."</td>
564                                 <td class ='detail' style='width:7%;'>".  htmlspecialchars($row['subscriber_dob'], ENT_NOQUOTES) ."</td>
565                                 <td class ='detail' style='width:6%;'>".  htmlspecialchars($row['subscriber_sex'], ENT_NOQUOTES) ."</td>
566                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars($row['subscriber_ss'], ENT_NOQUOTES) ."</td>
567                                 <td class ='detail' style='width:2%;'>
568                                         <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) . "\")'>
569                                 </td>
570                         </tr>
571                 ";
574         unset($elig);
575     }
577     if ($i==0) {
578         echo "  <tr>
579                                 <td class='norecord' colspan=9>
580                                         <div style='padding:5px;font-family:arial;font-size:13px;text-align:center;'>". htmlspecialchars(xl('No records found'), ENT_NOQUOTES) . "</div>
581                                 </td>
582                         </tr>   ";
583     }
585         echo "  </tbody>
586                         </table>";
589 // To Show Eligibility Verification data
591 function show_eligibility_information($pid)
594     $query = "  SELECT          eligr.response_description as ResponseMessage,
595                                         DATE_FORMAT(eligv.eligibility_check_date, '%d %M %Y') as VerificationDate,
596                                         eligv.copay, eligv.deductible, eligv.deductiblemet,
597                                         if(eligr.response_status = 'A','Active','Inactive') as Status,
598                                         insd.pid, insc.name
599                         FROM            eligibility_verification eligv
600                         INNER JOIN      eligibility_response eligr on eligr.response_id = eligv.response_id
601                         INNER JOIN      insurance_data insd on insd.id = eligv.insurance_id
602                         INNER JOIN      insurance_companies insc on insc.id = insd.provider
603                         WHERE           insd.pid = ?
604                         AND                     eligr.response_status = 'A'
605                         AND                     eligv.eligibility_check_date = (SELECT max(eligibility_check_date)
606                                                                                 FROM eligibility_verification
607                                                                                 WHERE   insurance_id = eligv.insurance_id)";
608     $result         = sqlStatement($query, array($pid));
610     $row = sqlFetchArray($result);
612         $showString="";
613     $showString .=  "<br><div class='text'>" .
614             "<b>" .
615             htmlspecialchars(xl('Insurance Provider'), ENT_NOQUOTES) . ":</b> " .
616             (!empty($row['name']) ? htmlspecialchars($row['name'], ENT_NOQUOTES) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
617             "<br>\n" .
618             "<b>" .
619             htmlspecialchars(xl('Status'), ENT_NOQUOTES) . ":</b> " .
620             (!empty($row['ResponseMessage']) ? htmlspecialchars($row['ResponseMessage'], ENT_NOQUOTES) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
621             "<br>\n" .
622             "<b>" .
623             htmlspecialchars(xl('Last Verified On'), ENT_NOQUOTES) . ":</b> " .
624             (!empty($row['VerificationDate']) ? htmlspecialchars($row['VerificationDate'], ENT_NOQUOTES) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
625             "<br>" .
626             "<b>" . htmlspecialchars(xl('Copay'), ENT_NOQUOTES) . ":</b> " .
627             (!empty($row['copay']) ? htmlspecialchars($row['copay'], ENT_NOQUOTES) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
628             "<br><b>" . htmlspecialchars(xl('Deductible'), ENT_NOQUOTES) . ":</b> " .
629             (!empty($row['deductible']) ? htmlspecialchars($row['deductible'], ENT_NOQUOTES) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
630             "<br><b>" . htmlspecialchars(xl('Deductible Met'), ENT_NOQUOTES) . ":</b> " .
631             (!empty($row['deductiblemet']) ? ($row['deductiblemet'] == 'Y' ? htmlspecialchars(xl('Yes'), ENT_NOQUOTES) : htmlspecialchars(xl('No'), ENT_NOQUOTES)) : htmlspecialchars(xl('n/a'), ENT_NOQUOTES)) .
632             "</div>";
634     echo $showString;
637 // For EDI 271
640 // Function to save the values in eligibility_response table
642 function eligibility_response_save($segmentVal, $vendorID)
645     $resCount = 0;
647     $query = "  SELECT   count(*) as RecordsCount
648                                 FROM     eligibility_response
649                                 WHERE    response_description = ? and
650                                          response_vendor_id       = ?";
652     $resCount = sqlStatement($query, array($segmentVal, $vendorID));
655     if (isset($resCount)) {
656         $row        = sqlFetchArray($resCount);
658         $resCount   = $row['RecordsCount'];
659     }
661     if ($resCount == 0) {
662         $query = "INSERT into eligibility_response SET  response_description =?,
663                                                                 response_vendor_id       = ?,
664                                                                 response_create_date = now(),
665                                                                 response_modify_date = now()";
666         $res    = sqlStatement($query, array($segmentVal, $vendorID));
667     }
670 // Function to save the values in eligibility_verification table
672 function eligibility_verification_save($segmentVal, $x12PartnerId, $patientId)
675     $resCount = 0;
677     // For fetching the response Id
679     $query = "  SELECT   response_id
680                                 FROM     eligibility_response
681                                 WHERE    response_description = ? and
682                                                  response_vendor_id       = ?";
684     $resId  = sqlStatement($query, array($segmentVal, $x12PartnerId));
686     // For fetching the insuarace data Id
688     $query = "  SELECT   id,copay
689                                 FROM     insurance_data
690                                 WHERE    type = 'primary' and
691                                                  pid = ?";
693     $insId  = sqlStatement($query, array($patientId));
695     if (isset($resId)) {
696         $row        = sqlFetchArray($resId);
698         $responseId     = $row['response_id'];
699     }
701     if (isset($insId)) {
702         $row        = sqlFetchArray($insId);
704         $insuranceId    = $row['id'];
705         $copay      = $row['copay'];
706     }
708     if ($resCount == 0) {
709         if (isset($insuranceId) && !empty($insuranceId)) {
710             //Set up the sql variable binding array (this prevents sql-injection attacks)
711             $sqlBindArray = array();
712             $query = "INSERT into eligibility_verification SET  response_id     = ? ,
713                                                                                 insurance_id    = ?,";
714             array_push($sqlBindArray, $responseId, $insuranceId);
716             if (!empty($copay)) {
717                 $query .= "copay = ?,";
718                 array_push($sqlBindArray, $copay);
719             }
721             $query .= "eligibility_check_date   = now(),
722                                    create_date                  = now()";
723             $res    = sqlStatement($query, $sqlBindArray);
724         }
725     }
728 // Function to fetch the Patient information - eligibility
730 function eligibility_information($insuranceId)
732     $insuranceId = 1;
734     $query = "  SELECT          *
735                         FROM            eligibility_verification
736                         WHERE       insuranceid = ?";
738     $result         = sqlStatement($query, array($insuranceId));
739     $row        = sqlFetchArray($result);
740     return $row;
742 // return array of X12 partners
744 function getX12Partner()
746     $rez = sqlStatement("select * from x12_partners");
747     for ($iter=0; $row=sqlFetchArray($rez); $iter++) {
748         $returnval[$iter]=$row;
749     }
751     return $returnval;
754 // return array of provider usernames
755 function getUsernames()
757     $rez = sqlStatement("select distinct username, lname, fname,id from users " .
758         "where authorized = 1 and username != ''");
759     for ($iter=0; $row=sqlFetchArray($rez); $iter++) {
760         $returnval[$iter]=$row;
761     }
763     return $returnval;
766 // return formated array
768 function arrFormated(&$item, $key)
770     $item = strstr($item, '_');
771     $item = substr($item, 1, strlen($item)-1);
772     $item = "'".$item;