public function processMarkResolvedAction()
 {
     $messagingId = $this->_getParam('messagingId');
     $messaging = new Messaging();
     $messaging->messagingId = $messagingId;
     $ret = false;
     if ($messaging->populate()) {
         $messaging->unresolved = 0;
         $messaging->persist();
         $ret = true;
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct($ret);
 }
示例#2
0
 public static function handlereFaxAct(Audit $audit, array $sourceData)
 {
     if ($audit->objectClass != 'ESignature') {
         return false;
     }
     $eSignature = new ESignature();
     $eSignature->eSignatureId = $audit->objectId;
     $eSignature->populate();
     if ($eSignature->objectClass != 'Medication') {
         return false;
     }
     $medication = new Medication();
     $medication->medicationId = $eSignature->objectId;
     $medication->populate();
     $audit = $sourceData['_audit'];
     $messaging = new Messaging(Messaging::TYPE_OUTBOUND_FAX);
     $messaging->messagingId = (int) $sourceData['transmissionId'];
     $messaging->transmissionId = $messaging->messagingId;
     $messaging->populate();
     $messaging->objectId = $messaging->messagingId;
     $messaging->objectClass = $audit->objectClass;
     $messaging->status = 'Faxed';
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->auditId = $audit->auditId;
     // this must be required for retransmission in case of error
     $messaging->persist();
     $efax = new eFaxOutbound();
     $url = Zend_Registry::get('config')->healthcloud->eFax->outboundUrl;
     $url .= '?apiKey=' . Zend_Registry::get('config')->healthcloud->apiKey;
     $efax->setUrl($url);
     $efax->setTransmissionId($sourceData['transmissionId']);
     $efax->setNoDuplicate(eFaxOutbound::NO_DUPLICATE_ENABLE);
     $efax->setDispositionMethod('POST');
     // use the default disposition URL
     $dispositionUrl = Zend_Registry::get('config')->healthcloud->eFax->dispositionUrl;
     $efax->setDispositionUrl($dispositionUrl);
     //$efax->setDispositionMethod('EMAIL');
     //$efax->addDispositionEmail('Arthur Layese','*****@*****.**');
     foreach ($sourceData['recipients'] as $recipient) {
         if ($messaging->resend && strlen($messaging->faxNumber) > 9) {
             // supersedes fax number from messaging
             $recipient['fax'] = $messaging->faxNumber;
         }
         $efax->addRecipient($recipient['fax'], $recipient['name'], $recipient['company']);
     }
     foreach ($sourceData['files'] as $file) {
         $efax->addFile($file['contents'], $file['type']);
     }
     $ret = $efax->send();
     if (!$ret) {
         $messaging->status = 'Fax Error';
         $messaging->note = implode(PHP_EOL, $efax->getErrors());
     } else {
         $messaging->docid = $efax->getDocId();
         $messaging->status = 'Fax Sent';
         $messaging->note = '';
     }
     if ($messaging->resend) {
         $messaging->resend = 0;
     }
     $messaging->retries++;
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->persist();
     return true;
 }
示例#3
0
 public static function activateDownload($daily)
 {
     $data = array();
     $data['daily'] = (int) $daily;
     //$data['clinicName'] = $practice->name;
     $type = 'full';
     if ($data['daily']) {
         $type = 'daily';
     }
     $messaging = new Messaging();
     //$messaging->messagingId = '';
     $messaging->messageType = 'DirectoryDownload';
     $messaging->populate();
     //$messaging->objectId = '';
     //$messaging->objectClass = '';
     $messaging->status = 'Downloading';
     $messaging->note = 'Downloading pharmacy (' . $type . ')';
     $messaging->dateStatus = date('Y-m-d H:i:s');
     //$messaging->auditId = '';
     $messaging->persist();
     trigger_error($messaging->note, E_USER_NOTICE);
     $ch = curl_init();
     $pharmacyActivateURL = Zend_Registry::get('config')->healthcloud->URL;
     $pharmacyActivateURL .= 'ss-manager.raw/activate-pharmacy-download?apiKey=' . Zend_Registry::get('config')->healthcloud->apiKey;
     $cookieFile = tempnam(sys_get_temp_dir(), 'ssddcookies_');
     trigger_error('URL: ' . $pharmacyActivateURL, E_USER_NOTICE);
     trigger_error('COOKIEFILE: ' . $cookieFile, E_USER_NOTICE);
     trigger_error('DATA: ' . print_r($data, true));
     curl_setopt($ch, CURLOPT_URL, $pharmacyActivateURL);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
     curl_setopt($ch, CURLOPT_USERPWD, 'admin:ch3!');
     $output = curl_exec($ch);
     $error = "";
     $downloadURL = "";
     $messaging->status = 'Downloaded';
     $messaging->note = 'Pharmacy downloaded (' . $type . ')';
     if (!curl_errno($ch)) {
         try {
             $responseXml = simplexml_load_string($output);
             if (isset($responseXml->error)) {
                 $error = (string) $responseXml->error->messageCode . ': ' . (string) $responseXml->error->message;
                 trigger_error("There was an error activating synchronization of pharmacies, Error code: " . $responseXml->error->code . " Error Message: " . $responseXml->error->message, E_USER_NOTICE);
             } elseif (isset($responseXml->data->SSDirectoryDownloadUrl)) {
                 $downloadURL = $responseXml->data->SSDirectoryDownloadUrl;
                 trigger_error('DOWNLOAD URL: ' . $downloadURL, E_USER_NOTICE);
             }
             if (isset($responseXml->rawMessage)) {
                 $messaging->rawMessage = base64_decode((string) $responseXml->rawMessage);
                 $messaging->rawMessageResponse = base64_decode((string) $responseXml->rawMessageResponse);
             }
         } catch (Exception $e) {
             $error = __("There was an error connecting to HealthCloud to activate synchronization of pharmacies. Please try again or contact the system administrator.");
             trigger_error("Curl error connecting to healthcloud to activate pharmacy sync: " . curl_error($ch), E_USER_NOTICE);
         }
     } else {
         $error = __("There was an error connecting to HealthCloud to activate synchronization of pharmacies. Please try again or contact the system administrator.");
         trigger_error("Curl error connecting to healthcloud to activate pharmacy sync: " . curl_error($ch), E_USER_NOTICE);
     }
     curl_close($ch);
     if (strlen($error) > 0) {
         $messaging->status = 'Error';
         $messaging->note .= ' ERROR: ' . $error;
         $ret = false;
     }
     if ($messaging->resend) {
         $messaging->resend = 0;
     }
     $messaging->retries++;
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->persist();
     trigger_error($messaging->note, E_USER_NOTICE);
     return array('downloadUrl' => $downloadURL, 'cookieFile' => $cookieFile, 'error' => $error);
 }
示例#4
0
 public static function sendResponse($data, Messaging $messaging)
 {
     $messageInfo = '';
     if (strlen($data['message']) > 0) {
         $xmlMessage = new SimpleXMLElement($data['message']);
         $lastName = (string) $xmlMessage->Body->RefillRequest->Patient->Name->LastName;
         $firstName = (string) $xmlMessage->Body->RefillRequest->Patient->Name->FirstName;
         $messageInfo = $lastName . ', ' . $firstName;
         $description = (string) $xmlMessage->Body->RefillRequest->MedicationPrescribed->DrugDescription;
         $datePrescribed = date('m/d/Y', strtotime((string) $xmlMessage->Body->RefillRequest->MedicationPrescribed->WrittenDate));
         $messageInfo .= ' - ' . $description . ' #' . $datePrescribed;
     }
     $tmpMessaging = $messaging;
     $messaging = new Messaging();
     //		$messaging->messagingId = $messageId;
     //		$messaging->populate();
     $messaging->messageType = 'RefillResponse';
     $messaging->objectId = $tmpMessaging->messagingId;
     $messaging->objectClass = 'MedicationRefillResponse';
     $messaging->status = 'Sending';
     $messaging->note = 'Sending refill response (' . $messageInfo . ')';
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->auditId = $tmpMessaging->auditId;
     $messaging->persist();
     if (isset($data['medicationId'])) {
         $medicationId = $data['medicationId'];
         unset($data['medicationId']);
         $medication = new Medication();
         $medication->medicationId = $medicationId;
         if ($medication->populate()) {
             $medData = array();
             $medData['description'] = $medication->description;
             $medData['strength'] = $medication->strength;
             $medData['strengthUnits'] = '00';
             //$medication->unit;
             $medData['quantity'] = $medication->quantity;
             $medData['quantityUnits'] = '00';
             $medData['directions'] = $medication->directions;
             $qualifier = 'R';
             if ($medication->prn) {
                 $qualifier = 'PRN';
             }
             $medData['refills'] = $medication->refills;
             $medData['refillsUnits'] = $qualifier;
             $medData['substitutions'] = $medication->substitution;
             $medData['writtenDate'] = date('Ymd', strtotime($medication->datePrescribed));
             $data['Medication'] = $medData;
         }
     }
     $query = http_build_query($data);
     $ch = curl_init();
     $ePrescribeURL = Zend_Registry::get('config')->healthcloud->URL;
     $ePrescribeURL .= 'ss-manager.raw/receive-response?apiKey=' . Zend_Registry::get('config')->healthcloud->apiKey;
     curl_setopt($ch, CURLOPT_URL, $ePrescribeURL);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $output = curl_exec($ch);
     $error = '';
     $rawMessage = '';
     $messaging->status = 'Sent';
     $messaging->note = 'Refill response pending';
     $messaging->unresolved = 1;
     if (!curl_errno($ch)) {
         try {
             $xml = new SimpleXMLElement($output);
             if (isset($xml->error)) {
                 $errorCode = (string) $xml->error->code;
                 $errorMsg = (string) $xml->error->message;
                 if (isset($xml->error->errorCode)) {
                     $errorCode = (string) $xml->error->errorCode;
                 }
                 if (isset($xml->error->errorMsg)) {
                     $errorMsg = (string) $xml->error->errorMsg;
                 }
                 $error = $errorMsg;
                 trigger_error('There was an error sending refill response, Error code: ' . $errorCode . ' Error Message: ' . $errorMsg, E_USER_NOTICE);
             } else {
                 if (isset($xml->status)) {
                     $messaging->note = 'Refill response awaiting confirmation';
                     if ((string) $xml->status->code == '010') {
                         // value 000 is for free standing error?
                         $messaging->status .= ' and Verified';
                         $messaging->note = 'Refill response sent and verified';
                         $messaging->unresolved = 0;
                     }
                 } else {
                     $error = 'Unrecognized HealthCloud response: ' . $output;
                 }
             }
             if (isset($xml->rawMessage)) {
                 $messaging->rawMessage = base64_decode((string) $xml->rawMessage);
                 $messaging->rawMessageResponse = base64_decode((string) $xml->rawMessageResponse);
             }
         } catch (Exception $e) {
             $error = __('There was an error, the response couldn\'t be parsed as XML: ' . $output);
             trigger_error($error, E_USER_NOTICE);
         }
     } else {
         $error = __('There was an error connecting to HealthCloud. Please try again or contact the system administrator.');
         trigger_error('Curl error connecting to healthcare: ' . curl_error($ch), E_USER_NOTICE);
     }
     $messaging->note .= ' (' . $messageInfo . ')';
     curl_close($ch);
     $ret = true;
     if (strlen($error) > 0) {
         $messaging->status = 'Error';
         $messaging->note = $error;
         $ret = $error;
     }
     if ($messaging->resend) {
         $messaging->resend = 0;
     }
     $messaging->retries++;
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->persist();
     return $ret;
 }
 protected function _syncLocation(EPrescriber $ePrescriber)
 {
     $ret = true;
     $dateActiveStart = strtotime($ePrescriber->dateActiveStart);
     $dateActiveEnd = strtotime($ePrescriber->dateActiveEnd);
     $serviceLevel = (int) $ePrescriber->serviceLevel;
     $personId = (int) $ePrescriber->providerId;
     $provider = new Provider();
     $provider->personId = $personId;
     $provider->populate();
     $provider->serviceLevel = $serviceLevel;
     if ($dateActiveStart > 0 && $dateActiveEnd > 0) {
         $provider->dateActiveStart = date('Y-m-d H:i:s', $dateActiveStart);
         $provider->dateActiveEnd = date('Y-m-d H:i:s', $dateActiveEnd);
     }
     $person = new Person();
     $person->personId = $personId;
     $person->populate();
     $practice = new Practice();
     $practice->practiceId = $person->primaryPracticeId;
     $practice->populate();
     $data = array();
     $data['deaNumber'] = $provider->deaNumber;
     $data['stateLicenseNumber'] = $provider->stateLicenseNumber;
     //$data['portalId'] = Zend_Registry::get('config')->sureScripts->portalId;
     //$data['accountId'] = Zend_Registry::get('config')->sureScripts->accountId;
     $data['clinicName'] = '' . $practice->name;
     $data['lastName'] = $person->lastName;
     $data['firstName'] = $person->firstName;
     $address = $ePrescriber->building;
     $data['addressLine1'] = $address->line1;
     $data['addressLine2'] = $address->line2;
     $data['addressCity'] = $address->city;
     $data['addressState'] = $address->state;
     $data['addressZipCode'] = $address->zipCode;
     $data['email'] = $person->email;
     $data['phones'] = array(array('number' => PhoneNumber::autoFixNumber($address->phoneNumber), 'type' => 'TE'), array('number' => PhoneNumber::autoFixNumber($address->fax), 'type' => 'FX'));
     $data['specialtyCode'] = $provider->specialty;
     $specialtyQualifier = '';
     if (strlen($provider->specialty) > 0) {
         $specialtyQualifier = 'AM';
     }
     $data['specialtyQualifier'] = $specialtyQualifier;
     $data['serviceLevel'] = $provider->serviceLevel;
     $now = strtotime('now');
     $days30 = strtotime('+30 days', $now);
     $activeStartTime = gmdate("Y-m-d\\TH:i:s.0", $now) . 'Z';
     $activeEndTime = gmdate("Y-m-d\\TH:i:s.0", $days30) . 'Z';
     $data['activeStartTime'] = $provider->dateActiveStartZ;
     $data['activeEndTime'] = $provider->dateActiveEndZ;
     $dateActiveEnd = strtotime(date('Y-m-d', strtotime($provider->dateActiveEndZ)));
     if ($dateActiveEnd <= strtotime(date('Y-m-d'))) {
         // to disable a prescriber ActiveEndTime must be set to current date and ServiceLevel must be set to zero.
         $data['activeEndTime'] = date('Y-m-d');
         $data['serviceLevel'] = 0;
         $provider->serviceLevel = 0;
     }
     $provider->persist();
     $identifierType = $provider->identifierType;
     if (strlen($identifierType) > 0) {
         $data[$identifierType] = $provider->identifier;
     }
     $messaging = new Messaging();
     //$messaging->messagingId = '';
     $type = 'add';
     $messaging->messageType = 'AddPrescriber';
     if (strlen($ePrescriber->SSID) > 0) {
         $messaging->messageType = 'UpdatePrescriber';
         $data['SPI'] = $ePrescriber->SSID;
         $type = 'update';
     } else {
         if (strlen($provider->sureScriptsSPI) > 0) {
             $messaging->messageType = 'AddPrescriberLocation';
             $data['SPI'] = substr($provider->sureScriptsSPI, 0, -3);
             $type = 'addLocation';
         }
     }
     // backupPortalId must be supplied if type is updateLocation
     $messaging->populate();
     //$messaging->objectId = '';
     //$messaging->objectClass = '';
     $messaging->status = 'Sending';
     $messaging->note = 'Sending prescriber data';
     $messaging->dateStatus = date('Y-m-d H:i:s');
     //$messaging->auditId = '';
     $messaging->persist();
     $query = http_build_query(array('type' => $type, 'data' => $data));
     $ch = curl_init();
     $ePrescribeURL = Zend_Registry::get('config')->healthcloud->URL;
     $ePrescribeURL .= 'ss-manager.raw/edit-prescriber?apiKey=' . Zend_Registry::get('config')->healthcloud->apiKey;
     curl_setopt($ch, CURLOPT_URL, $ePrescribeURL);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $output = curl_exec($ch);
     trigger_error('OUTPUT: ' . $output, E_USER_NOTICE);
     $error = "";
     $prescriberSPI = '';
     $messaging->status = 'Sent';
     $messaging->note = 'Prescriber data sent';
     if (!curl_errno($ch)) {
         try {
             $responseXml = simplexml_load_string($output);
             if (isset($responseXml->error)) {
                 $errorCode = (string) $responseXml->error->code;
                 $errorMsg = (string) $responseXml->error->message;
                 if (isset($responseXml->error->errorCode)) {
                     $errorCode = (string) $responseXml->error->errorCode;
                 }
                 if (isset($responseXml->error->errorMsg)) {
                     $errorMsg = (string) $responseXml->error->errorMsg;
                 }
                 $error = $errorMsg;
                 trigger_error('There was an error enabling an ePresciber, Error code: ' . $errorCode . ' Error Message: ' . $errorMsg, E_USER_NOTICE);
             } elseif (isset($responseXml->data)) {
                 $xml = new SimpleXMLElement($responseXml->data);
                 $prescriber = $xml->AddPrescriberResponse->Prescriber;
                 //if ($type == 'addLocation') {
                 if (isset($xml->AddPrescriberLocationResponse)) {
                     $prescriber = $xml->AddPrescriberLocationResponse->Prescriber;
                 }
                 $prescriberSPI = (string) $prescriber->Identification->SPI;
                 if (!strlen($prescriberSPI) > 0) {
                     $error = 'Registration failed for location ' . $ePrescriber->building->name;
                 }
             }
             if (isset($responseXml->rawMessage)) {
                 $messaging->rawMessage = base64_decode((string) $responseXml->rawMessage);
                 $messaging->rawMessageResponse = base64_decode((string) $responseXml->rawMessageResponse);
             }
         } catch (Exception $e) {
             $error = __("There was an error connecting to HealthCloud to enable ePrescribing for this provider. Please try again or contact the system administrator.");
             trigger_error("There was an error enabling an ePresciber, the response couldn't be parsed as XML: " . $output, E_USER_NOTICE);
         }
     } else {
         $error = __("There was an error connecting to HealthCloud to enable ePrescribing for this provider. Please try again or contact the system administrator.");
         trigger_error("Curl error connecting to healthcare enabled an ePrescribe record: " . curl_error($ch), E_USER_NOTICE);
     }
     curl_close($ch);
     if (strlen($error) > 0) {
         $messaging->status = 'Error';
         $messaging->note = $error;
         $ret = false;
     }
     if ($messaging->resend) {
         $messaging->resend = 0;
     }
     $messaging->retries++;
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->persist();
     if (strlen($error) > 0) {
         return $error;
     }
     if (!strlen($provider->sureScriptsSPI) > 0) {
         // handler of the first SPI
         $provider->sureScriptsSPI = $prescriberSPI;
         $provider->persist();
     }
     if ($type == 'add' || $type == 'addLocation') {
         $ePrescriber->SSID = $prescriberSPI;
         $ePrescriber->persist();
     }
     return $ret;
 }
 public function addProcessAction()
 {
     $dateActiveStart = strtotime($this->_getParam('dateActiveStart'));
     $dateActiveEnd = strtotime($this->_getParam('dateActiveEnd'));
     $serviceLevel = (int) $this->_getParam('serviceLevel');
     $personId = (int) $this->_getParam('personId');
     $provider = new Provider();
     $provider->person_id = $personId;
     $provider->populate();
     $provider->serviceLevel = $serviceLevel;
     if ($dateActiveStart > 0 && $dateActiveEnd > 0) {
         $provider->dateActiveStart = date('Y-m-d H:i:s', $dateActiveStart);
         $provider->dateActiveEnd = date('Y-m-d H:i:s', $dateActiveEnd);
     }
     $person = new Person();
     $person->person_id = $personId;
     $person->populate();
     $address = new Address();
     $address->person_id = $personId;
     $address->populateWithPersonId($personId);
     $phoneNumber = new PhoneNumber();
     $phoneNumber->person_id = $personId;
     $phoneNumber->populateWithType(4);
     $practice = new Practice();
     $practice->practiceId = $person->primaryPracticeId;
     $practice->populate();
     $data = array();
     $data['deaNumber'] = $provider->deaNumber;
     $data['stateLicenseNumber'] = $provider->stateLicenseNumber;
     //$data['portalId'] = Zend_Registry::get('config')->sureScripts->portalId;
     //$data['accountId'] = Zend_Registry::get('config')->sureScripts->accountId;
     $data['clinicName'] = '' . $practice->name;
     $data['lastName'] = $person->last_name;
     $data['firstName'] = $person->first_name;
     $address = new Address();
     $address->personId = $provider->personId;
     $address->populateWithType(4);
     $data['addressLine1'] = $address->line1;
     $data['addressLine2'] = $address->line2;
     $data['addressCity'] = $address->city;
     $data['addressState'] = $address->state;
     $data['addressZipCode'] = $address->zipCode;
     $data['email'] = $person->email;
     $phoneNumber = new PhoneNumber();
     $phoneNumber->personId = $provider->personId;
     /*
     $phoneNumberIterator = $phoneNumber->getIteratorByPatientId();
     $phones = array();
     foreach ($phoneNumberIterator as $number) {
     	if (!strlen($number->number) > 0) continue;
     	// SS Type options: BN - Beeper, CP - Cellular, FX - Fax, HP - Home, NP - Night, TE – Telephone*, WP – Work
     	$type = '';
     	switch ($number->type) {
     		case PhoneNumber::TYPE_HOME:
     			$type = 'HP'; 
     		case PhoneNumber::TYPE_WORK:
     			$type = 'WP';
     			break;
     		case PhoneNumber::TYPE_MOBILE:
     			$type = 'CP';
     			break;
     		case PhoneNumber::TYPE_FAX:
     			$type = 'FX';
     			break;
     		case PhoneNumber::TYPE_EMERGENCY:
     		case PhoneNumber::TYPE_EMPLOYER:
     		case PhoneNumber::TYPE_BILLING:
     			$type = 'TE';
     			break;
     		default:
     			continue;
     	}
     	$phones[$type] = array('number'=>$number->number,'type'=>$type);
     }
     $te = null;
     if (isset($phones['TE'])) {
     	$te = $phones['TE'];
     	unset($phones['TE']);
     }
     $fx = null;
     if (isset($phones['FX'])) {
     	$fx = $phones['FX'];
     	unset($phones['FX']);
     }
     if ($te === null) {
     	if (count($phones) > 0) {
     		$te = array_unshift($phones);
     	}
     	else if ($fx !== null) {
     		$te = $fx;
     	}
     }
     if ($fx === null) {
     	if (count($phones) > 0) {
     		$fx = array_unshift($phones);
     	}
     	else if ($te !== null) {
     		$fx = $te;
     	}
     }
     
     $data['phones'] = array();
     if ($te !== null) {
     	$data['phones'][] = $te;
     }
     if ($fx !== null) {
     	$data['phones'][] = $fx;
     }
     foreach ($phones as $p) {
     	$data['phones'][] = $p;
     }
     */
     $data['phones'] = $phoneNumber->phoneNumbers;
     /*$phoneNumbers = $phoneNumber->phoneNumbers;
     		$fax = '';
     		if (isset($phoneNumbers['FAX'])) {
     			$fax = $phoneNumbers['FAX'];
     			unset($phoneNumbers['FAX']);
     		}
     		$phone = $fax;
     		if (count($phoneNumbers) > 0) {
     			$phone = array_pop($phoneNumbers);
     		}
     		$data['phoneNumber'] = $phone;
     		$data['faxNumber'] = $fax;*/
     $data['specialtyCode'] = $provider->specialty;
     $specialtyQualifier = '';
     if (strlen($provider->specialty) > 0) {
         $specialtyQualifier = 'AM';
     }
     $data['specialtyQualifier'] = $specialtyQualifier;
     $data['serviceLevel'] = $provider->serviceLevel;
     $now = strtotime('now');
     $days30 = strtotime('+30 days', $now);
     $activeStartTime = gmdate("Y-m-d\\TH:i:s.0", $now) . 'Z';
     $activeEndTime = gmdate("Y-m-d\\TH:i:s.0", $days30) . 'Z';
     $data['activeStartTime'] = $provider->dateActiveStartZ;
     $data['activeEndTime'] = $provider->dateActiveEndZ;
     $dateActiveEnd = strtotime(date('Y-m-d', strtotime($provider->dateActiveEndZ)));
     if ($dateActiveEnd <= strtotime(date('Y-m-d'))) {
         // to disable a prescriber ActiveEndTime must be set to current date and ServiceLevel must be set to zero.
         $data['activeEndTime'] = date('Y-m-d');
         $data['serviceLevel'] = 0;
         $provider->serviceLevel = 0;
     }
     $provider->persist();
     $identifierType = $provider->identifierType;
     if (strlen($identifierType) > 0) {
         $data[$identifierType] = $provider->identifier;
     }
     /*
     		foreach ($data as $k=>$v) {
     			if (!strlen(trim($v)) > 0) {
     				$tmp = ' ';
     				if ($k == 'addressState') {
     					$tmp = 'AZ';
     				}
     				if ($k == 'addressZipCode') {
     					$tmp = '12345';
     				}
     				$data[$k] = $tmp;
     			}
     		}*/
     $messaging = new Messaging();
     //$messaging->messagingId = '';
     $type = 'add';
     $messaging->messageType = 'AddPrescriber';
     if (strlen($provider->sureScriptsSPI) > 0) {
         $messaging->messageType = 'UpdatePrescriber';
         $data['SPI'] = $provider->sureScriptsSPI;
         $type = 'update';
     }
     $messaging->populate();
     //$messaging->objectId = '';
     //$messaging->objectClass = '';
     $messaging->status = 'Sending';
     $messaging->note = 'Sending prescriber data';
     $messaging->dateStatus = date('Y-m-d H:i:s');
     //$messaging->auditId = '';
     $messaging->persist();
     $query = http_build_query(array('type' => $type, 'data' => $data));
     $ch = curl_init();
     $ePrescribeURL = Zend_Registry::get('config')->healthcloud->URL;
     $ePrescribeURL .= 'ss-manager.raw/edit-prescriber?apiKey=' . Zend_Registry::get('config')->healthcloud->apiKey;
     curl_setopt($ch, CURLOPT_URL, $ePrescribeURL);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $output = curl_exec($ch);
     trigger_error('OUTPUT: ' . $output, E_USER_NOTICE);
     $error = "";
     $prescriberSPI = '';
     $messaging->status = 'Sent';
     $messaging->note = 'Prescriber data sent';
     if (!curl_errno($ch)) {
         try {
             $responseXml = simplexml_load_string($output);
             if (isset($responseXml->error)) {
                 $errorCode = (string) $responseXml->error->code;
                 $errorMsg = (string) $responseXml->error->message;
                 if (isset($responseXml->error->errorCode)) {
                     $errorCode = (string) $responseXml->error->errorCode;
                 }
                 if (isset($responseXml->error->errorMsg)) {
                     $errorMsg = (string) $responseXml->error->errorMsg;
                 }
                 $error = $errorMsg;
                 trigger_error('There was an error enabling an ePresciber, Error code: ' . $errorCode . ' Error Message: ' . $errorMsg, E_USER_NOTICE);
             } elseif (isset($responseXml->data)) {
                 $xml = new SimpleXMLElement($responseXml->data);
                 $prescriber = $xml->AddPrescriberResponse->Prescriber;
                 if (isset($xml->AddPrescriberLocationResponse)) {
                     $prescriber = $xml->AddPrescriberLocationResponse->Prescriber;
                 }
                 $prescriberSPI = (string) $prescriber->Identification->SPI;
             }
             if (isset($responseXml->rawMessage)) {
                 $messaging->rawMessage = base64_decode((string) $responseXml->rawMessage);
                 $messaging->rawMessageResponse = base64_decode((string) $responseXml->rawMessageResponse);
             }
         } catch (Exception $e) {
             $error = __("There was an error connecting to HealthCloud to enable ePrescribing for this provider. Please try again or contact the system administrator.");
             trigger_error("There was an error enabling an ePresciber, the response couldn't be parsed as XML: " . $output, E_USER_NOTICE);
         }
     } else {
         $error = __("There was an error connecting to HealthCloud to enable ePrescribing for this provider. Please try again or contact the system administrator.");
         trigger_error("Curl error connecting to healthcare enabled an ePrescribe record: " . curl_error($ch), E_USER_NOTICE);
     }
     curl_close($ch);
     if (strlen($error) > 0) {
         $messaging->status = 'Error';
         $messaging->note = $error;
         $ret = false;
     }
     if ($messaging->resend) {
         $messaging->resend = 0;
     }
     $messaging->retries++;
     $messaging->dateStatus = date('Y-m-d H:i:s');
     $messaging->persist();
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $this->view->error = $error;
     if (strlen($error) > 0) {
         //$this->getResponse()->setHttpResponseCode(500);
         $json->direct(array('error' => $error));
         return;
     }
     if ($type == 'add') {
         $provider->sureScriptsSPI = trim($prescriberSPI);
         $provider->persist();
     } else {
         $prescriberSPI = $provider->sureScriptsSPI;
     }
     $this->view->message = "ePrescribing Information Saved for Provider, assigned SPI Number: " . $prescriberSPI;
     $this->view->prescriberSPI = $prescriberSPI;
     $json->direct(array('message' => $this->view->message, 'prescriberSPI' => $prescriberSPI, 'error' => $error));
 }