minor improvement to tabs style
[openemr.git] / interface / eRx_xml.php
blob84295066fe669197ffbd8e1582b34e3c90dcfc0a
1 <?php
3 /**
4 * interface/eRx_xml.php Functions for interacting with NewCrop communications.
6 * Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
8 * LICENSE: This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
11 * later version. This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details. You should have received a copy of the GNU
15 * General Public License along with this program.
16 * If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @subpackage NewCrop
20 * @author Eldho Chacko <eldho@zhservices.com>
21 * @author Vinish K <vinish@zhservices.com>
22 * @link http://www.open-emr.org
25 $facilityService = new \services\FacilityService();
27 function getErxPath()
29 return $GLOBALS['erx_newcrop_path'];
32 function getErxSoapPath()
34 return $GLOBALS['erx_newcrop_path_soap'];
37 function getErxCredentials()
39 $cred=array();
40 $cred[]=$GLOBALS['erx_account_partner_name'];
41 $cred[]=$GLOBALS['erx_account_name'];
42 $cred[]=$GLOBALS['erx_account_password'];
44 return $cred;
47 function validation($val_check,$val,$msg)
49 if(!$val)
50 $msg .= $val_check.' '.xl('missing').'<br>';
51 return $msg;
54 function stripSpecialCharacterFacility($str)
56 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/","",$str);
57 return $str;
60 function stripSpecialCharacter($str)
62 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/","",$str);
63 return $str;
66 function stripPhoneSlashes($str)
68 $str=preg_replace('/-/','',$str);
69 return $str;
72 function trimData($str,$length)
74 $str=substr($str,0,($length-1));
75 return $str;
78 function stringToNumeric($str)
80 if(is_numeric($str)){
81 return array($str,"");
83 else{
84 for($i=0;$i<strlen($str);$i++){
85 $x=substr($str,$i,1);
86 if(is_numeric($x) && !$txt){
87 $num.=$x;
89 else{
90 $txt.=$x;
94 return array($num,$txt);
96 $str=substr($str,0,($length-1));
97 return $str;
99 function credentials($doc,$r)
101 global $msg;
102 $cred=getErxCredentials();
103 $msg = validation(xl('Partner Name'),$cred['0'],$msg);
104 $b = $doc->createElement( "Credentials" );
105 $partnerName = $doc->createElement( "partnerName" );
106 $partnerName->appendChild(
107 $doc->createTextNode( $cred['0'] )
109 $b->appendChild( $partnerName );
110 $msg = validation(xl('ERX Name'),$cred['1'],$msg);
111 $name = $doc->createElement( "name" );
112 $name->appendChild(
113 $doc->createTextNode( $cred['1'] )
115 $b->appendChild( $name );
116 $msg = validation(xl('ERX Password'),$cred['2'],$msg);
117 $password = $doc->createElement( "password" );
118 $password->appendChild(
119 $doc->createTextNode( $cred['2'] )
121 $b->appendChild( $password );
122 $productName = $doc->createElement( "productName" );
123 $productName->appendChild(
124 $doc->createTextNode( 'OpenEMR' )
126 $b->appendChild( $productName );
127 $productVersion = $doc->createElement( "productVersion" );
128 $productVersion->appendChild(
129 $doc->createTextNode( $GLOBALS['openemr_version'] )
131 $b->appendChild( $productVersion );
132 $r->appendChild( $b );
135 function user_role($doc,$r)
137 global $msg;
138 $userRole=sqlQuery("select * from users where username=?",array($_SESSION['authUser']));
139 if(!$userRole['newcrop_user_role'])
140 {echo xl('Unauthorized access to ePrescription');die;}
141 $userRole['newcrop_user_role'] = preg_replace('/erx/','',$userRole['newcrop_user_role']);
142 if($userRole['newcrop_user_role'] == 'doctor')
143 $userRole['eRxUser'] = 'LicensedPrescriber';
144 elseif($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse')
145 $userRole['eRxUser'] = 'Staff';
146 elseif($userRole['newcrop_user_role'] == 'midlevelPrescriber')
147 $userRole['eRxUser'] = 'MidlevelPrescriber';
148 elseif($userRole['newcrop_user_role'] == 'supervisingDoctor')
149 $userRole['eRxUser'] = 'SupervisingDoctor';
150 $msg = validation(xl('ERX User'),$userRole['eRxUser'],$msg);
151 $b = $doc->createElement( "UserRole" );
152 $user = $doc->createElement( "user" );
153 $user->appendChild(
154 $doc->createTextNode( $userRole['eRxUser'] )
156 $b->appendChild( $user );
157 $msg = validation(xl('ERX Role'),$userRole['newcrop_user_role'],$msg);
158 $role = $doc->createElement( "role" );
159 $role->appendChild(
160 $doc->createTextNode( $userRole['newcrop_user_role'] )
162 $b->appendChild( $role );
163 $r->appendChild( $b );
166 function destination($doc,$r,$page='',$pid)
168 global $msg,$page;
169 $userRole=sqlQuery("select * from users where username=?",array($_SESSION['authUser']));
170 $userRole['newcrop_user_role'] = preg_replace('/erx/','',$userRole['newcrop_user_role']);
171 if(!$page)
173 $page='compose';
174 if($userRole['newcrop_user_role']=='admin')
175 $page='admin';
176 elseif($userRole['newcrop_user_role']=='manager')
177 $page='manager';
179 $b = $doc->createElement( "Destination" );
180 $requestedPage = $doc->createElement( "requestedPage" );
181 $requestedPage->appendChild(
182 $doc->createTextNode( $page )
184 $b->appendChild( $requestedPage );
185 $r->appendChild( $b );
188 function account($doc,$r)
190 global $msg, $facilityService;
191 $erxSiteID= $facilityService->getPrimaryBusinessEntity();
192 if(!$erxSiteID['federal_ein'])
194 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);
195 die;
197 $b = $doc->createElement( "Account" );
198 $b->setAttribute('ID', $GLOBALS['erx_account_id']);
199 $erxSiteID['name']=stripSpecialCharacterFacility($erxSiteID['name']);
200 $erxSiteID['name']=trimData($erxSiteID['name'],35);
201 $msg = validation(xl('Account Name'),$erxSiteID['name'],$msg);
202 $accountName = $doc->createElement( "accountName" );
203 $accountName->appendChild(
204 $doc->createTextNode( $erxSiteID['name'] )
206 $b->appendChild( $accountName );
207 $msg = validation(xl('Site ID'),$_SESSION['site_id'],$msg);
208 $siteID = $doc->createElement( "siteID" );
209 $siteID->appendChild(
210 $doc->createTextNode( $erxSiteID['federal_ein'] )
212 $b->appendChild( $siteID );
213 $erxSiteID['street']=stripSpecialCharacterFacility($erxSiteID['street']);
214 $erxSiteID['street']=trimData($erxSiteID['street'],35);
215 $AccountAddress = $doc->createElement( "AccountAddress" );
216 $msg = validation(xl('Facility Street'),$erxSiteID['street'],$msg);
217 $address1 = $doc->createElement( "address1" );
218 $address1->appendChild(
219 $doc->createTextNode( $erxSiteID['street'] )
221 $AccountAddress->appendChild( $address1 );
222 $msg = validation(xl('Facility City'),$erxSiteID['city'],$msg);
223 $city = $doc->createElement( "city" );
224 $city->appendChild(
225 $doc->createTextNode( $erxSiteID['city'] )
227 $AccountAddress->appendChild( $city );
228 $msg = validation(xl('Facility State'),$erxSiteID['state'],$msg);
229 $state = $doc->createElement( "state" );
230 $state->appendChild(
231 $doc->createTextNode( $erxSiteID['state'] )
233 $AccountAddress->appendChild( $state );
234 $jasonbigzip=$erxSiteID['postal_code'];
235 $jasonbigzip=preg_replace('/[^0-9]/','',$jasonbigzip);
236 if(strlen($jasonbigzip) >=5){
237 $jasonzip=substr($jasonbigzip,0,5);
238 $zip4=substr($jasonbigzip,5,4);
240 else{
241 $msg = validation(xl('Facility Zip'),$jasonzip,$msg);
243 $zip = $doc->createElement( "zip" );
244 $zip->appendChild(
245 $doc->createTextNode( $jasonzip )
247 $AccountAddress->appendChild( $zip );
248 if(strlen($zip4)==4){
249 $zipFour = $doc->createElement( "zip4" );
250 $zipFour->appendChild(
251 $doc->createTextNode( $zip4 )
253 $AccountAddress->appendChild( $zipFour );
255 $msg = validation(xl('Facility Country code'),$erxSiteID['country_code'],$msg);
256 $county_code = substr($erxSiteID['country_code'],0,2);
257 $country = $doc->createElement( "country" );
258 $country->appendChild(
259 $doc->createTextNode( $county_code )
261 $AccountAddress->appendChild( $country );
262 $b->appendChild( $AccountAddress );
263 $msg = validation(xl('Facility Phone'),$erxSiteID['phone'],$msg);
264 $accountPrimaryPhoneNumber = $doc->createElement( "accountPrimaryPhoneNumber" );
265 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
266 $accountPrimaryPhoneNumber->appendChild(
267 $doc->createTextNode( $erxSiteID['phone'] )
269 $b->appendChild( $accountPrimaryPhoneNumber );
270 $msg = validation(xl('Facility Fax'),$erxSiteID['fax'],$msg);
271 $accountPrimaryFaxNumber = $doc->createElement( "accountPrimaryFaxNumber" );
272 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
273 $accountPrimaryFaxNumber->appendChild(
274 $doc->createTextNode( $erxSiteID['fax'] )
276 $b->appendChild( $accountPrimaryFaxNumber );
277 $r->appendChild( $b );
280 function location($doc,$r)
282 global $msg;
283 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?",array($_SESSION['authUser']));
284 $b = $doc->createElement( "Location" );
285 $b->setAttribute('ID',$userRole['id']);
286 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
287 $userRole['name']=trimData($userRole['name'],35);
288 $locationName = $doc->createElement( 'locationName' );
289 $locationName->appendChild(
290 $doc->createTextNode( $userRole['name'] )
292 $b->appendChild($locationName);
293 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
294 $userRole['street']=trimData($userRole['street'],35);
295 $LocationAddress = $doc->createElement( 'LocationAddress' );
296 if($userRole['street']){
297 $address1 = $doc->createElement( 'address1' );
298 $address1->appendChild(
299 $doc->createTextNode( $userRole['street'] )
301 $LocationAddress->appendChild($address1);
303 if($userRole['city']){
304 $city = $doc->createElement( 'city' );
305 $city->appendChild(
306 $doc->createTextNode( $userRole['city'] )
308 $LocationAddress->appendChild( $city );
310 if($userRole['state']){
311 $state = $doc->createElement( 'state' );
312 $state->appendChild(
313 $doc->createTextNode( $userRole['state'] )
315 $LocationAddress->appendChild($state);
317 $jasonbigzip=$userRole['postal_code'];
318 $jasonbigzip=preg_replace('/[^0-9]/','',$jasonbigzip);
319 if(strlen($jasonbigzip) >=5){
320 $jasonzip=substr($jasonbigzip,0,5);
321 $zip4=substr($jasonbigzip,5,4);
323 else{
324 $msg = validation(xl('Facility Zip'),$jasonzip,$msg);
326 $zip = $doc->createElement( "zip" );
327 $zip->appendChild(
328 $doc->createTextNode( $jasonzip )
330 $LocationAddress->appendChild( $zip );
331 if(strlen($zip4)==4){
332 $zipFour = $doc->createElement( "zip4" );
333 $zipFour->appendChild(
334 $doc->createTextNode( $zip4 )
336 $LocationAddress->appendChild( $zipFour );
338 if($userRole['country_code']){
339 $county_code = substr($userRole['country_code'],0,2);
340 $country = $doc->createElement( 'country' );
341 $country->appendChild(
342 $doc->createTextNode( $county_code )
344 $LocationAddress->appendChild($country);
346 $b->appendChild($LocationAddress);
347 if($userRole['phone']){
348 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
349 $primaryPhoneNumber = $doc->createElement( 'primaryPhoneNumber' );
350 $primaryPhoneNumber->appendChild(
351 $doc->createTextNode( $userRole['phone'] )
353 $b->appendChild($primaryPhoneNumber);
355 if($userRole['fax']){
356 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
357 $primaryFaxNumber = $doc->createElement( 'primaryFaxNumber' );
358 $primaryFaxNumber->appendChild(
359 $doc->createTextNode( $userRole['fax'] )
361 $b->appendChild($primaryFaxNumber);
363 $pharmacyContactNumber = $doc->createElement( 'pharmacyContactNumber' );
364 $pharmacyContactNumber->appendChild(
365 $doc->createTextNode( $userRole['phone'] )
367 $b->appendChild($pharmacyContactNumber);
368 $r->appendChild( $b );
371 function LicensedPrescriber($doc,$r)
373 global $msg;
374 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
375 $b = $doc->createElement( "LicensedPrescriber" );
376 $b->setAttribute('ID',$user_details['npi']);
377 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
378 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
379 $msg = validation(xl('LicensedPrescriber Last name'),$user_details['lname'],$msg);
380 $last = $doc->createElement( "last" );
381 $last->appendChild(
382 $doc->createTextNode( $user_details['lname'] )
384 $LicensedPrescriberName->appendChild( $last );
385 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
386 $msg = validation(xl('User First name'),$user_details['fname'],$msg);
387 $first = $doc->createElement( "first" );
388 $first->appendChild(
389 $doc->createTextNode( $user_details['fname'] )
391 $LicensedPrescriberName->appendChild( $first );
392 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
393 $middle = $doc->createElement( "middle" );
394 $middle->appendChild(
395 $doc->createTextNode( $user_details['mname'] )
397 $LicensedPrescriberName->appendChild( $middle );
398 $b->appendChild( $LicensedPrescriberName );
399 $msg = validation(xl('DEA'),$user_details['federaldrugid'],$msg);
400 $dea = $doc->createElement( "dea" );
401 $dea->appendChild(
402 $doc->createTextNode( $user_details['federaldrugid'] )
404 $b->appendChild( $dea );
405 if($user_details['upin']){
406 $upin = $doc->createElement( "upin" );
407 $upin->appendChild(
408 $doc->createTextNode( $user_details['upin'] )
410 $b->appendChild( $upin );
412 $licenseNumber = $doc->createElement( "licenseNumber" );
413 $licenseNumber->appendChild(
414 $doc->createTextNode( $user_details['state_license_number'] )
416 $b->appendChild( $licenseNumber );
417 $msg = validation(xl('LicensedPrescriber NPI'),$user_details['npi'],$msg);
418 $npi = $doc->createElement( "npi" );
419 $npi->appendChild(
420 $doc->createTextNode( $user_details['npi'] )
422 $b->appendChild( $npi );
423 $r->appendChild( $b );
426 function Staff($doc,$r)
428 global $msg;
429 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
430 $b = $doc->createElement( "Staff" );
431 $b->setAttribute('ID',$user_details['username']);
432 $StaffName = $doc->createElement( "StaffName" );
433 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
434 $last = $doc->createElement( "last" );
435 $last->appendChild(
436 $doc->createTextNode( $user_details['lname'] )
438 $StaffName->appendChild( $last );
439 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
440 $first = $doc->createElement( "first" );
441 $first->appendChild(
442 $doc->createTextNode( $user_details['fname'] )
444 $StaffName->appendChild( $first );
445 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
446 $middle = $doc->createElement( "middle" );
447 $middle->appendChild(
448 $doc->createTextNode( $user_details['mname'] )
450 $StaffName->appendChild( $middle );
451 $b->appendChild( $StaffName );
452 $license = $doc->createElement( "license" );
453 $license->appendChild(
454 $doc->createTextNode( $user_details['license'] )
456 $b->appendChild( $license );
457 $r->appendChild( $b );
460 function SupervisingDoctor($doc,$r)
462 global $msg;
463 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
464 $b = $doc->createElement( "SupervisingDoctor" );
465 $b->setAttribute('ID',$user_details['npi']);
466 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
467 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
468 $msg = validation(xl('Supervising Doctor Last name'),$user_details['lname'],$msg);
469 $last = $doc->createElement( "last" );
470 $last->appendChild(
471 $doc->createTextNode( $user_details['lname'] )
473 $LicensedPrescriberName->appendChild( $last );
474 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
475 $msg = validation(xl('Supervising Doctor First name'),$user_details['fname'],$msg);
476 $first = $doc->createElement( "first" );
477 $first->appendChild(
478 $doc->createTextNode( $user_details['fname'] )
480 $LicensedPrescriberName->appendChild( $first );
481 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
482 $middle = $doc->createElement( "middle" );
483 $middle->appendChild(
484 $doc->createTextNode( $user_details['mname'] )
486 $LicensedPrescriberName->appendChild( $middle );
487 $b->appendChild( $LicensedPrescriberName );
488 $msg = validation(xl('Supervising Doctor DEA'),$user_details['federaldrugid'],$msg);
489 $dea = $doc->createElement( "dea" );
490 $dea->appendChild(
491 $doc->createTextNode( $user_details['federaldrugid'] )
493 $b->appendChild( $dea );
494 if($user_details['upin']){
495 $upin = $doc->createElement( "upin" );
496 $upin->appendChild(
497 $doc->createTextNode( $user_details['upin'] )
499 $b->appendChild( $upin );
501 $licenseNumber = $doc->createElement( "licenseNumber" );
502 $licenseNumber->appendChild(
503 $doc->createTextNode( $user_details['state_license_number'] )
505 $b->appendChild( $licenseNumber );
506 $msg = validation(xl('Supervising Doctor NPI'),$user_details['npi'],$msg);
507 $npi = $doc->createElement( "npi" );
508 $npi->appendChild(
509 $doc->createTextNode( $user_details['npi'] )
511 $b->appendChild( $npi );
512 $r->appendChild( $b );
515 function MidlevelPrescriber($doc,$r)
517 global $msg;
518 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?",array($_SESSION['authUserID']));
519 $b = $doc->createElement( "MidlevelPrescriber" );
520 $b->setAttribute('ID',$user_details['npi']);
521 $LicensedPrescriberName = $doc->createElement( "LicensedPrescriberName" );
522 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
523 $msg = validation(xl('Midlevel Prescriber Last name'),$user_details['lname'],$msg);
524 $last = $doc->createElement( "last" );
525 $last->appendChild(
526 $doc->createTextNode( $user_details['lname'] )
528 $LicensedPrescriberName->appendChild( $last );
529 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
530 $msg = validation(xl('Midlevel Prescriber First name'),$user_details['fname'],$msg);
531 $first = $doc->createElement( "first" );
532 $first->appendChild(
533 $doc->createTextNode( $user_details['fname'] )
535 $LicensedPrescriberName->appendChild( $first );
536 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
537 $middle = $doc->createElement( "middle" );
538 $middle->appendChild(
539 $doc->createTextNode( $user_details['mname'] )
541 $LicensedPrescriberName->appendChild( $middle );
542 if($user_details['title']){
543 $msg = validation(xl('Midlevel Prescriber Prefix'),$user_details['title'],$msg);
544 $prefix = $doc->createElement( "prefix" );
545 $prefix->appendChild(
546 $doc->createTextNode( $user_details['title'] )
548 $LicensedPrescriberName->appendChild( $prefix );
550 $b->appendChild( $LicensedPrescriberName );
551 $msg = validation(xl('Midlevel Prescriber DEA'),$user_details['federaldrugid'],$msg);
552 $dea = $doc->createElement( "dea" );
553 $dea->appendChild(
554 $doc->createTextNode( $user_details['federaldrugid'] )
556 $b->appendChild( $dea );
557 if($user_details['upin']){
558 $upin = $doc->createElement( "upin" );
559 $upin->appendChild(
560 $doc->createTextNode( $user_details['upin'] )
562 $b->appendChild( $upin );
564 $licenseNumber = $doc->createElement( "licenseNumber" );
565 $licenseNumber->appendChild(
566 $doc->createTextNode( $user_details['state_license_number'] )
568 $b->appendChild( $licenseNumber );
569 $r->appendChild( $b );
572 function Patient($doc,$r,$pid)
574 global $msg,$warning_msg,$dem_check;
575 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?",array($pid));
576 $b = $doc->createElement( "Patient" );
577 $b->setAttribute('ID',$patient_data['pid']);
578 $PatientName = $doc->createElement( "PatientName" );
579 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
580 $patient_data['lname']=trimData($patient_data['lname'],35);
581 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
582 if($patient_data['lname']=='')
583 $dem_check.=htmlspecialchars( xl("Patient Last name is missing"), ENT_NOQUOTES)."<br>";
584 $last = $doc->createElement( "last" );
585 $last->appendChild(
586 $doc->createTextNode( $patient_data['lname'] )
588 $PatientName->appendChild( $last );
589 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
590 $patient_data['fname']=trimData($patient_data['fname'],35);
591 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
592 if($patient_data['fname']=='')
593 $dem_check.=htmlspecialchars( xl("Patient First name is missing"), ENT_NOQUOTES)."<br>";
594 $first = $doc->createElement( "first" );
595 $first->appendChild(
596 $doc->createTextNode( $patient_data['fname'] )
598 $PatientName->appendChild( $first );
599 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
600 $patient_data['mname']=trimData($patient_data['mname'],35);
601 $middle = $doc->createElement( "middle" );
602 $middle->appendChild(
603 $doc->createTextNode( $patient_data['mname'] )
605 $PatientName->appendChild( $middle );
606 $b->appendChild( $PatientName );
607 $PatientAddress = $doc->createElement( "PatientAddress" );
608 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
609 $patient_data['street']=trimData($patient_data['street'],35);
610 $msg = validation(xl('Patient Address'),$patient_data['street'],$msg);
611 if(trim($patient_data['street'])=='')
612 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Address is missing"), ENT_NOQUOTES);
613 $address1 = $doc->createElement( "address1" );
614 $address1->appendChild(
615 $doc->createTextNode( $patient_data['street'] )
617 $PatientAddress->appendChild( $address1 );
618 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
619 if($patient_data['city']=='')
620 $dem_check.=htmlspecialchars( xl("Patient City is missing"), ENT_NOQUOTES)."<br>";
621 $city = $doc->createElement( "city" );
622 $city->appendChild(
623 $doc->createTextNode( $patient_data['city'] )
625 $PatientAddress->appendChild( $city );
626 if($patient_data['state']){
627 $state = $doc->createElement( "state" );
628 $state->appendChild(
629 $doc->createTextNode( $patient_data['state'] )
631 $PatientAddress->appendChild( $state );
633 if($patient_data['postal_code']){
634 $zip = $doc->createElement( "zip" );
635 $zip->appendChild(
636 $doc->createTextNode( $patient_data['postal_code'] )
638 $PatientAddress->appendChild( $zip );
640 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
641 if(trim($patient_data['country_code'])=='' && $GLOBALS['erx_default_patient_country']=='')
642 $dem_check.=htmlspecialchars( xl("Patient Country is missing. Also you have not set default Patient Country in Global Settings"), ENT_NOQUOTES)."<br>";
643 elseif(trim($patient_data['country_code'])=='')
644 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
645 $county_code = substr($patient_data['country_code'],0,2);
646 $country = $doc->createElement( "country" );
647 $country->appendChild(
648 $doc->createTextNode( $county_code )
650 $PatientAddress->appendChild( $country );
651 $b->appendChild( $PatientAddress );
652 $PatientContact = $doc->createElement( "PatientContact" );
653 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
654 if($patient_data['phone_home']){
655 $homeTelephone = $doc->createElement( "homeTelephone" );
656 $homeTelephone->appendChild(
657 $doc->createTextNode( $patient_data['phone_home'] )
659 $PatientContact->appendChild( $homeTelephone );
661 $b->appendChild( $PatientContact );
662 $PatientCharacteristics = $doc->createElement( "PatientCharacteristics" );
663 if(trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000')
664 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Date Of Birth is missing"), ENT_NOQUOTES);
665 if($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000'){
666 $dob = $doc->createElement( "dob" );
667 $dob->appendChild(
668 $doc->createTextNode( $patient_data['date_of_birth'] )
670 $PatientCharacteristics->appendChild( $dob );
672 if(trim($patient_data['sex'])=='')
673 $warning_msg .= "<br>".htmlspecialchars( xl("Patient Gender is missing"), ENT_NOQUOTES);
674 if($patient_data['sex']){
675 $gender_val=substr($patient_data['sex'],0,1);
676 $gender = $doc->createElement( "gender" );
677 $gender->appendChild(
678 $doc->createTextNode( $gender_val )
680 $PatientCharacteristics->appendChild( $gender );
682 $b->appendChild( $PatientCharacteristics );
683 PatientFreeformHealthplans($doc,$b,$pid);
684 $allergyId=PatientFreeformAllergy($doc,$b,$pid);
685 $r->appendChild( $b );
686 return $allergyId;
689 function OutsidePrescription($doc,$r,$pid,$prescid)
691 global $msg;
692 if($prescid)
694 $prec=sqlQuery("SELECT p.note,p.dosage,p.substitute,p.per_refill,p.form,p.route,p.size,p.interval,p.drug,l1.title AS title1,l2.title AS title2,l3.title AS title3,l4.title AS title4,p.id AS prescid,
695 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
696 FROM prescriptions AS p
697 LEFT JOIN users AS u ON p.provider_id=u.id
698 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
699 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
700 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
701 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
702 WHERE p.drug <> '' and p.id = ?",array($prescid));
703 $b = $doc->createElement( "OutsidePrescription" );
704 $externalId = $doc->createElement( "externalId" );
705 $externalId->appendChild(
706 $doc->createTextNode( $prec['prescid'] )
708 $b->appendChild( $externalId );
709 $date = $doc->createElement( "date" );
710 $date->appendChild(
711 $doc->createTextNode( $prec['date_added'] )
713 $b->appendChild( $date );
714 $doctorName = $doc->createElement( "doctorName" );
715 $doctorName->appendChild(
716 $doc->createTextNode( $prec['docname'] )
718 $b->appendChild( $doctorName );
719 $s=stripSpecialCharacter($prec['drug']);
720 $sig = $doc->createElement( "drug" );
721 $sig->appendChild(
722 $doc->createTextNode( trimData($s,80) )
724 $b->appendChild( $sig );
725 $x=stringToNumeric($prec['quantity']);
726 $dispenseNumber = $doc->createElement( "dispenseNumber" );
727 $dispenseNumber->appendChild(
728 $doc->createTextNode( $x[0] )
730 $b->appendChild( $dispenseNumber );
731 $s=trimData($x[1].$prec['size']." ".$prec['title4']." ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'],140);
732 $s=stripSpecialCharacter($s);
733 $sig = $doc->createElement( "sig" );
734 $sig->appendChild(
735 $doc->createTextNode( $s )
737 $b->appendChild( $sig );
738 $refillCount = $doc->createElement( "refillCount" );
739 $x=stringToNumeric($prec['per_refill']);
740 $refillCount->appendChild(
741 $doc->createTextNode( $x[0])
743 $b->appendChild( $refillCount );
744 $prescriptionType = $doc->createElement( "prescriptionType" );
745 $prescriptionType->appendChild(
746 $doc->createTextNode( 'reconcile' )
748 $b->appendChild( $prescriptionType );
749 $r->appendChild( $b );
753 function PatientMedication($doc,$r,$pid,$med_limit)
755 global $msg;
756 $active='';
757 if($GLOBALS['erx_upload_active']==1)
758 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
759 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
760 and erx_uploaded='0' $active order by enddate limit 0,$med_limit",array($pid));
761 $uploaded_med_arr="";
762 while($row_med=sqlFetchArray($res_med))
764 $uploaded_med_arr[]=$row_med['id'];
765 $b = $doc->createElement( "OutsidePrescription" );
766 $externalId = $doc->createElement( "externalId" );
767 $externalId->appendChild(
768 $doc->createTextNode( $row_med['id'] )
770 $b->appendChild( $externalId );
771 $date = $doc->createElement( "date" );
772 $date->appendChild(
773 $doc->createTextNode( $row_med['begdate'] )
775 $b->appendChild( $date );
776 $doctorName = $doc->createElement( "doctorName" );
777 $doctorName->appendChild(
778 $doc->createTextNode( "" )
780 $b->appendChild( $doctorName );
781 $row_med['title'] = stripSpecialCharacter($row_med['title']);
782 $sig = $doc->createElement( "drug" );
783 $sig->appendChild(
784 $doc->createTextNode( trimData($row_med['title'],80) )
786 $b->appendChild( $sig );
787 $dispenseNumber = $doc->createElement( "dispenseNumber" );
788 $dispenseNumber->appendChild(
789 $doc->createTextNode( $prec['quantity'] )
791 $b->appendChild( $dispenseNumber );
792 $sig = $doc->createElement( "sig" );
793 $sig->appendChild(
794 $doc->createTextNode( "" )
796 $b->appendChild( $sig );
797 $refillCount = $doc->createElement( "refillCount" );
798 $refillCount->appendChild(
799 $doc->createTextNode( "" )
801 $b->appendChild( $refillCount );
802 $prescriptionType = $doc->createElement( "prescriptionType" );
803 $prescriptionType->appendChild(
804 $doc->createTextNode( 'reconcile' )
806 $b->appendChild( $prescriptionType );
807 $r->appendChild( $b );
810 return $uploaded_med_arr;
813 function PatientFreeformAllergy($doc,$r,$pid)
815 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
816 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
817 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));
818 $allergyId=array();
819 while($row=sqlFetchArray($res))
821 $val=array();
822 $val['id']=$row['id'];
823 $val['title1']=$row['title1'];
824 $val['title2']=$row['title2'];
825 $val['comments']=$row['comments'];
826 $b = $doc->createElement( "PatientFreeformAllergy" );
827 $b->setAttribute('ID',$val['id']);
828 if($val['title1']){
829 $allergyName = $doc->createElement( "allergyName" );
830 $allergyName->appendChild(
831 $doc->createTextNode( trimData(stripSpecialCharacter($val['title1']),70) )
833 $b->appendChild( $allergyName );
835 if($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')){
836 $allergySeverityTypeID = $doc->createElement( "allergySeverityTypeID" );
837 $allergySeverityTypeID->appendChild(
838 $doc->createTextNode( $val['title2'] )
840 $b->appendChild( $allergySeverityTypeID );
842 if($val['comments']){
843 $allergyComment = $doc->createElement( "allergyComment" );
844 $allergyComment->appendChild(
845 $doc->createTextNode( trimData(stripSpecialCharacter($val['comments']),200) )
847 $b->appendChild( $allergyComment );
849 $r->appendChild( $b );
850 $allergyId[]=$row['id'];
852 return $allergyId;
855 function PatientFreeformHealthplans($doc, $r, $pid) {
856 $resource = sqlStatement('SELECT
857 `ins`.`name`
858 FROM (
859 SELECT
860 `id`.`type`,
861 `ic`.`name`
862 FROM `insurance_data` AS `id`
863 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
864 WHERE `id`.`pid` = ?
865 AND `id`.`subscriber_relationship` = \'self\'
866 AND `id`.`provider` > 0
867 ORDER BY `id`.`date` DESC
868 ) AS `ins`
869 GROUP BY `ins`.`type`;',
870 array($pid)
873 while($row = sqlFetchArray($resource)) {
874 $healthplanName = $doc->createElement('healthplanName');
875 $healthplanName->appendChild($doc->createTextNode(
876 stripSpecialCharacter(trimData($row['name'], 35))
879 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
880 $patientFreeformHealthplans->appendChild($healthplanName);
882 $r->appendChild($patientFreeformHealthplans);
886 function PrescriptionRenewalResponse($doc,$r,$pid)
888 $b = $doc->createElement( "PrescriptionRenewalResponse" );
889 $renewalRequestIdentifier = $doc->createElement( "renewalRequestIdentifier" );
890 $renewalRequestIdentifier->appendChild(
891 $doc->createTextNode( 'cbf51649-ce3c-44b8-8f91-6fda121a353d' )
893 $b->appendChild( $renewalRequestIdentifier );
894 $responseCode = $doc->createElement( "responseCode" );
895 $responseCode->appendChild(
896 $doc->createTextNode( 'Undetermined' )
898 $b->appendChild( $responseCode );
899 $r->appendChild( $b );
902 function checkError($xml)
904 $ch = curl_init($xml);
906 $data = array('RxInput' => $xml);
908 curl_setopt($ch, CURLOPT_URL, getErxPath());
909 curl_setopt($ch, CURLOPT_POST, 1);
910 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
911 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
912 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
913 curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
914 //curl_setopt($ch, CURLOPT_HEADER, 0);
915 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
916 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
917 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
918 curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
919 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
921 $result=curl_exec($ch) or die( curl_error($ch)) ;
922 preg_match('/<textarea.*>(.*)Original XML:/is',$result,$error_message);
923 if(strpos($result,'RxEntry.aspx')){
924 erx_error_log($xml);
925 erx_error_log($result);
927 $arr=explode('Error',$error_message[1]);
928 //echo "Te: ".count($arr);
929 //print_r($arr);
930 if(count($arr)==1)
932 echo nl2br($error_message[1]);
934 else
936 for($i=1;$i<count($arr);$i++)
938 echo $arr[$i]."<br><br>";
941 curl_close($ch);
942 if(strpos($result,'RxEntry.aspx'))
943 return '1';
944 else
945 return '0';
948 function erx_error_log($message)
950 $date = date("Y-m-d");
951 if(!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error'))
952 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error',0777,true);
953 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
954 $f=fopen($filename,'a');
955 fwrite($f,date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
956 fclose($f);
959 function stripStrings($str,$pattern)
961 $result = $str;
962 foreach($pattern as $key=>$value){
963 $result = preg_replace("/$key/",$value,$result);
965 return $result;