erx fix 4
[openemr.git] / interface / eRx_xml.php
blobef908f8585a8772560f3f19807d54a8e5abb86a5
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 function getErxPath()
29 //if($GLOBALS['erx_source']==1)
30 //return $GLOBALS['erx_path'];
31 //else if($GLOBALS['erx_source']==2)
32 return $GLOBALS['erx_path_production'];
35 function getErxSoapPath()
37 //if($GLOBALS['erx_source']==1)
38 //return $GLOBALS['erx_path_soap'];
39 //else if($GLOBALS['erx_source']==2)
40 return $GLOBALS['erx_path_soap_production'];
43 function getErxCredentials()
45 $cred=array();
46 //if($GLOBALS['erx_source']==1)
47 //{
48 // $cred[]=$GLOBALS['partner_name'];
49 // $cred[]=$GLOBALS['erx_name'];
50 // $cred[]=$GLOBALS['erx_password'];
51 //}
52 //else if($GLOBALS['erx_source']==2)
53 //{
54 $cred[]=$GLOBALS['partner_name_production'];
55 $cred[]=$GLOBALS['erx_name_production'];
56 $cred[]=$GLOBALS['erx_password_production'];
57 //}
58 return $cred;
61 function validation($val_check,$val,$msg)
63 if(!$val)
64 $msg .= $val_check.' '.xl('missing').'<br>';
65 return $msg;
68 function stripSpecialCharacterFacility($str)
70 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/","",$str);
71 return $str;
74 function stripSpecialCharacter($str)
76 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/","",$str);
77 return $str;
80 function stripPhoneSlashes($str)
82 $str=preg_replace('/-/','',$str);
83 return $str;
86 function trimData($str,$length)
88 $str=substr($str,0,($length-1));
89 return $str;
92 function credentials($doc,$r)
94 global $msg;
95 $cred=getErxCredentials();
96 $msg = validation(xl('Partner Name'),$cred['0'],$msg);
97 $b = $doc->createElement( "Credentials" );
98 $partnerName = $doc->createElement( "partnerName" );
99 $partnerName->appendChild(
100 $doc->createTextNode( $cred['0'] )
102 $b->appendChild( $partnerName );
103 $msg = validation(xl('ERX Name'),$cred['1'],$msg);
104 $name = $doc->createElement( "name" );
105 $name->appendChild(
106 $doc->createTextNode( $cred['1'] )
108 $b->appendChild( $name );
109 $msg = validation(xl('ERX Password'),$cred['2'],$msg);
110 $password = $doc->createElement( "password" );
111 $password->appendChild(
112 $doc->createTextNode( $cred['2'] )
114 $b->appendChild( $password );
115 $productName = $doc->createElement( "productName" );
116 $productName->appendChild(
117 $doc->createTextNode( 'OpenEMR' )
119 $b->appendChild( $productName );
120 $productVersion = $doc->createElement( "productVersion" );
121 $productVersion->appendChild(
122 $doc->createTextNode( $GLOBALS['openemr_version'] )
124 $b->appendChild( $productVersion );
125 $r->appendChild( $b );
128 function user_role($doc,$r)
130 global $msg;
131 $userRole=sqlQuery("select * from users where username=?",array($_SESSION['authUser']));
132 if(!$userRole['newcrop_user_role'])
133 {echo xl('Unauthorized access to ePrescription');die;}
134 $userRole['newcrop_user_role'] = preg_replace('/erx/','',$userRole['newcrop_user_role']);
135 if($userRole['newcrop_user_role'] == 'doctor')
136 $userRole['eRxUser'] = 'LicensedPrescriber';
137 elseif($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse')
138 $userRole['eRxUser'] = 'Staff';
139 elseif($userRole['newcrop_user_role'] == 'midlevelPrescriber')
140 $userRole['eRxUser'] = 'MidlevelPrescriber';
141 elseif($userRole['newcrop_user_role'] == 'supervisingDoctor')
142 $userRole['eRxUser'] = 'SupervisingDoctor';
143 $msg = validation(xl('ERX User'),$userRole['eRxUser'],$msg);
144 $b = $doc->createElement( "UserRole" );
145 $user = $doc->createElement( "user" );
146 $user->appendChild(
147 $doc->createTextNode( $userRole['eRxUser'] )
149 $b->appendChild( $user );
150 $msg = validation(xl('ERX Role'),$userRole['newcrop_user_role'],$msg);
151 $role = $doc->createElement( "role" );
152 $role->appendChild(
153 $doc->createTextNode( $userRole['newcrop_user_role'] )
155 $b->appendChild( $role );
156 $r->appendChild( $b );
159 function destination($doc,$r,$page='',$pid)
161 global $msg,$page;
162 $userRole=sqlQuery("select * from users where username=?",array($_SESSION['authUser']));
163 $userRole['newcrop_user_role'] = preg_replace('/erx/','',$userRole['newcrop_user_role']);
164 if(!$page)
166 $page='compose';
167 if($userRole['newcrop_user_role']=='admin')
168 $page='admin';
169 elseif($userRole['newcrop_user_role']=='manager')
170 $page='manager';
172 $b = $doc->createElement( "Destination" );
173 $requestedPage = $doc->createElement( "requestedPage" );
174 $requestedPage->appendChild(
175 $doc->createTextNode( $page )
177 $b->appendChild( $requestedPage );
178 $r->appendChild( $b );
181 function account($doc,$r)
183 global $msg;
184 $erxSiteID=sqlQuery("SELECT federal_ein FROM facility WHERE primary_business_entity='1'");
185 if(!$erxSiteID['federal_ein'])
186 {echo htmlspecialchars( xl("Please select a Primary Business Entity facility with 'Tax ID' as your facility Tax ID. If you are an individual practitioner, use your tax id. This is used for identifying you in the NewCrop system."), ENT_NOQUOTES);die;}
187 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?",array($_SESSION['authUser']));
188 $b = $doc->createElement( "Account" );
189 $b->setAttribute('ID','1');
190 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
191 $userRole['name']=trimData($userRole['name'],35);
192 $msg = validation(xl('Account Name'),$userRole['name'],$msg);
193 $accountName = $doc->createElement( "accountName" );
194 $accountName->appendChild(
195 $doc->createTextNode( $userRole['name'] )
197 $b->appendChild( $accountName );
198 $msg = validation(xl('Site ID'),$_SESSION['site_id'],$msg);
199 $siteID = $doc->createElement( "siteID" );
200 $siteID->appendChild(
201 $doc->createTextNode( $erxSiteID['federal_ein'] )
203 $b->appendChild( $siteID );
204 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
205 $userRole['street']=trimData($userRole['street'],35);
206 $AccountAddress = $doc->createElement( "AccountAddress" );
207 $msg = validation(xl('Facility Street'),$userRole['street'],$msg);
208 $address1 = $doc->createElement( "address1" );
209 $address1->appendChild(
210 $doc->createTextNode( $userRole['street'] )
212 $AccountAddress->appendChild( $address1 );
213 $msg = validation(xl('Facility City'),$userRole['city'],$msg);
214 $city = $doc->createElement( "city" );
215 $city->appendChild(
216 $doc->createTextNode( $userRole['city'] )
218 $AccountAddress->appendChild( $city );
219 $msg = validation(xl('Facility State'),$userRole['state'],$msg);
220 $state = $doc->createElement( "state" );
221 $state->appendChild(
222 $doc->createTextNode( $userRole['state'] )
224 $AccountAddress->appendChild( $state );
225 $msg = validation(xl('Facility Zip'),$userRole['postal_code'],$msg);
226 $zip = $doc->createElement( "zip" );
227 $zip->appendChild(
228 $doc->createTextNode( $userRole['postal_code'] )
230 $AccountAddress->appendChild( $zip );
231 $msg = validation(xl('Facility Country code'),$userRole['country_code'],$msg);
232 $county_code = substr($userRole['country_code'],0,2);
233 $country = $doc->createElement( "country" );
234 $country->appendChild(
235 $doc->createTextNode( $county_code )
237 $AccountAddress->appendChild( $country );
238 $b->appendChild( $AccountAddress );
239 $msg = validation(xl('Facility Phone'),$userRole['phone'],$msg);
240 $accountPrimaryPhoneNumber = $doc->createElement( "accountPrimaryPhoneNumber" );
241 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
242 $accountPrimaryPhoneNumber->appendChild(
243 $doc->createTextNode( $userRole['phone'] )
245 $b->appendChild( $accountPrimaryPhoneNumber );
246 $msg = validation(xl('Facility Fax'),$userRole['fax'],$msg);
247 $accountPrimaryFaxNumber = $doc->createElement( "accountPrimaryFaxNumber" );
248 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
249 $accountPrimaryFaxNumber->appendChild(
250 $doc->createTextNode( $userRole['fax'] )
252 $b->appendChild( $accountPrimaryFaxNumber );
253 $r->appendChild( $b );
256 function location($doc,$r)
258 global $msg;
259 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?",array($_SESSION['authUser']));
260 $b = $doc->createElement( "Location" );
261 $b->setAttribute('ID',$userRole['id']);
262 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
263 $userRole['name']=trimData($userRole['name'],35);
264 $locationName = $doc->createElement( 'locationName' );
265 $locationName->appendChild(
266 $doc->createTextNode( $userRole['name'] )
268 $b->appendChild($locationName);
269 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
270 $userRole['street']=trimData($userRole['street'],35);
271 $LocationAddress = $doc->createElement( 'LocationAddress' );
272 if($userRole['street']){
273 $address1 = $doc->createElement( 'address1' );
274 $address1->appendChild(
275 $doc->createTextNode( $userRole['street'] )
277 $LocationAddress->appendChild($address1);
279 if($userRole['city']){
280 $city = $doc->createElement( 'city' );
281 $city->appendChild(
282 $doc->createTextNode( $userRole['city'] )
284 $LocationAddress->appendChild( $city );
286 if($userRole['state']){
287 $state = $doc->createElement( 'state' );
288 $state->appendChild(
289 $doc->createTextNode( $userRole['state'] )
291 $LocationAddress->appendChild($state);
293 if($userRole['postal_code']){
294 $zip = $doc->createElement( 'zip' );
295 $zip->appendChild(
296 $doc->createTextNode( $userRole['postal_code'] )
298 $LocationAddress->appendChild($zip);
300 if($userRole['country_code']){
301 $county_code = substr($userRole['country_code'],0,2);
302 $country = $doc->createElement( 'country' );
303 $country->appendChild(
304 $doc->createTextNode( $county_code )
306 $LocationAddress->appendChild($country);
308 $b->appendChild($LocationAddress);
309 if($userRole['phone']){
310 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
311 $primaryPhoneNumber = $doc->createElement( 'primaryPhoneNumber' );
312 $primaryPhoneNumber->appendChild(
313 $doc->createTextNode( $userRole['phone'] )
315 $b->appendChild($primaryPhoneNumber);
317 if($userRole['fax']){
318 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
319 $primaryFaxNumber = $doc->createElement( 'primaryFaxNumber' );
320 $primaryFaxNumber->appendChild(
321 $doc->createTextNode( $userRole['fax'] )
323 $b->appendChild($primaryFaxNumber);
325 $pharmacyContactNumber = $doc->createElement( 'pharmacyContactNumber' );
326 $pharmacyContactNumber->appendChild(
327 $doc->createTextNode( $userRole['phone'] )
329 $b->appendChild($pharmacyContactNumber);
330 $r->appendChild( $b );
333 function LicensedPrescriber($doc,$r)
335 global $msg;
336 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
337 $b = $doc->createElement( "LicensedPrescriber" );
338 $b->setAttribute('ID',$user_details['npi']);
339 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
340 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
341 $msg = validation(xl('LicensedPrescriber Last name'),$user_details['lname'],$msg);
342 $last = $doc->createElement( "last" );
343 $last->appendChild(
344 $doc->createTextNode( $user_details['lname'] )
346 $LicensedPrescriberName->appendChild( $last );
347 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
348 $msg = validation(xl('User First name'),$user_details['fname'],$msg);
349 $first = $doc->createElement( "first" );
350 $first->appendChild(
351 $doc->createTextNode( $user_details['fname'] )
353 $LicensedPrescriberName->appendChild( $first );
354 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
355 $middle = $doc->createElement( "middle" );
356 $middle->appendChild(
357 $doc->createTextNode( $user_details['mname'] )
359 $LicensedPrescriberName->appendChild( $middle );
360 $b->appendChild( $LicensedPrescriberName );
361 $msg = validation(xl('DEA'),$user_details['federaldrugid'],$msg);
362 $dea = $doc->createElement( "dea" );
363 $dea->appendChild(
364 $doc->createTextNode( $user_details['federaldrugid'] )
366 $b->appendChild( $dea );
367 if($user_details['upin']){
368 $upin = $doc->createElement( "upin" );
369 $upin->appendChild(
370 $doc->createTextNode( $user_details['upin'] )
372 $b->appendChild( $upin );
374 $licenseNumber = $doc->createElement( "licenseNumber" );
375 $licenseNumber->appendChild(
376 $doc->createTextNode( $user_details['state_license_number'] )
378 $b->appendChild( $licenseNumber );
379 $msg = validation(xl('LicensedPrescriber NPI'),$user_details['npi'],$msg);
380 $npi = $doc->createElement( "npi" );
381 $npi->appendChild(
382 $doc->createTextNode( $user_details['npi'] )
384 $b->appendChild( $npi );
385 $r->appendChild( $b );
388 function Staff($doc,$r)
390 global $msg;
391 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
392 $b = $doc->createElement( "Staff" );
393 $b->setAttribute('ID',$user_details['username']);
394 $StaffName = $doc->createElement( "StaffName" );
395 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
396 $last = $doc->createElement( "last" );
397 $last->appendChild(
398 $doc->createTextNode( $user_details['lname'] )
400 $StaffName->appendChild( $last );
401 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
402 $first = $doc->createElement( "first" );
403 $first->appendChild(
404 $doc->createTextNode( $user_details['fname'] )
406 $StaffName->appendChild( $first );
407 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
408 $middle = $doc->createElement( "middle" );
409 $middle->appendChild(
410 $doc->createTextNode( $user_details['mname'] )
412 $StaffName->appendChild( $middle );
413 $b->appendChild( $StaffName );
414 $license = $doc->createElement( "license" );
415 $license->appendChild(
416 $doc->createTextNode( $user_details['license'] )
418 $b->appendChild( $license );
419 $r->appendChild( $b );
422 function SupervisingDoctor($doc,$r)
424 global $msg;
425 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
426 $b = $doc->createElement( "SupervisingDoctor" );
427 $b->setAttribute('ID',$user_details['npi']);
428 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
429 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
430 $msg = validation(xl('Supervising Doctor Last name'),$user_details['lname'],$msg);
431 $last = $doc->createElement( "last" );
432 $last->appendChild(
433 $doc->createTextNode( $user_details['lname'] )
435 $LicensedPrescriberName->appendChild( $last );
436 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
437 $msg = validation(xl('Supervising Doctor First name'),$user_details['fname'],$msg);
438 $first = $doc->createElement( "first" );
439 $first->appendChild(
440 $doc->createTextNode( $user_details['fname'] )
442 $LicensedPrescriberName->appendChild( $first );
443 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
444 $middle = $doc->createElement( "middle" );
445 $middle->appendChild(
446 $doc->createTextNode( $user_details['mname'] )
448 $LicensedPrescriberName->appendChild( $middle );
449 $b->appendChild( $LicensedPrescriberName );
450 $msg = validation(xl('Supervising Doctor DEA'),$user_details['federaldrugid'],$msg);
451 $dea = $doc->createElement( "dea" );
452 $dea->appendChild(
453 $doc->createTextNode( $user_details['federaldrugid'] )
455 $b->appendChild( $dea );
456 if($user_details['upin']){
457 $upin = $doc->createElement( "upin" );
458 $upin->appendChild(
459 $doc->createTextNode( $user_details['upin'] )
461 $b->appendChild( $upin );
463 $licenseNumber = $doc->createElement( "licenseNumber" );
464 $licenseNumber->appendChild(
465 $doc->createTextNode( $user_details['state_license_number'] )
467 $b->appendChild( $licenseNumber );
468 $msg = validation(xl('Supervising Doctor NPI'),$user_details['npi'],$msg);
469 $npi = $doc->createElement( "npi" );
470 $npi->appendChild(
471 $doc->createTextNode( $user_details['npi'] )
473 $b->appendChild( $npi );
474 $r->appendChild( $b );
477 function MidlevelPrescriber($doc,$r)
479 global $msg;
480 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
481 $b = $doc->createElement( "MidlevelPrescriber" );
482 $b->setAttribute('ID',$user_details['npi']);
483 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
484 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
485 $msg = validation(xl('Midlevel Prescriber Last name'),$user_details['lname'],$msg);
486 $last = $doc->createElement( "last" );
487 $last->appendChild(
488 $doc->createTextNode( $user_details['lname'] )
490 $LicensedPrescriberName->appendChild( $last );
491 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
492 $msg = validation(xl('Midlevel Prescriber First name'),$user_details['fname'],$msg);
493 $first = $doc->createElement( "first" );
494 $first->appendChild(
495 $doc->createTextNode( $user_details['fname'] )
497 $LicensedPrescriberName->appendChild( $first );
498 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
499 $middle = $doc->createElement( "middle" );
500 $middle->appendChild(
501 $doc->createTextNode( $user_details['mname'] )
503 $LicensedPrescriberName->appendChild( $middle );
504 if($user_details['title']){
505 $msg = validation(xl('Midlevel Prescriber Prefix'),$user_details['title'],$msg);
506 $prefix = $doc->createElement( "prefix" );
507 $prefix->appendChild(
508 $doc->createTextNode( $user_details['title'] )
510 $LicensedPrescriberName->appendChild( $prefix );
512 $b->appendChild( $LicensedPrescriberName );
513 $msg = validation(xl('Midlevel Prescriber DEA'),$user_details['federaldrugid'],$msg);
514 $dea = $doc->createElement( "dea" );
515 $dea->appendChild(
516 $doc->createTextNode( $user_details['federaldrugid'] )
518 $b->appendChild( $dea );
519 if($user_details['upin']){
520 $upin = $doc->createElement( "upin" );
521 $upin->appendChild(
522 $doc->createTextNode( $user_details['upin'] )
524 $b->appendChild( $upin );
526 $licenseNumber = $doc->createElement( "licenseNumber" );
527 $licenseNumber->appendChild(
528 $doc->createTextNode( $user_details['state_license_number'] )
530 $b->appendChild( $licenseNumber );
531 $r->appendChild( $b );
534 function Patient($doc,$r,$pid)
536 global $msg,$warning_msg,$dem_check;
537 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?",array($pid));
538 $b = $doc->createElement( "Patient" );
539 $b->setAttribute('ID',$patient_data['pid']);
540 $PatientName = $doc->createElement( "PatientName" );
541 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
542 $patient_data['lname']=trimData($patient_data['lname'],35);
543 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
544 if($patient_data['lname']=='')
545 $dem_check.=htmlspecialchars( xl("Patient Last name is missing"), ENT_NOQUOTES)."<br>";
546 $last = $doc->createElement( "last" );
547 $last->appendChild(
548 $doc->createTextNode( $patient_data['lname'] )
550 $PatientName->appendChild( $last );
551 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
552 $patient_data['fname']=trimData($patient_data['fname'],35);
553 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
554 if($patient_data['fname']=='')
555 $dem_check.=htmlspecialchars( xl("Patient First name is missing"), ENT_NOQUOTES)."<br>";
556 $first = $doc->createElement( "first" );
557 $first->appendChild(
558 $doc->createTextNode( $patient_data['fname'] )
560 $PatientName->appendChild( $first );
561 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
562 $patient_data['mname']=trimData($patient_data['mname'],35);
563 $middle = $doc->createElement( "middle" );
564 $middle->appendChild(
565 $doc->createTextNode( $patient_data['mname'] )
567 $PatientName->appendChild( $middle );
568 $b->appendChild( $PatientName );
569 $PatientAddress = $doc->createElement( "PatientAddress" );
570 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
571 $patient_data['street']=trimData($patient_data['street'],35);
572 $msg = validation(xl('Patient Address'),$patient_data['street'],$msg);
573 if(trim($patient_data['street'])=='')
574 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Address is missing"), ENT_NOQUOTES);
575 $address1 = $doc->createElement( "address1" );
576 $address1->appendChild(
577 $doc->createTextNode( $patient_data['street'] )
579 $PatientAddress->appendChild( $address1 );
580 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
581 if($patient_data['city']=='')
582 $dem_check.=htmlspecialchars( xl("Patient City is missing"), ENT_NOQUOTES)."<br>";
583 $city = $doc->createElement( "city" );
584 $city->appendChild(
585 $doc->createTextNode( $patient_data['city'] )
587 $PatientAddress->appendChild( $city );
588 if($patient_data['state']){
589 $state = $doc->createElement( "state" );
590 $state->appendChild(
591 $doc->createTextNode( $patient_data['state'] )
593 $PatientAddress->appendChild( $state );
595 if($patient_data['postal_code']){
596 $zip = $doc->createElement( "zip" );
597 $zip->appendChild(
598 $doc->createTextNode( $patient_data['postal_code'] )
600 $PatientAddress->appendChild( $zip );
602 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
603 if(trim($patient_data['country_code'])=='')
604 $dem_check.=htmlspecialchars( xl("Patient Country is missing"), ENT_NOQUOTES)."<br>";
605 $county_code = substr($patient_data['country_code'],0,2);
606 $country = $doc->createElement( "country" );
607 $country->appendChild(
608 $doc->createTextNode( $county_code )
610 $PatientAddress->appendChild( $country );
611 $b->appendChild( $PatientAddress );
612 $PatientContact = $doc->createElement( "PatientContact" );
613 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
614 if($patient_data['phone_home']){
615 $homeTelephone = $doc->createElement( "homeTelephone" );
616 $homeTelephone->appendChild(
617 $doc->createTextNode( $patient_data['phone_home'] )
619 $PatientContact->appendChild( $homeTelephone );
621 $b->appendChild( $PatientContact );
622 $PatientCharacteristics = $doc->createElement( "PatientCharacteristics" );
623 if(trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000')
624 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Date Of Birth is missing"), ENT_NOQUOTES);
625 if($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000'){
626 $dob = $doc->createElement( "dob" );
627 $dob->appendChild(
628 $doc->createTextNode( $patient_data['date_of_birth'] )
630 $PatientCharacteristics->appendChild( $dob );
632 if(trim($patient_data['sex'])=='')
633 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Gender is missing"), ENT_NOQUOTES);
634 if($patient_data['sex']){
635 $gender_val=substr($patient_data['sex'],0,1);
636 $gender = $doc->createElement( "gender" );
637 $gender->appendChild(
638 $doc->createTextNode( $gender_val )
640 $PatientCharacteristics->appendChild( $gender );
642 $b->appendChild( $PatientCharacteristics );
643 PatientFreeformHealthplans($doc,$b,$pid);
644 $allergyId=PatientFreeformAllergy($doc,$b,$pid);
645 $r->appendChild( $b );
646 return $allergyId;
649 function OutsidePrescription($doc,$r,$pid,$prescid)
651 global $msg;
652 if($prescid)
654 $prec=sqlQuery("SELECT p.note,p.dosage,p.substitute,p.per_refill,p.form,p.route,p.interval,p.drug,l1.title AS title1,l2.title AS title2,l3.title AS title3,p.id AS prescid,
655 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT(fname,' ',mname,' ',lname) AS docname,p.quantity
656 FROM prescriptions AS p
657 LEFT JOIN users AS u ON p.provider_id=u.id
658 LEFT JOIN list_options AS l1 ON l1.list_id='drug_form' AND l1.option_id=p.form
659 LEFT JOIN list_options AS l2 ON l2.list_id='drug_route' AND l2.option_id=p.route
660 LEFT JOIN list_options AS l3 ON l3.list_id='drug_interval' AND l3.option_id=p.interval
661 WHERE p.drug<>'' and p.id=?",array($prescid));
662 $b = $doc->createElement( "OutsidePrescription" );
663 $externalId = $doc->createElement( "externalId" );
664 $externalId->appendChild(
665 $doc->createTextNode( $prec['prescid'] )
667 $b->appendChild( $externalId );
668 $date = $doc->createElement( "date" );
669 $date->appendChild(
670 $doc->createTextNode( $prec['date_added'] )
672 $b->appendChild( $date );
673 $doctorName = $doc->createElement( "doctorName" );
674 $doctorName->appendChild(
675 $doc->createTextNode( $prec['docname'] )
677 $b->appendChild( $doctorName );
678 $s=stripSpecialCharacter($prec['drug']);
679 $sig = $doc->createElement( "drug" );
680 $sig->appendChild(
681 $doc->createTextNode( $s )
683 $b->appendChild( $sig );
684 $dispenseNumber = $doc->createElement( "dispenseNumber" );
685 $dispenseNumber->appendChild(
686 $doc->createTextNode( $prec['quantity'] )
688 $b->appendChild( $dispenseNumber );
689 $s="Take ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'];
690 $s=stripSpecialCharacter($s);
691 $sig = $doc->createElement( "sig" );
692 $sig->appendChild(
693 $doc->createTextNode( $s )
695 $b->appendChild( $sig );
696 $refillCount = $doc->createElement( "refillCount" );
697 $refillCount->appendChild(
698 $doc->createTextNode( $prec['per_refill'] )
700 $b->appendChild( $refillCount );
701 $prescriptionType = $doc->createElement( "prescriptionType" );
702 $prescriptionType->appendChild(
703 $doc->createTextNode( 'reconcile' )
705 $b->appendChild( $prescriptionType );
706 $r->appendChild( $b );
710 function PatientMedication($doc,$r,$pid,$med_limit)
712 global $msg;
713 $active='';
714 if($GLOBALS['erx_upload_active']==1)
715 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
716 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
717 and erx_uploaded='0' $active order by enddate limit 0,$med_limit",array($pid));
718 $uploaded_med_arr="";
719 while($row_med=sqlFetchArray($res_med))
721 $uploaded_med_arr[]=$row_med['id'];
722 $b = $doc->createElement( "OutsidePrescription" );
723 $externalId = $doc->createElement( "externalId" );
724 $externalId->appendChild(
725 $doc->createTextNode( $row_med['id'] )
727 $b->appendChild( $externalId );
728 $date = $doc->createElement( "date" );
729 $date->appendChild(
730 $doc->createTextNode( $row_med['begdate'] )
732 $b->appendChild( $date );
733 $doctorName = $doc->createElement( "doctorName" );
734 $doctorName->appendChild(
735 $doc->createTextNode( "" )
737 $b->appendChild( $doctorName );
738 $row_med['title'] = stripSpecialCharacter($row_med['title']);
739 $sig = $doc->createElement( "drug" );
740 $sig->appendChild(
741 $doc->createTextNode( $row_med['title'] )
743 $b->appendChild( $sig );
744 $dispenseNumber = $doc->createElement( "dispenseNumber" );
745 $dispenseNumber->appendChild(
746 $doc->createTextNode( $prec['quantity'] )
748 $b->appendChild( $dispenseNumber );
749 $sig = $doc->createElement( "sig" );
750 $sig->appendChild(
751 $doc->createTextNode( "" )
753 $b->appendChild( $sig );
754 $refillCount = $doc->createElement( "refillCount" );
755 $refillCount->appendChild(
756 $doc->createTextNode( "" )
758 $b->appendChild( $refillCount );
759 $prescriptionType = $doc->createElement( "prescriptionType" );
760 $prescriptionType->appendChild(
761 $doc->createTextNode( 'reconcile' )
763 $b->appendChild( $prescriptionType );
764 $r->appendChild( $b );
767 return $uploaded_med_arr;
770 function PatientFreeformAllergy($doc,$r,$pid)
772 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
773 LEFT JOIN list_options AS lo ON l.outcome=lo.option_id AND lo.list_id='outcome'
774 WHERE `type`='allergy' AND pid=? AND erx_source='0' and erx_uploaded='0' AND (enddate is null or enddate = '' or enddate = '0000-00-00')",array($pid));
775 $allergyId=array();
776 while($row=sqlFetchArray($res))
778 $val=array();
779 $val['id']=$row['id'];
780 $val['title1']=$row['title1'];
781 $val['title2']=$row['title2'];
782 $val['comments']=$row['comments'];
783 $b = $doc->createElement( "PatientFreeformAllergy" );
784 $b->setAttribute('ID',$val['id']);
785 if($val['title1']){
786 $allergyName = $doc->createElement( "allergyName" );
787 $allergyName->appendChild(
788 $doc->createTextNode( $val['title1'] )
790 $b->appendChild( $allergyName );
792 if($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')){
793 $allergySeverityTypeID = $doc->createElement( "allergySeverityTypeID" );
794 $allergySeverityTypeID->appendChild(
795 $doc->createTextNode( $val['title2'] )
797 $b->appendChild( $allergySeverityTypeID );
799 if($val['comments']){
800 $allergyComment = $doc->createElement( "allergyComment" );
801 $allergyComment->appendChild(
802 $doc->createTextNode( stripSpecialCharacter($val['comments']) )
804 $b->appendChild( $allergyComment );
806 $r->appendChild( $b );
807 $allergyId[]=$row['id'];
809 return $allergyId;
812 function PatientFreeformHealthplans($doc,$r,$pid)
814 $res=sqlStatement("SELECT `name`,`type` FROM insurance_companies AS ic, insurance_data AS id
815 WHERE ic.id=id.provider AND id.pid=?",array($pid));
816 while($row=sqlFetchArray($res))
818 $b = $doc->createElement( "PatientFreeformHealthplans" );
819 $allergyName = $doc->createElement( "healthplanName" );
820 $allergyName->appendChild(
821 $doc->createTextNode( stripSpecialCharacter(trimData($row['name'],35)) )
823 $b->appendChild( $allergyName );
824 $r->appendChild( $b );
828 function PrescriptionRenewalResponse($doc,$r,$pid)
830 $b = $doc->createElement( "PrescriptionRenewalResponse" );
831 $renewalRequestIdentifier = $doc->createElement( "renewalRequestIdentifier" );
832 $renewalRequestIdentifier->appendChild(
833 $doc->createTextNode( 'cbf51649-ce3c-44b8-8f91-6fda121a353d' )
835 $b->appendChild( $renewalRequestIdentifier );
836 $responseCode = $doc->createElement( "responseCode" );
837 $responseCode->appendChild(
838 $doc->createTextNode( 'Undetermined' )
840 $b->appendChild( $responseCode );
841 $r->appendChild( $b );
844 function checkError($xml)
846 if(!extension_loaded('soap')){
847 die("PLEASE ENABLE SOAP EXTENSION");
849 if(!extension_loaded('curl')){
850 die("PLEASE ENABLE CURL EXTENSION");
852 if(!extension_loaded('openssl')){
853 die("PLEASE ENABLE OPENSSL EXTENSION");
855 $ch = curl_init($xml);
857 $data = array('RxInput' => $xml);
859 curl_setopt($ch, CURLOPT_URL, getErxPath());
860 curl_setopt($ch, CURLOPT_POST, 1);
861 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
862 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
863 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
864 curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
865 //curl_setopt($ch, CURLOPT_HEADER, 0);
866 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
867 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
868 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
869 curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
870 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
872 $result=curl_exec($ch) or die( curl_error($ch)) ;
873 preg_match('/<textarea.*>(.*)Original XML:/is',$result,$error_message);
874 if(strpos($result,'RxEntry.aspx')){
875 erx_error_log($xml);
876 erx_error_log($result);
878 $arr=split('Error',$error_message[1]);
879 //echo "Te: ".count($arr);
880 //print_r($arr);
881 if(count($arr)==1)
883 echo nl2br($error_message[1]);
885 else
887 for($i=1;$i<count($arr);$i++)
889 echo $arr[$i]."<br><br>";
892 curl_close($ch);
893 if(strpos($result,'RxEntry.aspx'))
894 return '1';
895 else
896 return '0';
899 function erx_error_log($message)
901 $date = date("Y-m-d");
902 if(!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error'))
903 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error',0777,true);
904 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
905 $f=fopen($filename,'a');
906 fwrite($f,date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
907 fclose($f);
910 function stripStrings($str,$pattern)
912 $result = $str;
913 foreach($pattern as $key=>$value){
914 $result = preg_replace("/$key/",$value,$result);
916 return $result;