public function editAction()
 {
     $tableName = preg_replace('/[^a-zA-Z]+/', '', $this->_getParam("id", ""));
     $prettyName = preg_replace('/([A-Z]{1})/', ' \\1', substr($tableName, 9));
     $this->view->tableName = $tableName;
     $this->view->prettyName = $prettyName;
     $this->view->chBaseMed24Url = Zend_Registry::get('config')->healthcloud->CHMED->chBaseMed24Url;
     $this->view->chBaseMed24DetailUrl = Zend_Registry::get('config')->healthcloud->CHMED->chBaseMed24DetailUrl;
     $name = Medication::ENUM_ADMIN_SCHED;
     $enumeration = new Enumeration();
     $enumeration->populateByEnumerationName($name);
     $enumerationsClosure = new EnumerationsClosure();
     $rowset = $enumerationsClosure->getAllDescendants($enumeration->enumerationId, 1);
     $scheduleOptions = array();
     $adminSchedules = array();
     foreach ($rowset as $row) {
         $scheduleOptions[] = $row->key;
         $adminSchedules[$row->key] = $row->name;
     }
     $this->view->scheduleOptions = $scheduleOptions;
     $this->view->quantityQualifiers = Medication::listQuantityQualifiersMapping();
     $this->render();
 }
 public static function handlerSSSourceData(Audit $audit)
 {
     $data = array();
     if ($audit->objectClass != 'ESignature') {
         return $data;
     }
     $eSignature = new ESignature();
     $eSignature->eSignatureId = $audit->objectId;
     $eSignature->populate();
     if ($eSignature->objectClass != 'Medication' || !strlen($eSignature->signature) > 0) {
         WebVista::log('esignature is not signed or medication');
         return $data;
     }
     // MEDICATION DATA
     $medication = new Medication();
     $medication->medicationId = (int) $eSignature->objectId;
     $medication->populate();
     if ($medication->transmit != 'ePrescribe' || $medication->isScheduled()) {
         WebVista::log('medication is either scheduled or not an eprescribe');
         return $data;
     }
     WebVista::log('generating source data');
     $data['_audit'] = $audit;
     $uuid = NSDR::create_guid();
     $data['messageId'] = str_replace('-', '', $uuid);
     $data['prescriberOrderNumber'] = $medication->medicationId . '_' . $audit->auditId;
     $data['rxReferenceNumber'] = $medication->rxReferenceNumber;
     WebVista::log('messageId:[' . $data['messageId'] . '] prescriberOrderNumber:[' . $data['prescriberOrderNumber'] . '], rxReferenceNumber:[' . $data['rxReferenceNumber'] . ']');
     $medData = array();
     $medData['description'] = $medication->description;
     $medData['strength'] = $medication->dose;
     $qualifiers = Medication::listQuantityQualifiersMapping();
     $medData['strengthUnits'] = $qualifiers[$medication->quantityQualifier];
     // temporarily set to the same with quantity
     $medData['quantity'] = $medication->quantity;
     $medData['quantityUnits'] = $qualifiers[$medication->quantityQualifier];
     $medData['daysSupply'] = $medication->daysSupply;
     $medData['directions'] = $medication->directions;
     $qualifier = 'R';
     if ($medication->prn) {
         $qualifier = 'PRN';
     }
     $medData['refills'] = $medication->refills;
     $medData['refillsUnits'] = $qualifier;
     $medData['substitutions'] = $medication->substitution ? '0' : '1';
     $writtenDate = date('Ymd', strtotime($medication->datePrescribed));
     if ($medication->datePrescribed == '0000-00-00 00:00:00') {
         $writtenDate = '';
     }
     $medData['writtenDate'] = $writtenDate;
     $medData['productCode'] = $medication->hipaaNDC;
     $medData['productQualifier'] = 'ND';
     $medData['dosageForm'] = DataTables::getDosageForm($medication->chmedDose);
     $medData['drugDBCode'] = $medication->pkey;
     $medData['drugDBQualifier'] = '';
     //'pkey'; valid options: "E|G|FG|FS|MC|MD|MG|MM"
     $medData['note'] = $medication->comment;
     $data['Medication'] = $medData;
     WebVista::log('medication data: ' . print_r($medData, true));
     // PHARMACY DATA
     $pharmacy = new Pharmacy();
     $pharmacy->pharmacyId = $medication->pharmacyId;
     $pharmacy->populate();
     $pharmacyData = array();
     $pharmacyData['NCPDPID'] = $pharmacy->NCPDPID;
     $pharmacyData['fileId'] = $pharmacy->pharmacyId;
     $pharmacyData['NPI'] = $pharmacy->NPI;
     $pharmacyData['storeName'] = $pharmacy->StoreName;
     $pharmacyData['storeNumber'] = $pharmacy->StoreNumber;
     $pharmacyData['email'] = $pharmacy->Email;
     $pharmacyData['twentyFourHourFlag'] = $pharmacy->TwentyFourHourFlag;
     $pharmacyData['crossStreet'] = $pharmacy->CrossStreet;
     $pharmacyData['addressLine1'] = $pharmacy->AddressLine1;
     $pharmacyData['addressLine2'] = $pharmacy->AddressLine2;
     $pharmacyData['city'] = $pharmacy->City;
     $pharmacyData['state'] = $pharmacy->State;
     $pharmacyData['zip'] = $pharmacy->Zip;
     $phones = array();
     $phones[] = array('number' => $pharmacy->PhonePrimary, 'type' => 'TE');
     $phones[] = array('number' => $pharmacy->Fax, 'type' => 'FX');
     $phones[] = array('number' => $pharmacy->PhoneAlt1, 'type' => $pharmacy->PhoneAlt1Qualifier);
     $phones[] = array('number' => $pharmacy->PhoneAlt2, 'type' => $pharmacy->PhoneAlt2Qualifier);
     $phones[] = array('number' => $pharmacy->PhoneAlt3, 'type' => $pharmacy->PhoneAlt3Qualifier);
     $phones[] = array('number' => $pharmacy->PhoneAlt4, 'type' => $pharmacy->PhoneAlt4Qualifier);
     $phones[] = array('number' => $pharmacy->PhoneAlt5, 'type' => $pharmacy->PhoneAlt5Qualifier);
     $pharmacyData['phones'] = $phones;
     $data['Pharmacy'] = $pharmacyData;
     WebVista::log('pharmacy data: ' . print_r($pharmacyData, true));
     // PRESCRIBER DATA
     $provider = new Provider();
     $provider->personId = $medication->prescriberPersonId;
     $provider->populate();
     $prescriberData = array();
     $prescriberData['DEANumber'] = $provider->deaNumber;
     // it has conflicts with DEANumber
     //$prescriberData['stateLicenseNumber'] = $provider->stateLicenseNumber;
     $prescriberData['fileId'] = $provider->personId;
     $prescriberData['clinicName'] = '';
     $identifierType = $provider->identifierType;
     if (strlen($identifierType) > 0) {
         //	$prescriberData[$identifierType] = $provider->identifier;
     }
     $prescriberData['lastName'] = $provider->person->lastName;
     $prescriberData['firstName'] = $provider->person->firstName;
     $prescriberData['middleName'] = $provider->person->middleName;
     $prescriberData['suffix'] = $provider->person->suffix;
     $prescriberData['prefix'] = '';
     $prescriberData['email'] = $provider->person->email;
     $prescriberData['specialtyCode'] = $provider->specialty;
     $specialtyQualifier = '';
     if (strlen($provider->specialty) > 0) {
         $specialtyQualifier = 'AM';
     }
     $prescriberData['specialtyQualifier'] = $specialtyQualifier;
     $building = Building::getBuildingDefaultLocation((int) $provider->personId);
     $ePrescriber = new EPrescriber();
     $ePrescriber->providerId = (int) $provider->personId;
     $ePrescriber->buildingId = (int) $building->buildingId;
     $ePrescriber->populateWithBuildingProvider();
     $prescriberData['SPI'] = $ePrescriber->SSID;
     $prescriberData['addressLine1'] = $building->line1;
     $prescriberData['addressLine2'] = $building->line2;
     $prescriberData['city'] = $building->city;
     $prescriberData['state'] = $building->state;
     $prescriberData['zip'] = $building->zipCode;
     $prescriberData['phones'] = $building->phoneNumbers;
     $data['Prescriber'] = $prescriberData;
     WebVista::log('prescriber data: ' . print_r($prescriberData, true));
     // PATIENT DATA
     $patient = new Patient();
     $patient->personId = $medication->personId;
     $patient->populate();
     $patientData = array();
     $patientData['lastName'] = $patient->person->lastName;
     $patientData['firstName'] = $patient->person->firstName;
     $patientData['middleName'] = $patient->person->middleName;
     $patientData['suffix'] = $patient->person->suffix;
     $patientData['prefix'] = '';
     $patientData['email'] = $patient->person->email;
     $patientData['fileId'] = $patient->recordNumber;
     $patientData['medicareNumber'] = '';
     // TODO: to be implemented
     $identifierType = $patient->identifierType;
     if (strlen($identifierType) > 0) {
         $patientData[$identifierType] = $patient->identifier;
     }
     $patientData['gender'] = $patient->person->getDisplayGender();
     $dateOfBirth = date('Ymd', strtotime($patient->person->dateOfBirth));
     if ($patient->person->dateOfBirth == '0000-00-00') {
         $dateOfBirth = '';
     }
     $patientData['dateOfBirth'] = $dateOfBirth;
     $address = new Address();
     $address->personId = $patient->personId;
     $addressIterator = $address->getIteratorByPersonId();
     foreach ($addressIterator as $address) {
         break;
         // retrieves the top address
     }
     $patientData['addressLine1'] = $address->line1;
     $patientData['addressLine2'] = $address->line2;
     $patientData['city'] = $address->city;
     $patientData['state'] = $address->state;
     $patientData['zip'] = $address->zipCode;
     $phoneNumber = new PhoneNumber();
     $phoneNumber->personId = $patient->personId;
     $patientData['phones'] = $phoneNumber->phoneNumbers;
     $data['Patient'] = $patientData;
     WebVista::log('patient data: ' . print_r($patientData, true));
     // CHECK for attending/supervisor
     $attendingId = (int) TeamMember::getAttending($patient->teamId);
     $building = Building::getBuildingDefaultLocation($attendingId);
     $ePrescriber = new EPrescriber();
     $ePrescriber->providerId = $attendingId;
     $ePrescriber->buildingId = (int) $building->buildingId;
     $ePrescriber->populateWithBuildingProvider();
     if ($attendingId > 0 && strlen($ePrescriber->SSID) > 0) {
         // SUPERVISOR
         $provider = new Provider();
         $provider->personId = $attendingId;
         $provider->populate();
         $supervisorData = array();
         $supervisorData['DEANumber'] = $provider->deaNumber;
         $supervisorData['SPI'] = $ePrescriber->SSID;
         // it has conflicts with DEANumber
         //$supervisorData['stateLicenseNumber'] = $provider->stateLicenseNumber;
         $supervisorData['fileId'] = $provider->personId;
         $supervisorData['clinicName'] = '';
         $identifierType = $provider->identifierType;
         if (strlen($identifierType) > 0) {
             //	$prescriberData[$identifierType] = $provider->identifier;
         }
         $phoneNumber = new PhoneNumber();
         $phoneNumber->personId = $provider->personId;
         $supervisorData['phones'] = $phoneNumber->phoneNumbers;
         $supervisorData['lastName'] = $provider->person->lastName;
         $supervisorData['firstName'] = $provider->person->firstName;
         $supervisorData['middleName'] = $provider->person->middleName;
         $supervisorData['suffix'] = $provider->person->suffix;
         $supervisorData['prefix'] = '';
         $supervisorData['email'] = $provider->person->email;
         $supervisorData['specialtyCode'] = $provider->specialty;
         $specialtyQualifier = '';
         if (strlen($provider->specialty) > 0) {
             $specialtyQualifier = 'AM';
         }
         $supervisorData['specialtyQualifier'] = $specialtyQualifier;
         $supervisorData['addressLine1'] = $building->line1;
         $supervisorData['addressLine2'] = $building->line2;
         $supervisorData['city'] = $building->city;
         $supervisorData['state'] = $building->state;
         $supervisorData['zip'] = $building->zipCode;
         $supervisorData['phones'] = $building->phoneNumbers;
         $data['Supervisor'] = $supervisorData;
         WebVista::log('supervisor data: ' . print_r($supervisorData, true));
     }
     return $data;
 }
    function listItemsAction()
    {
        $eSignIterator = new ESignatureIterator();
        $objectId = (int) $this->_getParam('objectId');
        if ($objectId > 0) {
            $eSignIterator->setFilter($objectId, 'objectId');
        } else {
            $eSignIterator->setFilter((int) Zend_Auth::getInstance()->getIdentity()->personId, 'signList');
        }
        //var_dump($db->query($cnSelect)->fetchAll());exit;
        $baseStr = "<?xml version='1.0' standalone='yes'?><rows></rows>";
        $xml = new SimpleXMLElement($baseStr);
        $currentCat = null;
        $category = null;
        // override the include_path to include controllers path
        set_include_path(realpath(Zend_Registry::get('basePath') . '/application/controllers') . PATH_SEPARATOR . get_include_path());
        foreach ($eSignIterator as $row) {
            $row = $row->toArray();
            $obj = new $row['objectClass']();
            foreach ($obj->_primaryKeys as $key) {
                $obj->{$key} = $row['objectId'];
            }
            if (!$obj->populate()) {
                continue;
            }
            // signing but actual object does not exists
            if ($currentCat != $row['objectClass']) {
                $currentCat = $row['objectClass'];
                $category = $xml->addChild("row");
                $category->addAttribute("style", 'height:20px;');
                $category->addAttribute("id", $row['objectClass']);
                $cell = $category->addChild("cell", call_user_func($currentCat . "::" . "getPrettyName", array()));
                $cell = $category->addChild("cell", '');
                $controllerName = call_user_func($currentCat . "::" . "getControllerName");
                $jumpLink = call_user_func_array($controllerName . "::" . "buildJSJumpLink", array($row['objectId'], $row['signingUserId'], $row['objectClass']));
                //$jumpLink = $this->buildJSJumpLink($row['objectId'],$row['signingUserId'],$row['objectClass']);
                $js = "function jumpLink{$row['objectClass']}(objectId,patientId) {\n{$jumpLink}\n}";
                //				$cell = $category->addChild('cell',$js);
                $userdata = $category->addChild('userdata', $js);
                $userdata->addAttribute('name', 'js');
            }
            $leaf = $category->addChild("row");
            $leaf->addAttribute('id', $row['eSignatureId']);
            $leaf->addChild('cell', $row['dateTime'] . " " . preg_replace('/[<>]/', '', $row['summary']));
            $leaf->addChild('cell', '');
            $userdata = $leaf->addChild('userdata', $row['objectId']);
            $userdata->addAttribute('name', 'objectId');
            // hidden column that will load the correct tab
            //			$leaf->addChild('cell',$row['objectId']); // temporary set to objectId
            //$leaf->addChild('cell',$this->buildJSJumpLink($row['objectId'],$row['signingUserId']));
            // for patientId hidden column, not sure if this is the correct field.
            //$leaf->addChild('cell',$row['signingUserId']);
            $patientId = $obj->personId;
            $userdata = $leaf->addChild('userdata', $patientId);
            $userdata->addAttribute('name', 'patientId');
            //$leaf->addChild('cell',$patientId);
            // add a subrow for other info
            if ($row['objectClass'] == 'Medication') {
                $pharmacyInfo = array();
                // $obj refers to Medication ORM
                if ($obj->isScheduled()) {
                    $pharmacyInfo[] = 'Medication is a controlled substance, it cannot be sent electronically. The Rx will be printed and needs a wet signature before it can be faxed to the pharmacy or handed to the patient.';
                } else {
                    $pharmacy = $obj->pharmacy;
                    if (strlen($pharmacy->StoreName) > 0) {
                        $pharmacyInfo[] = $pharmacy->StoreName;
                        $address = $pharmacy->AddressLine1;
                        if (strlen($pharmacy->AddressLine2) > 0) {
                            $address .= ' ' . $pharmacy->AddressLine2;
                        }
                        $address .= ', ' . $pharmacy->City;
                        $address .= ', ' . $pharmacy->State;
                        $address .= ', ' . $pharmacy->Zip;
                        $pharmacyInfo[] = $address;
                        $phones = array();
                        $phones[] = $pharmacy->PhonePrimary;
                        if (strlen($pharmacy->Fax) > 0) {
                            $phones[] = $pharmacy->Fax;
                        }
                        if (strlen($pharmacy->PhoneAlt1) > 0) {
                            $phones[] = $pharmacy->PhoneAlt1;
                        }
                        if (strlen($pharmacy->PhoneAlt2) > 0) {
                            $phones[] = $pharmacy->PhoneAlt2;
                        }
                        if (strlen($pharmacy->PhoneAlt3) > 0) {
                            $phones[] = $pharmacy->PhoneAlt3;
                        }
                        if (strlen($pharmacy->PhoneAlt4) > 0) {
                            $phones[] = $pharmacy->PhoneAlt4;
                        }
                        if (strlen($pharmacy->PhoneAlt5) > 0) {
                            $phones[] = $pharmacy->PhoneAlt5;
                        }
                        $pharmacyInfo[] = implode(', ', $phones);
                    } else {
                        $pharmacyInfo[] = 'No pharmacy selected';
                    }
                }
                $pharmacyInfo = implode(" <br /> ", $pharmacyInfo);
                $patient = new Patient();
                $patient->personId = $obj->personId;
                $patient->populate();
                $patientInfo = $patient->lastName . ", " . $patient->firstName . " " . strtoupper(substr($patient->middleName, 0, 1)) . " #" . $patient->recordNumber;
                $qualifiers = Medication::listQuantityQualifiersMapping();
                $medicationInfo = array();
                $rxn = $obj->rxReferenceNumber;
                if (strlen($rxn) > 0) {
                    $medicationInfo[] = 'Rx Reference Number: ' . $rxn;
                }
                $medicationInfo[] = 'Description: ' . htmlspecialchars($obj->description);
                $medicationInfo[] = 'Directions: ' . htmlspecialchars($obj->directions);
                $medicationInfo[] = 'Quantity: ' . $obj->quantity . ' ' . $qualifiers[$obj->quantityQualifier];
                //$medicationInfo[] = 'Quantity: '.$obj->quantity.' '.$obj->quantityQualifier;
                $medicationInfo[] = 'Strength: ' . $obj->dose . ' ' . $qualifiers[$obj->quantityQualifier];
                //$medicationInfo[] = 'Strength: '.$obj->dose.' '.$obj->quantityQualifier;
                $medicationInfo[] = 'Days Supply: ' . $obj->daysSupply;
                $refills = $obj->refills;
                if ($obj->prn) {
                    $refills = 'PRN';
                }
                $medicationInfo[] = 'Refills: ' . $refills;
                $substitution = 'Permitted';
                if ($obj->substitution == 0) {
                    $substitution = 'Not Permitted';
                }
                $medicationInfo[] = 'Substitutions: ' . $substitution;
                $medicationInfo[] = 'Date Prescribed: ' . date('Y-m-d', strtotime($obj->datePrescribed));
                //$medicationInfo[] = 'NDC: '.$obj->hipaaNDC;
                //$medicationInfo[] = 'Dosage Form: '.DataTables::getDosageForm($obj->chmedDose);
                //$medicationInfo[] = 'DB Code: '.$obj->pkey;
                $medicationInfo[] = 'Note: ' . htmlspecialchars($obj->comment);
                $medicationInfo = implode(' <br /> ', $medicationInfo);
                $info = '<div style="margin-left:75px;margin-top:-18px;margin-bottom:5px;">
					<fieldset>
						<legend title="' . htmlspecialchars($patientInfo) . '">' . __('Patient') . '</legend>' . htmlspecialchars($patientInfo) . '
					</fieldset>
					<fieldset>
						<legend title="' . htmlspecialchars($pharmacyInfo) . '">' . __('Pharmacy') . '</legend>' . htmlspecialchars($pharmacyInfo) . '
					</fieldset>
					<fieldset title="' . htmlspecialchars($medicationInfo) . '">
						<legend>' . __('Medication') . '</legend>' . htmlspecialchars($medicationInfo) . '
					</fieldset></div>';
                $node = $leaf->addChild('row');
                $guid = NSDR::create_guid();
                $node->addAttribute('id', $guid);
                //				$node->addAttribute('style','vertical-align:top;height:50px;');
                $node->addChild('cell', '<![CDATA[' . $info . ']]>');
                $node->addChild('cell', '');
                $node->addChild('cell', '');
                $node->addChild('cell', '');
            }
        }
        header('content-type: text/xml');
        $this->view->content = $xml->asXml();
        $this->view->content = html_entity_decode($this->view->content);
        file_put_contents('/tmp/esign.xml', $this->view->content);
        $this->render();
    }