Fix birthday popup and reminder popup to working together (#1008)
[openemr.git] / interface / eRx_xml.php
blob799d4c26da27618cbf5f8c376122c1b80e23972e
1 <?php
3 /**
4 * interface/eRx_xml.php Functions for interacting with NewCrop communications.
6 * Copyright (C) 2011 ZMG LLC <sam@zhservices.com>
8 * LICENSE: This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
11 * later version. This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details. You should have received a copy of the GNU
15 * General Public License along with this program.
16 * If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @subpackage NewCrop
20 * @author Eldho Chacko <eldho@zhservices.com>
21 * @author Vinish K <vinish@zhservices.com>
22 * @link http://www.open-emr.org
25 $facilityService = new \services\FacilityService();
27 function getErxPath()
29 return $GLOBALS['erx_newcrop_path'];
32 function getErxSoapPath()
34 return $GLOBALS['erx_newcrop_path_soap'];
37 function getErxCredentials()
39 $cred=array();
40 $cred[]=$GLOBALS['erx_account_partner_name'];
41 $cred[]=$GLOBALS['erx_account_name'];
42 $cred[]=$GLOBALS['erx_account_password'];
44 return $cred;
47 function validation($val_check, $val, $msg)
49 if (!$val) {
50 $msg .= $val_check.' '.xl('missing').'<br>';
53 return $msg;
56 function stripSpecialCharacterFacility($str)
58 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
59 return $str;
62 function stripSpecialCharacter($str)
64 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
65 return $str;
68 function stripPhoneSlashes($str)
70 $str=preg_replace('/-/', '', $str);
71 return $str;
74 function trimData($str, $length)
76 $str=substr($str, 0, ($length-1));
77 return $str;
80 function stringToNumeric($str)
82 if (is_numeric($str)) {
83 return array($str,"");
84 } else {
85 for ($i=0; $i<strlen($str); $i++) {
86 $x=substr($str, $i, 1);
87 if (is_numeric($x) && !$txt) {
88 $num.=$x;
89 } else {
90 $txt.=$x;
94 return array($num,$txt);
97 $str=substr($str, 0, ($length-1));
98 return $str;
100 function credentials($doc, $r)
102 global $msg;
103 $cred=getErxCredentials();
104 $msg = validation(xl('Partner Name'), $cred['0'], $msg);
105 $b = $doc->createElement("Credentials");
106 $partnerName = $doc->createElement("partnerName");
107 $partnerName->appendChild(
108 $doc->createTextNode($cred['0'])
110 $b->appendChild($partnerName);
111 $msg = validation(xl('ERX Name'), $cred['1'], $msg);
112 $name = $doc->createElement("name");
113 $name->appendChild(
114 $doc->createTextNode($cred['1'])
116 $b->appendChild($name);
117 $msg = validation(xl('ERX Password'), $cred['2'], $msg);
118 $password = $doc->createElement("password");
119 $password->appendChild(
120 $doc->createTextNode($cred['2'])
122 $b->appendChild($password);
123 $productName = $doc->createElement("productName");
124 $productName->appendChild(
125 $doc->createTextNode('OpenEMR')
127 $b->appendChild($productName);
128 $productVersion = $doc->createElement("productVersion");
129 $productVersion->appendChild(
130 $doc->createTextNode($GLOBALS['openemr_version'])
132 $b->appendChild($productVersion);
133 $r->appendChild($b);
136 function user_role($doc, $r)
138 global $msg;
139 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
140 if (!$userRole['newcrop_user_role']) {
141 echo xl('Unauthorized access to ePrescription');
142 die;
145 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
146 if ($userRole['newcrop_user_role'] == 'doctor') {
147 $userRole['eRxUser'] = 'LicensedPrescriber';
148 } elseif ($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse') {
149 $userRole['eRxUser'] = 'Staff';
150 } elseif ($userRole['newcrop_user_role'] == 'midlevelPrescriber') {
151 $userRole['eRxUser'] = 'MidlevelPrescriber';
152 } elseif ($userRole['newcrop_user_role'] == 'supervisingDoctor') {
153 $userRole['eRxUser'] = 'SupervisingDoctor';
156 $msg = validation(xl('ERX User'), $userRole['eRxUser'], $msg);
157 $b = $doc->createElement("UserRole");
158 $user = $doc->createElement("user");
159 $user->appendChild(
160 $doc->createTextNode($userRole['eRxUser'])
162 $b->appendChild($user);
163 $msg = validation(xl('ERX Role'), $userRole['newcrop_user_role'], $msg);
164 $role = $doc->createElement("role");
165 $role->appendChild(
166 $doc->createTextNode($userRole['newcrop_user_role'])
168 $b->appendChild($role);
169 $r->appendChild($b);
172 function destination($doc, $r, $page = '', $pid)
174 global $msg,$page;
175 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
176 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
177 if (!$page) {
178 $page='compose';
179 if ($userRole['newcrop_user_role']=='admin') {
180 $page='admin';
181 } elseif ($userRole['newcrop_user_role']=='manager') {
182 $page='manager';
186 $b = $doc->createElement("Destination");
187 $requestedPage = $doc->createElement("requestedPage");
188 $requestedPage->appendChild(
189 $doc->createTextNode($page)
191 $b->appendChild($requestedPage);
192 $r->appendChild($b);
195 function account($doc, $r)
197 global $msg, $facilityService;
198 $erxSiteID= $facilityService->getPrimaryBusinessEntity();
199 if (!$erxSiteID['federal_ein']) {
200 echo htmlspecialchars(xl("Please select a Primary Business Entity facility with 'Tax ID' as your facility Tax ID. If you are an individual practitioner, use your tax id. This is used for identifying you in the NewCrop system."), ENT_NOQUOTES);
201 die;
204 $b = $doc->createElement("Account");
205 $b->setAttribute('ID', $GLOBALS['erx_account_id']);
206 $erxSiteID['name']=stripSpecialCharacterFacility($erxSiteID['name']);
207 $erxSiteID['name']=trimData($erxSiteID['name'], 35);
208 $msg = validation(xl('Account Name'), $erxSiteID['name'], $msg);
209 $accountName = $doc->createElement("accountName");
210 $accountName->appendChild(
211 $doc->createTextNode($erxSiteID['name'])
213 $b->appendChild($accountName);
214 $msg = validation(xl('Site ID'), $_SESSION['site_id'], $msg);
215 $siteID = $doc->createElement("siteID");
216 $siteID->appendChild(
217 $doc->createTextNode($erxSiteID['federal_ein'])
219 $b->appendChild($siteID);
220 $erxSiteID['street']=stripSpecialCharacterFacility($erxSiteID['street']);
221 $erxSiteID['street']=trimData($erxSiteID['street'], 35);
222 $AccountAddress = $doc->createElement("AccountAddress");
223 $msg = validation(xl('Facility Street'), $erxSiteID['street'], $msg);
224 $address1 = $doc->createElement("address1");
225 $address1->appendChild(
226 $doc->createTextNode($erxSiteID['street'])
228 $AccountAddress->appendChild($address1);
229 $msg = validation(xl('Facility City'), $erxSiteID['city'], $msg);
230 $city = $doc->createElement("city");
231 $city->appendChild(
232 $doc->createTextNode($erxSiteID['city'])
234 $AccountAddress->appendChild($city);
235 $msg = validation(xl('Facility State'), $erxSiteID['state'], $msg);
236 $state = $doc->createElement("state");
237 $state->appendChild(
238 $doc->createTextNode($erxSiteID['state'])
240 $AccountAddress->appendChild($state);
241 $jasonbigzip=$erxSiteID['postal_code'];
242 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
243 if (strlen($jasonbigzip) >=5) {
244 $jasonzip=substr($jasonbigzip, 0, 5);
245 $zip4=substr($jasonbigzip, 5, 4);
246 } else {
247 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
250 $zip = $doc->createElement("zip");
251 $zip->appendChild(
252 $doc->createTextNode($jasonzip)
254 $AccountAddress->appendChild($zip);
255 if (strlen($zip4)==4) {
256 $zipFour = $doc->createElement("zip4");
257 $zipFour->appendChild(
258 $doc->createTextNode($zip4)
260 $AccountAddress->appendChild($zipFour);
263 $msg = validation(xl('Facility Country code'), $erxSiteID['country_code'], $msg);
264 $county_code = substr($erxSiteID['country_code'], 0, 2);
265 $country = $doc->createElement("country");
266 $country->appendChild(
267 $doc->createTextNode($county_code)
269 $AccountAddress->appendChild($country);
270 $b->appendChild($AccountAddress);
271 $msg = validation(xl('Facility Phone'), $erxSiteID['phone'], $msg);
272 $accountPrimaryPhoneNumber = $doc->createElement("accountPrimaryPhoneNumber");
273 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
274 $accountPrimaryPhoneNumber->appendChild(
275 $doc->createTextNode($erxSiteID['phone'])
277 $b->appendChild($accountPrimaryPhoneNumber);
278 $msg = validation(xl('Facility Fax'), $erxSiteID['fax'], $msg);
279 $accountPrimaryFaxNumber = $doc->createElement("accountPrimaryFaxNumber");
280 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
281 $accountPrimaryFaxNumber->appendChild(
282 $doc->createTextNode($erxSiteID['fax'])
284 $b->appendChild($accountPrimaryFaxNumber);
285 $r->appendChild($b);
288 function location($doc, $r)
290 global $msg;
291 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?", array($_SESSION['authUser']));
292 $b = $doc->createElement("Location");
293 $b->setAttribute('ID', $userRole['id']);
294 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
295 $userRole['name']=trimData($userRole['name'], 35);
296 $locationName = $doc->createElement('locationName');
297 $locationName->appendChild(
298 $doc->createTextNode($userRole['name'])
300 $b->appendChild($locationName);
301 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
302 $userRole['street']=trimData($userRole['street'], 35);
303 $LocationAddress = $doc->createElement('LocationAddress');
304 if ($userRole['street']) {
305 $address1 = $doc->createElement('address1');
306 $address1->appendChild(
307 $doc->createTextNode($userRole['street'])
309 $LocationAddress->appendChild($address1);
312 if ($userRole['city']) {
313 $city = $doc->createElement('city');
314 $city->appendChild(
315 $doc->createTextNode($userRole['city'])
317 $LocationAddress->appendChild($city);
320 if ($userRole['state']) {
321 $state = $doc->createElement('state');
322 $state->appendChild(
323 $doc->createTextNode($userRole['state'])
325 $LocationAddress->appendChild($state);
328 $jasonbigzip=$userRole['postal_code'];
329 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
330 if (strlen($jasonbigzip) >=5) {
331 $jasonzip=substr($jasonbigzip, 0, 5);
332 $zip4=substr($jasonbigzip, 5, 4);
333 } else {
334 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
337 $zip = $doc->createElement("zip");
338 $zip->appendChild(
339 $doc->createTextNode($jasonzip)
341 $LocationAddress->appendChild($zip);
342 if (strlen($zip4)==4) {
343 $zipFour = $doc->createElement("zip4");
344 $zipFour->appendChild(
345 $doc->createTextNode($zip4)
347 $LocationAddress->appendChild($zipFour);
350 if ($userRole['country_code']) {
351 $county_code = substr($userRole['country_code'], 0, 2);
352 $country = $doc->createElement('country');
353 $country->appendChild(
354 $doc->createTextNode($county_code)
356 $LocationAddress->appendChild($country);
359 $b->appendChild($LocationAddress);
360 if ($userRole['phone']) {
361 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
362 $primaryPhoneNumber = $doc->createElement('primaryPhoneNumber');
363 $primaryPhoneNumber->appendChild(
364 $doc->createTextNode($userRole['phone'])
366 $b->appendChild($primaryPhoneNumber);
369 if ($userRole['fax']) {
370 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
371 $primaryFaxNumber = $doc->createElement('primaryFaxNumber');
372 $primaryFaxNumber->appendChild(
373 $doc->createTextNode($userRole['fax'])
375 $b->appendChild($primaryFaxNumber);
378 $pharmacyContactNumber = $doc->createElement('pharmacyContactNumber');
379 $pharmacyContactNumber->appendChild(
380 $doc->createTextNode($userRole['phone'])
382 $b->appendChild($pharmacyContactNumber);
383 $r->appendChild($b);
386 function LicensedPrescriber($doc, $r)
388 global $msg;
389 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
390 $b = $doc->createElement("LicensedPrescriber");
391 $b->setAttribute('ID', $user_details['npi']);
392 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
393 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
394 $msg = validation(xl('LicensedPrescriber Last name'), $user_details['lname'], $msg);
395 $last = $doc->createElement("last");
396 $last->appendChild(
397 $doc->createTextNode($user_details['lname'])
399 $LicensedPrescriberName->appendChild($last);
400 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
401 $msg = validation(xl('User First name'), $user_details['fname'], $msg);
402 $first = $doc->createElement("first");
403 $first->appendChild(
404 $doc->createTextNode($user_details['fname'])
406 $LicensedPrescriberName->appendChild($first);
407 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
408 $middle = $doc->createElement("middle");
409 $middle->appendChild(
410 $doc->createTextNode($user_details['mname'])
412 $LicensedPrescriberName->appendChild($middle);
413 $b->appendChild($LicensedPrescriberName);
414 $msg = validation(xl('DEA'), $user_details['federaldrugid'], $msg);
415 $dea = $doc->createElement("dea");
416 $dea->appendChild(
417 $doc->createTextNode($user_details['federaldrugid'])
419 $b->appendChild($dea);
420 if ($user_details['upin']) {
421 $upin = $doc->createElement("upin");
422 $upin->appendChild(
423 $doc->createTextNode($user_details['upin'])
425 $b->appendChild($upin);
428 $licenseNumber = $doc->createElement("licenseNumber");
429 $licenseNumber->appendChild(
430 $doc->createTextNode($user_details['state_license_number'])
432 $b->appendChild($licenseNumber);
433 $msg = validation(xl('LicensedPrescriber NPI'), $user_details['npi'], $msg);
434 $npi = $doc->createElement("npi");
435 $npi->appendChild(
436 $doc->createTextNode($user_details['npi'])
438 $b->appendChild($npi);
439 $r->appendChild($b);
442 function Staff($doc, $r)
444 global $msg;
445 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
446 $b = $doc->createElement("Staff");
447 $b->setAttribute('ID', $user_details['username']);
448 $StaffName = $doc->createElement("StaffName");
449 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
450 $last = $doc->createElement("last");
451 $last->appendChild(
452 $doc->createTextNode($user_details['lname'])
454 $StaffName->appendChild($last);
455 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
456 $first = $doc->createElement("first");
457 $first->appendChild(
458 $doc->createTextNode($user_details['fname'])
460 $StaffName->appendChild($first);
461 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
462 $middle = $doc->createElement("middle");
463 $middle->appendChild(
464 $doc->createTextNode($user_details['mname'])
466 $StaffName->appendChild($middle);
467 $b->appendChild($StaffName);
468 $license = $doc->createElement("license");
469 $license->appendChild(
470 $doc->createTextNode($user_details['license'])
472 $b->appendChild($license);
473 $r->appendChild($b);
476 function SupervisingDoctor($doc, $r)
478 global $msg;
479 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
480 $b = $doc->createElement("SupervisingDoctor");
481 $b->setAttribute('ID', $user_details['npi']);
482 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
483 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
484 $msg = validation(xl('Supervising Doctor Last name'), $user_details['lname'], $msg);
485 $last = $doc->createElement("last");
486 $last->appendChild(
487 $doc->createTextNode($user_details['lname'])
489 $LicensedPrescriberName->appendChild($last);
490 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
491 $msg = validation(xl('Supervising Doctor First name'), $user_details['fname'], $msg);
492 $first = $doc->createElement("first");
493 $first->appendChild(
494 $doc->createTextNode($user_details['fname'])
496 $LicensedPrescriberName->appendChild($first);
497 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
498 $middle = $doc->createElement("middle");
499 $middle->appendChild(
500 $doc->createTextNode($user_details['mname'])
502 $LicensedPrescriberName->appendChild($middle);
503 $b->appendChild($LicensedPrescriberName);
504 $msg = validation(xl('Supervising Doctor DEA'), $user_details['federaldrugid'], $msg);
505 $dea = $doc->createElement("dea");
506 $dea->appendChild(
507 $doc->createTextNode($user_details['federaldrugid'])
509 $b->appendChild($dea);
510 if ($user_details['upin']) {
511 $upin = $doc->createElement("upin");
512 $upin->appendChild(
513 $doc->createTextNode($user_details['upin'])
515 $b->appendChild($upin);
518 $licenseNumber = $doc->createElement("licenseNumber");
519 $licenseNumber->appendChild(
520 $doc->createTextNode($user_details['state_license_number'])
522 $b->appendChild($licenseNumber);
523 $msg = validation(xl('Supervising Doctor NPI'), $user_details['npi'], $msg);
524 $npi = $doc->createElement("npi");
525 $npi->appendChild(
526 $doc->createTextNode($user_details['npi'])
528 $b->appendChild($npi);
529 $r->appendChild($b);
532 function MidlevelPrescriber($doc, $r)
534 global $msg;
535 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
536 $b = $doc->createElement("MidlevelPrescriber");
537 $b->setAttribute('ID', $user_details['npi']);
538 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
539 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
540 $msg = validation(xl('Midlevel Prescriber Last name'), $user_details['lname'], $msg);
541 $last = $doc->createElement("last");
542 $last->appendChild(
543 $doc->createTextNode($user_details['lname'])
545 $LicensedPrescriberName->appendChild($last);
546 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
547 $msg = validation(xl('Midlevel Prescriber First name'), $user_details['fname'], $msg);
548 $first = $doc->createElement("first");
549 $first->appendChild(
550 $doc->createTextNode($user_details['fname'])
552 $LicensedPrescriberName->appendChild($first);
553 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
554 $middle = $doc->createElement("middle");
555 $middle->appendChild(
556 $doc->createTextNode($user_details['mname'])
558 $LicensedPrescriberName->appendChild($middle);
559 if ($user_details['title']) {
560 $msg = validation(xl('Midlevel Prescriber Prefix'), $user_details['title'], $msg);
561 $prefix = $doc->createElement("prefix");
562 $prefix->appendChild(
563 $doc->createTextNode($user_details['title'])
565 $LicensedPrescriberName->appendChild($prefix);
568 $b->appendChild($LicensedPrescriberName);
569 $msg = validation(xl('Midlevel Prescriber DEA'), $user_details['federaldrugid'], $msg);
570 $dea = $doc->createElement("dea");
571 $dea->appendChild(
572 $doc->createTextNode($user_details['federaldrugid'])
574 $b->appendChild($dea);
575 if ($user_details['upin']) {
576 $upin = $doc->createElement("upin");
577 $upin->appendChild(
578 $doc->createTextNode($user_details['upin'])
580 $b->appendChild($upin);
583 $licenseNumber = $doc->createElement("licenseNumber");
584 $licenseNumber->appendChild(
585 $doc->createTextNode($user_details['state_license_number'])
587 $b->appendChild($licenseNumber);
588 $r->appendChild($b);
591 function Patient($doc, $r, $pid)
593 global $msg,$warning_msg,$dem_check;
594 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?", array($pid));
595 $b = $doc->createElement("Patient");
596 $b->setAttribute('ID', $patient_data['pid']);
597 $PatientName = $doc->createElement("PatientName");
598 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
599 $patient_data['lname']=trimData($patient_data['lname'], 35);
600 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
601 if ($patient_data['lname']=='') {
602 $dem_check.=htmlspecialchars(xl("Patient Last name is missing"), ENT_NOQUOTES)."<br>";
605 $last = $doc->createElement("last");
606 $last->appendChild(
607 $doc->createTextNode($patient_data['lname'])
609 $PatientName->appendChild($last);
610 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
611 $patient_data['fname']=trimData($patient_data['fname'], 35);
612 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
613 if ($patient_data['fname']=='') {
614 $dem_check.=htmlspecialchars(xl("Patient First name is missing"), ENT_NOQUOTES)."<br>";
617 $first = $doc->createElement("first");
618 $first->appendChild(
619 $doc->createTextNode($patient_data['fname'])
621 $PatientName->appendChild($first);
622 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
623 $patient_data['mname']=trimData($patient_data['mname'], 35);
624 $middle = $doc->createElement("middle");
625 $middle->appendChild(
626 $doc->createTextNode($patient_data['mname'])
628 $PatientName->appendChild($middle);
629 $b->appendChild($PatientName);
630 $PatientAddress = $doc->createElement("PatientAddress");
631 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
632 $patient_data['street']=trimData($patient_data['street'], 35);
633 $msg = validation(xl('Patient Address'), $patient_data['street'], $msg);
634 if (trim($patient_data['street'])=='') {
635 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Address is missing"), ENT_NOQUOTES);
638 $address1 = $doc->createElement("address1");
639 $address1->appendChild(
640 $doc->createTextNode($patient_data['street'])
642 $PatientAddress->appendChild($address1);
643 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
644 if ($patient_data['city']=='') {
645 $dem_check.=htmlspecialchars(xl("Patient City is missing"), ENT_NOQUOTES)."<br>";
648 $city = $doc->createElement("city");
649 $city->appendChild(
650 $doc->createTextNode($patient_data['city'])
652 $PatientAddress->appendChild($city);
653 if ($patient_data['state']) {
654 $state = $doc->createElement("state");
655 $state->appendChild(
656 $doc->createTextNode($patient_data['state'])
658 $PatientAddress->appendChild($state);
661 if ($patient_data['postal_code']) {
662 $zip = $doc->createElement("zip");
663 $zip->appendChild(
664 $doc->createTextNode($patient_data['postal_code'])
666 $PatientAddress->appendChild($zip);
669 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
670 if (trim($patient_data['country_code'])=='' && $GLOBALS['erx_default_patient_country']=='') {
671 $dem_check.=htmlspecialchars(xl("Patient Country is missing. Also you have not set default Patient Country in Global Settings"), ENT_NOQUOTES)."<br>";
672 } elseif (trim($patient_data['country_code'])=='') {
673 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
676 $county_code = substr($patient_data['country_code'], 0, 2);
677 $country = $doc->createElement("country");
678 $country->appendChild(
679 $doc->createTextNode($county_code)
681 $PatientAddress->appendChild($country);
682 $b->appendChild($PatientAddress);
683 $PatientContact = $doc->createElement("PatientContact");
684 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
685 if ($patient_data['phone_home']) {
686 $homeTelephone = $doc->createElement("homeTelephone");
687 $homeTelephone->appendChild(
688 $doc->createTextNode($patient_data['phone_home'])
690 $PatientContact->appendChild($homeTelephone);
693 $b->appendChild($PatientContact);
694 $PatientCharacteristics = $doc->createElement("PatientCharacteristics");
695 if (trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000') {
696 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Date Of Birth is missing"), ENT_NOQUOTES);
699 if ($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000') {
700 $dob = $doc->createElement("dob");
701 $dob->appendChild(
702 $doc->createTextNode($patient_data['date_of_birth'])
704 $PatientCharacteristics->appendChild($dob);
707 if (trim($patient_data['sex'])=='') {
708 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Gender is missing"), ENT_NOQUOTES);
711 if ($patient_data['sex']) {
712 $gender_val=substr($patient_data['sex'], 0, 1);
713 $gender = $doc->createElement("gender");
714 $gender->appendChild(
715 $doc->createTextNode($gender_val)
717 $PatientCharacteristics->appendChild($gender);
720 $b->appendChild($PatientCharacteristics);
721 PatientFreeformHealthplans($doc, $b, $pid);
722 $allergyId=PatientFreeformAllergy($doc, $b, $pid);
723 $r->appendChild($b);
724 return $allergyId;
727 function OutsidePrescription($doc, $r, $pid, $prescid)
729 global $msg;
730 if ($prescid) {
731 $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,
732 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
733 FROM prescriptions AS p
734 LEFT JOIN users AS u ON p.provider_id=u.id
735 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
736 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
737 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
738 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
739 WHERE p.drug <> '' and p.id = ?", array($prescid));
740 $b = $doc->createElement("OutsidePrescription");
741 $externalId = $doc->createElement("externalId");
742 $externalId->appendChild(
743 $doc->createTextNode($prec['prescid'])
745 $b->appendChild($externalId);
746 $date = $doc->createElement("date");
747 $date->appendChild(
748 $doc->createTextNode($prec['date_added'])
750 $b->appendChild($date);
751 $doctorName = $doc->createElement("doctorName");
752 $doctorName->appendChild(
753 $doc->createTextNode($prec['docname'])
755 $b->appendChild($doctorName);
756 $s=stripSpecialCharacter($prec['drug']);
757 $sig = $doc->createElement("drug");
758 $sig->appendChild(
759 $doc->createTextNode(trimData($s, 80))
761 $b->appendChild($sig);
762 $x=stringToNumeric($prec['quantity']);
763 $dispenseNumber = $doc->createElement("dispenseNumber");
764 $dispenseNumber->appendChild(
765 $doc->createTextNode($x[0])
767 $b->appendChild($dispenseNumber);
768 $s=trimData($x[1].$prec['size']." ".$prec['title4']." ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'], 140);
769 $s=stripSpecialCharacter($s);
770 $sig = $doc->createElement("sig");
771 $sig->appendChild(
772 $doc->createTextNode($s)
774 $b->appendChild($sig);
775 $refillCount = $doc->createElement("refillCount");
776 $x=stringToNumeric($prec['per_refill']);
777 $refillCount->appendChild(
778 $doc->createTextNode($x[0])
780 $b->appendChild($refillCount);
781 $prescriptionType = $doc->createElement("prescriptionType");
782 $prescriptionType->appendChild(
783 $doc->createTextNode('reconcile')
785 $b->appendChild($prescriptionType);
786 $r->appendChild($b);
790 function PatientMedication($doc, $r, $pid, $med_limit)
792 global $msg;
793 $active='';
794 if ($GLOBALS['erx_upload_active']==1) {
795 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
798 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
799 and erx_uploaded='0' $active order by enddate limit 0,$med_limit", array($pid));
800 $uploaded_med_arr="";
801 while ($row_med=sqlFetchArray($res_med)) {
802 $uploaded_med_arr[]=$row_med['id'];
803 $b = $doc->createElement("OutsidePrescription");
804 $externalId = $doc->createElement("externalId");
805 $externalId->appendChild(
806 $doc->createTextNode($row_med['id'])
808 $b->appendChild($externalId);
809 $date = $doc->createElement("date");
810 $date->appendChild(
811 $doc->createTextNode($row_med['begdate'])
813 $b->appendChild($date);
814 $doctorName = $doc->createElement("doctorName");
815 $doctorName->appendChild(
816 $doc->createTextNode("")
818 $b->appendChild($doctorName);
819 $row_med['title'] = stripSpecialCharacter($row_med['title']);
820 $sig = $doc->createElement("drug");
821 $sig->appendChild(
822 $doc->createTextNode(trimData($row_med['title'], 80))
824 $b->appendChild($sig);
825 $dispenseNumber = $doc->createElement("dispenseNumber");
826 $dispenseNumber->appendChild(
827 $doc->createTextNode($prec['quantity'])
829 $b->appendChild($dispenseNumber);
830 $sig = $doc->createElement("sig");
831 $sig->appendChild(
832 $doc->createTextNode("")
834 $b->appendChild($sig);
835 $refillCount = $doc->createElement("refillCount");
836 $refillCount->appendChild(
837 $doc->createTextNode("")
839 $b->appendChild($refillCount);
840 $prescriptionType = $doc->createElement("prescriptionType");
841 $prescriptionType->appendChild(
842 $doc->createTextNode('reconcile')
844 $b->appendChild($prescriptionType);
845 $r->appendChild($b);
848 return $uploaded_med_arr;
851 function PatientFreeformAllergy($doc, $r, $pid)
853 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
854 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
855 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));
856 $allergyId=array();
857 while ($row=sqlFetchArray($res)) {
858 $val=array();
859 $val['id']=$row['id'];
860 $val['title1']=$row['title1'];
861 $val['title2']=$row['title2'];
862 $val['comments']=$row['comments'];
863 $b = $doc->createElement("PatientFreeformAllergy");
864 $b->setAttribute('ID', $val['id']);
865 if ($val['title1']) {
866 $allergyName = $doc->createElement("allergyName");
867 $allergyName->appendChild(
868 $doc->createTextNode(trimData(stripSpecialCharacter($val['title1']), 70))
870 $b->appendChild($allergyName);
873 if ($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')) {
874 $allergySeverityTypeID = $doc->createElement("allergySeverityTypeID");
875 $allergySeverityTypeID->appendChild(
876 $doc->createTextNode($val['title2'])
878 $b->appendChild($allergySeverityTypeID);
881 if ($val['comments']) {
882 $allergyComment = $doc->createElement("allergyComment");
883 $allergyComment->appendChild(
884 $doc->createTextNode(trimData(stripSpecialCharacter($val['comments']), 200))
886 $b->appendChild($allergyComment);
889 $r->appendChild($b);
890 $allergyId[]=$row['id'];
893 return $allergyId;
896 function PatientFreeformHealthplans($doc, $r, $pid)
898 $resource = sqlStatement(
899 'SELECT
900 `ins`.`name`
901 FROM (
902 SELECT
903 `id`.`type`,
904 `ic`.`name`
905 FROM `insurance_data` AS `id`
906 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
907 WHERE `id`.`pid` = ?
908 AND `id`.`subscriber_relationship` = \'self\'
909 AND `id`.`provider` > 0
910 ORDER BY `id`.`date` DESC
911 ) AS `ins`
912 GROUP BY `ins`.`type`;',
913 array($pid)
916 while ($row = sqlFetchArray($resource)) {
917 $healthplanName = $doc->createElement('healthplanName');
918 $healthplanName->appendChild($doc->createTextNode(
919 stripSpecialCharacter(trimData($row['name'], 35))
922 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
923 $patientFreeformHealthplans->appendChild($healthplanName);
925 $r->appendChild($patientFreeformHealthplans);
929 function PrescriptionRenewalResponse($doc, $r, $pid)
931 $b = $doc->createElement("PrescriptionRenewalResponse");
932 $renewalRequestIdentifier = $doc->createElement("renewalRequestIdentifier");
933 $renewalRequestIdentifier->appendChild(
934 $doc->createTextNode('cbf51649-ce3c-44b8-8f91-6fda121a353d')
936 $b->appendChild($renewalRequestIdentifier);
937 $responseCode = $doc->createElement("responseCode");
938 $responseCode->appendChild(
939 $doc->createTextNode('Undetermined')
941 $b->appendChild($responseCode);
942 $r->appendChild($b);
945 function checkError($xml)
947 $ch = curl_init($xml);
949 $data = array('RxInput' => $xml);
951 curl_setopt($ch, CURLOPT_URL, getErxPath());
952 curl_setopt($ch, CURLOPT_POST, 1);
953 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
954 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
955 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
956 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
957 //curl_setopt($ch, CURLOPT_HEADER, 0);
958 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
959 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
960 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
961 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
962 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
964 $result=curl_exec($ch) or die(curl_error($ch)) ;
965 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $error_message);
966 if (strpos($result, 'RxEntry.aspx')) {
967 erx_error_log($xml);
968 erx_error_log($result);
971 $arr=explode('Error', $error_message[1]);
972 //echo "Te: ".count($arr);
973 //print_r($arr);
974 if (count($arr)==1) {
975 echo nl2br($error_message[1]);
976 } else {
977 for ($i=1; $i<count($arr); $i++) {
978 echo $arr[$i]."<br><br>";
982 curl_close($ch);
983 if (strpos($result, 'RxEntry.aspx')) {
984 return '1';
985 } else {
986 return '0';
990 function erx_error_log($message)
992 $date = date("Y-m-d");
993 if (!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error')) {
994 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error', 0777, true);
997 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
998 $f=fopen($filename, 'a');
999 fwrite($f, date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
1000 fclose($f);
1003 function stripStrings($str, $pattern)
1005 $result = $str;
1006 foreach ($pattern as $key => $value) {
1007 $result = preg_replace("/$key/", $value, $result);
1010 return $result;