fix issue #1176
[openemr.git] / interface / eRx_xml.php
blob5f262839f49bd0ad3c6a5a44d09424b6cfbbaee3
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 use OpenEMR\Services\FacilityService;
27 $facilityService = new FacilityService();
29 function getErxPath()
31 return $GLOBALS['erx_newcrop_path'];
34 function getErxSoapPath()
36 return $GLOBALS['erx_newcrop_path_soap'];
39 function getErxCredentials()
41 $cred=array();
42 $cred[]=$GLOBALS['erx_account_partner_name'];
43 $cred[]=$GLOBALS['erx_account_name'];
44 $cred[]=$GLOBALS['erx_account_password'];
46 return $cred;
49 function validation($val_check, $val, $msg)
51 if (!$val) {
52 $msg .= $val_check.' '.xl('missing').'<br>';
55 return $msg;
58 function stripSpecialCharacterFacility($str)
60 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
61 return $str;
64 function stripSpecialCharacter($str)
66 $str=preg_replace("/[^a-zA-Z0-9 '().,#:\/\-@_%]/", "", $str);
67 return $str;
70 function stripPhoneSlashes($str)
72 $str=preg_replace('/-/', '', $str);
73 return $str;
76 function trimData($str, $length)
78 $str=substr($str, 0, ($length-1));
79 return $str;
82 function stringToNumeric($str)
84 if (is_numeric($str)) {
85 return array($str,"");
86 } else {
87 for ($i=0; $i<strlen($str); $i++) {
88 $x=substr($str, $i, 1);
89 if (is_numeric($x) && !$txt) {
90 $num.=$x;
91 } else {
92 $txt.=$x;
96 return array($num,$txt);
99 $str=substr($str, 0, ($length-1));
100 return $str;
102 function credentials($doc, $r)
104 global $msg;
105 $cred=getErxCredentials();
106 $msg = validation(xl('Partner Name'), $cred['0'], $msg);
107 $b = $doc->createElement("Credentials");
108 $partnerName = $doc->createElement("partnerName");
109 $partnerName->appendChild(
110 $doc->createTextNode($cred['0'])
112 $b->appendChild($partnerName);
113 $msg = validation(xl('ERX Name'), $cred['1'], $msg);
114 $name = $doc->createElement("name");
115 $name->appendChild(
116 $doc->createTextNode($cred['1'])
118 $b->appendChild($name);
119 $msg = validation(xl('ERX Password'), $cred['2'], $msg);
120 $password = $doc->createElement("password");
121 $password->appendChild(
122 $doc->createTextNode($cred['2'])
124 $b->appendChild($password);
125 $productName = $doc->createElement("productName");
126 $productName->appendChild(
127 $doc->createTextNode('OpenEMR')
129 $b->appendChild($productName);
130 $productVersion = $doc->createElement("productVersion");
131 $productVersion->appendChild(
132 $doc->createTextNode($GLOBALS['openemr_version'])
134 $b->appendChild($productVersion);
135 $r->appendChild($b);
138 function user_role($doc, $r)
140 global $msg;
141 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
142 if (!$userRole['newcrop_user_role']) {
143 echo xl('Unauthorized access to ePrescription');
144 die;
147 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
148 if ($userRole['newcrop_user_role'] == 'doctor') {
149 $userRole['eRxUser'] = 'LicensedPrescriber';
150 } elseif ($userRole['newcrop_user_role'] == 'admin' || $userRole['newcrop_user_role'] == 'manager' || $userRole['newcrop_user_role'] == 'nurse') {
151 $userRole['eRxUser'] = 'Staff';
152 } elseif ($userRole['newcrop_user_role'] == 'midlevelPrescriber') {
153 $userRole['eRxUser'] = 'MidlevelPrescriber';
154 } elseif ($userRole['newcrop_user_role'] == 'supervisingDoctor') {
155 $userRole['eRxUser'] = 'SupervisingDoctor';
158 $msg = validation(xl('ERX User'), $userRole['eRxUser'], $msg);
159 $b = $doc->createElement("UserRole");
160 $user = $doc->createElement("user");
161 $user->appendChild(
162 $doc->createTextNode($userRole['eRxUser'])
164 $b->appendChild($user);
165 $msg = validation(xl('ERX Role'), $userRole['newcrop_user_role'], $msg);
166 $role = $doc->createElement("role");
167 $role->appendChild(
168 $doc->createTextNode($userRole['newcrop_user_role'])
170 $b->appendChild($role);
171 $r->appendChild($b);
174 function destination($doc, $r, $page = '', $pid)
176 global $msg,$page;
177 $userRole=sqlQuery("select * from users where username=?", array($_SESSION['authUser']));
178 $userRole['newcrop_user_role'] = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
179 if (!$page) {
180 $page='compose';
181 if ($userRole['newcrop_user_role']=='admin') {
182 $page='admin';
183 } elseif ($userRole['newcrop_user_role']=='manager') {
184 $page='manager';
188 $b = $doc->createElement("Destination");
189 $requestedPage = $doc->createElement("requestedPage");
190 $requestedPage->appendChild(
191 $doc->createTextNode($page)
193 $b->appendChild($requestedPage);
194 $r->appendChild($b);
197 function account($doc, $r)
199 global $msg, $facilityService;
200 $erxSiteID= $facilityService->getPrimaryBusinessEntity();
201 if (!$erxSiteID['federal_ein']) {
202 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);
203 die;
206 $b = $doc->createElement("Account");
207 $b->setAttribute('ID', $GLOBALS['erx_account_id']);
208 $erxSiteID['name']=stripSpecialCharacterFacility($erxSiteID['name']);
209 $erxSiteID['name']=trimData($erxSiteID['name'], 35);
210 $msg = validation(xl('Account Name'), $erxSiteID['name'], $msg);
211 $accountName = $doc->createElement("accountName");
212 $accountName->appendChild(
213 $doc->createTextNode($erxSiteID['name'])
215 $b->appendChild($accountName);
216 $msg = validation(xl('Site ID'), $_SESSION['site_id'], $msg);
217 $siteID = $doc->createElement("siteID");
218 $siteID->appendChild(
219 $doc->createTextNode($erxSiteID['federal_ein'])
221 $b->appendChild($siteID);
222 $erxSiteID['street']=stripSpecialCharacterFacility($erxSiteID['street']);
223 $erxSiteID['street']=trimData($erxSiteID['street'], 35);
224 $AccountAddress = $doc->createElement("AccountAddress");
225 $msg = validation(xl('Facility Street'), $erxSiteID['street'], $msg);
226 $address1 = $doc->createElement("address1");
227 $address1->appendChild(
228 $doc->createTextNode($erxSiteID['street'])
230 $AccountAddress->appendChild($address1);
231 $msg = validation(xl('Facility City'), $erxSiteID['city'], $msg);
232 $city = $doc->createElement("city");
233 $city->appendChild(
234 $doc->createTextNode($erxSiteID['city'])
236 $AccountAddress->appendChild($city);
237 $msg = validation(xl('Facility State'), $erxSiteID['state'], $msg);
238 $state = $doc->createElement("state");
239 $state->appendChild(
240 $doc->createTextNode($erxSiteID['state'])
242 $AccountAddress->appendChild($state);
243 $jasonbigzip=$erxSiteID['postal_code'];
244 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
245 if (strlen($jasonbigzip) >=5) {
246 $jasonzip=substr($jasonbigzip, 0, 5);
247 $zip4=substr($jasonbigzip, 5, 4);
248 } else {
249 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
252 $zip = $doc->createElement("zip");
253 $zip->appendChild(
254 $doc->createTextNode($jasonzip)
256 $AccountAddress->appendChild($zip);
257 if (strlen($zip4)==4) {
258 $zipFour = $doc->createElement("zip4");
259 $zipFour->appendChild(
260 $doc->createTextNode($zip4)
262 $AccountAddress->appendChild($zipFour);
265 $msg = validation(xl('Facility Country code'), $erxSiteID['country_code'], $msg);
266 $county_code = substr($erxSiteID['country_code'], 0, 2);
267 $country = $doc->createElement("country");
268 $country->appendChild(
269 $doc->createTextNode($county_code)
271 $AccountAddress->appendChild($country);
272 $b->appendChild($AccountAddress);
273 $msg = validation(xl('Facility Phone'), $erxSiteID['phone'], $msg);
274 $accountPrimaryPhoneNumber = $doc->createElement("accountPrimaryPhoneNumber");
275 $erxSiteID['phone'] = stripPhoneSlashes($erxSiteID['phone']);
276 $accountPrimaryPhoneNumber->appendChild(
277 $doc->createTextNode($erxSiteID['phone'])
279 $b->appendChild($accountPrimaryPhoneNumber);
280 $msg = validation(xl('Facility Fax'), $erxSiteID['fax'], $msg);
281 $accountPrimaryFaxNumber = $doc->createElement("accountPrimaryFaxNumber");
282 $erxSiteID['fax'] = stripPhoneSlashes($erxSiteID['fax']);
283 $accountPrimaryFaxNumber->appendChild(
284 $doc->createTextNode($erxSiteID['fax'])
286 $b->appendChild($accountPrimaryFaxNumber);
287 $r->appendChild($b);
290 function location($doc, $r)
292 global $msg;
293 $userRole=sqlQuery("SELECT * FROM users AS u LEFT JOIN facility AS f ON f.id=u.facility_id WHERE u.username=?", array($_SESSION['authUser']));
294 $b = $doc->createElement("Location");
295 $b->setAttribute('ID', $userRole['id']);
296 $userRole['name']=stripSpecialCharacterFacility($userRole['name']);
297 $userRole['name']=trimData($userRole['name'], 35);
298 $locationName = $doc->createElement('locationName');
299 $locationName->appendChild(
300 $doc->createTextNode($userRole['name'])
302 $b->appendChild($locationName);
303 $userRole['street']=stripSpecialCharacterFacility($userRole['street']);
304 $userRole['street']=trimData($userRole['street'], 35);
305 $LocationAddress = $doc->createElement('LocationAddress');
306 if ($userRole['street']) {
307 $address1 = $doc->createElement('address1');
308 $address1->appendChild(
309 $doc->createTextNode($userRole['street'])
311 $LocationAddress->appendChild($address1);
314 if ($userRole['city']) {
315 $city = $doc->createElement('city');
316 $city->appendChild(
317 $doc->createTextNode($userRole['city'])
319 $LocationAddress->appendChild($city);
322 if ($userRole['state']) {
323 $state = $doc->createElement('state');
324 $state->appendChild(
325 $doc->createTextNode($userRole['state'])
327 $LocationAddress->appendChild($state);
330 $jasonbigzip=$userRole['postal_code'];
331 $jasonbigzip=preg_replace('/[^0-9]/', '', $jasonbigzip);
332 if (strlen($jasonbigzip) >=5) {
333 $jasonzip=substr($jasonbigzip, 0, 5);
334 $zip4=substr($jasonbigzip, 5, 4);
335 } else {
336 $msg = validation(xl('Facility Zip'), $jasonzip, $msg);
339 $zip = $doc->createElement("zip");
340 $zip->appendChild(
341 $doc->createTextNode($jasonzip)
343 $LocationAddress->appendChild($zip);
344 if (strlen($zip4)==4) {
345 $zipFour = $doc->createElement("zip4");
346 $zipFour->appendChild(
347 $doc->createTextNode($zip4)
349 $LocationAddress->appendChild($zipFour);
352 if ($userRole['country_code']) {
353 $county_code = substr($userRole['country_code'], 0, 2);
354 $country = $doc->createElement('country');
355 $country->appendChild(
356 $doc->createTextNode($county_code)
358 $LocationAddress->appendChild($country);
361 $b->appendChild($LocationAddress);
362 if ($userRole['phone']) {
363 $userRole['phone'] = stripPhoneSlashes($userRole['phone']);
364 $primaryPhoneNumber = $doc->createElement('primaryPhoneNumber');
365 $primaryPhoneNumber->appendChild(
366 $doc->createTextNode($userRole['phone'])
368 $b->appendChild($primaryPhoneNumber);
371 if ($userRole['fax']) {
372 $userRole['fax'] = stripPhoneSlashes($userRole['fax']);
373 $primaryFaxNumber = $doc->createElement('primaryFaxNumber');
374 $primaryFaxNumber->appendChild(
375 $doc->createTextNode($userRole['fax'])
377 $b->appendChild($primaryFaxNumber);
380 $pharmacyContactNumber = $doc->createElement('pharmacyContactNumber');
381 $pharmacyContactNumber->appendChild(
382 $doc->createTextNode($userRole['phone'])
384 $b->appendChild($pharmacyContactNumber);
385 $r->appendChild($b);
388 function LicensedPrescriber($doc, $r)
390 global $msg;
391 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
392 $b = $doc->createElement("LicensedPrescriber");
393 $b->setAttribute('ID', $user_details['npi']);
394 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
395 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
396 $msg = validation(xl('LicensedPrescriber Last name'), $user_details['lname'], $msg);
397 $last = $doc->createElement("last");
398 $last->appendChild(
399 $doc->createTextNode($user_details['lname'])
401 $LicensedPrescriberName->appendChild($last);
402 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
403 $msg = validation(xl('User First name'), $user_details['fname'], $msg);
404 $first = $doc->createElement("first");
405 $first->appendChild(
406 $doc->createTextNode($user_details['fname'])
408 $LicensedPrescriberName->appendChild($first);
409 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
410 $middle = $doc->createElement("middle");
411 $middle->appendChild(
412 $doc->createTextNode($user_details['mname'])
414 $LicensedPrescriberName->appendChild($middle);
415 $b->appendChild($LicensedPrescriberName);
416 $msg = validation(xl('DEA'), $user_details['federaldrugid'], $msg);
417 $dea = $doc->createElement("dea");
418 $dea->appendChild(
419 $doc->createTextNode($user_details['federaldrugid'])
421 $b->appendChild($dea);
422 if ($user_details['upin']) {
423 $upin = $doc->createElement("upin");
424 $upin->appendChild(
425 $doc->createTextNode($user_details['upin'])
427 $b->appendChild($upin);
430 $licenseNumber = $doc->createElement("licenseNumber");
431 $licenseNumber->appendChild(
432 $doc->createTextNode($user_details['state_license_number'])
434 $b->appendChild($licenseNumber);
435 $msg = validation(xl('LicensedPrescriber NPI'), $user_details['npi'], $msg);
436 $npi = $doc->createElement("npi");
437 $npi->appendChild(
438 $doc->createTextNode($user_details['npi'])
440 $b->appendChild($npi);
441 $r->appendChild($b);
444 function Staff($doc, $r)
446 global $msg;
447 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
448 $b = $doc->createElement("Staff");
449 $b->setAttribute('ID', $user_details['username']);
450 $StaffName = $doc->createElement("StaffName");
451 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
452 $last = $doc->createElement("last");
453 $last->appendChild(
454 $doc->createTextNode($user_details['lname'])
456 $StaffName->appendChild($last);
457 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
458 $first = $doc->createElement("first");
459 $first->appendChild(
460 $doc->createTextNode($user_details['fname'])
462 $StaffName->appendChild($first);
463 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
464 $middle = $doc->createElement("middle");
465 $middle->appendChild(
466 $doc->createTextNode($user_details['mname'])
468 $StaffName->appendChild($middle);
469 $b->appendChild($StaffName);
470 $license = $doc->createElement("license");
471 $license->appendChild(
472 $doc->createTextNode($user_details['license'])
474 $b->appendChild($license);
475 $r->appendChild($b);
478 function SupervisingDoctor($doc, $r)
480 global $msg;
481 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
482 $b = $doc->createElement("SupervisingDoctor");
483 $b->setAttribute('ID', $user_details['npi']);
484 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
485 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
486 $msg = validation(xl('Supervising Doctor Last name'), $user_details['lname'], $msg);
487 $last = $doc->createElement("last");
488 $last->appendChild(
489 $doc->createTextNode($user_details['lname'])
491 $LicensedPrescriberName->appendChild($last);
492 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
493 $msg = validation(xl('Supervising Doctor First name'), $user_details['fname'], $msg);
494 $first = $doc->createElement("first");
495 $first->appendChild(
496 $doc->createTextNode($user_details['fname'])
498 $LicensedPrescriberName->appendChild($first);
499 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
500 $middle = $doc->createElement("middle");
501 $middle->appendChild(
502 $doc->createTextNode($user_details['mname'])
504 $LicensedPrescriberName->appendChild($middle);
505 $b->appendChild($LicensedPrescriberName);
506 $msg = validation(xl('Supervising Doctor DEA'), $user_details['federaldrugid'], $msg);
507 $dea = $doc->createElement("dea");
508 $dea->appendChild(
509 $doc->createTextNode($user_details['federaldrugid'])
511 $b->appendChild($dea);
512 if ($user_details['upin']) {
513 $upin = $doc->createElement("upin");
514 $upin->appendChild(
515 $doc->createTextNode($user_details['upin'])
517 $b->appendChild($upin);
520 $licenseNumber = $doc->createElement("licenseNumber");
521 $licenseNumber->appendChild(
522 $doc->createTextNode($user_details['state_license_number'])
524 $b->appendChild($licenseNumber);
525 $msg = validation(xl('Supervising Doctor NPI'), $user_details['npi'], $msg);
526 $npi = $doc->createElement("npi");
527 $npi->appendChild(
528 $doc->createTextNode($user_details['npi'])
530 $b->appendChild($npi);
531 $r->appendChild($b);
534 function MidlevelPrescriber($doc, $r)
536 global $msg;
537 $user_details = sqlQuery("SELECT * FROM users WHERE id = ?", array($_SESSION['authUserID']));
538 $b = $doc->createElement("MidlevelPrescriber");
539 $b->setAttribute('ID', $user_details['npi']);
540 $LicensedPrescriberName = $doc->createElement("LicensedPrescriberName");
541 $user_details['lname']=stripSpecialCharacter($user_details['lname']);
542 $msg = validation(xl('Midlevel Prescriber Last name'), $user_details['lname'], $msg);
543 $last = $doc->createElement("last");
544 $last->appendChild(
545 $doc->createTextNode($user_details['lname'])
547 $LicensedPrescriberName->appendChild($last);
548 $user_details['fname']=stripSpecialCharacter($user_details['fname']);
549 $msg = validation(xl('Midlevel Prescriber First name'), $user_details['fname'], $msg);
550 $first = $doc->createElement("first");
551 $first->appendChild(
552 $doc->createTextNode($user_details['fname'])
554 $LicensedPrescriberName->appendChild($first);
555 $user_details['mname']=stripSpecialCharacter($user_details['mname']);
556 $middle = $doc->createElement("middle");
557 $middle->appendChild(
558 $doc->createTextNode($user_details['mname'])
560 $LicensedPrescriberName->appendChild($middle);
561 if ($user_details['title']) {
562 $msg = validation(xl('Midlevel Prescriber Prefix'), $user_details['title'], $msg);
563 $prefix = $doc->createElement("prefix");
564 $prefix->appendChild(
565 $doc->createTextNode($user_details['title'])
567 $LicensedPrescriberName->appendChild($prefix);
570 $b->appendChild($LicensedPrescriberName);
571 $msg = validation(xl('Midlevel Prescriber DEA'), $user_details['federaldrugid'], $msg);
572 $dea = $doc->createElement("dea");
573 $dea->appendChild(
574 $doc->createTextNode($user_details['federaldrugid'])
576 $b->appendChild($dea);
577 if ($user_details['upin']) {
578 $upin = $doc->createElement("upin");
579 $upin->appendChild(
580 $doc->createTextNode($user_details['upin'])
582 $b->appendChild($upin);
585 $licenseNumber = $doc->createElement("licenseNumber");
586 $licenseNumber->appendChild(
587 $doc->createTextNode($user_details['state_license_number'])
589 $b->appendChild($licenseNumber);
590 $r->appendChild($b);
593 function Patient($doc, $r, $pid)
595 global $msg,$warning_msg,$dem_check;
596 $patient_data=sqlQuery("select *, DATE_FORMAT(DOB,'%Y%m%d') AS date_of_birth from patient_data where pid=?", array($pid));
597 $b = $doc->createElement("Patient");
598 $b->setAttribute('ID', $patient_data['pid']);
599 $PatientName = $doc->createElement("PatientName");
600 $patient_data['lname']=stripSpecialCharacter($patient_data['lname']);
601 $patient_data['lname']=trimData($patient_data['lname'], 35);
602 //$msg = validation(xl('Patient Last name'),$patient_data['lname'],$msg);
603 if ($patient_data['lname']=='') {
604 $dem_check.=htmlspecialchars(xl("Patient Last name is missing"), ENT_NOQUOTES)."<br>";
607 $last = $doc->createElement("last");
608 $last->appendChild(
609 $doc->createTextNode($patient_data['lname'])
611 $PatientName->appendChild($last);
612 $patient_data['fname']=stripSpecialCharacter($patient_data['fname']);
613 $patient_data['fname']=trimData($patient_data['fname'], 35);
614 //$msg = validation(xl('Patient First name'),$patient_data['fname'],$msg);
615 if ($patient_data['fname']=='') {
616 $dem_check.=htmlspecialchars(xl("Patient First name is missing"), ENT_NOQUOTES)."<br>";
619 $first = $doc->createElement("first");
620 $first->appendChild(
621 $doc->createTextNode($patient_data['fname'])
623 $PatientName->appendChild($first);
624 $patient_data['mname']=stripSpecialCharacter($patient_data['mname']);
625 $patient_data['mname']=trimData($patient_data['mname'], 35);
626 $middle = $doc->createElement("middle");
627 $middle->appendChild(
628 $doc->createTextNode($patient_data['mname'])
630 $PatientName->appendChild($middle);
631 $b->appendChild($PatientName);
632 $PatientAddress = $doc->createElement("PatientAddress");
633 $patient_data['street']=stripSpecialCharacter($patient_data['street']);
634 $patient_data['street']=trimData($patient_data['street'], 35);
635 $msg = validation(xl('Patient Address'), $patient_data['street'], $msg);
636 if (trim($patient_data['street'])=='') {
637 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Address is missing"), ENT_NOQUOTES);
640 $address1 = $doc->createElement("address1");
641 $address1->appendChild(
642 $doc->createTextNode($patient_data['street'])
644 $PatientAddress->appendChild($address1);
645 //$msg = validation(xl('Patient City'),$patient_data['city'],$msg);
646 if ($patient_data['city']=='') {
647 $dem_check.=htmlspecialchars(xl("Patient City is missing"), ENT_NOQUOTES)."<br>";
650 $city = $doc->createElement("city");
651 $city->appendChild(
652 $doc->createTextNode($patient_data['city'])
654 $PatientAddress->appendChild($city);
655 if ($patient_data['state']) {
656 $state = $doc->createElement("state");
657 $state->appendChild(
658 $doc->createTextNode($patient_data['state'])
660 $PatientAddress->appendChild($state);
663 if ($patient_data['postal_code']) {
664 $zip = $doc->createElement("zip");
665 $zip->appendChild(
666 $doc->createTextNode($patient_data['postal_code'])
668 $PatientAddress->appendChild($zip);
671 //$msg = validation(xl('Patient Country'),$patient_data['country_code'],$msg);
672 if (trim($patient_data['country_code'])=='' && $GLOBALS['erx_default_patient_country']=='') {
673 $dem_check.=htmlspecialchars(xl("Patient Country is missing. Also you have not set default Patient Country in Global Settings"), ENT_NOQUOTES)."<br>";
674 } elseif (trim($patient_data['country_code'])=='') {
675 $patient_data['country_code'] = $GLOBALS['erx_default_patient_country'];
678 $county_code = substr($patient_data['country_code'], 0, 2);
679 $country = $doc->createElement("country");
680 $country->appendChild(
681 $doc->createTextNode($county_code)
683 $PatientAddress->appendChild($country);
684 $b->appendChild($PatientAddress);
685 $PatientContact = $doc->createElement("PatientContact");
686 $patient_data['phone_home']=stripPhoneSlashes($patient_data['phone_home']);
687 if ($patient_data['phone_home']) {
688 $homeTelephone = $doc->createElement("homeTelephone");
689 $homeTelephone->appendChild(
690 $doc->createTextNode($patient_data['phone_home'])
692 $PatientContact->appendChild($homeTelephone);
695 $b->appendChild($PatientContact);
696 $PatientCharacteristics = $doc->createElement("PatientCharacteristics");
697 if (trim($patient_data['date_of_birth'])=='' || $patient_data['date_of_birth']=='00000000') {
698 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Date Of Birth is missing"), ENT_NOQUOTES);
701 if ($patient_data['date_of_birth'] && $patient_data['date_of_birth']!='00000000') {
702 $dob = $doc->createElement("dob");
703 $dob->appendChild(
704 $doc->createTextNode($patient_data['date_of_birth'])
706 $PatientCharacteristics->appendChild($dob);
709 if (trim($patient_data['sex'])=='') {
710 $warning_msg .= "<br>".htmlspecialchars(xl("Patient Gender is missing"), ENT_NOQUOTES);
713 if ($patient_data['sex']) {
714 $gender_val=substr($patient_data['sex'], 0, 1);
715 $gender = $doc->createElement("gender");
716 $gender->appendChild(
717 $doc->createTextNode($gender_val)
719 $PatientCharacteristics->appendChild($gender);
722 $b->appendChild($PatientCharacteristics);
723 PatientFreeformHealthplans($doc, $b, $pid);
724 $allergyId=PatientFreeformAllergy($doc, $b, $pid);
725 $r->appendChild($b);
726 return $allergyId;
729 function OutsidePrescription($doc, $r, $pid, $prescid)
731 global $msg;
732 if ($prescid) {
733 $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,
734 DATE_FORMAT(date_added,'%Y%m%d') AS date_added,CONCAT_WS(fname,' ',mname,' ',lname) AS docname,p.quantity
735 FROM prescriptions AS p
736 LEFT JOIN users AS u ON p.provider_id=u.id
737 LEFT JOIN list_options AS l1 ON l1.list_id = 'drug_form' AND l1.option_id = p.form AND l1.activity = 1
738 LEFT JOIN list_options AS l2 ON l2.list_id = 'drug_route' AND l2.option_id = p.route AND l2.activity = 1
739 LEFT JOIN list_options AS l3 ON l3.list_id = 'drug_interval' AND l3.option_id = p.interval AND l3.activity = 1
740 LEFT JOIN list_options AS l4 ON l4.list_id = 'drug_units' AND l4.option_id = p.unit AND l4.activity = 1
741 WHERE p.drug <> '' and p.id = ?", array($prescid));
742 $b = $doc->createElement("OutsidePrescription");
743 $externalId = $doc->createElement("externalId");
744 $externalId->appendChild(
745 $doc->createTextNode($prec['prescid'])
747 $b->appendChild($externalId);
748 $date = $doc->createElement("date");
749 $date->appendChild(
750 $doc->createTextNode($prec['date_added'])
752 $b->appendChild($date);
753 $doctorName = $doc->createElement("doctorName");
754 $doctorName->appendChild(
755 $doc->createTextNode($prec['docname'])
757 $b->appendChild($doctorName);
758 $s=stripSpecialCharacter($prec['drug']);
759 $sig = $doc->createElement("drug");
760 $sig->appendChild(
761 $doc->createTextNode(trimData($s, 80))
763 $b->appendChild($sig);
764 $x=stringToNumeric($prec['quantity']);
765 $dispenseNumber = $doc->createElement("dispenseNumber");
766 $dispenseNumber->appendChild(
767 $doc->createTextNode($x[0])
769 $b->appendChild($dispenseNumber);
770 $s=trimData($x[1].$prec['size']." ".$prec['title4']." ".$prec['dosage']." In ".$prec['title1']." ".$prec['title2']." ".$prec['title3'], 140);
771 $s=stripSpecialCharacter($s);
772 $sig = $doc->createElement("sig");
773 $sig->appendChild(
774 $doc->createTextNode($s)
776 $b->appendChild($sig);
777 $refillCount = $doc->createElement("refillCount");
778 $x=stringToNumeric($prec['per_refill']);
779 $refillCount->appendChild(
780 $doc->createTextNode($x[0])
782 $b->appendChild($refillCount);
783 $prescriptionType = $doc->createElement("prescriptionType");
784 $prescriptionType->appendChild(
785 $doc->createTextNode('reconcile')
787 $b->appendChild($prescriptionType);
788 $r->appendChild($b);
792 function PatientMedication($doc, $r, $pid, $med_limit)
794 global $msg;
795 $active='';
796 if ($GLOBALS['erx_upload_active']==1) {
797 $active = " and (enddate is null or enddate = '' or enddate = '0000-00-00' )";
800 $res_med=sqlStatement("select * from lists where type='medication' and pid=? and title<>''
801 and erx_uploaded='0' $active order by enddate limit 0,$med_limit", array($pid));
802 $uploaded_med_arr="";
803 while ($row_med=sqlFetchArray($res_med)) {
804 $uploaded_med_arr[]=$row_med['id'];
805 $b = $doc->createElement("OutsidePrescription");
806 $externalId = $doc->createElement("externalId");
807 $externalId->appendChild(
808 $doc->createTextNode($row_med['id'])
810 $b->appendChild($externalId);
811 $date = $doc->createElement("date");
812 $date->appendChild(
813 $doc->createTextNode($row_med['begdate'])
815 $b->appendChild($date);
816 $doctorName = $doc->createElement("doctorName");
817 $doctorName->appendChild(
818 $doc->createTextNode("")
820 $b->appendChild($doctorName);
821 $row_med['title'] = stripSpecialCharacter($row_med['title']);
822 $sig = $doc->createElement("drug");
823 $sig->appendChild(
824 $doc->createTextNode(trimData($row_med['title'], 80))
826 $b->appendChild($sig);
827 $dispenseNumber = $doc->createElement("dispenseNumber");
828 $dispenseNumber->appendChild(
829 $doc->createTextNode($prec['quantity'])
831 $b->appendChild($dispenseNumber);
832 $sig = $doc->createElement("sig");
833 $sig->appendChild(
834 $doc->createTextNode("")
836 $b->appendChild($sig);
837 $refillCount = $doc->createElement("refillCount");
838 $refillCount->appendChild(
839 $doc->createTextNode("")
841 $b->appendChild($refillCount);
842 $prescriptionType = $doc->createElement("prescriptionType");
843 $prescriptionType->appendChild(
844 $doc->createTextNode('reconcile')
846 $b->appendChild($prescriptionType);
847 $r->appendChild($b);
850 return $uploaded_med_arr;
853 function PatientFreeformAllergy($doc, $r, $pid)
855 $res=sqlStatement("SELECT id,l.title as title1,lo.title as title2,comments FROM lists AS l
856 LEFT JOIN list_options AS lo ON l.outcome = lo.option_id AND lo.list_id = 'outcome' AND lo.activity = 1
857 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));
858 $allergyId=array();
859 while ($row=sqlFetchArray($res)) {
860 $val=array();
861 $val['id']=$row['id'];
862 $val['title1']=$row['title1'];
863 $val['title2']=$row['title2'];
864 $val['comments']=$row['comments'];
865 $b = $doc->createElement("PatientFreeformAllergy");
866 $b->setAttribute('ID', $val['id']);
867 if ($val['title1']) {
868 $allergyName = $doc->createElement("allergyName");
869 $allergyName->appendChild(
870 $doc->createTextNode(trimData(stripSpecialCharacter($val['title1']), 70))
872 $b->appendChild($allergyName);
875 if ($val['title2'] && ($val['title2']=='Mild' || $val['title2']=='Moderate' || $val['title2']=='Severe')) {
876 $allergySeverityTypeID = $doc->createElement("allergySeverityTypeID");
877 $allergySeverityTypeID->appendChild(
878 $doc->createTextNode($val['title2'])
880 $b->appendChild($allergySeverityTypeID);
883 if ($val['comments']) {
884 $allergyComment = $doc->createElement("allergyComment");
885 $allergyComment->appendChild(
886 $doc->createTextNode(trimData(stripSpecialCharacter($val['comments']), 200))
888 $b->appendChild($allergyComment);
891 $r->appendChild($b);
892 $allergyId[]=$row['id'];
895 return $allergyId;
898 function PatientFreeformHealthplans($doc, $r, $pid)
900 $resource = sqlStatement(
901 'SELECT
902 `ins`.`name`
903 FROM (
904 SELECT
905 `id`.`type`,
906 `ic`.`name`
907 FROM `insurance_data` AS `id`
908 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
909 WHERE `id`.`pid` = ?
910 AND `id`.`subscriber_relationship` = \'self\'
911 AND `id`.`provider` > 0
912 ORDER BY `id`.`date` DESC
913 ) AS `ins`
914 GROUP BY `ins`.`type`;',
915 array($pid)
918 while ($row = sqlFetchArray($resource)) {
919 $healthplanName = $doc->createElement('healthplanName');
920 $healthplanName->appendChild($doc->createTextNode(
921 stripSpecialCharacter(trimData($row['name'], 35))
924 $patientFreeformHealthplans = $doc->createElement('PatientFreeformHealthplans');
925 $patientFreeformHealthplans->appendChild($healthplanName);
927 $r->appendChild($patientFreeformHealthplans);
931 function PrescriptionRenewalResponse($doc, $r, $pid)
933 $b = $doc->createElement("PrescriptionRenewalResponse");
934 $renewalRequestIdentifier = $doc->createElement("renewalRequestIdentifier");
935 $renewalRequestIdentifier->appendChild(
936 $doc->createTextNode('cbf51649-ce3c-44b8-8f91-6fda121a353d')
938 $b->appendChild($renewalRequestIdentifier);
939 $responseCode = $doc->createElement("responseCode");
940 $responseCode->appendChild(
941 $doc->createTextNode('Undetermined')
943 $b->appendChild($responseCode);
944 $r->appendChild($b);
947 function checkError($xml)
949 $ch = curl_init($xml);
951 $data = array('RxInput' => $xml);
953 curl_setopt($ch, CURLOPT_URL, getErxPath());
954 curl_setopt($ch, CURLOPT_POST, 1);
955 curl_setopt($ch, CURLOPT_POSTFIELDS, "RxInput=".$xml);
956 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
957 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
958 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
959 //curl_setopt($ch, CURLOPT_HEADER, 0);
960 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
961 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
962 curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
963 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
964 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
966 $result=curl_exec($ch) or die(curl_error($ch)) ;
967 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $error_message);
968 if (strpos($result, 'RxEntry.aspx')) {
969 erx_error_log($xml);
970 erx_error_log($result);
973 $arr=explode('Error', $error_message[1]);
974 //echo "Te: ".count($arr);
975 //print_r($arr);
976 if (count($arr)==1) {
977 echo nl2br($error_message[1]);
978 } else {
979 for ($i=1; $i<count($arr); $i++) {
980 echo $arr[$i]."<br><br>";
984 curl_close($ch);
985 if (strpos($result, 'RxEntry.aspx')) {
986 return '1';
987 } else {
988 return '0';
992 function erx_error_log($message)
994 $date = date("Y-m-d");
995 if (!is_dir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error')) {
996 mkdir($GLOBALS['OE_SITE_DIR'].'/documents/erx_error', 0777, true);
999 $filename = $GLOBALS['OE_SITE_DIR']."/documents/erx_error/erx_error"."-".$date.".log";
1000 $f=fopen($filename, 'a');
1001 fwrite($f, date("Y-m-d H:i:s")." ==========> ".$message."\r\n");
1002 fclose($f);
1005 function stripStrings($str, $pattern)
1007 $result = $str;
1008 foreach ($pattern as $key => $value) {
1009 $result = preg_replace("/$key/", $value, $result);
1012 return $result;