Fix misspelt variable name (#4830)
[openemr.git] / interface / eRxXMLBuilder.php
blob8edc769867134e290ff86a111df89568e08048cd
1 <?php
3 /**
4 * interface/eRxXMLBuilder.php Functions for building NewCrop XML.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Sam Likins <sam.likins@wsi-services.com>
9 * @author Ken Chapple <ken@mi-squared.com>
10 * @copyright Copyright (c) 2015 Sam Likins <sam.likins@wsi-services.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once(__DIR__ . "/../library/patient.inc");
16 class eRxXMLBuilder
18 private $globals;
19 private $store;
21 private $document;
22 private $ncScript;
24 private $sentAllergyIds = array();
25 private $sentMedicationIds = array();
26 private $sentPrescriptionIds = array();
28 private $fieldEmptyMessages = array();
29 private $demographicsCheckMessages = array();
30 private $warningMessages = array();
32 public function __construct($globals = null, $store = null)
34 if ($globals) {
35 $this->setGlobals($globals);
38 if ($store) {
39 $this->setStore($store);
43 /**
44 * Set Globals for retrieving eRx global configurations
45 * @param object $globals The eRx Globals object to use for processing
46 * @return eRxPage This object is returned for method chaining
48 public function setGlobals($globals)
50 $this->globals = $globals;
52 return $this;
55 /**
56 * Get Globals for retrieving eRx global configurations
57 * @return object The eRx Globals object to use for processing
59 public function getGlobals()
61 return $this->globals;
64 /**
65 * Set Store to handle eRx cashed data
66 * @param object $store The eRx Store object to use for processing
67 * @return eRxPage This object is returned for method chaining
69 public function setStore($store)
71 $this->store = $store;
73 return $this;
76 /**
77 * Get Store for handling eRx cashed data
78 * @return object The eRx Store object to use for processing
80 public function getStore()
82 return $this->store;
85 protected function trimData($string, $length)
87 return substr($string, 0, $length - 1);
90 protected function stripSpecialCharacter($string)
92 return preg_replace('/[^a-zA-Z0-9 \'().,#:\/\-@_%]/', '', $string);
95 public function checkError($xml)
97 $curlHandler = curl_init($xml);
98 $sitePath = $this->getGlobals()->getOpenEMRSiteDirectory();
99 $data = array('RxInput' => $xml);
101 curl_setopt($curlHandler, CURLOPT_URL, $this->getGlobals()->getPath());
102 curl_setopt($curlHandler, CURLOPT_POST, 1);
103 curl_setopt($curlHandler, CURLOPT_POSTFIELDS, 'RxInput=' . $xml);
104 curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, 0);
105 curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1);
106 curl_setopt($curlHandler, CURLOPT_COOKIESESSION, true);
107 curl_setopt($curlHandler, CURLOPT_COOKIEFILE, $sitePath . '/newcrop-cookiefile');
108 curl_setopt($curlHandler, CURLOPT_COOKIEJAR, $sitePath . '/newcrop-cookiefile');
109 curl_setopt($curlHandler, CURLOPT_COOKIE, session_name() . '=' . session_id());
110 curl_setopt($curlHandler, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
111 curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
113 $result = curl_exec($curlHandler) or die(curl_error($curlHandler));
115 curl_close($curlHandler);
117 return $result;
120 public function addSentAllergyIds($allergyIds)
122 if (is_array($allergyIds)) {
123 foreach ($allergyIds as $allergyId) {
124 $this->sentAllergyIds[] = $allergyId;
126 } else {
127 $this->sentAllergyIds[] = $allergyIds;
131 public function getSentAllergyIds()
133 return $this->sentAllergyIds;
136 public function addSentMedicationIds($medicationIds)
138 if (is_array($medicationIds)) {
139 foreach ($medicationIds as $medicationId) {
140 $this->sentMedicationIds[] = $medicationId;
142 } else {
143 $this->sentMedicationIds[] = $medicationIds;
147 public function getSentMedicationIds()
149 return $this->sentMedicationIds;
152 public function addSentPrescriptionId($prescriptionIds)
154 if (is_array($prescriptionIds)) {
155 foreach ($prescriptionIds as $prescriptionId) {
156 $this->sentPrescriptionIds[] = $prescriptionId;
158 } else {
159 $this->sentPrescriptionIds[] = $prescriptionIds;
163 public function getSentPrescriptionIds()
165 return $this->sentPrescriptionIds;
168 public function fieldEmpty($value, $message)
170 if (empty($value)) {
171 $this->fieldEmptyMessages[] = $message;
175 public function getFieldEmptyMessages()
177 return $this->fieldEmptyMessages;
180 public function demographicsCheck($value, $message)
182 if (empty($value)) {
183 $this->demographicsCheckMessages[] = $message;
187 public function getDemographicsCheckMessages()
189 return $this->demographicsCheckMessages;
192 public function warningMessage($value, $message)
194 if (empty($value)) {
195 $this->warningMessages[] = $message;
199 public function getWarningMessages()
201 return $this->warningMessages;
204 public function createElementText($name, $value)
206 $element = $this->getDocument()->createElement($name);
207 $element->appendChild($this->getDocument()->createTextNode($value));
209 return $element;
212 public function createElementTextFieldEmpty($name, $value, $validationText)
214 $this->fieldEmpty($value, $validationText);
216 return $this->createElementText($name, $value);
219 public function createElementTextDemographicsCheck($name, $value, $validationText)
221 $this->demographicsCheck($value, $validationText);
223 return $this->createElementText($name, $value);
226 public function appendChildren($element, &$children)
228 if (is_a($element, 'DOMNode') && is_array($children) && count($children)) {
229 foreach ($children as $child) {
230 // if(is_array($child)) {
231 // $this->appendChildren($element, $child);
232 // }
233 $element->appendChild($child);
238 public function getDocument()
240 if ($this->document === null) {
241 $this->document = new DOMDocument();
242 $this->document->formatOutput = true;
245 return $this->document;
248 public function getNCScript()
250 if ($this->ncScript === null) {
251 $document = $this->getDocument();
253 $this->ncScript = $document->createElement('NCScript');
254 $this->ncScript->setAttribute('xmlns', 'http://secure.newcropaccounts.com/interfaceV7');
255 $this->ncScript->setAttribute('xmlns:NCStandard', 'http://secure.newcropaccounts.com/interfaceV7:NCStandard');
256 $this->ncScript->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
258 $document->appendChild($this->ncScript);
261 return $this->ncScript;
264 public function getCredentials()
266 $eRxCredentials = $this->getGlobals()
267 ->getCredentials();
269 $element = $this->getDocument()->createElement('Credentials');
270 $element->appendChild($this->createElementTextFieldEmpty('partnerName', $eRxCredentials['0'], xl('NewCrop eRx Partner Name')));
271 $element->appendChild($this->createElementTextFieldEmpty('name', $eRxCredentials['1'], xl('NewCrop eRx Account Name')));
272 $element->appendChild($this->createElementTextFieldEmpty('password', $eRxCredentials['2'], xl('NewCrop eRx Password')));
273 $element->appendChild($this->createElementText('productName', 'OpenEMR'));
274 $element->appendChild($this->createElementText('productVersion', $this->getGlobals()->getOpenEMRVersion()));
276 return $element;
279 public function getUserRole($authUserId)
281 $eRxUserRole = $this->getStore()
282 ->getUserById($authUserId);
284 $eRxUserRole = $eRxUserRole['newcrop_user_role'];
286 $this->fieldEmpty($eRxUserRole, xl('NewCrop eRx User Role'));
287 if (!$eRxUserRole) {
288 echo xlt('Unauthorized access to ePrescription');
289 die;
292 $eRxUserRole = preg_replace('/erx/', '', $eRxUserRole);
294 switch ($eRxUserRole) {
295 case 'admin':
296 case 'manager':
297 case 'nurse':
298 $newCropUser = 'Staff';
299 break;
300 case 'doctor':
301 $newCropUser = 'LicensedPrescriber';
302 break;
303 case 'supervisingDoctor':
304 $newCropUser = 'SupervisingDoctor';
305 break;
306 case 'midlevelPrescriber':
307 $newCropUser = 'MidlevelPrescriber';
308 break;
309 default:
310 $newCropUser = '';
313 $element = $this->getDocument()->createElement('UserRole');
314 $element->appendChild($this->createElementTextFieldEmpty('user', $newCropUser, xl('NewCrop eRx User Role * invalid selection *')));
315 $element->appendChild($this->createElementText('role', $eRxUserRole));
317 return $element;
320 public function getDestination($authUserId, $page = null)
322 $eRxUserRole = $this->getStore()
323 ->getUserById($authUserId);
325 $eRxUserRole = $eRxUserRole['newcrop_user_role'];
327 $eRxUserRole = preg_replace('/erx/', '', $eRxUserRole);
329 if (!$page) {
330 if ($eRxUserRole == 'admin') {
331 $page = 'admin';
332 } elseif ($eRxUserRole == 'manager') {
333 $page = 'manager';
334 } else {
335 $page = 'compose';
339 $element = $this->getDocument()->createElement('Destination');
340 $element->appendChild($this->createElementText('requestedPage', $page));
342 return $element;
345 public function getAccountAddress($facility)
347 $postalCode = preg_replace('/[^0-9]/', '', $facility['postal_code']);
348 $postalCodePostfix = substr($postalCode, 5, 4);
349 $postalCode = substr($postalCode, 0, 5);
351 if (strlen($postalCode) < 5) {
352 $this->fieldEmpty('', xl('Primary Facility Zip Code'));
355 $element = $this->getDocument()->createElement('AccountAddress');
356 $element->appendChild($this->createElementTextFieldEmpty('address1', $this->trimData($this->stripSpecialCharacter($facility['street']), 35), xl('Primary Facility Street Address')));
357 $element->appendChild($this->createElementTextFieldEmpty('city', $facility['city'], xl('Primary Facility City')));
358 $element->appendChild($this->createElementTextFieldEmpty('state', $facility['state'], xl('Primary Facility State')));
359 $element->appendChild($this->createElementText('zip', $postalCode));
360 if (strlen($postalCodePostfix) == 4) {
361 $element->appendChild($this->createElementText('zip4', $postalCodePostfix));
364 $element->appendChild($this->createElementTextFieldEmpty('country', substr($facility['country_code'], 0, 2), xl('Primary Facility Country code')));
366 return $element;
369 public function getAccount()
371 $facility = $this->getStore()
372 ->getFacilityPrimary();
374 if (!$facility['federal_ein']) {
375 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.');
376 die;
379 $element = $this->getDocument()->createElement('Account');
380 $element->setAttribute('ID', $this->getGlobals()->getAccountId());
381 $element->appendChild($this->createElementTextFieldEmpty('accountName', $this->trimData($this->stripSpecialCharacter($facility['name']), 35), xl('Facility Name')));
382 $element->appendChild($this->createElementText('siteID', $facility['federal_ein'], 'Site ID'));
383 $element->appendChild($this->getAccountAddress($facility));
384 $element->appendChild($this->createElementTextFieldEmpty('accountPrimaryPhoneNumber', preg_replace('/[^0-9]/', '', $facility['phone']), xl('Facility Phone')));
385 $element->appendChild($this->createElementTextFieldEmpty('accountPrimaryFaxNumber', preg_replace('/[^0-9]/', '', $facility['fax']), xl('Facility Fax')));
387 return $element;
390 public function getLocationAddress($facility)
392 $postalCode = preg_replace('/[^0-9]/', '', $facility['postal_code']);
393 $postalCodePostfix = substr($postalCode, 5, 4);
394 $postalCode = substr($postalCode, 0, 5);
396 if (strlen($postalCode) < 5) {
397 $this->fieldEmpty('', xl('Facility Zip Code'));
400 $element = $this->getDocument()->createElement('LocationAddress');
401 if ($facility['street']) {
402 $element->appendChild($this->createElementText('address1', $this->trimData($this->stripSpecialCharacter($facility['street']), 35)));
405 if ($facility['city']) {
406 $element->appendChild($this->createElementText('city', $facility['city']));
409 if ($facility['state']) {
410 $element->appendChild($this->createElementText('state', $facility['state']));
413 $element->appendChild($this->createElementText('zip', $postalCode));
414 if (strlen($postalCodePostfix) == 4) {
415 $element->appendChild($this->createElementText('zip4', $postalCodePostfix));
418 if ($facility['country_code']) {
419 $element->appendChild($this->createElementText('country', substr($facility['country_code'], 0, 2)));
422 return $element;
425 public function getLocation($authUserId)
427 $userFacility = $this->getStore()
428 ->getUserFacility($authUserId);
430 $element = $this->getDocument()->createElement('Location');
431 $element->setAttribute('ID', $userFacility['id']);
432 $element->appendChild($this->createElementText('locationName', $this->trimData($this->stripSpecialCharacter($userFacility['name']), 35)));
433 $element->appendChild($this->getLocationAddress($userFacility));
434 if ($userFacility['phone']) {
435 $element->appendChild($this->createElementText('primaryPhoneNumber', preg_replace('/[^0-9]/', '', $userFacility['phone'])));
438 if ($userFacility['fax']) {
439 $element->appendChild($this->createElementText('primaryFaxNumber', preg_replace('/[^0-9]/', '', $userFacility['fax'])));
442 if ($userFacility['phone']) {
443 $element->appendChild($this->createElementText('pharmacyContactNumber', preg_replace('/[^0-9]/', '', $userFacility['phone'])));
446 return $element;
449 public function getLicensedPrescriber($authUserId)
451 $userDetails = $this->getStore()
452 ->getUserById($authUserId);
454 $element = $this->getDocument()->createElement('LicensedPrescriber');
455 $element->setAttribute('ID', $userDetails['npi']);
456 $element->appendChild($this->getLicensedPrescriberName($userDetails, xl('Licensed Prescriber')));
457 $element->appendChild($this->createElementTextFieldEmpty('dea', $userDetails['federaldrugid'], 'Licensed Prescriber DEA'));
458 if ($userDetails['upin']) {
459 $element->appendChild($this->createElementText('upin', $userDetails['upin']));
462 $element->appendChild($this->createElementText('licenseNumber', $userDetails['state_license_number']));
463 $element->appendChild($this->createElementTextFieldEmpty('npi', $userDetails['npi'], xl('Licensed Prescriber NPI')));
465 return $element;
468 public function getStaffName($user)
470 $element = $this->getDocument()->createElement('StaffName');
471 $element->appendChild($this->createElementText('last', $this->stripSpecialCharacter($user['lname'])));
472 $element->appendChild($this->createElementText('first', $this->stripSpecialCharacter($user['fname'])));
473 $element->appendChild($this->createElementText('middle', $this->stripSpecialCharacter($user['mname'])));
475 return $element;
478 public function getStaff($authUserId)
480 $userDetails = $this->getStore()
481 ->getUserById($authUserId);
483 $element = $this->getDocument()->createElement('Staff');
484 $element->setAttribute('ID', $userDetails['username']);
485 $element->appendChild($this->getStaffName($userDetails));
486 $element->appendChild($this->createElementText('license', $userDetails['state_license_number']));
488 return $element;
491 public function getLicensedPrescriberName($user, $prescriberType, $prefix = false)
493 $element = $this->getDocument()->createElement('LicensedPrescriberName');
494 $element->appendChild($this->createElementTextFieldEmpty('last', $this->stripSpecialCharacter($user['lname']), $prescriberType . ' ' . xl('Licensed Prescriber Last Name')));
495 $element->appendChild($this->createElementTextFieldEmpty('first', $this->stripSpecialCharacter($user['fname']), $prescriberType . ' ' . xl('Licensed Prescriber First Name')));
496 $element->appendChild($this->createElementText('middle', $this->stripSpecialCharacter($user['mname'])));
497 if ($prefix && $user['title']) {
498 $element->appendChild($this->createElementTextFieldEmpty('prefix', $user['title'], $prescriberType . ' ' . xl('Licensed Prescriber Title (Prefix)')));
501 return $element;
504 public function getSupervisingDoctor($authUserId)
506 $userDetails = $this->getStore()
507 ->getUserById($authUserId);
509 $element = $this->getDocument()->createElement('SupervisingDoctor');
510 $element->setAttribute('ID', $userDetails['npi']);
511 $element->appendChild($this->getLicensedPrescriberName($userDetails, xl('Supervising Doctor')));
512 $element->appendChild($this->createElementTextFieldEmpty('dea', $userDetails['federaldrugid'], xl('Supervising Doctor DEA')));
513 if ($userDetails['upin']) {
514 $element->appendChild($this->createElementText('upin', $userDetails['upin']));
517 $element->appendChild($this->createElementText('licenseNumber', $userDetails['state_license_number']));
518 $element->appendChild($this->createElementTextFieldEmpty('npi', $userDetails['npi'], xl('Supervising Doctor NPI')));
520 return $element;
523 public function getMidlevelPrescriber($authUserId)
525 $userDetails = $this->getStore()
526 ->getUserById($authUserId);
528 $element = $this->getDocument()->createElement('MidlevelPrescriber');
529 $element->setAttribute('ID', $userDetails['npi']);
530 $element->appendChild($this->getLicensedPrescriberName($userDetails, xl('Midlevel Prescriber'), true));
531 $element->appendChild($this->createElementTextFieldEmpty('dea', $userDetails['federaldrugid'], xl('Midlevel Prescriber DEA')));
532 if ($userDetails['upin']) {
533 $element->appendChild($this->createElementText('upin', $userDetails['upin']));
536 $element->appendChild($this->createElementText('licenseNumber', $userDetails['state_license_number']));
537 $element->appendChild($this->createElementTextFieldEmpty('npi', $userDetails['npi'], xl('Midlevel Prescriber NPI')));
539 return $element;
542 public function getStaffElements($authUserId, $destination)
544 $userRole = $this->getStore()->getUserById($authUserId);
545 $userRole = preg_replace('/erx/', '', $userRole['newcrop_user_role']);
547 $elements = array();
549 if ($userRole != 'manager') {
550 $elements[] = $this->getLocation($authUserId);
553 if ($userRole == 'doctor' || $destination == 'renewal') {
554 $elements[] = $this->getLicensedPrescriber($authUserId);
557 if ($userRole == 'manager' || $userRole == 'admin' || $userRole == 'nurse') {
558 $elements[] = $this->getStaff($authUserId);
559 } elseif ($userRole == 'supervisingDoctor') {
560 $elements[] = $this->getSupervisingDoctor($authUserId);
561 } elseif ($userRole == 'midlevelPrescriber') {
562 $elements[] = $this->getMidlevelPrescriber($authUserId);
565 return $elements;
568 public function getPatientName($patient)
570 $element = $this->getDocument()->createElement('PatientName');
571 $element->appendChild($this->createElementTextDemographicsCheck('last', $this->trimData($this->stripSpecialCharacter($patient['lname']), 35), xl('Patient Last Name')));
572 $element->appendChild($this->createElementTextDemographicsCheck('first', $this->trimData($this->stripSpecialCharacter($patient['fname']), 35), xl('Patient First Name')));
573 $element->appendChild($this->createElementText('middle', $this->trimData($this->stripSpecialCharacter($patient['mname']), 35)));
575 return $element;
578 public function getPatientAddress($patient)
580 $patient['street'] = $this->trimData($this->stripSpecialCharacter($patient['street']), 35);
581 $this->warningMessage($patient['street'], xl('Patient Street Address'));
584 if (trim($patient['country_code']) == '') {
585 $eRxDefaultPatientCountry = $this->getGlobals()->getDefaultPatientCountry();
587 if ($eRxDefaultPatientCountry == '') {
588 $this->demographicsCheck('', xl('Global Default Patient Country'));
589 } else {
590 $patient['country_code'] = $eRxDefaultPatientCountry;
593 $this->demographicsCheck('', xl('Patient Country'));
596 $element = $this->getDocument()->createElement('PatientAddress');
597 $element->appendChild($this->createElementTextFieldEmpty('address1', $patient['street'], xl('Patient Street Address')));
598 $element->appendChild($this->createElementTextDemographicsCheck('city', $patient['city'], xl('Patient City')));
599 if ($patient['state']) {
600 $element->appendChild($this->createElementText('state', $patient['state']));
603 if ($patient['postal_code']) {
604 $element->appendChild($this->createElementText('zip', $patient['postal_code']));
607 $element->appendChild($this->createElementText('country', substr($patient['country_code'], 0, 2)));
609 return $element;
612 public function getPatientContact($patient)
614 $element = $this->getDocument()->createElement('PatientContact');
615 if ($patient['phone_home']) {
616 $element->appendChild($this->createElementText('homeTelephone', preg_replace('/-/', '', $patient['phone_home'])));
619 return $element;
622 public function getPatientCharacteristics($patient)
624 if (trim($patient['date_of_birth']) == '' || $patient['date_of_birth'] == '00000000') {
625 $this->warningMessage('', xl('Patient Date Of Birth'));
628 $this->warningMessage(trim($patient['sex']), xl('Patient Gender'));
630 $element = $this->getDocument()->createElement('PatientCharacteristics');
631 if ($patient['date_of_birth'] && $patient['date_of_birth'] != '00000000') {
632 $element->appendChild($this->createElementText('dob', $patient['date_of_birth']));
635 if ($patient['sex']) {
636 $element->appendChild($this->createElementText('gender', substr($patient['sex'], 0, 1)));
639 $vitals = $this->getStore()->getPatientVitalsByPatientId($patient['pid']);
640 $age = getPatientAgeYMD($patient['date_of_birth']);
642 if (
643 $vitals['height'] &&
644 $vitals['height_units']
646 $element->appendChild($this->createElementText('height', $vitals['height']));
647 $element->appendChild($this->createElementText('heightUnits', $vitals['height_units']));
648 } elseif ($age['age'] < 19) {
649 $this->warningMessage('', xl('Patient Height Vital is required under age 19'));
652 if (
653 $vitals['weight'] &&
654 $vitals['weight_units']
656 $element->appendChild($this->createElementText('weight', $vitals['weight']));
657 $element->appendChild($this->createElementText('weightUnits', $vitals['weight_units']));
658 } elseif ($age['age'] < 19) {
659 $this->warningMessage('', xl('Patient Weight Vital is required under age 19'));
662 return $element;
665 public function getPatientFreeformHealthplans($patientId)
667 $healthplans = $this->getStore()
668 ->getPatientHealthplansByPatientId($patientId);
670 $elements = array();
672 while ($healthplan = sqlFetchArray($healthplans)) {
673 $element = $this->getDocument()->createElement('PatientFreeformHealthplans');
674 $element->appendChild($this->createElementText('healthplanName', $this->trimData($this->stripSpecialCharacter($healthplan['name']), 35)));
676 $elements[] = $element;
679 return $elements;
682 public function getPatientFreeformAllergy($patientId)
684 $allergyData = $this->getStore()
685 ->getPatientAllergiesByPatientId($patientId);
687 $elements = array();
689 while ($allergy = sqlFetchArray($allergyData)) {
690 $element = $this->getDocument()->createElement('PatientFreeformAllergy');
691 $element->setAttribute('ID', $allergy['id']);
693 if ($allergy['title1']) {
694 $element->appendChild($this->createElementText('allergyName', $this->trimData($this->stripSpecialCharacter($allergy['title1']), 70)));
697 if ($allergy['title2'] == 'Mild' || $allergy['title2'] == 'Moderate' || $allergy['title2'] == 'Severe') {
698 $element->appendChild($this->createElementText('allergySeverityTypeID', $allergy['title2']));
701 if ($allergy['comments']) {
702 $element->appendChild($this->createElementText('allergyComment', $this->trimData($this->stripSpecialCharacter($allergy['comments']), 200)));
705 $elements[] = $element;
707 $this->addSentAllergyIds($allergy['id']);
710 return $elements;
713 public function getPatientDiagnosis($patientId)
715 $diagnosisData = $this->getStore()
716 ->getPatientDiagnosisByPatientId($patientId);
718 $elements = array();
719 while ($diagnosis = sqlFetchArray($diagnosisData)) {
720 if ($diagnosis['diagnosis']) {
721 // For issues that have multiple diagnosis coded, they are semicolon-separated
722 // explode() will return an array containing the individual diagnosis if there is no semicolon
723 $multiple = explode(";", $diagnosis['diagnosis']);
724 foreach ($multiple as $individual) {
725 $element = $this->getDocument()->createElement('PatientDiagnosis');
726 $res = explode(":", $individual); //split diagnosis type and code
728 $element->appendChild($this->createElementText('diagnosisID', $res[1]));
729 $element->appendChild($this->createElementText('diagnosisType', $res[0]));
731 if ($diagnosis['begdate']) {
732 $element->appendChild($this->createElementText('onsetDate', str_replace("-", "", $diagnosis['begdate'])));
735 if ($diagnosis['title']) {
736 $element->appendChild($this->createElementText('diagnosisName', $diagnosis['title']));
739 if ($diagnosis['date']) {
740 $date = new DateTime($diagnosis['date']);
741 $element->appendChild($this->createElementText('recordedDate', date_format($date, 'Ymd')));
744 $elements[] = $element;
749 return $elements;
752 public function getPatient($patientId)
754 $patientData = $this->getStore()
755 ->getPatientByPatientId($patientId);
757 $element = $this->getDocument()->createElement('Patient');
758 $element->setAttribute('ID', $patientData['pid']);
759 $element->appendChild($this->getPatientName($patientData));
760 $element->appendChild($this->getPatientAddress($patientData));
761 $element->appendChild($this->getPatientContact($patientData));
762 $element->appendChild($this->getPatientCharacteristics($patientData));
763 $this->appendChildren($element, $this->getPatientDiagnosis($patientId));
764 $this->appendChildren($element, $this->getPatientFreeformHealthplans($patientId));
765 $this->appendChildren($element, $this->getPatientFreeformAllergy($patientId));
767 return $element;
770 public function getOutsidePrescription($prescription)
772 $element = $this->getDocument()->createElement('OutsidePrescription');
773 $element->appendChild($this->createElementText('externalId', $prescription['externalId']));
774 $element->appendChild($this->createElementText('date', $prescription['date']));
775 $element->appendChild($this->createElementText('doctorName', $prescription['doctorName']));
776 $element->appendChild($this->createElementText('drug', $prescription['drug']));
777 $element->appendChild($this->createElementText('dispenseNumber', $prescription['dispenseNumber']));
778 $element->appendChild($this->createElementText('sig', $prescription['sig']));
779 $element->appendChild($this->createElementText('refillCount', $prescription['refillCount']));
780 $element->appendChild($this->createElementText('prescriptionType', $prescription['prescriptionType']));
782 return $element;
785 public function getPatientPrescriptions($prescriptionIds)
787 $elements = array();
789 foreach ($prescriptionIds as $prescriptionId) {
790 if ($prescriptionId) {
791 $prescription = $this->getStore()
792 ->getPrescriptionById($prescriptionId);
794 $element = $this->getOutsidePrescription(array(
795 'externalId' => $prescription['prescid'],
796 'date' => $prescription['date_added'],
797 'doctorName' => $prescription['docname'],
798 'drug' => $this->trimData($this->stripSpecialCharacter($prescription['drug']), 80),
799 'dispenseNumber' => intval($prescription['quantity']),
800 'sig' => $this->trimData($this->stripSpecialCharacter($prescription['quantity'][1] . $prescription['size'] . ' ' . $prescription['title4'] . ' ' . $prescription['dosage'] . ' In ' . $prescription['title1'] . ' ' . $prescription['title2'] . ' ' . $prescription['title3']), 140),
801 'refillCount' => intval($prescription['per_refill']),
802 'prescriptionType' => 'reconcile'
805 $this->addSentPrescriptionId($prescriptionId);
807 $elements[] = $element;
811 return $elements;
814 public function getPatientMedication($patientId, $uploadActive, $count)
816 $medications = $this->getStore()
817 ->selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $count);
819 $elements = array();
821 while ($medication = sqlFetchArray($medications)) {
822 $elements[] = $this->getOutsidePrescription(array(
823 'externalId' => $medication['id'],
824 'date' => $medication['begdate'],
825 'doctorName' => '',
826 'drug' => $this->trimData($medication['title'], 80),
827 'dispenseNumber' => '',
828 'sig' => '',
829 'refillCount' => '',
830 'prescriptionType' => 'reconcile'
833 $this->addSentMedicationIds($medication['id']);
836 return $elements;
839 public function getPatientElements($patientId, $totalCount, $requestedPrescriptionIds)
841 $elements = array();
843 if ($patientId) {
844 $uploadActive = $this->getGlobals()->getUploadActive();
846 $elements[] = $this->getPatient($patientId);
848 $selectPrescriptionIds = $this->getStore()
849 ->selectPrescriptionIdsNotUploadedByPatientId(
850 $patientId,
851 $uploadActive,
852 $totalCount
855 $selectPrescriptionIdsCount = sqlNumRows($selectPrescriptionIds);
857 $prescriptionIds = array();
859 while ($selectPrescriptionId = sqlFetchArray($selectPrescriptionIds)) {
860 $prescriptionIds[] = $selectPrescriptionId['id'];
863 if (
864 is_array($requestedPrescriptionIds) &&
865 count($requestedPrescriptionIds) > 0
867 $elements = array_merge($elements, $this->getPatientPrescriptions($requestedPrescriptionIds));
868 } elseif (
869 is_array($requestedPrescriptionIds) &&
870 count($prescriptionIds) > 0
872 $elements = array_merge($elements, $this->getPatientPrescriptions($prescriptionIds));
873 } else {
874 $this->getPatientPrescriptions(array(0));
877 if ($selectPrescriptionIdsCount < $totalCount) {
878 $elements = array_merge($elements, $this->getPatientMedication($patientId, $uploadActive, $totalCount - $selectPrescriptionIdsCount));
882 return $elements;