2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Vinish K <vinish@zhservices.com>
26 // +------------------------------------------------------------------------------+
27 //SANITIZE ALL ESCAPES
28 $sanitize_all_escapes=true;
31 //STOP FAKE REGISTER GLOBALS
32 $fake_register_globals=false;
34 require_once('../globals.php');
35 require_once('../eRx_xml.php');
36 require_once('../../library/xmltoarray_parser_htmlfix.php');
37 require_once($GLOBALS['fileroot'] . "/library/amc.php");
42 return preg_replace('/[^0-9.]/','',$str);
47 if($_REQUEST['patient'])
48 $patientid=$_REQUEST['patient'];
49 $cred=getErxCredentials();
50 $path = getErxSoapPath();
51 $path = explode(';',$path);
52 $client = new SoapClient($path[0]);
53 $xml1_0['credentials']['PartnerName']=$cred['0'];
54 $xml1_0['credentials']['Name']=$cred['1'];
55 $xml1_0['credentials']['Password']=$cred['2'];
57 $erxSiteID=sqlQuery("SELECT federal_ein FROM facility WHERE primary_business_entity='1'");
58 $account=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?",array($_SESSION['authUser']));
59 $xml1_0['accountRequest']['AccountId']='1';
60 $xml1_0['accountRequest']['SiteId']=$erxSiteID['federal_ein'];
62 $xml1_0['patientRequest']['PatientId']=$patientid;
64 $xml1_0['prescriptionHistoryRequest']['StartHistory']='2011-01-01T00:00:00.000';
65 $xml1_0['prescriptionHistoryRequest']['EndHistory']=date('Y-m-d').'T23:59:59.000';
66 $xml1_0['prescriptionHistoryRequest']['PrescriptionStatus']='C';
67 $xml1_0['prescriptionHistoryRequest']['PrescriptionSubStatus']='%';
68 $xml1_0['prescriptionHistoryRequest']['PrescriptionArchiveStatus']='N';
70 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
71 $xml1_0['patientInformationRequester']['UserType']='D';
72 $xml1_0['patientInformationRequester']['UserId']=$user_details['id'];
74 $xml1_0['patientIdType']='';
75 $xml1_0['includeSchema']='';
77 $xml = $client->GetPatientFullMedicationHistory6($xml1_0);
79 $xml_response=$xml->GetPatientFullMedicationHistory6Result
->XmlResponse
;
80 $xml_response_count=$xml->GetPatientFullMedicationHistory6Result
->RowCount
;
81 $xml_response = base64_decode($xml_response);
83 $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
84 $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE
, 1); //set options same as xml_parser_set_option
85 $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING
, 0);
86 $xmltoarray->xmlparser_fix_into_struct($xml_response); //fixes html values for XML
87 $array = $xmltoarray->createArray(); //creates an array with fixed html values
88 foreach($array as $key => $value){
89 $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
91 $medArray=$array['NewDataSet']['Table'];
92 //print_r($medArray);die;
93 sqlQuery("update prescriptions set active=0 where patient_id=? and erx_source='1'",array($patientid));
95 for($i=0;$i<sizeof($medArray);$i++
)
97 $provider=sqlQuery("select id from users where username=?",array($medArray[$i]['ExternalPhysicianID']));
98 if($medArray[$i]['DosageForm']){
99 $qin=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_form' AND title = ?",array($medArray[$i]['DosageForm']));
100 $rin=sqlFetchArray($qin);
101 if(sqlNumRows($qin)<=0)
103 $rin=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_form' ORDER BY ABS(option_id) DESC LIMIT 1");
104 sqlQuery("INSERT INTO list_options (list_id,option_id,title,seq) VALUES ('drug_form',?,?,?)",array(($rin['option_id']+
1),$medArray[$i]['DosageForm'],($rin['option_id']+
1)));
105 $rin['option_id']=$rin['option_id']+
1;
109 if($medArray[$i]['Route']){
110 $qroute=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_route' AND title = ?",array($medArray[$i]['Route']));
111 $rroute=sqlFetchArray($qroute);
112 if(sqlNumRows($qroute)<=0)
114 $rroute=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_route' ORDER BY ABS(option_id) DESC LIMIT 1");
115 sqlQuery("INSERT INTO list_options (list_id,option_id,title,seq) VALUES ('drug_route',?,?,?)",array(($rroute['option_id']+
1),$medArray[$i]['Route'],($rroute['option_id']+
1)));
116 $rroute['option_id']=$rroute['option_id']+
1;
120 if($medArray[$i]['StrengthUOM']){
121 $qunit=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_units' AND title = ?",array($medArray[$i]['StrengthUOM']));
122 $runit=sqlFetchArray($qunit);
123 if(sqlNumRows($qunit)<=0)
125 $runit=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_units' ORDER BY ABS(option_id) DESC LIMIT 1");
126 sqlQuery("INSERT INTO list_options (list_id,option_id,title,seq) VALUES ('drug_units',?,?,?)",array(($runit['option_id']+
1),$medArray[$i]['StrengthUOM'],($runit['option_id']+
1)));
127 $runit['option_id']=$runit['option_id']+
1;
131 if($medArray[$i]['DosageFrequencyDescription']){
132 $qint=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_interval' AND title = ?",array($medArray[$i]['DosageFrequencyDescription']));
133 $rint=sqlFetchArray($qint);
134 if(sqlNumRows($qint)<=0)
136 $rint=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_interval' ORDER BY ABS(option_id) DESC LIMIT 1");
137 sqlQuery("INSERT INTO list_options (list_id,option_id,title,seq) VALUES ('drug_interval',?,?,?)",array(($rint['option_id']+
1),$medArray[$i]['DosageFrequencyDescription'],($rint['option_id']+
1)));
138 $rint['option_id']=$rint['option_id']+
1;
142 $check=sqlStatement("select * from prescriptions where prescriptionguid=? and patient_id=? and prescriptionguid is not null",array($medArray[$i]['PrescriptionGuid'],$medArray[$i]['ExternalPatientID']));
144 if(sqlNumRows($check)==0)
146 $prescription_id=sqlInsert("insert into prescriptions
148 patient_id,provider_id,encounter,date_added,drug,drug_id,drug_info_erx,form,dosage,size,unit,route,`INTERVAL`,refills,note,`DATETIME`,
149 `USER`,site,prescriptionguid,erx_source,rxnorm_drugcode
152 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),?,?,?,'1',?)",
153 array($medArray[$i]['ExternalPatientID'], $provider['id'],$encounter, substr($medArray[$i]['PrescriptionDate'],0,10), $medArray[$i]['DrugName'],
154 $medArray[$i]['DrugID'], $medArray[$i]['DrugInfo'], $rin['option_id'], $medArray[$i]['DosageNumberDescription'], number($medArray[$i]['Strength']), $runit['option_id'],
155 $rroute['option_id'], $rint['option_id'], $medArray[$i]['Refills'], $medArray[$i]['PrescriptionNotes'],
156 $_SESSION['authUserID'], $medArray[$i]['SiteID'], $medArray[$i]['PrescriptionGuid'], $medArray[$i]['rxcui']));
161 sqlQuery("update prescriptions set
162 provider_id=?, drug=?, drug_id=?, drug_info_erx=?, form=?, dosage=?, size=? ,unit=?, route=?, `INTERVAL`=?, refills=?, note=?,
163 `DATETIME`=NOW(),`USER`=?, site=? ,erx_source='1', rxnorm_drugcode=?, active='1'
164 WHERE prescriptionguid=? AND patient_id=?
165 ",array($provider['id'],$medArray[$i]['DrugName'],$medArray[$i]['DrugID'],$medArray[$i]['DrugInfo'],$rin['option_id'],$medArray[$i]['DosageNumberDescription'],
166 number($medArray[$i]['Strength']),$runit['option_id'],$rroute['option_id'],$rint['option_id'],$medArray[$i]['Refills'],
167 $medArray[$i]['PrescriptionNotes'],$_SESSION['authUserID'],
168 $medArray[$i]['SiteID'],$medArray[$i]['rxcui'],$medArray[$i]['PrescriptionGuid'],$medArray[$i]['ExternalPatientID']));
170 $result=sqlFetchArray($check);
172 $prescription_id=$result['id'];
173 processAmcCall('e_prescribe_amc', true, 'add', $medArray[$i]['ExternalPatientID'], 'prescriptions', $prescription_id);
176 sqlQuery("update patient_data set soap_import_status=? where pid=?",array('2',$pid));
177 if($xml_response_count==0)
178 echo htmlspecialchars( xl("Nothing to import for Prescription"), ENT_NOQUOTES
);
179 elseif($xml_response_count>0)
180 echo htmlspecialchars( xl("Prescription History import successfully completed"), ENT_NOQUOTES
);