Access Control Improvements (#473)
[openemr.git] / library / edi.inc
blob13fa1bd59e46e18400ca8a731a4800960882540d
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) {
33         $ISA     =      array();
35         $ISA[0] = "ISA";                                                        // Interchange Control Header Segment ID
37         $ISA[1] = "00";                                                         // Author Info Qualifier
39         $ISA[2] = str_pad("0000000",10," ");            // Author Information
41         $ISA[3] = "00";                                                         //   Security Information Qualifier
42                                                                                                 //   MEDI-CAL NOTE: For Leased-Line & Dial-Up use '01',
43                                                                                                 //   for BATCH use '00'.
44                                                                                                 //   '00' No Security Information Present
45                                                                                                 //   (No Meaningful Information in I04)
47         $ISA[4] = str_pad("0000000000",10," ");         // Security Information
49         $ISA[5] = str_pad("ZZ",2," ");                          // Interchange ID Qualifier
51         $ISA[6] = str_pad($X12info[2],15," ");          // INTERCHANGE SENDER ID
53         $ISA[7] = str_pad("ZZ",2," ");                          // Interchange ID Qualifier
55         $ISA[8] = str_pad($X12info[3],15," ");          // INTERCHANGE RECEIVER ID
57         $ISA[9] = str_pad(date('ymd'),6," ");           // Interchange Date (YYMMDD)
59         $ISA[10] = str_pad(date('Hi'),4," ");           // Interchange Time (HHMM)
61         $ISA[11] = "^";                                                         // Interchange Control Standards Identifier
63         $ISA[12] = str_pad("00501",5," ");                      // Interchange Control Version Number
65         $ISA[13] = str_pad("000000001",9," ");          // INTERCHANGE CONTROL NUMBER
67         $ISA[14] = str_pad("1",1," ");                          // Acknowledgment Request [0= not requested, 1= requested]
69         $ISA[15] =  str_pad("P",1," ");                         // Usage Indicator [ P = Production Data, T = Test Data ]
71         $ISA['Created'] = implode('*', $ISA);           // Data Element Separator
73         $ISA['Created'] = $ISA['Created'] ."*";
75         $ISA['Created'] = $ISA ['Created'] . $compEleSep . $segTer;
77         return trim($ISA['Created']);
81 // GS Segment  - EDI-270 format
83 function create_GS($row,$X12info,$segTer,$compEleSep) {
85         $GS        = array();
87         $GS[0] = "GS";                                          // Functional Group Header Segment ID
89         $GS[1] = "HS";                                          // Functional ID Code [ HS = Eligibility, Coverage or Benefit Inquiry (270) ]
91         $GS[2] =  $X12info[2];                          // Application Sender's ID
93         $GS[3] =  $X12info[3];                          // Application Receiver's ID
95         $GS[4] = date('Ymd');                           // Date [CCYYMMDD]
97         $GS[5] = date('His');                           // Time [HHMM] Group Creation Time
99         $GS[6] = "2";                       // Group Control Number No zeros for 5010
101         $GS[7] = "X";                                   // Responsible Agency Code Accredited Standards Committee X12 ]
103         $GS[8] = "005010X279A1";                        // Version Release / Industry[ Identifier Code Query 005010X279A1
105         $GS['Created'] = implode('*', $GS);             // Data Element Separator
107         $GS['Created'] = $GS ['Created'] . $segTer;  // change the information in the tag or change the tag
109         return trim($GS['Created']);
113 // ST Segment  - EDI-270 format
115 function create_ST($row,$X12info,$segTer,$compEleSep) {
117         $ST        =    array();
119         $ST[0] = "ST";                                                          // Transaction Set Header Segment ID
121         $ST[1] = "270";                                                         // Transaction Set Identifier Code (Inquiry Request)
123         $ST[2] = "000000003";                                           // Transaction Set Control Number - Must match SE's
125     $ST[3] = "005010X279A1";                    // Standard 005010X279A1 in $ST[3]
127         $ST['Created'] = implode('*', $ST);                     // Data Element Separator
129         $ST['Created'] = $ST ['Created'] . $segTer;
131         return trim($ST['Created']);
135 // BHT Segment  - EDI-270 format
137 function create_BHT($row,$X12info,$segTer,$compEleSep) {
139         $BHT    =       array();
141         $BHT[0] = "BHT";                                                // Beginning of Hierarchical Transaction Segment ID
143         $BHT[1] = "0022";                                               // Subscriber Structure Code
145         $BHT[2] = "13";                                                 // Purpose Code - This is a Request
147         $BHT[3] = "PROVTest600";                                //  Submitter Transaction Identifier
148                                                                                         //This information is required by the information Receiver
149                                                                                         //when using Real Time transactions.
150                                                                                         //For BATCH this can be used for optional information.
152         $BHT[4] = str_pad(date('Ymd'),8," ");                   // Date Transaction Set Created
154         $BHT[5] = str_pad(date('Hi'),4," ");                    // Time Transaction Set Created no space after and 1300 is plenty
156         $BHT['Created'] = implode('*', $BHT);                   // Data Element Separator
158         $BHT['Created'] = $BHT ['Created'] . $segTer;
160         return trim($BHT['Created']);
164 // HL Segment  - EDI-270 format
166 function create_HL($row, $nHlCounter,$X12info,$segTer,$compEleSep) {
168         $HL             = array();
170         $HL[0]          = "HL";                 // Hierarchical Level Segment ID
171         $HL_LEN[0]      =  2;
173         $HL[1] = $nHlCounter;           // Hierarchical ID No.
175         if($nHlCounter == 1)
176         {
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         }
181         else if($nHlCounter == 2)
182         {
183                 $HL[2] = 1;                             // Hierarchical Parent ID Number
184                 $HL[3] = 21;                    // Hierarchical Level Code. '21' Information Receiver
185                 $HL[4] = 1;                             // 1 Additional Subordinate HL Data Segment in This Hierarchical Structure.
186         }
187         else
188         {
189                 $HL[2] = 2;
190                 $HL[3] = 22;                    // Hierarchical Level Code.'22' Subscriber
191                 $HL[4] = 0;                             // 0 no Additional Subordinate in the Hierarchical Structure.
192         }
194         $HL['Created'] = implode('*', $HL);             // Data Element Separator
196         $HL['Created'] = $HL ['Created'] . $segTer;
198         return trim($HL['Created']);
202 // NM1 Segment  - EDI-270 format
204 function create_NM1($row,$nm1Cast,$X12info,$segTer,$compEleSep) {
206         $NM1            = array();
208         $NM1[0]         = "NM1";                                        // Subscriber Name Segment ID
210         if($nm1Cast == 'PR')
211         {
212                 $NM1[1] = "PR";                                         // Entity ID Code - Payer [PR Payer]
213                 $NM1[2] = "2";                                          // Entity Type - Non-Person
214                 $NM1[3] = $row["payer_name"];           // Organizational Name
215                 $NM1[4] = "";                                           // Data Element not required.
216                 $NM1[5] = "";                                           // Data Element not required.
217                 $NM1[6] = "";                                           // Data Element not required.
218                 $NM1[7] = "";                                           // Data Element not required.
219                 $NM1[8] = "46";                                         // 46 - Electronic Transmitter Identification Number (ETIN)
220                 $NM1[9] = $X12info[3];                          // Application Sender's ID
221         }
222         else if($nm1Cast == 'FA')
223         {
224                 $NM1[1] = "FA";                                         // Entity ID Code - Facility [FA Facility]
225                 $NM1[2] = "2";                                          // Entity Type - Non-Person
226                 $NM1[3] = $row['facility_name'];                        // Organizational Name
227                 $NM1[4] = "";                   // Data Element not required.
228                 $NM1[5] = "";                   // Data Element not required.
229                 $NM1[6] = "";                                           // Data Element not required.
230                 $NM1[7] = "";                                           // Data Element not required.
231                 $NM1[8] = "FI";
232                 $NM1[9] = $row['facility_npi'];
233         }
234         else if($nm1Cast == 'IL')
235         {
236                 $NM1[1] = "IL";                                         // Insured or Subscriber
237                 $NM1[2] = "1";                                          // Entity Type - Person
238                 $NM1[3] = $row['lname'];                                // last Name
239                 $NM1[4] = $row['fname'];                                // first Name
240                 $NM1[5] = $row['mname'];                                // middle Name
241                 $NM1[6] = "";                                           // data element
242                 $NM1[7] = "";                                           // data element
243                 $NM1[8] = "MI";                                         // Identification Code Qualifier
244                 $NM1[9] = $row['subscriber_ss'];                        // Identification Code
245         }
247         $NM1['Created'] = implode('*', $NM1);                           // Data Element Separator
249         $NM1['Created'] = $NM1['Created'] . $segTer;
251         return trim($NM1['Created']);
255 // REF Segment  - EDI-270 format
257 function create_REF($row,$ref,$X12info,$segTer,$compEleSep) {
259         $REF    =       array();
261         $REF[0] = "REF";                                                // Subscriber Additional Identification    does not want this for anything
263         if($ref == '1P')
264         {
265                 $REF[1] = "4A";                                         // Reference Identification Qualifier
266                 $REF[2] = $row['provider_pin'];                         // Provider Pin.
267         }
268         else
269         {
270                 $REF[1] = "EJ";                                         // 'EJ' for Patient Account Number     does not want this for patient
271                 $REF[2] = $row['pid'];                                  // Patient Account No.
272         }
273         $REF['Created'] = implode('*', $REF);                           // Data Element Separator
275         $REF['Created'] = $REF['Created'] . $segTer;
277         return trim($REF['Created']);
281 // TRN Segment - EDI-270 format
283 function create_TRN($row,$tracno,$refiden,$X12info,$segTer,$compEleSep) {
285         $TRN    =       array();
287         $TRN[0] = "TRN";                                                // Subscriber Trace Number Segment ID
289         $TRN[1] = "1";                                                  // Trace Type Code ï¿½ Current Transaction Trace Numbers
291         $TRN[2] = $tracno;                                              // Trace Number
293         $TRN[3] = "9000000000";                                         // Originating Company ID ï¿½ must be 10 positions in length
295         $TRN[4] = $refiden;                                             // Additional Entity Identifier (i.e. Subdivision)
297         $TRN['Created'] = implode('*', $TRN);                           // Data Element Separator
299         $TRN['Created'] = $TRN['Created'] . $segTer;
301         return trim($TRN['Created']);
305 // DMG Segment - EDI-270 format
307 function create_DMG($row,$X12info,$segTer,$compEleSep) {
309         $DMG    =       array();
311         $DMG[0] = "DMG";                                                        // Date or Time or Period Segment ID
313         $DMG[1] = "D8";                                                         // Date Format Qualifier - (D8 means CCYYMMDD)
315         $DMG[2] = $row['dob'];                                          // Subscriber's Birth date
317         $DMG['Created'] = implode('*', $DMG);           // Data Element Separator
319         $DMG['Created'] = $DMG['Created'] .  $segTer;
321         return trim($DMG['Created']);
324 // DTP Segment - EDI-270 format
326 function create_DTP($row,$qual,$X12info,$segTer,$compEleSep) {
328         $DTP    =       array();
330         $DTP[0] = "DTP";                                                // Date or Time or Period Segment ID
332         $DTP[1] = $qual;                                                // Qualifier - Date of Service
334         $DTP[2] = "D8";                                                 // Date Format Qualifier - (D8 means CCYYMMDD)
336         if($qual == '102'){
337                 $DTP[3] = $row['date'];                         // Date
338         }else{
339                 $DTP[3] = $row['pc_eventDate'];         // Date of Service
340         }
341         $DTP['Created'] = implode('*', $DTP);   // Data Element Separator
343         $DTP['Created'] = $DTP['Created'] .  $segTer;
345         return trim($DTP['Created']);
348 // EQ Segment - EDI-270 format
350 function create_EQ($row,$X12info,$segTer,$compEleSep) {
352         $EQ             =       array();
354         $EQ[0]  = "EQ";                                                                 // Subscriber Eligibility or Benefit Inquiry Information
356         $EQ[1]  = "30";                                                                 // Service Type Code
358         $EQ['Created'] = implode('*', $EQ);                             // Data Element Separator
360         $EQ['Created'] = $EQ['Created'] . $segTer;
362         return trim($EQ['Created']);
365 // SE Segment - EDI-270 format
367 function create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep) {
369         $SE     =       array();
371         $SE[0] = "SE";                                                          // Transaction Set Trailer Segment ID
373         $SE[1] = $segmentcount;                                         // Segment Count
375         $SE[2] = "000000003";                                           // Transaction Set Control Number - Must match ST's
377         $SE['Created'] = implode('*', $SE);                     // Data Element Separator
379         $SE['Created'] = $SE['Created'] . $segTer;
381         return trim($SE['Created']);
384 // GE Segment - EDI-270 format
386 function create_GE($row,$X12info,$segTer,$compEleSep) {
388         $GE     =       array();
390         $GE[0]  = "GE";                                                 // Functional Group Trailer Segment ID
392         $GE[1]  = "1";                                                  // Number of included Transaction Sets
394         $GE[2]  = "2";                          // Group Control Number
396         $GE['Created'] = implode('*', $GE);                             // Data Element Separator
398         $GE['Created'] = $GE['Created'] . $segTer;
400         return trim($GE['Created']);
403 // IEA Segment - EDI-270 format
405 function create_IEA($row,$X12info,$segTer,$compEleSep) {
407         $IEA    =       array();
409         $IEA[0] = "IEA";                                                // Interchange Control Trailer Segment ID
411         $IEA[1] = "1";                                                  // Number of included Functional Groups
413         $IEA[2] = "000000001";                                          // Interchange Control Number
415         $IEA['Created'] = implode('*', $IEA);
417         $IEA['Created'] = $IEA['Created'] .  $segTer;
419         return trim($IEA['Created']);
422 function translate_relationship($relationship) {
423         switch ($relationship) {
424                 case "spouse":
425                         return "01";
426                         break;
427                 case "child":
428                         return "19";
429                         break;
430                 case "self":
431                 default:
432                         return "S";
433         }
436 // EDI-270 Batch file Generation
438 function print_elig($res,$X12info,$segTer,$compEleSep){
440         $i=1;
442         $PATEDI    = "";
444         // For Header Segment
446         $nHlCounter = 1;
447         $rowCount       = 0;
448         $trcNo          = 1234501;
449         $refiden        = 5432101;
451         while ($row = sqlFetchArray($res))
452         {
454                 if($nHlCounter == 1)
455                 {
456                         // create ISA
457                         $PATEDI    = create_ISA($row,$X12info,$segTer,$compEleSep);
459                         // create GS
460                         $PATEDI   .= create_GS($row,$X12info,$segTer,$compEleSep);
462                         // create ST
463                         $PATEDI   .= create_ST($row,$X12info,$segTer,$compEleSep);
465                         // create BHT
466                         $PATEDI   .= create_BHT($row,$X12info,$segTer,$compEleSep);
468                         // For Payer Segment
470                         $PATEDI  .= create_HL($row,1,$X12info,$segTer,$compEleSep);
471                         $PATEDI  .= create_NM1($row,'PR',$X12info,$segTer,$compEleSep);
473                         // For Provider Segment
475                         $PATEDI  .= create_HL($row,2,$X12info,$segTer,$compEleSep);
476                         $PATEDI  .= create_NM1($row,'FA',$X12info,$segTer,$compEleSep);
478                         $nHlCounter = $nHlCounter + 2;
479                         $segmentcount = 7; // segement counts - start from ST
480                 }
482                 // For Subscriber Segment
484                 $PATEDI  .= create_HL($row,$nHlCounter,$X12info,$segTer,$compEleSep);
485                 $PATEDI  .= create_NM1($row,'IL',$X12info,$segTer,$compEleSep);
486                 $PATEDI  .= create_DMG($row,$X12info,$segTer,$compEleSep);
488                 $PATEDI  .= create_DTP($row,'291',$X12info,$segTer,$compEleSep);
489                 $PATEDI  .= create_EQ($row,$X12info,$segTer,$compEleSep);
491                 $segmentcount   = $segmentcount + 7;
492                 $nHlCounter     = $nHlCounter + 1;
493                 $rowCount       = $rowCount + 1;
494                 $trcNo          = $trcNo + 1;
495                 $refiden        = $refiden + 1;
498                 if($rowCount == sqlNumRows($res))
499                 {
500                         $segmentcount = $segmentcount + 1;
501                         $PATEDI   .= create_SE($row,$segmentcount,$X12info,$segTer,$compEleSep);
502                         $PATEDI   .= create_GE($row,$X12info,$segTer,$compEleSep);
503                         $PATEDI   .= create_IEA($row,$X12info,$segTer,$compEleSep);
504                 }
505         }
507         echo $PATEDI;
510 // Report Generation
512 function show_elig($res,$X12info,$segTer,$compEleSep){
514         $i=0;
515         echo "  <div id='report_results'>
516                         <table>
517                                 <thead>
519                                         <th style='width:12%;'> ". htmlspecialchars( xl('Facility Name'), ENT_NOQUOTES) ."</th>
520                                         <th style='width:9%;' > ". htmlspecialchars( xl('Facility NPI'), ENT_NOQUOTES) ."</th>
521                                         <th style='width:15%;'> ". htmlspecialchars( xl('Insurance Comp'), ENT_NOQUOTES) ."</th>
522                                         <th style='width:8%;' > ". htmlspecialchars( xl('Policy No'), ENT_NOQUOTES) ."</th>
523                                         <th style='width:16%;'> ". htmlspecialchars( xl('Patient Name'), ENT_NOQUOTES) ."</th>
524                                         <th style='width:7%;' > ". htmlspecialchars( xl('DOB'), ENT_NOQUOTES) ."</th>
525                                         <th style='width:6%;' > ". htmlspecialchars( xl('Gender'), ENT_NOQUOTES) ."</th>
526                                         <th style='width:9%;' > ". htmlspecialchars( xl('SSN'), ENT_NOQUOTES) ."</th>
527                                         <th style='width:2%;' > &nbsp;                    </th>
528                                 </thead>
530                                 <tbody>
532                 ";
534         while ($row = sqlFetchArray($res)) {
537                 $i= $i+1;
539                 if($i%2 == 0){
540                         $background = '#FFF';
541                 }else{
542                         $background = '#FFF';
543                 }
545                 $elig     = array();
546                 $elig[0]  = $row['facility_name'];                              // Inquiring Provider Name  calendadr
547                 $elig[1]  = $row['facility_npi'];                               // Inquiring Provider NPI
548                 $elig[2]  = $row['payer_name'];                                 // Payer Name  our insurance co name
549                 $elig[3]  = $row['policy_number'];                              // Subscriber ID
550                 $elig[4]  = $row['subscriber_lname'];                           // Subscriber Last Name
551                 $elig[5]  = $row['subscriber_fname'];                           // Subscriber First Name
552                 $elig[6]  = $row['subscriber_mname'];                           // Subscriber Middle Initial
553                 $elig[7]  = $row['subscriber_dob'];                             // Subscriber Date of Birth
554                 $elig[8]  = substr($row['subscriber_sex'], 0, 1);               // Subscriber Sex
555                 $elig[9]  = $row['subscriber_ss'];                              // Subscriber SSN
556                 $elig[10] = translate_relationship($row['subscriber_relationship']);    // Pt Relationship to insured
557                 $elig[11] = $row['lname'];                                      // Dependent Last Name
558                 $elig[12] = $row['fname'];                                      // Dependent First Name
559                 $elig[13] = $row['mname'];                                      // Dependent Middle Initial
560                 $elig[14] = $row['dob'];                                        // Dependent Date of Birth
561                 $elig[15] = substr($row['sex'], 0, 1);                          // Dependent Sex
562                 $elig[16] = $row['pc_eventDate'];                               // Date of service
563                 $elig[17] = "30";                                               // Service Type
564                 $elig[18] = $row['pubpid'];                                     // Patient Account Number pubpid
566                 echo "  <tr id='PR".$i."_". htmlspecialchars( $row['policy_number'], ENT_QUOTES)."'>
567                                 <td class ='detail' style='width:12%;'>". htmlspecialchars( $row['facility_name'], ENT_NOQUOTES) ."</td>
568                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['facility_npi'], ENT_NOQUOTES) ."</td>
569                                 <td class ='detail' style='width:15%;'>". htmlspecialchars( $row['payer_name'], ENT_NOQUOTES) ."</td>
570                                 <td class ='detail' style='width:8%;'>".  htmlspecialchars( $row['policy_number'], ENT_NOQUOTES) ."</td>
571                                 <td class ='detail' style='width:16%;'>". htmlspecialchars( $row['subscriber_lname']." ".$row['subscriber_fname'], ENT_NOQUOTES) ."</td>
572                                 <td class ='detail' style='width:7%;'>".  htmlspecialchars( $row['subscriber_dob'], ENT_NOQUOTES) ."</td>
573                                 <td class ='detail' style='width:6%;'>".  htmlspecialchars( $row['subscriber_sex'], ENT_NOQUOTES) ."</td>
574                                 <td class ='detail' style='width:9%;'>".  htmlspecialchars( $row['subscriber_ss'], ENT_NOQUOTES) ."</td>
575                                 <td class ='detail' style='width:2%;'>
576                                         <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) . "\")'>
577                                 </td>
578                         </tr>
579                 ";
582                 unset($elig);
583         }
585         if($i==0){
587                 echo "  <tr>
588                                 <td class='norecord' colspan=9>
589                                         <div style='padding:5px;font-family:arial;font-size:13px;text-align:center;'>". htmlspecialchars( xl('No records found'), ENT_NOQUOTES) . "</div>
590                                 </td>
591                         </tr>   ";
592         }
593                 echo "  </tbody>
594                         </table>";
597 // To Show Eligibility Verification data
599 function show_eligibility_information($pid)
602         $query = "      SELECT          eligr.response_description as ResponseMessage,
603                                         DATE_FORMAT(eligv.eligibility_check_date, '%d %M %Y') as VerificationDate,
604                                         eligv.copay, eligv.deductible, eligv.deductiblemet,
605                                         if(eligr.response_status = 'A','Active','Inactive') as Status,
606                                         insd.pid, insc.name
607                         FROM            eligibility_verification eligv
608                         INNER JOIN      eligibility_response eligr on eligr.response_id = eligv.response_id
609                         INNER JOIN      insurance_data insd on insd.id = eligv.insurance_id
610                         INNER JOIN      insurance_companies insc on insc.id = insd.provider
611                         WHERE           insd.pid = ?
612                         AND                     eligr.response_status = 'A'
613                         AND                     eligv.eligibility_check_date = (SELECT max(eligibility_check_date)
614                                                                                 FROM eligibility_verification
615                                                                                 WHERE   insurance_id = eligv.insurance_id)";
616         $result         = sqlStatement($query, array($pid) );
618         $row = sqlFetchArray($result);
620         $showString="";
621         $showString .=  "<br><div class='text'>" .
622                         "<b>" .
623                         htmlspecialchars( xl('Insurance Provider'), ENT_NOQUOTES) . ":</b> " .
624                         (!empty($row['name']) ? htmlspecialchars( $row['name'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
625                         "<br>\n" .
626                         "<b>" .
627                         htmlspecialchars( xl('Status'), ENT_NOQUOTES) . ":</b> " .
628                         (!empty($row['ResponseMessage']) ? htmlspecialchars( $row['ResponseMessage'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
629                         "<br>\n" .
630                         "<b>" .
631                         htmlspecialchars( xl('Last Verified On'), ENT_NOQUOTES) . ":</b> " .
632                         (!empty($row['VerificationDate']) ? htmlspecialchars( $row['VerificationDate'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
633                         "<br>" .
634                         "<b>" . htmlspecialchars( xl('Copay'), ENT_NOQUOTES) . ":</b> " .
635                         (!empty($row['copay']) ? htmlspecialchars( $row['copay'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
636                         "<br><b>" . htmlspecialchars( xl('Deductible'), ENT_NOQUOTES) . ":</b> " .
637                         (!empty($row['deductible']) ? htmlspecialchars( $row['deductible'], ENT_NOQUOTES) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
638                         "<br><b>" . htmlspecialchars( xl('Deductible Met'), ENT_NOQUOTES) . ":</b> " .
639                         (!empty($row['deductiblemet']) ? ($row['deductiblemet'] == 'Y' ? htmlspecialchars( xl('Yes'), ENT_NOQUOTES) : htmlspecialchars( xl('No'), ENT_NOQUOTES)) : htmlspecialchars( xl('n/a'), ENT_NOQUOTES)) .
640                         "</div>";
642         echo $showString;
645 // For EDI 271
648 // Function to save the values in eligibility_response table
650 function eligibility_response_save($segmentVal,$vendorID)
653         $resCount = 0;
655         $query = "  SELECT   count(*) as RecordsCount
656                                 FROM     eligibility_response
657                                 WHERE    response_description = ? and
658                                          response_vendor_id       = ?";
660         $resCount = sqlStatement($query, array($segmentVal, $vendorID) );
663         if(isset($resCount))
664         {
666                 $row            = sqlFetchArray($resCount);
668                 $resCount       = $row['RecordsCount'];
670         }
672         if($resCount == 0)
673         {
675                 $query = "INSERT into eligibility_response SET  response_description =?,
676                                                                 response_vendor_id       = ?,
677                                                                 response_create_date = now(),
678                                                                 response_modify_date = now()";
679                 $res    = sqlStatement($query, array($segmentVal, $vendorID) );
680         }
684 // Function to save the values in eligibility_verification table
686 function eligibility_verification_save($segmentVal,$x12PartnerId,$patientId)
689         $resCount = 0;
691         // For fetching the response Id
693         $query = "  SELECT       response_id
694                                 FROM     eligibility_response
695                                 WHERE    response_description = ? and
696                                                  response_vendor_id       = ?";
698         $resId  = sqlStatement($query, array($segmentVal, $x12PartnerId) );
700         // For fetching the insuarace data Id
702         $query = "  SELECT       id,copay
703                                 FROM     insurance_data
704                                 WHERE    type = 'primary' and
705                                                  pid = ?";
707         $insId  = sqlStatement($query, array($patientId) );
709         if(isset($resId))
710         {
712                 $row            = sqlFetchArray($resId);
714                 $responseId     = $row['response_id'];
716         }
717         if(isset($insId))
718         {
720                 $row            = sqlFetchArray($insId);
722                 $insuranceId    = $row['id'];
723                 $copay          = $row['copay'];
725         }
727         if($resCount == 0)
728         {
730                 if(isset($insuranceId) && !empty($insuranceId)){
732                         //Set up the sql variable binding array (this prevents sql-injection attacks)
733                         $sqlBindArray = array();
734                         $query = "INSERT into eligibility_verification SET      response_id     = ? ,
735                                                                                 insurance_id    = ?,";
736                         array_push($sqlBindArray, $responseId, $insuranceId);
738                         if(!empty($copay))
739                         {
740                                 $query .= "copay = ?,";
741                                 array_push($sqlBindArray, $copay);
742                         }
743                         $query .= "eligibility_check_date       = now(),
744                                    create_date                  = now()";
745                         $res    = sqlStatement($query, $sqlBindArray);
746                 }
747         }
750 // Function to fetch the Patient information - eligibility
752 function eligibility_information($insuranceId)
754         $insuranceId = 1;
756         $query = "      SELECT          *
757                         FROM            eligibility_verification
758                         WHERE       insuranceid = ?";
760         $result         = sqlStatement($query, array($insuranceId) );
761         $row            = sqlFetchArray($result);
762         return $row;
765 // return array of X12 partners
767 function getX12Partner() {
768         $rez = sqlStatement("select * from x12_partners");
769         for($iter=0; $row=sqlFetchArray($rez); $iter++)
770                 $returnval[$iter]=$row;
772         return $returnval;
775 // return array of provider usernames
776 function getUsernames() {
777         $rez = sqlStatement("select distinct username, lname, fname,id from users " .
778                 "where authorized = 1 and username != ''");
779         for($iter=0; $row=sqlFetchArray($rez); $iter++)
780                 $returnval[$iter]=$row;
782         return $returnval;
785 // return formated array
787 function arrFormated(&$item, $key){
788         $item = strstr($item, '_');
789         $item = substr($item,1,strlen($item)-1);
790         $item = "'".$item;