Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / interface / eRx_xml.php
blobb04c742e80f939c0e26c60b506acce894d22275a
1 <?php
2 /**
3 * interface/eRx_xml.php Functions for interacting with NewCrop communications.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Eldho Chacko <eldho@zhservices.com>
8 * @author Vinish K <vinish@zhservices.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2011 ZMG LLC <sam@zhservices.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 use OpenEMR\Common\Crypto\CryptoGen;
17 use OpenEMR\Services\FacilityService;
19 $facilityService = new FacilityService();
21 function getErxPath()
23 return $GLOBALS['erx_newcrop_path'];
26 function getErxSoapPath()
28 return $GLOBALS['erx_newcrop_path_soap'];
31 function getErxCredentials()
33 $cred=array();
34 $cred[]=$GLOBALS['erx_account_partner_name'];
35 $cred[]=$GLOBALS['erx_account_name'];
36 $cryptoGen = new CryptoGen();
37 $cred[]=$cryptoGen->decryptStandard($GLOBALS['erx_account_password']);
39 return $cred;
42 function validation($val_check, $val, $msg)
44 if (!$val) {
45 $msg .= $val_check.' '.xl('missing').'<br>';
48 return $msg;
51 function stripSpecialCharacterFacility($str)
53 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
54 return $str;
57 function stripSpecialCharacter($str)
59 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
60 return $str;
63 function stripPhoneSlashes($str)
65 $str=preg_replace('/-/', '', $str);
66 return $str;
69 function trimData($str, $length)
71 $str=substr($str, 0, ($length-1));
72 return $str;
75 function stringToNumeric($str)
77 if (is_numeric($str)) {
78 return array($str,"");
79 } else {
80 for ($i=0; $i<strlen($str); $i++) {
81 $x=substr($str, $i, 1);
82 if (is_numeric($x) && !$txt) {
83 $num.=$x;
84 } else {
85 $txt.=$x;
89 return array($num,$txt);
92 $str=substr($str, 0, ($length-1));
93 return $str;
95 function credentials($doc, $r)
97 global $msg;
98 $cred=getErxCredentials();
99 $msg = validation(xl('Partner Name'), $cred['0'], $msg);
100 $b = $doc->createElement("Credentials");
101 $partnerName = $doc->createElement("partnerName");
102 $partnerName->appendChild(
103 $doc->createTextNode($cred['0'])
105 $b->appendChild($partnerName);
106 $msg = validation(xl('ERX Name'), $cred['1'], $msg);
107 $name = $doc->createElement("name");
108 $name->appendChild(
109 $doc->createTextNode($cred['1'])
111 $b->appendChild($name);
112 $msg = validation(xl('ERX Password'), $cred['2'], $msg);
113 $password = $doc->createElement("password");
114 $password->appendChild(
115 $doc->createTextNode($cred['2'])
117 $b->appendChild($password);
118 $productName = $doc->createElement("productName");
119 $productName->appendChild(
120 $doc->createTextNode('OpenEMR')
122 $b->appendChild($productName);
123 $productVersion = $doc->createElement("productVersion");
124 $productVersion->appendChild(
125 $doc->createTextNode($GLOBALS['openemr_version'])
127 $b->appendChild($productVersion);
128 $r->appendChild($b);
131 function user_role($doc, $r)
133 global $msg;
134 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
135 if (!$userRole['newcrop_user_role']) {
136 echo xlt('Unauthorized access to ePrescription');
137 die;
140 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
141 if ($userRole['newcrop_user_role'] == 'doctor') {
142 $userRole['eRxUser'] = 'LicensedPrescriber';
143 } elseif ($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse') {
144 $userRole['eRxUser'] = 'Staff';
145 } elseif ($userRole['newcrop_user_role'] == 'midlevelPrescriber') {
146 $userRole['eRxUser'] = 'MidlevelPrescriber';
147 } elseif ($userRole['newcrop_user_role'] == 'supervisingDoctor') {
148 $userRole['eRxUser'] = 'SupervisingDoctor';
151 $msg = validation(xl('ERX User'), $userRole['eRxUser'], $msg);
152 $b = $doc->createElement("UserRole");
153 $user = $doc->createElement("user");
154 $user->appendChild(
155 $doc->createTextNode($userRole['eRxUser'])
157 $b->appendChild($user);
158 $msg = validation(xl('ERX Role'), $userRole['newcrop_user_role'], $msg);
159 $role = $doc->createElement("role");
160 $role->appendChild(
161 $doc->createTextNode($userRole['newcrop_user_role'])
163 $b->appendChild($role);
164 $r->appendChild($b);
167 function destination($doc, $r, $page = '', $pid)
169 global $msg,$page;
170 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
171 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
172 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';
181 $b = $doc->createElement("Destination");
182 $requestedPage = $doc->createElement("requestedPage");
183 $requestedPage->appendChild(
184 $doc->createTextNode($page)
186 $b->appendChild($requestedPage);
187 $r->appendChild($b);
190 function account($doc, $r)
192 global $msg, $facilityService;
193 $erxSiteID= $facilityService->getPrimaryBusinessEntity();
194 if (!$erxSiteID['federal_ein']) {
195 echo xlt("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.");
196 die;
199 $b = $doc->createElement("Account");
200 $b->setAttribute('ID', $GLOBALS['erx_account_id']);
201 $erxSiteID['name']=stripSpecialCharacterFacility($erxSiteID['name']);
202 $erxSiteID['name']=trimData($erxSiteID['name'], 35);
203 $msg = validation(xl('Account Name'), $erxSiteID['name'], $msg);
204 $accountName = $doc->createElement("accountName");
205 $accountName->appendChild(
206 $doc->createTextNode($erxSiteID['name'])
208 $b->appendChild($accountName);
209 $msg = validation(xl('Site ID'), $_SESSION['site_id'], $msg);
210 $siteID = $doc->createElement("siteID");
211 $siteID->appendChild(
212 $doc->createTextNode($erxSiteID['federal_ein'])
214 $b->appendChild($siteID);
215 $erxSiteID['street']=stripSpecialCharacterFacility($erxSiteID['street']);
216 $erxSiteID['street']=trimData($erxSiteID['street'], 35);
217 $AccountAddress = $doc->createElement("AccountAddress");
218 $msg = validation(xl('Facility Street'), $erxSiteID['street'], $msg);
219 $address1 = $doc->createElement("address1");
220 $address1->appendChild(
221 $doc->createTextNode($erxSiteID['street'])
223 $AccountAddress->appendChild($address1);
224 $msg = validation(xl('Facility City'), $erxSiteID['city'], $msg);
225 $city = $doc->createElement("city");
226 $city->appendChild(
227 $doc->createTextNode($erxSiteID['city'])
229 $AccountAddress->appendChild($city);
230 $msg = validation(xl('Facility State'), $erxSiteID['state'], $msg);
231 $state = $doc->createElement("state");
232 $state->appendChild(
233 $doc->createTextNode($erxSiteID['state'])
235 $AccountAddress->appendChild($state);
236 $jasonbigzip=$erxSiteID['postal_code'];
237 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
238 if (strlen($jasonbigzip) >=5) {
239 $jasonzip=substr($jasonbigzip, 0, 5);
240 $zip4=substr($jasonbigzip, 5, 4);
241 } else {
242 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
245 $zip = $doc->createElement("zip");
246 $zip->appendChild(
247 $doc->createTextNode($jasonzip)
249 $AccountAddress->appendChild($zip);
250 if (strlen($zip4)==4) {
251 $zipFour = $doc->createElement("zip4");
252 $zipFour->appendChild(
253 $doc->createTextNode($zip4)
255 $AccountAddress->appendChild($zipFour);
258 $msg = validation(xl('Facility Country code'), $erxSiteID['country_code'], $msg);
259 $county_code = substr($erxSiteID['country_code'], 0, 2);
260 $country = $doc->createElement("country");
261 $country->appendChild(
262 $doc->createTextNode($county_code)
264 $AccountAddress->appendChild($country);
265 $b->appendChild($AccountAddress);
266 $msg = validation(xl('Facility Phone'), $erxSiteID['phone'], $msg);
267 $accountPrimaryPhoneNumber = $doc->createElement("accountPrimaryPhoneNumber");
268 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
269 $accountPrimaryPhoneNumber->appendChild(
270 $doc->createTextNode($erxSiteID['phone'])
272 $b->appendChild($accountPrimaryPhoneNumber);
273 $msg = validation(xl('Facility Fax'), $erxSiteID['fax'], $msg);
274 $accountPrimaryFaxNumber = $doc->createElement("accountPrimaryFaxNumber");
275 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
276 $accountPrimaryFaxNumber->appendChild(
277 $doc->createTextNode($erxSiteID['fax'])
279 $b->appendChild($accountPrimaryFaxNumber);
280 $r->appendChild($b);
283 function location($doc, $r)
285 global $msg;
286 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?", array($_SESSION['authUser']));
287 $b = $doc->createElement("Location");
288 $b->setAttribute('ID', $userRole['id']);
289 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
290 $userRole['name']=trimData($userRole['name'], 35);
291 $locationName = $doc->createElement('locationName');
292 $locationName->appendChild(
293 $doc->createTextNode($userRole['name'])
295 $b->appendChild($locationName);
296 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
297 $userRole['street']=trimData($userRole['street'], 35);
298 $LocationAddress = $doc->createElement('LocationAddress');
299 if ($userRole['street']) {
300 $address1 = $doc->createElement('address1');
301 $address1->appendChild(
302 $doc->createTextNode($userRole['street'])
304 $LocationAddress->appendChild($address1);
307 if ($userRole['city']) {
308 $city = $doc->createElement('city');
309 $city->appendChild(
310 $doc->createTextNode($userRole['city'])
312 $LocationAddress->appendChild($city);
315 if ($userRole['state']) {
316 $state = $doc->createElement('state');
317 $state->appendChild(
318 $doc->createTextNode($userRole['state'])
320 $LocationAddress->appendChild($state);
323 $jasonbigzip=$userRole['postal_code'];
324 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
325 if (strlen($jasonbigzip) >=5) {
326 $jasonzip=substr($jasonbigzip, 0, 5);
327 $zip4=substr($jasonbigzip, 5, 4);
328 } else {
329 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
332 $zip = $doc->createElement("zip");
333 $zip->appendChild(
334 $doc->createTextNode($jasonzip)
336 $LocationAddress->appendChild($zip);
337 if (strlen($zip4)==4) {
338 $zipFour = $doc->createElement("zip4");
339 $zipFour->appendChild(
340 $doc->createTextNode($zip4)
342 $LocationAddress->appendChild($zipFour);
345 if ($userRole['country_code']) {
346 $county_code = substr($userRole['country_code'], 0, 2);
347 $country = $doc->createElement('country');
348 $country->appendChild(
349 $doc->createTextNode($county_code)
351 $LocationAddress->appendChild($country);
354 $b->appendChild($LocationAddress);
355 if ($userRole['phone']) {
356 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
357 $primaryPhoneNumber = $doc->createElement('primaryPhoneNumber');
358 $primaryPhoneNumber->appendChild(
359 $doc->createTextNode($userRole['phone'])
361 $b->appendChild($primaryPhoneNumber);
364 if ($userRole['fax']) {
365 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
366 $primaryFaxNumber = $doc->createElement('primaryFaxNumber');
367 $primaryFaxNumber->appendChild(
368 $doc->createTextNode($userRole['fax'])
370 $b->appendChild($primaryFaxNumber);
373 $pharmacyContactNumber = $doc->createElement('pharmacyContactNumber');
374 $pharmacyContactNumber->appendChild(
375 $doc->createTextNode($userRole['phone'])
377 $b->appendChild($pharmacyContactNumber);
378 $r->appendChild($b);
381 function LicensedPrescriber($doc, $r)
383 global $msg;
384 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
385 $b = $doc->createElement("LicensedPrescriber");
386 $b->setAttribute('ID', $user_details['npi']);
387 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
388 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
389 $msg = validation(xl('LicensedPrescriber Last name'), $user_details['lname'], $msg);
390 $last = $doc->createElement("last");
391 $last->appendChild(
392 $doc->createTextNode($user_details['lname'])
394 $LicensedPrescriberName->appendChild($last);
395 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
396 $msg = validation(xl('User First name'), $user_details['fname'], $msg);
397 $first = $doc->createElement("first");
398 $first->appendChild(
399 $doc->createTextNode($user_details['fname'])
401 $LicensedPrescriberName->appendChild($first);
402 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
403 $middle = $doc->createElement("middle");
404 $middle->appendChild(
405 $doc->createTextNode($user_details['mname'])
407 $LicensedPrescriberName->appendChild($middle);
408 $b->appendChild($LicensedPrescriberName);
409 $msg = validation(xl('DEA'), $user_details['federaldrugid'], $msg);
410 $dea = $doc->createElement("dea");
411 $dea->appendChild(
412 $doc->createTextNode($user_details['federaldrugid'])
414 $b->appendChild($dea);
415 if ($user_details['upin']) {
416 $upin = $doc->createElement("upin");
417 $upin->appendChild(
418 $doc->createTextNode($user_details['upin'])
420 $b->appendChild($upin);
423 $licenseNumber = $doc->createElement("licenseNumber");
424 $licenseNumber->appendChild(
425 $doc->createTextNode($user_details['state_license_number'])
427 $b->appendChild($licenseNumber);
428 $msg = validation(xl('LicensedPrescriber NPI'), $user_details['npi'], $msg);
429 $npi = $doc->createElement("npi");
430 $npi->appendChild(
431 $doc->createTextNode($user_details['npi'])
433 $b->appendChild($npi);
434 $r->appendChild($b);
437 function Staff($doc, $r)
439 global $msg;
440 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
441 $b = $doc->createElement("Staff");
442 $b->setAttribute('ID', $user_details['username']);
443 $StaffName = $doc->createElement("StaffName");
444 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
445 $last = $doc->createElement("last");
446 $last->appendChild(
447 $doc->createTextNode($user_details['lname'])
449 $StaffName->appendChild($last);
450 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
451 $first = $doc->createElement("first");
452 $first->appendChild(
453 $doc->createTextNode($user_details['fname'])
455 $StaffName->appendChild($first);
456 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
457 $middle = $doc->createElement("middle");
458 $middle->appendChild(
459 $doc->createTextNode($user_details['mname'])
461 $StaffName->appendChild($middle);
462 $b->appendChild($StaffName);
463 $license = $doc->createElement("license");
464 $license->appendChild(
465 $doc->createTextNode($user_details['license'])
467 $b->appendChild($license);
468 $r->appendChild($b);
471 function SupervisingDoctor($doc, $r)
473 global $msg;
474 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
475 $b = $doc->createElement("SupervisingDoctor");
476 $b->setAttribute('ID', $user_details['npi']);
477 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
478 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
479 $msg = validation(xl('Supervising Doctor Last name'), $user_details['lname'], $msg);
480 $last = $doc->createElement("last");
481 $last->appendChild(
482 $doc->createTextNode($user_details['lname'])
484 $LicensedPrescriberName->appendChild($last);
485 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
486 $msg = validation(xl('Supervising Doctor First name'), $user_details['fname'], $msg);
487 $first = $doc->createElement("first");
488 $first->appendChild(
489 $doc->createTextNode($user_details['fname'])
491 $LicensedPrescriberName->appendChild($first);
492 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
493 $middle = $doc->createElement("middle");
494 $middle->appendChild(
495 $doc->createTextNode($user_details['mname'])
497 $LicensedPrescriberName->appendChild($middle);
498 $b->appendChild($LicensedPrescriberName);
499 $msg = validation(xl('Supervising Doctor DEA'), $user_details['federaldrugid'], $msg);
500 $dea = $doc->createElement("dea");
501 $dea->appendChild(
502 $doc->createTextNode($user_details['federaldrugid'])
504 $b->appendChild($dea);
505 if ($user_details['upin']) {
506 $upin = $doc->createElement("upin");
507 $upin->appendChild(
508 $doc->createTextNode($user_details['upin'])
510 $b->appendChild($upin);
513 $licenseNumber = $doc->createElement("licenseNumber");
514 $licenseNumber->appendChild(
515 $doc->createTextNode($user_details['state_license_number'])
517 $b->appendChild($licenseNumber);
518 $msg = validation(xl('Supervising Doctor NPI'), $user_details['npi'], $msg);
519 $npi = $doc->createElement("npi");
520 $npi->appendChild(
521 $doc->createTextNode($user_details['npi'])
523 $b->appendChild($npi);
524 $r->appendChild($b);
527 function MidlevelPrescriber($doc, $r)
529 global $msg;
530 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
531 $b = $doc->createElement("MidlevelPrescriber");
532 $b->setAttribute('ID', $user_details['npi']);
533 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
534 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
535 $msg = validation(xl('Midlevel Prescriber Last name'), $user_details['lname'], $msg);
536 $last = $doc->createElement("last");
537 $last->appendChild(
538 $doc->createTextNode($user_details['lname'])
540 $LicensedPrescriberName->appendChild($last);
541 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
542 $msg = validation(xl('Midlevel Prescriber First name'), $user_details['fname'], $msg);
543 $first = $doc->createElement("first");
544 $first->appendChild(
545 $doc->createTextNode($user_details['fname'])
547 $LicensedPrescriberName->appendChild($first);
548 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
549 $middle = $doc->createElement("middle");
550 $middle->appendChild(
551 $doc->createTextNode($user_details['mname'])
553 $LicensedPrescriberName->appendChild($middle);
554 if ($user_details['title']) {
555 $msg = validation(xl('Midlevel Prescriber Prefix'), $user_details['title'], $msg);
556 $prefix = $doc->createElement("prefix");
557 $prefix->appendChild(
558 $doc->createTextNode($user_details['title'])
560 $LicensedPrescriberName->appendChild($prefix);
563 $b->appendChild($LicensedPrescriberName);
564 $msg = validation(xl('Midlevel Prescriber DEA'), $user_details['federaldrugid'], $msg);
565 $dea = $doc->createElement("dea");
566 $dea->appendChild(
567 $doc->createTextNode($user_details['federaldrugid'])
569 $b->appendChild($dea);
570 if ($user_details['upin']) {
571 $upin = $doc->createElement("upin");
572 $upin->appendChild(
573 $doc->createTextNode($user_details['upin'])
575 $b->appendChild($upin);
578 $licenseNumber = $doc->createElement("licenseNumber");
579 $licenseNumber->appendChild(
580 $doc->createTextNode($user_details['state_license_number'])
582 $b->appendChild($licenseNumber);
583 $r->appendChild($b);
586 function Patient($doc, $r, $pid)
588 global $msg,$warning_msg,$dem_check;
589 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?", array($pid));
590 $b = $doc->createElement("Patient");
591 $b->setAttribute('ID', $patient_data['pid']);
592 $PatientName = $doc->createElement("PatientName");
593 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
594 $patient_data['lname']=trimData($patient_data['lname'], 35);
595 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
596 if ($patient_data['lname']=='') {
597 $dem_check.=xlt("Patient Last name is missing")."<br>";
600 $last = $doc->createElement("last");
601 $last->appendChild(
602 $doc->createTextNode($patient_data['lname'])
604 $PatientName->appendChild($last);
605 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
606 $patient_data['fname']=trimData($patient_data['fname'], 35);
607 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
608 if ($patient_data['fname']=='') {
609 $dem_check.=xlt("Patient First name is missing")."<br>";
612 $first = $doc->createElement("first");
613 $first->appendChild(
614 $doc->createTextNode($patient_data['fname'])
616 $PatientName->appendChild($first);
617 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
618 $patient_data['mname']=trimData($patient_data['mname'], 35);
619 $middle = $doc->createElement("middle");
620 $middle->appendChild(
621 $doc->createTextNode($patient_data['mname'])
623 $PatientName->appendChild($middle);
624 $b->appendChild($PatientName);
625 $PatientAddress = $doc->createElement("PatientAddress");
626 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
627 $patient_data['street']=trimData($patient_data['street'], 35);
628 $msg = validation(xl('Patient Address'), $patient_data['street'], $msg);
629 if (trim($patient_data['street'])=='') {
630 $warning_msg .= "<br>".xlt("Patient Address is missing");
633 $address1 = $doc->createElement("address1");
634 $address1->appendChild(
635 $doc->createTextNode($patient_data['street'])
637 $PatientAddress->appendChild($address1);
638 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
639 if ($patient_data['city']=='') {
640 $dem_check.=xlt("Patient City is missing")."<br>";
643 $city = $doc->createElement("city");
644 $city->appendChild(
645 $doc->createTextNode($patient_data['city'])
647 $PatientAddress->appendChild($city);
648 if ($patient_data['state']) {
649 $state = $doc->createElement("state");
650 $state->appendChild(
651 $doc->createTextNode($patient_data['state'])
653 $PatientAddress->appendChild($state);
656 if ($patient_data['postal_code']) {
657 $zip = $doc->createElement("zip");
658 $zip->appendChild(
659 $doc->createTextNode($patient_data['postal_code'])
661 $PatientAddress->appendChild($zip);
664 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
665 if (trim($patient_data['country_code'])=='' && $GLOBALS['erx_default_patient_country']=='') {
666 $dem_check.=xlt("Patient Country is missing. Also you have not set default Patient Country in Global Settings")."<br>";
667 } elseif (trim($patient_data['country_code'])=='') {
668 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
671 $county_code = substr($patient_data['country_code'], 0, 2);
672 $country = $doc->createElement("country");
673 $country->appendChild(
674 $doc->createTextNode($county_code)
676 $PatientAddress->appendChild($country);
677 $b->appendChild($PatientAddress);
678 $PatientContact = $doc->createElement("PatientContact");
679 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
680 if ($patient_data['phone_home']) {
681 $homeTelephone = $doc->createElement("homeTelephone");
682 $homeTelephone->appendChild(
683 $doc->createTextNode($patient_data['phone_home'])
685 $PatientContact->appendChild($homeTelephone);
688 $b->appendChild($PatientContact);
689 $PatientCharacteristics = $doc->createElement("PatientCharacteristics");
690 if (trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000') {
691 $warning_msg .= "<br>".xlt("Patient Date Of Birth is missing");
694 if ($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000') {
695 $dob = $doc->createElement("dob");
696 $dob->appendChild(
697 $doc->createTextNode($patient_data['date_of_birth'])
699 $PatientCharacteristics->appendChild($dob);
702 if (trim($patient_data['sex'])=='') {
703 $warning_msg .= "<br>".xlt("Patient Gender is missing");
706 if ($patient_data['sex']) {
707 $gender_val=substr($patient_data['sex'], 0, 1);
708 $gender = $doc->createElement("gender");
709 $gender->appendChild(
710 $doc->createTextNode($gender_val)
712 $PatientCharacteristics->appendChild($gender);
715 $b->appendChild($PatientCharacteristics);
716 PatientFreeformHealthplans($doc, $b, $pid);
717 $allergyId=PatientFreeformAllergy($doc, $b, $pid);
718 $r->appendChild($b);
719 return $allergyId;
722 function OutsidePrescription($doc, $r, $pid, $prescid)
724 global $msg;
725 if ($prescid) {
726 $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,
727 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
728 FROM prescriptions AS p
729 LEFT JOIN users AS u ON p.provider_id=u.id
730 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
731 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
732 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
733 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
734 WHERE p.drug <> '' and p.id = ?", array($prescid));
735 $b = $doc->createElement("OutsidePrescription");
736 $externalId = $doc->createElement("externalId");
737 $externalId->appendChild(
738 $doc->createTextNode($prec['prescid'])
740 $b->appendChild($externalId);
741 $date = $doc->createElement("date");
742 $date->appendChild(
743 $doc->createTextNode($prec['date_added'])
745 $b->appendChild($date);
746 $doctorName = $doc->createElement("doctorName");
747 $doctorName->appendChild(
748 $doc->createTextNode($prec['docname'])
750 $b->appendChild($doctorName);
751 $s=stripSpecialCharacter($prec['drug']);
752 $sig = $doc->createElement("drug");
753 $sig->appendChild(
754 $doc->createTextNode(trimData($s, 80))
756 $b->appendChild($sig);
757 $x=stringToNumeric($prec['quantity']);
758 $dispenseNumber = $doc->createElement("dispenseNumber");
759 $dispenseNumber->appendChild(
760 $doc->createTextNode($x[0])
762 $b->appendChild($dispenseNumber);
763 $s=trimData($x[1].$prec['size']." ".$prec['title4']." ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'], 140);
764 $s=stripSpecialCharacter($s);
765 $sig = $doc->createElement("sig");
766 $sig->appendChild(
767 $doc->createTextNode($s)
769 $b->appendChild($sig);
770 $refillCount = $doc->createElement("refillCount");
771 $x=stringToNumeric($prec['per_refill']);
772 $refillCount->appendChild(
773 $doc->createTextNode($x[0])
775 $b->appendChild($refillCount);
776 $prescriptionType = $doc->createElement("prescriptionType");
777 $prescriptionType->appendChild(
778 $doc->createTextNode('reconcile')
780 $b->appendChild($prescriptionType);
781 $r->appendChild($b);
785 function PatientMedication($doc, $r, $pid, $med_limit)
787 global $msg;
788 $active='';
789 if ($GLOBALS['erx_upload_active']==1) {
790 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
793 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
794 and erx_uploaded='0' $active order by enddate limit 0," . escape_limit($med_limit), array($pid));
795 $uploaded_med_arr="";
796 while ($row_med=sqlFetchArray($res_med)) {
797 $uploaded_med_arr[]=$row_med['id'];
798 $b = $doc->createElement("OutsidePrescription");
799 $externalId = $doc->createElement("externalId");
800 $externalId->appendChild(
801 $doc->createTextNode($row_med['id'])
803 $b->appendChild($externalId);
804 $date = $doc->createElement("date");
805 $date->appendChild(
806 $doc->createTextNode($row_med['begdate'])
808 $b->appendChild($date);
809 $doctorName = $doc->createElement("doctorName");
810 $doctorName->appendChild(
811 $doc->createTextNode("")
813 $b->appendChild($doctorName);
814 $row_med['title'] = stripSpecialCharacter($row_med['title']);
815 $sig = $doc->createElement("drug");
816 $sig->appendChild(
817 $doc->createTextNode(trimData($row_med['title'], 80))
819 $b->appendChild($sig);
820 $dispenseNumber = $doc->createElement("dispenseNumber");
821 $dispenseNumber->appendChild(
822 $doc->createTextNode($prec['quantity'])
824 $b->appendChild($dispenseNumber);
825 $sig = $doc->createElement("sig");
826 $sig->appendChild(
827 $doc->createTextNode("")
829 $b->appendChild($sig);
830 $refillCount = $doc->createElement("refillCount");
831 $refillCount->appendChild(
832 $doc->createTextNode("")
834 $b->appendChild($refillCount);
835 $prescriptionType = $doc->createElement("prescriptionType");
836 $prescriptionType->appendChild(
837 $doc->createTextNode('reconcile')
839 $b->appendChild($prescriptionType);
840 $r->appendChild($b);
843 return $uploaded_med_arr;
846 function PatientFreeformAllergy($doc, $r, $pid)
848 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
849 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
850 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));
851 $allergyId=array();
852 while ($row=sqlFetchArray($res)) {
853 $val=array();
854 $val['id']=$row['id'];
855 $val['title1']=$row['title1'];
856 $val['title2']=$row['title2'];
857 $val['comments']=$row['comments'];
858 $b = $doc->createElement("PatientFreeformAllergy");
859 $b->setAttribute('ID', $val['id']);
860 if ($val['title1']) {
861 $allergyName = $doc->createElement("allergyName");
862 $allergyName->appendChild(
863 $doc->createTextNode(trimData(stripSpecialCharacter($val['title1']), 70))
865 $b->appendChild($allergyName);
868 if ($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')) {
869 $allergySeverityTypeID = $doc->createElement("allergySeverityTypeID");
870 $allergySeverityTypeID->appendChild(
871 $doc->createTextNode($val['title2'])
873 $b->appendChild($allergySeverityTypeID);
876 if ($val['comments']) {
877 $allergyComment = $doc->createElement("allergyComment");
878 $allergyComment->appendChild(
879 $doc->createTextNode(trimData(stripSpecialCharacter($val['comments']), 200))
881 $b->appendChild($allergyComment);
884 $r->appendChild($b);
885 $allergyId[]=$row['id'];
888 return $allergyId;
891 function PatientFreeformHealthplans($doc, $r, $pid)
893 $resource = sqlStatement(
894 'SELECT
895 `ins`.`name`
896 FROM (
897 SELECT
898 `id`.`type`,
899 `ic`.`name`
900 FROM `insurance_data` AS `id`
901 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
902 WHERE `id`.`pid` = ?
903 AND `id`.`subscriber_relationship` = \'self\'
904 AND `id`.`provider` > 0
905 ORDER BY `id`.`date` DESC
906 ) AS `ins`
907 GROUP BY `ins`.`type`;',
908 array($pid)
911 while ($row = sqlFetchArray($resource)) {
912 $healthplanName = $doc->createElement('healthplanName');
913 $healthplanName->appendChild($doc->createTextNode(
914 stripSpecialCharacter(trimData($row['name'], 35))
917 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
918 $patientFreeformHealthplans->appendChild($healthplanName);
920 $r->appendChild($patientFreeformHealthplans);
924 function PrescriptionRenewalResponse($doc, $r, $pid)
926 $b = $doc->createElement("PrescriptionRenewalResponse");
927 $renewalRequestIdentifier = $doc->createElement("renewalRequestIdentifier");
928 $renewalRequestIdentifier->appendChild(
929 $doc->createTextNode('cbf51649-ce3c-44b8-8f91-6fda121a353d')
931 $b->appendChild($renewalRequestIdentifier);
932 $responseCode = $doc->createElement("responseCode");
933 $responseCode->appendChild(
934 $doc->createTextNode('Undetermined')
936 $b->appendChild($responseCode);
937 $r->appendChild($b);
940 function checkError($xml)
942 $ch = curl_init($xml);
944 $data = array('RxInput' => $xml);
946 curl_setopt($ch, CURLOPT_URL, getErxPath());
947 curl_setopt($ch, CURLOPT_POST, 1);
948 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
949 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
950 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
951 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
952 //curl_setopt($ch, CURLOPT_HEADER, 0);
953 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
954 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
955 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
956 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
957 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
959 $result=curl_exec($ch) or die(curl_error($ch)) ;
960 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $error_message);
961 if (strpos($result, 'RxEntry.aspx')) {
962 erx_error_log($xml);
963 erx_error_log($result);
966 $arr=explode('Error', $error_message[1]);
967 //echo "Te: ".count($arr);
968 //print_r($arr);
969 if (count($arr)==1) {
970 echo nl2br($error_message[1]);
971 } else {
972 for ($i=1; $i<count($arr); $i++) {
973 echo $arr[$i]."<br><br>";
977 curl_close($ch);
978 if (strpos($result, 'RxEntry.aspx')) {
979 return '1';
980 } else {
981 return '0';
985 function erx_error_log($message)
987 $date = date("Y-m-d");
988 if (!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error')) {
989 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error', 0777, true);
992 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
993 $f=fopen($filename, 'a');
994 fwrite($f, date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
995 fclose($f);
998 function stripStrings($str, $pattern)
1000 $result = $str;
1001 foreach ($pattern as $key => $value) {
1002 $result = preg_replace("/$key/", $value, $result);
1005 return $result;