bug fix bundle (#2018)
[openemr.git] / interface / eRx_xml.php
blobf0a78103a3b85eafea8a0b4091fb7d6186aff9aa
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\Services\FacilityService;
18 $facilityService = new FacilityService();
20 function getErxPath()
22 return $GLOBALS['erx_newcrop_path'];
25 function getErxSoapPath()
27 return $GLOBALS['erx_newcrop_path_soap'];
30 function getErxCredentials()
32 $cred=array();
33 $cred[]=$GLOBALS['erx_account_partner_name'];
34 $cred[]=$GLOBALS['erx_account_name'];
35 $cred[]=$GLOBALS['erx_account_password'];
37 return $cred;
40 function validation($val_check, $val, $msg)
42 if (!$val) {
43 $msg .= $val_check.' '.xl('missing').'<br>';
46 return $msg;
49 function stripSpecialCharacterFacility($str)
51 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
52 return $str;
55 function stripSpecialCharacter($str)
57 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
58 return $str;
61 function stripPhoneSlashes($str)
63 $str=preg_replace('/-/', '', $str);
64 return $str;
67 function trimData($str, $length)
69 $str=substr($str, 0, ($length-1));
70 return $str;
73 function stringToNumeric($str)
75 if (is_numeric($str)) {
76 return array($str,"");
77 } else {
78 for ($i=0; $i<strlen($str); $i++) {
79 $x=substr($str, $i, 1);
80 if (is_numeric($x) && !$txt) {
81 $num.=$x;
82 } else {
83 $txt.=$x;
87 return array($num,$txt);
90 $str=substr($str, 0, ($length-1));
91 return $str;
93 function credentials($doc, $r)
95 global $msg;
96 $cred=getErxCredentials();
97 $msg = validation(xl('Partner Name'), $cred['0'], $msg);
98 $b = $doc->createElement("Credentials");
99 $partnerName = $doc->createElement("partnerName");
100 $partnerName->appendChild(
101 $doc->createTextNode($cred['0'])
103 $b->appendChild($partnerName);
104 $msg = validation(xl('ERX Name'), $cred['1'], $msg);
105 $name = $doc->createElement("name");
106 $name->appendChild(
107 $doc->createTextNode($cred['1'])
109 $b->appendChild($name);
110 $msg = validation(xl('ERX Password'), $cred['2'], $msg);
111 $password = $doc->createElement("password");
112 $password->appendChild(
113 $doc->createTextNode($cred['2'])
115 $b->appendChild($password);
116 $productName = $doc->createElement("productName");
117 $productName->appendChild(
118 $doc->createTextNode('OpenEMR')
120 $b->appendChild($productName);
121 $productVersion = $doc->createElement("productVersion");
122 $productVersion->appendChild(
123 $doc->createTextNode($GLOBALS['openemr_version'])
125 $b->appendChild($productVersion);
126 $r->appendChild($b);
129 function user_role($doc, $r)
131 global $msg;
132 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
133 if (!$userRole['newcrop_user_role']) {
134 echo xlt('Unauthorized access to ePrescription');
135 die;
138 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
139 if ($userRole['newcrop_user_role'] == 'doctor') {
140 $userRole['eRxUser'] = 'LicensedPrescriber';
141 } elseif ($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse') {
142 $userRole['eRxUser'] = 'Staff';
143 } elseif ($userRole['newcrop_user_role'] == 'midlevelPrescriber') {
144 $userRole['eRxUser'] = 'MidlevelPrescriber';
145 } elseif ($userRole['newcrop_user_role'] == 'supervisingDoctor') {
146 $userRole['eRxUser'] = 'SupervisingDoctor';
149 $msg = validation(xl('ERX User'), $userRole['eRxUser'], $msg);
150 $b = $doc->createElement("UserRole");
151 $user = $doc->createElement("user");
152 $user->appendChild(
153 $doc->createTextNode($userRole['eRxUser'])
155 $b->appendChild($user);
156 $msg = validation(xl('ERX Role'), $userRole['newcrop_user_role'], $msg);
157 $role = $doc->createElement("role");
158 $role->appendChild(
159 $doc->createTextNode($userRole['newcrop_user_role'])
161 $b->appendChild($role);
162 $r->appendChild($b);
165 function destination($doc, $r, $page = '', $pid)
167 global $msg,$page;
168 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
169 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
170 if (!$page) {
171 $page='compose';
172 if ($userRole['newcrop_user_role']=='admin') {
173 $page='admin';
174 } elseif ($userRole['newcrop_user_role']=='manager') {
175 $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']) {
193 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.");
194 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);
239 } else {
240 $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);
256 $msg = validation(xl('Facility Country code'), $erxSiteID['country_code'], $msg);
257 $county_code = substr($erxSiteID['country_code'], 0, 2);
258 $country = $doc->createElement("country");
259 $country->appendChild(
260 $doc->createTextNode($county_code)
262 $AccountAddress->appendChild($country);
263 $b->appendChild($AccountAddress);
264 $msg = validation(xl('Facility Phone'), $erxSiteID['phone'], $msg);
265 $accountPrimaryPhoneNumber = $doc->createElement("accountPrimaryPhoneNumber");
266 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
267 $accountPrimaryPhoneNumber->appendChild(
268 $doc->createTextNode($erxSiteID['phone'])
270 $b->appendChild($accountPrimaryPhoneNumber);
271 $msg = validation(xl('Facility Fax'), $erxSiteID['fax'], $msg);
272 $accountPrimaryFaxNumber = $doc->createElement("accountPrimaryFaxNumber");
273 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
274 $accountPrimaryFaxNumber->appendChild(
275 $doc->createTextNode($erxSiteID['fax'])
277 $b->appendChild($accountPrimaryFaxNumber);
278 $r->appendChild($b);
281 function location($doc, $r)
283 global $msg;
284 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?", array($_SESSION['authUser']));
285 $b = $doc->createElement("Location");
286 $b->setAttribute('ID', $userRole['id']);
287 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
288 $userRole['name']=trimData($userRole['name'], 35);
289 $locationName = $doc->createElement('locationName');
290 $locationName->appendChild(
291 $doc->createTextNode($userRole['name'])
293 $b->appendChild($locationName);
294 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
295 $userRole['street']=trimData($userRole['street'], 35);
296 $LocationAddress = $doc->createElement('LocationAddress');
297 if ($userRole['street']) {
298 $address1 = $doc->createElement('address1');
299 $address1->appendChild(
300 $doc->createTextNode($userRole['street'])
302 $LocationAddress->appendChild($address1);
305 if ($userRole['city']) {
306 $city = $doc->createElement('city');
307 $city->appendChild(
308 $doc->createTextNode($userRole['city'])
310 $LocationAddress->appendChild($city);
313 if ($userRole['state']) {
314 $state = $doc->createElement('state');
315 $state->appendChild(
316 $doc->createTextNode($userRole['state'])
318 $LocationAddress->appendChild($state);
321 $jasonbigzip=$userRole['postal_code'];
322 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
323 if (strlen($jasonbigzip) >=5) {
324 $jasonzip=substr($jasonbigzip, 0, 5);
325 $zip4=substr($jasonbigzip, 5, 4);
326 } else {
327 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
330 $zip = $doc->createElement("zip");
331 $zip->appendChild(
332 $doc->createTextNode($jasonzip)
334 $LocationAddress->appendChild($zip);
335 if (strlen($zip4)==4) {
336 $zipFour = $doc->createElement("zip4");
337 $zipFour->appendChild(
338 $doc->createTextNode($zip4)
340 $LocationAddress->appendChild($zipFour);
343 if ($userRole['country_code']) {
344 $county_code = substr($userRole['country_code'], 0, 2);
345 $country = $doc->createElement('country');
346 $country->appendChild(
347 $doc->createTextNode($county_code)
349 $LocationAddress->appendChild($country);
352 $b->appendChild($LocationAddress);
353 if ($userRole['phone']) {
354 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
355 $primaryPhoneNumber = $doc->createElement('primaryPhoneNumber');
356 $primaryPhoneNumber->appendChild(
357 $doc->createTextNode($userRole['phone'])
359 $b->appendChild($primaryPhoneNumber);
362 if ($userRole['fax']) {
363 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
364 $primaryFaxNumber = $doc->createElement('primaryFaxNumber');
365 $primaryFaxNumber->appendChild(
366 $doc->createTextNode($userRole['fax'])
368 $b->appendChild($primaryFaxNumber);
371 $pharmacyContactNumber = $doc->createElement('pharmacyContactNumber');
372 $pharmacyContactNumber->appendChild(
373 $doc->createTextNode($userRole['phone'])
375 $b->appendChild($pharmacyContactNumber);
376 $r->appendChild($b);
379 function LicensedPrescriber($doc, $r)
381 global $msg;
382 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
383 $b = $doc->createElement("LicensedPrescriber");
384 $b->setAttribute('ID', $user_details['npi']);
385 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
386 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
387 $msg = validation(xl('LicensedPrescriber Last name'), $user_details['lname'], $msg);
388 $last = $doc->createElement("last");
389 $last->appendChild(
390 $doc->createTextNode($user_details['lname'])
392 $LicensedPrescriberName->appendChild($last);
393 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
394 $msg = validation(xl('User First name'), $user_details['fname'], $msg);
395 $first = $doc->createElement("first");
396 $first->appendChild(
397 $doc->createTextNode($user_details['fname'])
399 $LicensedPrescriberName->appendChild($first);
400 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
401 $middle = $doc->createElement("middle");
402 $middle->appendChild(
403 $doc->createTextNode($user_details['mname'])
405 $LicensedPrescriberName->appendChild($middle);
406 $b->appendChild($LicensedPrescriberName);
407 $msg = validation(xl('DEA'), $user_details['federaldrugid'], $msg);
408 $dea = $doc->createElement("dea");
409 $dea->appendChild(
410 $doc->createTextNode($user_details['federaldrugid'])
412 $b->appendChild($dea);
413 if ($user_details['upin']) {
414 $upin = $doc->createElement("upin");
415 $upin->appendChild(
416 $doc->createTextNode($user_details['upin'])
418 $b->appendChild($upin);
421 $licenseNumber = $doc->createElement("licenseNumber");
422 $licenseNumber->appendChild(
423 $doc->createTextNode($user_details['state_license_number'])
425 $b->appendChild($licenseNumber);
426 $msg = validation(xl('LicensedPrescriber NPI'), $user_details['npi'], $msg);
427 $npi = $doc->createElement("npi");
428 $npi->appendChild(
429 $doc->createTextNode($user_details['npi'])
431 $b->appendChild($npi);
432 $r->appendChild($b);
435 function Staff($doc, $r)
437 global $msg;
438 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
439 $b = $doc->createElement("Staff");
440 $b->setAttribute('ID', $user_details['username']);
441 $StaffName = $doc->createElement("StaffName");
442 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
443 $last = $doc->createElement("last");
444 $last->appendChild(
445 $doc->createTextNode($user_details['lname'])
447 $StaffName->appendChild($last);
448 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
449 $first = $doc->createElement("first");
450 $first->appendChild(
451 $doc->createTextNode($user_details['fname'])
453 $StaffName->appendChild($first);
454 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
455 $middle = $doc->createElement("middle");
456 $middle->appendChild(
457 $doc->createTextNode($user_details['mname'])
459 $StaffName->appendChild($middle);
460 $b->appendChild($StaffName);
461 $license = $doc->createElement("license");
462 $license->appendChild(
463 $doc->createTextNode($user_details['license'])
465 $b->appendChild($license);
466 $r->appendChild($b);
469 function SupervisingDoctor($doc, $r)
471 global $msg;
472 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
473 $b = $doc->createElement("SupervisingDoctor");
474 $b->setAttribute('ID', $user_details['npi']);
475 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
476 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
477 $msg = validation(xl('Supervising Doctor Last name'), $user_details['lname'], $msg);
478 $last = $doc->createElement("last");
479 $last->appendChild(
480 $doc->createTextNode($user_details['lname'])
482 $LicensedPrescriberName->appendChild($last);
483 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
484 $msg = validation(xl('Supervising Doctor First name'), $user_details['fname'], $msg);
485 $first = $doc->createElement("first");
486 $first->appendChild(
487 $doc->createTextNode($user_details['fname'])
489 $LicensedPrescriberName->appendChild($first);
490 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
491 $middle = $doc->createElement("middle");
492 $middle->appendChild(
493 $doc->createTextNode($user_details['mname'])
495 $LicensedPrescriberName->appendChild($middle);
496 $b->appendChild($LicensedPrescriberName);
497 $msg = validation(xl('Supervising Doctor DEA'), $user_details['federaldrugid'], $msg);
498 $dea = $doc->createElement("dea");
499 $dea->appendChild(
500 $doc->createTextNode($user_details['federaldrugid'])
502 $b->appendChild($dea);
503 if ($user_details['upin']) {
504 $upin = $doc->createElement("upin");
505 $upin->appendChild(
506 $doc->createTextNode($user_details['upin'])
508 $b->appendChild($upin);
511 $licenseNumber = $doc->createElement("licenseNumber");
512 $licenseNumber->appendChild(
513 $doc->createTextNode($user_details['state_license_number'])
515 $b->appendChild($licenseNumber);
516 $msg = validation(xl('Supervising Doctor NPI'), $user_details['npi'], $msg);
517 $npi = $doc->createElement("npi");
518 $npi->appendChild(
519 $doc->createTextNode($user_details['npi'])
521 $b->appendChild($npi);
522 $r->appendChild($b);
525 function MidlevelPrescriber($doc, $r)
527 global $msg;
528 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
529 $b = $doc->createElement("MidlevelPrescriber");
530 $b->setAttribute('ID', $user_details['npi']);
531 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
532 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
533 $msg = validation(xl('Midlevel Prescriber Last name'), $user_details['lname'], $msg);
534 $last = $doc->createElement("last");
535 $last->appendChild(
536 $doc->createTextNode($user_details['lname'])
538 $LicensedPrescriberName->appendChild($last);
539 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
540 $msg = validation(xl('Midlevel Prescriber First name'), $user_details['fname'], $msg);
541 $first = $doc->createElement("first");
542 $first->appendChild(
543 $doc->createTextNode($user_details['fname'])
545 $LicensedPrescriberName->appendChild($first);
546 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
547 $middle = $doc->createElement("middle");
548 $middle->appendChild(
549 $doc->createTextNode($user_details['mname'])
551 $LicensedPrescriberName->appendChild($middle);
552 if ($user_details['title']) {
553 $msg = validation(xl('Midlevel Prescriber Prefix'), $user_details['title'], $msg);
554 $prefix = $doc->createElement("prefix");
555 $prefix->appendChild(
556 $doc->createTextNode($user_details['title'])
558 $LicensedPrescriberName->appendChild($prefix);
561 $b->appendChild($LicensedPrescriberName);
562 $msg = validation(xl('Midlevel Prescriber DEA'), $user_details['federaldrugid'], $msg);
563 $dea = $doc->createElement("dea");
564 $dea->appendChild(
565 $doc->createTextNode($user_details['federaldrugid'])
567 $b->appendChild($dea);
568 if ($user_details['upin']) {
569 $upin = $doc->createElement("upin");
570 $upin->appendChild(
571 $doc->createTextNode($user_details['upin'])
573 $b->appendChild($upin);
576 $licenseNumber = $doc->createElement("licenseNumber");
577 $licenseNumber->appendChild(
578 $doc->createTextNode($user_details['state_license_number'])
580 $b->appendChild($licenseNumber);
581 $r->appendChild($b);
584 function Patient($doc, $r, $pid)
586 global $msg,$warning_msg,$dem_check;
587 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?", array($pid));
588 $b = $doc->createElement("Patient");
589 $b->setAttribute('ID', $patient_data['pid']);
590 $PatientName = $doc->createElement("PatientName");
591 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
592 $patient_data['lname']=trimData($patient_data['lname'], 35);
593 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
594 if ($patient_data['lname']=='') {
595 $dem_check.=xlt("Patient Last name is missing")."<br>";
598 $last = $doc->createElement("last");
599 $last->appendChild(
600 $doc->createTextNode($patient_data['lname'])
602 $PatientName->appendChild($last);
603 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
604 $patient_data['fname']=trimData($patient_data['fname'], 35);
605 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
606 if ($patient_data['fname']=='') {
607 $dem_check.=xlt("Patient First name is missing")."<br>";
610 $first = $doc->createElement("first");
611 $first->appendChild(
612 $doc->createTextNode($patient_data['fname'])
614 $PatientName->appendChild($first);
615 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
616 $patient_data['mname']=trimData($patient_data['mname'], 35);
617 $middle = $doc->createElement("middle");
618 $middle->appendChild(
619 $doc->createTextNode($patient_data['mname'])
621 $PatientName->appendChild($middle);
622 $b->appendChild($PatientName);
623 $PatientAddress = $doc->createElement("PatientAddress");
624 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
625 $patient_data['street']=trimData($patient_data['street'], 35);
626 $msg = validation(xl('Patient Address'), $patient_data['street'], $msg);
627 if (trim($patient_data['street'])=='') {
628 $warning_msg .= "<br>".xlt("Patient Address is missing");
631 $address1 = $doc->createElement("address1");
632 $address1->appendChild(
633 $doc->createTextNode($patient_data['street'])
635 $PatientAddress->appendChild($address1);
636 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
637 if ($patient_data['city']=='') {
638 $dem_check.=xlt("Patient City is missing")."<br>";
641 $city = $doc->createElement("city");
642 $city->appendChild(
643 $doc->createTextNode($patient_data['city'])
645 $PatientAddress->appendChild($city);
646 if ($patient_data['state']) {
647 $state = $doc->createElement("state");
648 $state->appendChild(
649 $doc->createTextNode($patient_data['state'])
651 $PatientAddress->appendChild($state);
654 if ($patient_data['postal_code']) {
655 $zip = $doc->createElement("zip");
656 $zip->appendChild(
657 $doc->createTextNode($patient_data['postal_code'])
659 $PatientAddress->appendChild($zip);
662 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
663 if (trim($patient_data['country_code'])=='' && $GLOBALS['erx_default_patient_country']=='') {
664 $dem_check.=xlt("Patient Country is missing. Also you have not set default Patient Country in Global Settings")."<br>";
665 } elseif (trim($patient_data['country_code'])=='') {
666 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
669 $county_code = substr($patient_data['country_code'], 0, 2);
670 $country = $doc->createElement("country");
671 $country->appendChild(
672 $doc->createTextNode($county_code)
674 $PatientAddress->appendChild($country);
675 $b->appendChild($PatientAddress);
676 $PatientContact = $doc->createElement("PatientContact");
677 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
678 if ($patient_data['phone_home']) {
679 $homeTelephone = $doc->createElement("homeTelephone");
680 $homeTelephone->appendChild(
681 $doc->createTextNode($patient_data['phone_home'])
683 $PatientContact->appendChild($homeTelephone);
686 $b->appendChild($PatientContact);
687 $PatientCharacteristics = $doc->createElement("PatientCharacteristics");
688 if (trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000') {
689 $warning_msg .= "<br>".xlt("Patient Date Of Birth is missing");
692 if ($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000') {
693 $dob = $doc->createElement("dob");
694 $dob->appendChild(
695 $doc->createTextNode($patient_data['date_of_birth'])
697 $PatientCharacteristics->appendChild($dob);
700 if (trim($patient_data['sex'])=='') {
701 $warning_msg .= "<br>".xlt("Patient Gender is missing");
704 if ($patient_data['sex']) {
705 $gender_val=substr($patient_data['sex'], 0, 1);
706 $gender = $doc->createElement("gender");
707 $gender->appendChild(
708 $doc->createTextNode($gender_val)
710 $PatientCharacteristics->appendChild($gender);
713 $b->appendChild($PatientCharacteristics);
714 PatientFreeformHealthplans($doc, $b, $pid);
715 $allergyId=PatientFreeformAllergy($doc, $b, $pid);
716 $r->appendChild($b);
717 return $allergyId;
720 function OutsidePrescription($doc, $r, $pid, $prescid)
722 global $msg;
723 if ($prescid) {
724 $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,
725 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
726 FROM prescriptions AS p
727 LEFT JOIN users AS u ON p.provider_id=u.id
728 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
729 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
730 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
731 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
732 WHERE p.drug <> '' and p.id = ?", array($prescid));
733 $b = $doc->createElement("OutsidePrescription");
734 $externalId = $doc->createElement("externalId");
735 $externalId->appendChild(
736 $doc->createTextNode($prec['prescid'])
738 $b->appendChild($externalId);
739 $date = $doc->createElement("date");
740 $date->appendChild(
741 $doc->createTextNode($prec['date_added'])
743 $b->appendChild($date);
744 $doctorName = $doc->createElement("doctorName");
745 $doctorName->appendChild(
746 $doc->createTextNode($prec['docname'])
748 $b->appendChild($doctorName);
749 $s=stripSpecialCharacter($prec['drug']);
750 $sig = $doc->createElement("drug");
751 $sig->appendChild(
752 $doc->createTextNode(trimData($s, 80))
754 $b->appendChild($sig);
755 $x=stringToNumeric($prec['quantity']);
756 $dispenseNumber = $doc->createElement("dispenseNumber");
757 $dispenseNumber->appendChild(
758 $doc->createTextNode($x[0])
760 $b->appendChild($dispenseNumber);
761 $s=trimData($x[1].$prec['size']." ".$prec['title4']." ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'], 140);
762 $s=stripSpecialCharacter($s);
763 $sig = $doc->createElement("sig");
764 $sig->appendChild(
765 $doc->createTextNode($s)
767 $b->appendChild($sig);
768 $refillCount = $doc->createElement("refillCount");
769 $x=stringToNumeric($prec['per_refill']);
770 $refillCount->appendChild(
771 $doc->createTextNode($x[0])
773 $b->appendChild($refillCount);
774 $prescriptionType = $doc->createElement("prescriptionType");
775 $prescriptionType->appendChild(
776 $doc->createTextNode('reconcile')
778 $b->appendChild($prescriptionType);
779 $r->appendChild($b);
783 function PatientMedication($doc, $r, $pid, $med_limit)
785 global $msg;
786 $active='';
787 if ($GLOBALS['erx_upload_active']==1) {
788 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
791 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
792 and erx_uploaded='0' $active order by enddate limit 0," . escape_limit($med_limit), array($pid));
793 $uploaded_med_arr="";
794 while ($row_med=sqlFetchArray($res_med)) {
795 $uploaded_med_arr[]=$row_med['id'];
796 $b = $doc->createElement("OutsidePrescription");
797 $externalId = $doc->createElement("externalId");
798 $externalId->appendChild(
799 $doc->createTextNode($row_med['id'])
801 $b->appendChild($externalId);
802 $date = $doc->createElement("date");
803 $date->appendChild(
804 $doc->createTextNode($row_med['begdate'])
806 $b->appendChild($date);
807 $doctorName = $doc->createElement("doctorName");
808 $doctorName->appendChild(
809 $doc->createTextNode("")
811 $b->appendChild($doctorName);
812 $row_med['title'] = stripSpecialCharacter($row_med['title']);
813 $sig = $doc->createElement("drug");
814 $sig->appendChild(
815 $doc->createTextNode(trimData($row_med['title'], 80))
817 $b->appendChild($sig);
818 $dispenseNumber = $doc->createElement("dispenseNumber");
819 $dispenseNumber->appendChild(
820 $doc->createTextNode($prec['quantity'])
822 $b->appendChild($dispenseNumber);
823 $sig = $doc->createElement("sig");
824 $sig->appendChild(
825 $doc->createTextNode("")
827 $b->appendChild($sig);
828 $refillCount = $doc->createElement("refillCount");
829 $refillCount->appendChild(
830 $doc->createTextNode("")
832 $b->appendChild($refillCount);
833 $prescriptionType = $doc->createElement("prescriptionType");
834 $prescriptionType->appendChild(
835 $doc->createTextNode('reconcile')
837 $b->appendChild($prescriptionType);
838 $r->appendChild($b);
841 return $uploaded_med_arr;
844 function PatientFreeformAllergy($doc, $r, $pid)
846 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
847 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
848 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));
849 $allergyId=array();
850 while ($row=sqlFetchArray($res)) {
851 $val=array();
852 $val['id']=$row['id'];
853 $val['title1']=$row['title1'];
854 $val['title2']=$row['title2'];
855 $val['comments']=$row['comments'];
856 $b = $doc->createElement("PatientFreeformAllergy");
857 $b->setAttribute('ID', $val['id']);
858 if ($val['title1']) {
859 $allergyName = $doc->createElement("allergyName");
860 $allergyName->appendChild(
861 $doc->createTextNode(trimData(stripSpecialCharacter($val['title1']), 70))
863 $b->appendChild($allergyName);
866 if ($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')) {
867 $allergySeverityTypeID = $doc->createElement("allergySeverityTypeID");
868 $allergySeverityTypeID->appendChild(
869 $doc->createTextNode($val['title2'])
871 $b->appendChild($allergySeverityTypeID);
874 if ($val['comments']) {
875 $allergyComment = $doc->createElement("allergyComment");
876 $allergyComment->appendChild(
877 $doc->createTextNode(trimData(stripSpecialCharacter($val['comments']), 200))
879 $b->appendChild($allergyComment);
882 $r->appendChild($b);
883 $allergyId[]=$row['id'];
886 return $allergyId;
889 function PatientFreeformHealthplans($doc, $r, $pid)
891 $resource = sqlStatement(
892 'SELECT
893 `ins`.`name`
894 FROM (
895 SELECT
896 `id`.`type`,
897 `ic`.`name`
898 FROM `insurance_data` AS `id`
899 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
900 WHERE `id`.`pid` = ?
901 AND `id`.`subscriber_relationship` = \'self\'
902 AND `id`.`provider` > 0
903 ORDER BY `id`.`date` DESC
904 ) AS `ins`
905 GROUP BY `ins`.`type`;',
906 array($pid)
909 while ($row = sqlFetchArray($resource)) {
910 $healthplanName = $doc->createElement('healthplanName');
911 $healthplanName->appendChild($doc->createTextNode(
912 stripSpecialCharacter(trimData($row['name'], 35))
915 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
916 $patientFreeformHealthplans->appendChild($healthplanName);
918 $r->appendChild($patientFreeformHealthplans);
922 function PrescriptionRenewalResponse($doc, $r, $pid)
924 $b = $doc->createElement("PrescriptionRenewalResponse");
925 $renewalRequestIdentifier = $doc->createElement("renewalRequestIdentifier");
926 $renewalRequestIdentifier->appendChild(
927 $doc->createTextNode('cbf51649-ce3c-44b8-8f91-6fda121a353d')
929 $b->appendChild($renewalRequestIdentifier);
930 $responseCode = $doc->createElement("responseCode");
931 $responseCode->appendChild(
932 $doc->createTextNode('Undetermined')
934 $b->appendChild($responseCode);
935 $r->appendChild($b);
938 function checkError($xml)
940 $ch = curl_init($xml);
942 $data = array('RxInput' => $xml);
944 curl_setopt($ch, CURLOPT_URL, getErxPath());
945 curl_setopt($ch, CURLOPT_POST, 1);
946 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
947 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
948 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
949 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
950 //curl_setopt($ch, CURLOPT_HEADER, 0);
951 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
952 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
953 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
954 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
955 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
957 $result=curl_exec($ch) or die(curl_error($ch)) ;
958 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $error_message);
959 if (strpos($result, 'RxEntry.aspx')) {
960 erx_error_log($xml);
961 erx_error_log($result);
964 $arr=explode('Error', $error_message[1]);
965 //echo "Te: ".count($arr);
966 //print_r($arr);
967 if (count($arr)==1) {
968 echo nl2br($error_message[1]);
969 } else {
970 for ($i=1; $i<count($arr); $i++) {
971 echo $arr[$i]."<br><br>";
975 curl_close($ch);
976 if (strpos($result, 'RxEntry.aspx')) {
977 return '1';
978 } else {
979 return '0';
983 function erx_error_log($message)
985 $date = date("Y-m-d");
986 if (!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error')) {
987 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error', 0777, true);
990 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
991 $f=fopen($filename, 'a');
992 fwrite($f, date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
993 fclose($f);
996 function stripStrings($str, $pattern)
998 $result = $str;
999 foreach ($pattern as $key => $value) {
1000 $result = preg_replace("/$key/", $value, $result);
1003 return $result;