acknowledgments update
[openemr.git] / interface / soap_functions / soap_patientfullmedication.php
blobc99ac99e75412c60274e48103d1efce237c633b4
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
4 //
5 //
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");
38 require_once("{$GLOBALS['srcdir']}/lists.inc");
39 set_time_limit(0);
41 function number($str)
43 return preg_replace('/[^0-9.]/','',$str);
46 if(!$patientid)
47 $patientid=$pid;
48 if($_REQUEST['patient'])
49 $patientid=$_REQUEST['patient'];
50 $cred=getErxCredentials();
51 $path = getErxSoapPath();
52 $path = explode(';',$path);
53 $client = new SoapClient($path[0]);
54 $xml1_0['credentials']['PartnerName']=$cred['0'];
55 $xml1_0['credentials']['Name']=$cred['1'];
56 $xml1_0['credentials']['Password']=$cred['2'];
58 $erxSiteID=sqlQuery("SELECT federal_ein FROM facility WHERE primary_business_entity='1'");
59 $account=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?",array($_SESSION['authUser']));
60 $xml1_0['accountRequest']['AccountId']='1';
61 $xml1_0['accountRequest']['SiteId']=$erxSiteID['federal_ein'];
63 $xml1_0['patientRequest']['PatientId']=$patientid;
65 $xml1_0['prescriptionHistoryRequest']['StartHistory']='2011-01-01T00:00:00.000';
66 $xml1_0['prescriptionHistoryRequest']['EndHistory']=date('Y-m-d').'T23:59:59.000';
67 $xml1_0['prescriptionHistoryRequest']['PrescriptionStatus']='C';
68 $xml1_0['prescriptionHistoryRequest']['PrescriptionSubStatus']='%';
69 $xml1_0['prescriptionHistoryRequest']['PrescriptionArchiveStatus']='N';
71 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
72 $xml1_0['patientInformationRequester']['UserType']='D';
73 $xml1_0['patientInformationRequester']['UserId']=$user_details['id'];
75 $xml1_0['patientIdType']='';
76 $xml1_0['includeSchema']='';
78 $xml = $client->GetPatientFullMedicationHistory6($xml1_0);
80 $xml_response=$xml->GetPatientFullMedicationHistory6Result->XmlResponse;
81 $xml_response_count=$xml->GetPatientFullMedicationHistory6Result->RowCount;
82 $xml_response = base64_decode($xml_response);
84 $xmltoarray = new xmltoarray_parser_htmlfix(); //create instance of class
85 $xmltoarray->xmlparser_setoption(XML_OPTION_SKIP_WHITE, 1); //set options same as xml_parser_set_option
86 $xmltoarray->xmlparser_setoption(XML_OPTION_CASE_FOLDING, 0);
87 $xmltoarray->xmlparser_fix_into_struct($xml_response); //fixes html values for XML
88 $array = $xmltoarray->createArray(); //creates an array with fixed html values
89 foreach($array as $key => $value){
90 $array[$key] = $xmltoarray->fix_html_entities($value); //returns proper html values
92 $medArray=$array['NewDataSet']['Table'];
93 //print_r($medArray);die;
94 sqlQuery("update prescriptions set active=0 where patient_id=? and erx_source='1'",array($patientid));
95 $j=0;
96 for($i=0;$i<sizeof($medArray);$i++)
98 $provider=sqlQuery("select id from users where username=?",array($medArray[$i]['ExternalPhysicianID']));
99 if($medArray[$i]['DosageForm']){
100 $qin=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_form' AND title = ?",array($medArray[$i]['DosageForm']));
101 $rin=sqlFetchArray($qin);
102 if(sqlNumRows($qin)<=0)
104 $rin=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_form' ORDER BY ABS(option_id) DESC LIMIT 1");
105 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)));
106 $rin['option_id']=$rin['option_id']+1;
110 if($medArray[$i]['Route']){
111 $qroute=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_route' AND title = ?",array($medArray[$i]['Route']));
112 $rroute=sqlFetchArray($qroute);
113 if(sqlNumRows($qroute)<=0)
115 $rroute=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_route' ORDER BY ABS(option_id) DESC LIMIT 1");
116 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)));
117 $rroute['option_id']=$rroute['option_id']+1;
121 if($medArray[$i]['StrengthUOM']){
122 $qunit=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_units' AND title = ?",array($medArray[$i]['StrengthUOM']));
123 $runit=sqlFetchArray($qunit);
124 if(sqlNumRows($qunit)<=0)
126 $runit=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_units' ORDER BY ABS(option_id) DESC LIMIT 1");
127 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)));
128 $runit['option_id']=$runit['option_id']+1;
132 if($medArray[$i]['DosageFrequencyDescription']){
133 $qint=sqlStatement("SELECT option_id FROM list_options WHERE list_id='drug_interval' AND title = ?",array($medArray[$i]['DosageFrequencyDescription']));
134 $rint=sqlFetchArray($qint);
135 if(sqlNumRows($qint)<=0)
137 $rint=sqlQuery("SELECT option_id AS option_id FROM list_options WHERE list_id='drug_interval' ORDER BY ABS(option_id) DESC LIMIT 1");
138 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)));
139 $rint['option_id']=$rint['option_id']+1;
143 $check=sqlStatement("select * from prescriptions where prescriptionguid=? and patient_id=? and prescriptionguid is not null",array($medArray[$i]['PrescriptionGuid'],$medArray[$i]['ExternalPatientID']));
144 $prescription_id='';
145 if(sqlNumRows($check)==0)
147 $prescription_id=sqlInsert("insert into prescriptions
149 patient_id,provider_id,encounter,date_added,drug,drug_id,drug_info_erx,form,dosage,size,unit,route,`INTERVAL`,refills,note,`DATETIME`,
150 `USER`,site,prescriptionguid,erx_source,rxnorm_drugcode
152 values
153 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),?,?,?,'1',?)",
154 array($medArray[$i]['ExternalPatientID'], $provider['id'],$encounter, substr($medArray[$i]['PrescriptionDate'],0,10), $medArray[$i]['DrugName'],
155 $medArray[$i]['DrugID'], $medArray[$i]['DrugInfo'], $rin['option_id'], $medArray[$i]['DosageNumberDescription'], number($medArray[$i]['Strength']), $runit['option_id'],
156 $rroute['option_id'], $rint['option_id'], $medArray[$i]['Refills'], $medArray[$i]['PrescriptionNotes'],
157 $_SESSION['authUserID'], $medArray[$i]['SiteID'], $medArray[$i]['PrescriptionGuid'], $medArray[$i]['rxcui']));
158 $j++;
159 setListTouch ($patientid,'prescription_erx');
161 else
163 sqlQuery("update prescriptions set
164 provider_id=?, drug=?, drug_id=?, drug_info_erx=?, form=?, dosage=?, size=? ,unit=?, route=?, `INTERVAL`=?, refills=?, note=?,
165 `DATETIME`=NOW(),`USER`=?, site=? ,erx_source='1', rxnorm_drugcode=?, active='1'
166 WHERE prescriptionguid=? AND patient_id=?
167 ",array($provider['id'],$medArray[$i]['DrugName'],$medArray[$i]['DrugID'],$medArray[$i]['DrugInfo'],$rin['option_id'],$medArray[$i]['DosageNumberDescription'],
168 number($medArray[$i]['Strength']),$runit['option_id'],$rroute['option_id'],$rint['option_id'],$medArray[$i]['Refills'],
169 $medArray[$i]['PrescriptionNotes'],$_SESSION['authUserID'],
170 $medArray[$i]['SiteID'],$medArray[$i]['rxcui'],$medArray[$i]['PrescriptionGuid'],$medArray[$i]['ExternalPatientID']));
172 $result=sqlFetchArray($check);
173 if($result['id'])
174 $prescription_id=$result['id'];
175 processAmcCall('e_prescribe_amc', true, 'add', $medArray[$i]['ExternalPatientID'], 'prescriptions', $prescription_id);
177 if($j!=0)
178 sqlQuery("update patient_data set soap_import_status=? where pid=?",array('2',$pid));
179 if($xml_response_count==0)
180 echo htmlspecialchars( xl("Nothing to import for Prescription"), ENT_NOQUOTES);
181 elseif($xml_response_count>0)
182 echo htmlspecialchars( xl("Prescription History import successfully completed"), ENT_NOQUOTES);