Fix: delete entry in immunization (#7346)
[openemr.git] / interface / eRx_xml.php
blobfe237d20f9d54c82e00e7168c9328a21cafdefc7
1 <?php
3 /**
4 * interface/eRx_xml.php Functions for interacting with NewCrop communications.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Eldho Chacko <eldho@zhservices.com>
9 * @author Vinish K <vinish@zhservices.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2011 ZMG LLC <sam@zhservices.com>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @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;
18 use OpenEMR\Services\VersionService;
20 $facilityService = new FacilityService();
22 function getErxPath()
24 return $GLOBALS['erx_newcrop_path'];
27 function getErxSoapPath()
29 return $GLOBALS['erx_newcrop_path_soap'];
32 function getErxCredentials()
34 $cred = array();
35 $cred[] = $GLOBALS['erx_account_partner_name'];
36 $cred[] = $GLOBALS['erx_account_name'];
37 $cryptoGen = new CryptoGen();
38 $cred[] = $cryptoGen->decryptStandard($GLOBALS['erx_account_password']);
40 return $cred;
43 function validation($val_check, $val, $msg)
45 if (!$val) {
46 $msg .= $val_check . ' ' . xl('missing') . '<br />';
49 return $msg;
52 function stripSpecialCharacterFacility($str)
54 $str = preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
55 return $str;
58 function stripSpecialCharacter($str)
60 $str = preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
61 return $str;
64 function stripPhoneSlashes($str)
66 $str = preg_replace('/-/', '', $str);
67 return $str;
70 function trimData($str, $length)
72 $str = substr($str, 0, ($length - 1));
73 return $str;
76 function stringToNumeric($str)
78 if (is_numeric($str)) {
79 return array($str,"");
80 } else {
81 for ($i = 0; $i < strlen($str); $i++) {
82 $x = substr($str, $i, 1);
83 if (is_numeric($x) && !$txt) {
84 $num .= $x;
85 } else {
86 $txt .= $x;
90 return array($num,$txt);
93 $str = substr($str, 0, ($length - 1));
94 return $str;
96 function credentials($doc, $r)
98 global $msg;
99 $cred = getErxCredentials();
100 $msg = validation(xl('Partner Name'), $cred['0'], $msg);
101 $b = $doc->createElement("Credentials");
102 $partnerName = $doc->createElement("partnerName");
103 $partnerName->appendChild(
104 $doc->createTextNode($cred['0'])
106 $b->appendChild($partnerName);
107 $msg = validation(xl('ERX Name'), $cred['1'], $msg);
108 $name = $doc->createElement("name");
109 $name->appendChild(
110 $doc->createTextNode($cred['1'])
112 $b->appendChild($name);
113 $msg = validation(xl('ERX Password'), $cred['2'], $msg);
114 $password = $doc->createElement("password");
115 $password->appendChild(
116 $doc->createTextNode($cred['2'])
118 $b->appendChild($password);
119 $productName = $doc->createElement("productName");
120 $productName->appendChild(
121 $doc->createTextNode('OpenEMR')
123 $b->appendChild($productName);
124 $productVersion = $doc->createElement("productVersion");
125 $productVersion->appendChild(
126 $doc->createTextNode((new VersionService())->asString())
128 $b->appendChild($productVersion);
129 $r->appendChild($b);
132 function user_role($doc, $r)
134 global $msg;
135 $userRole = sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
136 if (!$userRole['newcrop_user_role']) {
137 echo xlt('Unauthorized access to ePrescription');
138 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';
152 $msg = validation(xl('ERX User'), $userRole['eRxUser'], $msg);
153 $b = $doc->createElement("UserRole");
154 $user = $doc->createElement("user");
155 $user->appendChild(
156 $doc->createTextNode($userRole['eRxUser'])
158 $b->appendChild($user);
159 $msg = validation(xl('ERX Role'), $userRole['newcrop_user_role'], $msg);
160 $role = $doc->createElement("role");
161 $role->appendChild(
162 $doc->createTextNode($userRole['newcrop_user_role'])
164 $b->appendChild($role);
165 $r->appendChild($b);
168 function destination($doc, $r, string $page = null, $pid)
170 global $msg,$page;
171 $userRole = sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
172 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
173 if (!$page) {
174 $page = 'compose';
175 if ($userRole['newcrop_user_role'] == 'admin') {
176 $page = 'admin';
177 } elseif ($userRole['newcrop_user_role'] == 'manager') {
178 $page = 'manager';
182 $b = $doc->createElement("Destination");
183 $requestedPage = $doc->createElement("requestedPage");
184 $requestedPage->appendChild(
185 $doc->createTextNode($page)
187 $b->appendChild($requestedPage);
188 $r->appendChild($b);
191 function account($doc, $r)
193 global $msg, $facilityService;
194 $erxSiteID = $facilityService->getPrimaryBusinessEntity();
195 if (!$erxSiteID['federal_ein']) {
196 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.");
197 die;
200 $b = $doc->createElement("Account");
201 $b->setAttribute('ID', $GLOBALS['erx_account_id']);
202 $erxSiteID['name'] = stripSpecialCharacterFacility($erxSiteID['name']);
203 $erxSiteID['name'] = trimData($erxSiteID['name'], 35);
204 $msg = validation(xl('Account Name'), $erxSiteID['name'], $msg);
205 $accountName = $doc->createElement("accountName");
206 $accountName->appendChild(
207 $doc->createTextNode($erxSiteID['name'])
209 $b->appendChild($accountName);
210 $msg = validation(xl('Site ID'), $_SESSION['site_id'], $msg);
211 $siteID = $doc->createElement("siteID");
212 $siteID->appendChild(
213 $doc->createTextNode($erxSiteID['federal_ein'])
215 $b->appendChild($siteID);
216 $erxSiteID['street'] = stripSpecialCharacterFacility($erxSiteID['street']);
217 $erxSiteID['street'] = trimData($erxSiteID['street'], 35);
218 $AccountAddress = $doc->createElement("AccountAddress");
219 $msg = validation(xl('Facility Street'), $erxSiteID['street'], $msg);
220 $address1 = $doc->createElement("address1");
221 $address1->appendChild(
222 $doc->createTextNode($erxSiteID['street'])
224 $AccountAddress->appendChild($address1);
225 $msg = validation(xl('Facility City'), $erxSiteID['city'], $msg);
226 $city = $doc->createElement("city");
227 $city->appendChild(
228 $doc->createTextNode($erxSiteID['city'])
230 $AccountAddress->appendChild($city);
231 $msg = validation(xl('Facility State'), $erxSiteID['state'], $msg);
232 $state = $doc->createElement("state");
233 $state->appendChild(
234 $doc->createTextNode($erxSiteID['state'])
236 $AccountAddress->appendChild($state);
237 $jasonbigzip = $erxSiteID['postal_code'];
238 $jasonbigzip = preg_replace('/[^0-9]/', '', $jasonbigzip);
239 if (strlen($jasonbigzip) >= 5) {
240 $jasonzip = substr($jasonbigzip, 0, 5);
241 $zip4 = substr($jasonbigzip, 5, 4);
242 } else {
243 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
246 $zip = $doc->createElement("zip");
247 $zip->appendChild(
248 $doc->createTextNode($jasonzip)
250 $AccountAddress->appendChild($zip);
251 if (strlen($zip4) == 4) {
252 $zipFour = $doc->createElement("zip4");
253 $zipFour->appendChild(
254 $doc->createTextNode($zip4)
256 $AccountAddress->appendChild($zipFour);
259 $msg = validation(xl('Facility Country code'), $erxSiteID['country_code'], $msg);
260 $county_code = substr($erxSiteID['country_code'], 0, 2);
261 $country = $doc->createElement("country");
262 $country->appendChild(
263 $doc->createTextNode($county_code)
265 $AccountAddress->appendChild($country);
266 $b->appendChild($AccountAddress);
267 $msg = validation(xl('Facility Phone'), $erxSiteID['phone'], $msg);
268 $accountPrimaryPhoneNumber = $doc->createElement("accountPrimaryPhoneNumber");
269 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
270 $accountPrimaryPhoneNumber->appendChild(
271 $doc->createTextNode($erxSiteID['phone'])
273 $b->appendChild($accountPrimaryPhoneNumber);
274 $msg = validation(xl('Facility Fax'), $erxSiteID['fax'], $msg);
275 $accountPrimaryFaxNumber = $doc->createElement("accountPrimaryFaxNumber");
276 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
277 $accountPrimaryFaxNumber->appendChild(
278 $doc->createTextNode($erxSiteID['fax'])
280 $b->appendChild($accountPrimaryFaxNumber);
281 $r->appendChild($b);
284 function location($doc, $r)
286 global $msg;
287 $userRole = sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?", array($_SESSION['authUser']));
288 $b = $doc->createElement("Location");
289 $b->setAttribute('ID', $userRole['id']);
290 $userRole['name'] = stripSpecialCharacterFacility($userRole['name']);
291 $userRole['name'] = trimData($userRole['name'], 35);
292 $locationName = $doc->createElement('locationName');
293 $locationName->appendChild(
294 $doc->createTextNode($userRole['name'])
296 $b->appendChild($locationName);
297 $userRole['street'] = stripSpecialCharacterFacility($userRole['street']);
298 $userRole['street'] = trimData($userRole['street'], 35);
299 $LocationAddress = $doc->createElement('LocationAddress');
300 if ($userRole['street']) {
301 $address1 = $doc->createElement('address1');
302 $address1->appendChild(
303 $doc->createTextNode($userRole['street'])
305 $LocationAddress->appendChild($address1);
308 if ($userRole['city']) {
309 $city = $doc->createElement('city');
310 $city->appendChild(
311 $doc->createTextNode($userRole['city'])
313 $LocationAddress->appendChild($city);
316 if ($userRole['state']) {
317 $state = $doc->createElement('state');
318 $state->appendChild(
319 $doc->createTextNode($userRole['state'])
321 $LocationAddress->appendChild($state);
324 $jasonbigzip = $userRole['postal_code'];
325 $jasonbigzip = preg_replace('/[^0-9]/', '', $jasonbigzip);
326 if (strlen($jasonbigzip) >= 5) {
327 $jasonzip = substr($jasonbigzip, 0, 5);
328 $zip4 = substr($jasonbigzip, 5, 4);
329 } else {
330 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
333 $zip = $doc->createElement("zip");
334 $zip->appendChild(
335 $doc->createTextNode($jasonzip)
337 $LocationAddress->appendChild($zip);
338 if (strlen($zip4) == 4) {
339 $zipFour = $doc->createElement("zip4");
340 $zipFour->appendChild(
341 $doc->createTextNode($zip4)
343 $LocationAddress->appendChild($zipFour);
346 if ($userRole['country_code']) {
347 $county_code = substr($userRole['country_code'], 0, 2);
348 $country = $doc->createElement('country');
349 $country->appendChild(
350 $doc->createTextNode($county_code)
352 $LocationAddress->appendChild($country);
355 $b->appendChild($LocationAddress);
356 if ($userRole['phone']) {
357 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
358 $primaryPhoneNumber = $doc->createElement('primaryPhoneNumber');
359 $primaryPhoneNumber->appendChild(
360 $doc->createTextNode($userRole['phone'])
362 $b->appendChild($primaryPhoneNumber);
365 if ($userRole['fax']) {
366 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
367 $primaryFaxNumber = $doc->createElement('primaryFaxNumber');
368 $primaryFaxNumber->appendChild(
369 $doc->createTextNode($userRole['fax'])
371 $b->appendChild($primaryFaxNumber);
374 $pharmacyContactNumber = $doc->createElement('pharmacyContactNumber');
375 $pharmacyContactNumber->appendChild(
376 $doc->createTextNode($userRole['phone'])
378 $b->appendChild($pharmacyContactNumber);
379 $r->appendChild($b);
382 function LicensedPrescriber($doc, $r)
384 global $msg;
385 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
386 $b = $doc->createElement("LicensedPrescriber");
387 $b->setAttribute('ID', $user_details['npi']);
388 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
389 $user_details['lname'] = stripSpecialCharacter($user_details['lname']);
390 $msg = validation(xl('LicensedPrescriber Last name'), $user_details['lname'], $msg);
391 $last = $doc->createElement("last");
392 $last->appendChild(
393 $doc->createTextNode($user_details['lname'])
395 $LicensedPrescriberName->appendChild($last);
396 $user_details['fname'] = stripSpecialCharacter($user_details['fname']);
397 $msg = validation(xl('User First name'), $user_details['fname'], $msg);
398 $first = $doc->createElement("first");
399 $first->appendChild(
400 $doc->createTextNode($user_details['fname'])
402 $LicensedPrescriberName->appendChild($first);
403 $user_details['mname'] = stripSpecialCharacter($user_details['mname']);
404 $middle = $doc->createElement("middle");
405 $middle->appendChild(
406 $doc->createTextNode($user_details['mname'])
408 $LicensedPrescriberName->appendChild($middle);
409 $b->appendChild($LicensedPrescriberName);
410 $msg = validation(xl('DEA'), $user_details['federaldrugid'], $msg);
411 $dea = $doc->createElement("dea");
412 $dea->appendChild(
413 $doc->createTextNode($user_details['federaldrugid'])
415 $b->appendChild($dea);
416 if ($user_details['upin']) {
417 $upin = $doc->createElement("upin");
418 $upin->appendChild(
419 $doc->createTextNode($user_details['upin'])
421 $b->appendChild($upin);
424 $licenseNumber = $doc->createElement("licenseNumber");
425 $licenseNumber->appendChild(
426 $doc->createTextNode($user_details['state_license_number'])
428 $b->appendChild($licenseNumber);
429 $msg = validation(xl('LicensedPrescriber NPI'), $user_details['npi'], $msg);
430 $npi = $doc->createElement("npi");
431 $npi->appendChild(
432 $doc->createTextNode($user_details['npi'])
434 $b->appendChild($npi);
435 $r->appendChild($b);
438 function Staff($doc, $r)
440 global $msg;
441 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
442 $b = $doc->createElement("Staff");
443 $b->setAttribute('ID', $user_details['username']);
444 $StaffName = $doc->createElement("StaffName");
445 $user_details['lname'] = stripSpecialCharacter($user_details['lname']);
446 $last = $doc->createElement("last");
447 $last->appendChild(
448 $doc->createTextNode($user_details['lname'])
450 $StaffName->appendChild($last);
451 $user_details['fname'] = stripSpecialCharacter($user_details['fname']);
452 $first = $doc->createElement("first");
453 $first->appendChild(
454 $doc->createTextNode($user_details['fname'])
456 $StaffName->appendChild($first);
457 $user_details['mname'] = stripSpecialCharacter($user_details['mname']);
458 $middle = $doc->createElement("middle");
459 $middle->appendChild(
460 $doc->createTextNode($user_details['mname'])
462 $StaffName->appendChild($middle);
463 $b->appendChild($StaffName);
464 $license = $doc->createElement("license");
465 $license->appendChild(
466 $doc->createTextNode($user_details['license'])
468 $b->appendChild($license);
469 $r->appendChild($b);
472 function SupervisingDoctor($doc, $r)
474 global $msg;
475 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
476 $b = $doc->createElement("SupervisingDoctor");
477 $b->setAttribute('ID', $user_details['npi']);
478 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
479 $user_details['lname'] = stripSpecialCharacter($user_details['lname']);
480 $msg = validation(xl('Supervising Doctor Last name'), $user_details['lname'], $msg);
481 $last = $doc->createElement("last");
482 $last->appendChild(
483 $doc->createTextNode($user_details['lname'])
485 $LicensedPrescriberName->appendChild($last);
486 $user_details['fname'] = stripSpecialCharacter($user_details['fname']);
487 $msg = validation(xl('Supervising Doctor First name'), $user_details['fname'], $msg);
488 $first = $doc->createElement("first");
489 $first->appendChild(
490 $doc->createTextNode($user_details['fname'])
492 $LicensedPrescriberName->appendChild($first);
493 $user_details['mname'] = stripSpecialCharacter($user_details['mname']);
494 $middle = $doc->createElement("middle");
495 $middle->appendChild(
496 $doc->createTextNode($user_details['mname'])
498 $LicensedPrescriberName->appendChild($middle);
499 $b->appendChild($LicensedPrescriberName);
500 $msg = validation(xl('Supervising Doctor DEA'), $user_details['federaldrugid'], $msg);
501 $dea = $doc->createElement("dea");
502 $dea->appendChild(
503 $doc->createTextNode($user_details['federaldrugid'])
505 $b->appendChild($dea);
506 if ($user_details['upin']) {
507 $upin = $doc->createElement("upin");
508 $upin->appendChild(
509 $doc->createTextNode($user_details['upin'])
511 $b->appendChild($upin);
514 $licenseNumber = $doc->createElement("licenseNumber");
515 $licenseNumber->appendChild(
516 $doc->createTextNode($user_details['state_license_number'])
518 $b->appendChild($licenseNumber);
519 $msg = validation(xl('Supervising Doctor NPI'), $user_details['npi'], $msg);
520 $npi = $doc->createElement("npi");
521 $npi->appendChild(
522 $doc->createTextNode($user_details['npi'])
524 $b->appendChild($npi);
525 $r->appendChild($b);
528 function MidlevelPrescriber($doc, $r)
530 global $msg;
531 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
532 $b = $doc->createElement("MidlevelPrescriber");
533 $b->setAttribute('ID', $user_details['npi']);
534 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
535 $user_details['lname'] = stripSpecialCharacter($user_details['lname']);
536 $msg = validation(xl('Midlevel Prescriber Last name'), $user_details['lname'], $msg);
537 $last = $doc->createElement("last");
538 $last->appendChild(
539 $doc->createTextNode($user_details['lname'])
541 $LicensedPrescriberName->appendChild($last);
542 $user_details['fname'] = stripSpecialCharacter($user_details['fname']);
543 $msg = validation(xl('Midlevel Prescriber First name'), $user_details['fname'], $msg);
544 $first = $doc->createElement("first");
545 $first->appendChild(
546 $doc->createTextNode($user_details['fname'])
548 $LicensedPrescriberName->appendChild($first);
549 $user_details['mname'] = stripSpecialCharacter($user_details['mname']);
550 $middle = $doc->createElement("middle");
551 $middle->appendChild(
552 $doc->createTextNode($user_details['mname'])
554 $LicensedPrescriberName->appendChild($middle);
555 if ($user_details['title']) {
556 $msg = validation(xl('Midlevel Prescriber Prefix'), $user_details['title'], $msg);
557 $prefix = $doc->createElement("prefix");
558 $prefix->appendChild(
559 $doc->createTextNode($user_details['title'])
561 $LicensedPrescriberName->appendChild($prefix);
564 $b->appendChild($LicensedPrescriberName);
565 $msg = validation(xl('Midlevel Prescriber DEA'), $user_details['federaldrugid'], $msg);
566 $dea = $doc->createElement("dea");
567 $dea->appendChild(
568 $doc->createTextNode($user_details['federaldrugid'])
570 $b->appendChild($dea);
571 if ($user_details['upin']) {
572 $upin = $doc->createElement("upin");
573 $upin->appendChild(
574 $doc->createTextNode($user_details['upin'])
576 $b->appendChild($upin);
579 $licenseNumber = $doc->createElement("licenseNumber");
580 $licenseNumber->appendChild(
581 $doc->createTextNode($user_details['state_license_number'])
583 $b->appendChild($licenseNumber);
584 $r->appendChild($b);
587 function Patient($doc, $r, $pid)
589 global $msg,$warning_msg,$dem_check;
590 $patient_data = sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?", array($pid));
591 $b = $doc->createElement("Patient");
592 $b->setAttribute('ID', $patient_data['pid']);
593 $PatientName = $doc->createElement("PatientName");
594 $patient_data['lname'] = stripSpecialCharacter($patient_data['lname']);
595 $patient_data['lname'] = trimData($patient_data['lname'], 35);
596 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
597 if ($patient_data['lname'] == '') {
598 $dem_check .= xlt("Patient Last name is missing") . "<br />";
601 $last = $doc->createElement("last");
602 $last->appendChild(
603 $doc->createTextNode($patient_data['lname'])
605 $PatientName->appendChild($last);
606 $patient_data['fname'] = stripSpecialCharacter($patient_data['fname']);
607 $patient_data['fname'] = trimData($patient_data['fname'], 35);
608 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
609 if ($patient_data['fname'] == '') {
610 $dem_check .= xlt("Patient First name is missing") . "<br />";
613 $first = $doc->createElement("first");
614 $first->appendChild(
615 $doc->createTextNode($patient_data['fname'])
617 $PatientName->appendChild($first);
618 $patient_data['mname'] = stripSpecialCharacter($patient_data['mname']);
619 $patient_data['mname'] = trimData($patient_data['mname'], 35);
620 $middle = $doc->createElement("middle");
621 $middle->appendChild(
622 $doc->createTextNode($patient_data['mname'])
624 $PatientName->appendChild($middle);
625 $b->appendChild($PatientName);
626 $PatientAddress = $doc->createElement("PatientAddress");
627 $patient_data['street'] = stripSpecialCharacter($patient_data['street']);
628 $patient_data['street'] = trimData($patient_data['street'], 35);
629 $msg = validation(xl('Patient Address'), $patient_data['street'], $msg);
630 if (trim($patient_data['street']) == '') {
631 $warning_msg .= "<br />" . xlt("Patient Address is missing");
634 $address1 = $doc->createElement("address1");
635 $address1->appendChild(
636 $doc->createTextNode($patient_data['street'])
638 $PatientAddress->appendChild($address1);
639 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
640 if ($patient_data['city'] == '') {
641 $dem_check .= xlt("Patient City is missing") . "<br />";
644 $city = $doc->createElement("city");
645 $city->appendChild(
646 $doc->createTextNode($patient_data['city'])
648 $PatientAddress->appendChild($city);
649 if ($patient_data['state']) {
650 $state = $doc->createElement("state");
651 $state->appendChild(
652 $doc->createTextNode($patient_data['state'])
654 $PatientAddress->appendChild($state);
657 if ($patient_data['postal_code']) {
658 $zip = $doc->createElement("zip");
659 $zip->appendChild(
660 $doc->createTextNode($patient_data['postal_code'])
662 $PatientAddress->appendChild($zip);
665 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
666 if (trim($patient_data['country_code']) == '' && $GLOBALS['erx_default_patient_country'] == '') {
667 $dem_check .= xlt("Patient Country is missing. Also you have not set default Patient Country in Global Settings") . "<br />";
668 } elseif (trim($patient_data['country_code']) == '') {
669 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
672 $county_code = substr($patient_data['country_code'], 0, 2);
673 $country = $doc->createElement("country");
674 $country->appendChild(
675 $doc->createTextNode($county_code)
677 $PatientAddress->appendChild($country);
678 $b->appendChild($PatientAddress);
679 $PatientContact = $doc->createElement("PatientContact");
680 $patient_data['phone_home'] = stripPhoneSlashes($patient_data['phone_home']);
681 if ($patient_data['phone_home']) {
682 $homeTelephone = $doc->createElement("homeTelephone");
683 $homeTelephone->appendChild(
684 $doc->createTextNode($patient_data['phone_home'])
686 $PatientContact->appendChild($homeTelephone);
689 $b->appendChild($PatientContact);
690 $PatientCharacteristics = $doc->createElement("PatientCharacteristics");
691 if (trim($patient_data['date_of_birth']) == '' || $patient_data['date_of_birth'] == '00000000') {
692 $warning_msg .= "<br />" . xlt("Patient Date Of Birth is missing");
695 if ($patient_data['date_of_birth'] && $patient_data['date_of_birth'] != '00000000') {
696 $dob = $doc->createElement("dob");
697 $dob->appendChild(
698 $doc->createTextNode($patient_data['date_of_birth'])
700 $PatientCharacteristics->appendChild($dob);
703 if (trim($patient_data['sex']) == '') {
704 $warning_msg .= "<br />" . xlt("Patient Gender is missing");
707 if ($patient_data['sex']) {
708 $gender_val = substr($patient_data['sex'], 0, 1);
709 $gender = $doc->createElement("gender");
710 $gender->appendChild(
711 $doc->createTextNode($gender_val)
713 $PatientCharacteristics->appendChild($gender);
716 $b->appendChild($PatientCharacteristics);
717 PatientFreeformHealthplans($doc, $b, $pid);
718 $allergyId = PatientFreeformAllergy($doc, $b, $pid);
719 $r->appendChild($b);
720 return $allergyId;
723 function OutsidePrescription($doc, $r, $pid, $prescid)
725 global $msg;
726 if ($prescid) {
727 $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,
728 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
729 FROM prescriptions AS p
730 LEFT JOIN users AS u ON p.provider_id=u.id
731 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
732 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
733 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
734 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
735 WHERE p.drug <> '' and p.id = ?", array($prescid));
736 $b = $doc->createElement("OutsidePrescription");
737 $externalId = $doc->createElement("externalId");
738 $externalId->appendChild(
739 $doc->createTextNode($prec['prescid'])
741 $b->appendChild($externalId);
742 $date = $doc->createElement("date");
743 $date->appendChild(
744 $doc->createTextNode($prec['date_added'])
746 $b->appendChild($date);
747 $doctorName = $doc->createElement("doctorName");
748 $doctorName->appendChild(
749 $doc->createTextNode($prec['docname'])
751 $b->appendChild($doctorName);
752 $s = stripSpecialCharacter($prec['drug']);
753 $sig = $doc->createElement("drug");
754 $sig->appendChild(
755 $doc->createTextNode(trimData($s, 80))
757 $b->appendChild($sig);
758 $x = stringToNumeric($prec['quantity']);
759 $dispenseNumber = $doc->createElement("dispenseNumber");
760 $dispenseNumber->appendChild(
761 $doc->createTextNode($x[0])
763 $b->appendChild($dispenseNumber);
764 $s = trimData($x[1] . $prec['size'] . " " . $prec['title4'] . " " . $prec['dosage'] . " In " . $prec['title1'] . " " . $prec['title2'] . " " . $prec['title3'], 140);
765 $s = stripSpecialCharacter($s);
766 $sig = $doc->createElement("sig");
767 $sig->appendChild(
768 $doc->createTextNode($s)
770 $b->appendChild($sig);
771 $refillCount = $doc->createElement("refillCount");
772 $x = stringToNumeric($prec['per_refill']);
773 $refillCount->appendChild(
774 $doc->createTextNode($x[0])
776 $b->appendChild($refillCount);
777 $prescriptionType = $doc->createElement("prescriptionType");
778 $prescriptionType->appendChild(
779 $doc->createTextNode('reconcile')
781 $b->appendChild($prescriptionType);
782 $r->appendChild($b);
786 function PatientMedication($doc, $r, $pid, $med_limit)
788 global $msg;
789 $active = '';
790 if ($GLOBALS['erx_upload_active'] == 1) {
791 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
794 $res_med = sqlStatement("select * from lists where type='medication' and pid=? and title<>''
795 and erx_uploaded='0' $active order by enddate limit 0," . escape_limit($med_limit), array($pid));
796 $uploaded_med_arr = "";
797 while ($row_med = sqlFetchArray($res_med)) {
798 $uploaded_med_arr[] = $row_med['id'];
799 $b = $doc->createElement("OutsidePrescription");
800 $externalId = $doc->createElement("externalId");
801 $externalId->appendChild(
802 $doc->createTextNode($row_med['id'])
804 $b->appendChild($externalId);
805 $date = $doc->createElement("date");
806 $date->appendChild(
807 $doc->createTextNode($row_med['begdate'])
809 $b->appendChild($date);
810 $doctorName = $doc->createElement("doctorName");
811 $doctorName->appendChild(
812 $doc->createTextNode("")
814 $b->appendChild($doctorName);
815 $row_med['title'] = stripSpecialCharacter($row_med['title']);
816 $sig = $doc->createElement("drug");
817 $sig->appendChild(
818 $doc->createTextNode(trimData($row_med['title'], 80))
820 $b->appendChild($sig);
821 $dispenseNumber = $doc->createElement("dispenseNumber");
822 $dispenseNumber->appendChild(
823 $doc->createTextNode($prec['quantity'])
825 $b->appendChild($dispenseNumber);
826 $sig = $doc->createElement("sig");
827 $sig->appendChild(
828 $doc->createTextNode("")
830 $b->appendChild($sig);
831 $refillCount = $doc->createElement("refillCount");
832 $refillCount->appendChild(
833 $doc->createTextNode("")
835 $b->appendChild($refillCount);
836 $prescriptionType = $doc->createElement("prescriptionType");
837 $prescriptionType->appendChild(
838 $doc->createTextNode('reconcile')
840 $b->appendChild($prescriptionType);
841 $r->appendChild($b);
844 return $uploaded_med_arr;
847 function PatientFreeformAllergy($doc, $r, $pid)
849 $res = sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
850 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
851 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));
852 $allergyId = array();
853 while ($row = sqlFetchArray($res)) {
854 $val = array();
855 $val['id'] = $row['id'];
856 $val['title1'] = $row['title1'];
857 $val['title2'] = $row['title2'];
858 $val['comments'] = $row['comments'];
859 $b = $doc->createElement("PatientFreeformAllergy");
860 $b->setAttribute('ID', $val['id']);
861 if ($val['title1']) {
862 $allergyName = $doc->createElement("allergyName");
863 $allergyName->appendChild(
864 $doc->createTextNode(trimData(stripSpecialCharacter($val['title1']), 70))
866 $b->appendChild($allergyName);
869 if ($val['title2'] && ($val['title2'] == 'Mild' || $val['title2'] == 'Moderate' || $val['title2'] == 'Severe')) {
870 $allergySeverityTypeID = $doc->createElement("allergySeverityTypeID");
871 $allergySeverityTypeID->appendChild(
872 $doc->createTextNode($val['title2'])
874 $b->appendChild($allergySeverityTypeID);
877 if ($val['comments']) {
878 $allergyComment = $doc->createElement("allergyComment");
879 $allergyComment->appendChild(
880 $doc->createTextNode(trimData(stripSpecialCharacter($val['comments']), 200))
882 $b->appendChild($allergyComment);
885 $r->appendChild($b);
886 $allergyId[] = $row['id'];
889 return $allergyId;
892 function PatientFreeformHealthplans($doc, $r, $pid)
894 $resource = sqlStatement(
895 'SELECT
896 `ins`.`name`
897 FROM (
898 SELECT
899 `id`.`type`,
900 `ic`.`name`
901 FROM `insurance_data` AS `id`
902 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
903 WHERE `id`.`pid` = ?
904 AND `id`.`subscriber_relationship` = \'self\'
905 AND `id`.`provider` > 0
906 ORDER BY `id`.`date` DESC
907 ) AS `ins`
908 GROUP BY `ins`.`type`;',
909 array($pid)
912 while ($row = sqlFetchArray($resource)) {
913 $healthplanName = $doc->createElement('healthplanName');
914 $healthplanName->appendChild($doc->createTextNode(
915 stripSpecialCharacter(trimData($row['name'], 35))
918 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
919 $patientFreeformHealthplans->appendChild($healthplanName);
921 $r->appendChild($patientFreeformHealthplans);
925 function PrescriptionRenewalResponse($doc, $r, $pid)
927 $b = $doc->createElement("PrescriptionRenewalResponse");
928 $renewalRequestIdentifier = $doc->createElement("renewalRequestIdentifier");
929 $renewalRequestIdentifier->appendChild(
930 $doc->createTextNode('cbf51649-ce3c-44b8-8f91-6fda121a353d')
932 $b->appendChild($renewalRequestIdentifier);
933 $responseCode = $doc->createElement("responseCode");
934 $responseCode->appendChild(
935 $doc->createTextNode('Undetermined')
937 $b->appendChild($responseCode);
938 $r->appendChild($b);
941 function checkError($xml)
943 $ch = curl_init($xml);
945 $data = array('RxInput' => $xml);
947 curl_setopt($ch, CURLOPT_URL, getErxPath());
948 curl_setopt($ch, CURLOPT_POST, 1);
949 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=" . $xml);
950 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
951 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
952 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
953 //curl_setopt($ch, CURLOPT_HEADER, 0);
954 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
955 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
956 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
957 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
958 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
960 $result = curl_exec($ch) or die(curl_error($ch)) ;
961 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $error_message);
962 if (strpos($result, 'RxEntry.aspx')) {
963 erx_error_log($xml);
964 erx_error_log($result);
967 $arr = explode('Error', $error_message[1]);
968 //echo "Te: ".count($arr);
969 //print_r($arr);
970 if (count($arr) == 1) {
971 echo nl2br($error_message[1]);
972 } else {
973 for ($i = 1; $i < count($arr); $i++) {
974 echo $arr[$i] . "<br /><br />";
978 curl_close($ch);
979 if (strpos($result, 'RxEntry.aspx')) {
980 return '1';
981 } else {
982 return '0';
986 function erx_error_log($message)
988 $date = date("Y-m-d");
989 if (!is_dir($GLOBALS['OE_SITE_DIR'] . '/documents/erx_error')) {
990 mkdir($GLOBALS['OE_SITE_DIR'] . '/documents/erx_error', 0777, true);
993 $filename = $GLOBALS['OE_SITE_DIR'] . "/documents/erx_error/erx_error" . "-" . $date . ".log";
994 $f = fopen($filename, 'a');
995 fwrite($f, date("Y-m-d H:i:s") . " ==========> " . $message . "\r\n");
996 fclose($f);
999 function stripStrings($str, $pattern)
1001 $result = $str;
1002 foreach ($pattern as $key => $value) {
1003 $result = preg_replace("/$key/", $value, $result);
1006 return $result;