Inheritance: extends BaseActiveRecordVersioned
 /**
  * @param Patient $patient
  * @param array   $exclude
  *
  * @return array|CActiveRecord[]|mixed|null
  */
 public function getPrescriptionItemsForPatient(Patient $patient, $exclude = array())
 {
     $prescriptionCriteria = new CDbCriteria(array('order' => 'event_date DESC'));
     $prescriptionCriteria->addCondition('episode.patient_id = :id');
     $prescriptionCriteria->addNotInCondition('t.id', $exclude);
     $prescriptionCriteria->params = array_merge($prescriptionCriteria->params, array(':id' => $patient->id));
     $prescriptionItems = OphDrPrescription_Item::model()->with('prescription', 'drug', 'duration', 'prescription.event', 'prescription.event.episode')->findAll($prescriptionCriteria);
     return $prescriptionItems;
 }
 /**
  * Create and save appropriate Element_OphDrPrescription_Item and Element_OphDrPrescription_Item_Taper
  * models for this prescription based on the $items structure passed in.
  *
  * This instance must have been saved already.
  *
  * @param array() $items
  *
  * @throws Exception
  */
 public function updateItems($items)
 {
     $itemCount = count($items);
     if ($itemCount == 0) {
         throw new Exception('Item cannot be blank.');
     } else {
         if (!$this->id) {
             throw new Exception('Cannot call updateItems on unsaved instance.');
         }
         // Get a list of ids so we can keep track of what's been removed
         $existing_item_ids = array();
         $existing_taper_ids = array();
         // can't rely on relation, as this will have been set already
         foreach (OphDrPrescription_Item::model()->findAll('prescription_id = :pid', array(':pid' => $this->id)) as $item) {
             $existing_item_ids[$item->id] = $item->id;
             foreach ($item->tapers as $taper) {
                 $existing_taper_ids[$taper->id] = $taper->id;
             }
         }
         // Process (any) posted prescription items
         foreach ($items as $item) {
             if (isset($item['id']) && isset($existing_item_ids[$item['id']])) {
                 // Item is being updated
                 $item_model = OphDrPrescription_Item::model()->findByPk($item['id']);
                 unset($existing_item_ids[$item['id']]);
             } else {
                 // Item is new
                 $item_model = new OphDrPrescription_Item();
                 $item_model->prescription_id = $this->id;
                 $item_model->drug_id = $item['drug_id'];
             }
             // Save main item attributes
             $item_model->dose = $item['dose'];
             $item_model->route_id = $item['route_id'];
             if (isset($item['route_option_id'])) {
                 $item_model->route_option_id = $item['route_option_id'];
             } else {
                 $item_model->route_option_id = null;
             }
             $item_model->frequency_id = $item['frequency_id'];
             $item_model->duration_id = $item['duration_id'];
             if (isset($item['continue_by_gp'])) {
                 $item_model->continue_by_gp = $item['continue_by_gp'];
             } else {
                 $item_model->continue_by_gp = 0;
             }
             $item_model->save();
             // Tapering
             $new_tapers = isset($item['taper']) ? $item['taper'] : array();
             foreach ($new_tapers as $taper) {
                 if (isset($taper['id']) && isset($existing_taper_ids[$taper['id']])) {
                     // Taper is being updated
                     $taper_model = OphDrPrescription_ItemTaper::model()->findByPk($taper['id']);
                     unset($existing_taper_ids[$taper['id']]);
                 } else {
                     // Taper is new
                     $taper_model = new OphDrPrescription_ItemTaper();
                     $taper_model->item_id = $item_model->id;
                 }
                 $taper_model->dose = $taper['dose'];
                 $taper_model->frequency_id = $taper['frequency_id'];
                 $taper_model->duration_id = $taper['duration_id'];
                 $taper_model->save();
             }
         }
         // Delete remaining (removed) ids
         OphDrPrescription_ItemTaper::model()->deleteByPk(array_values($existing_taper_ids));
         OphDrPrescription_Item::model()->deleteByPk(array_values($existing_item_ids));
     }
 }
Example #3
0
 /**
  * Render the form for a OphDrPrescription_Item, DrugSetItem or Drug (by id).
  *
  * @param $key
  * @param OphDrPrescription_Item|DrugSetItem|int $source
  *
  * @throws CException
  */
 public function renderPrescriptionItem($key, $source)
 {
     $item = new OphDrPrescription_Item();
     if (is_a($source, 'OphDrPrescription_Item')) {
         // Source is a prescription item, so we should clone it
         foreach (array('drug_id', 'duration_id', 'frequency_id', 'dose', 'route_option_id', 'route_id', 'continue_by_gp') as $field) {
             $item->{$field} = $source->{$field};
         }
         if ($source->tapers) {
             $tapers = array();
             foreach ($source->tapers as $taper) {
                 $taper_model = new OphDrPrescription_ItemTaper();
                 $taper_model->dose = $taper->dose;
                 $taper_model->frequency_id = $taper->frequency_id;
                 $taper_model->duration_id = $taper->duration_id;
                 $tapers[] = $taper_model;
             }
             $item->tapers = $tapers;
         }
     } else {
         if (is_a($source, 'DrugSetItem')) {
             // Source is an drug set item which contains frequency and duration data
             $item->drug_id = $source->drug_id;
             $item->loadDefaults();
             foreach (array('duration_id', 'frequency_id', 'dose') as $field) {
                 if ($source->{$field}) {
                     $item->{$field} = $source->{$field};
                 }
             }
             if ($source->tapers) {
                 $tapers = array();
                 foreach ($source->tapers as $taper) {
                     $taper_model = new OphDrPrescription_ItemTaper();
                     foreach (array('duration_id', 'frequency_id', 'dose') as $field) {
                         if ($taper->{$field}) {
                             $taper_model->{$field} = $taper->{$field};
                         } else {
                             $taper_model->{$field} = $item->{$field};
                         }
                     }
                     $tapers[] = $taper_model;
                 }
                 $item->tapers = $tapers;
             }
         } elseif (is_int($source) || (int) $source) {
             // Source is an integer, so we use it as a drug_id
             $item->drug_id = $source;
             $item->loadDefaults();
         } else {
             throw new CException('Invalid prescription item source: ' . print_r($source));
         }
         // Populate route option from episode for Eye
         if ($episode = $this->episode) {
             if ($principal_eye = $episode->eye) {
                 $route_option_id = DrugRouteOption::model()->find('name = :eye_name', array(':eye_name' => $principal_eye->name));
                 $item->route_option_id = $route_option_id ? $route_option_id : null;
             }
             //check operation note eye and use instead of original diagnosis
             if ($api = Yii::app()->moduleAPI->get('OphTrOperationnote')) {
                 if ($apieye = $api->getLastEye($this->patient)) {
                     $item->route_option_id = $apieye;
                 }
             }
         }
     }
     if (isset($this->patient)) {
         $this->renderPartial('/default/form_Element_OphDrPrescription_Details_Item', array('key' => $key, 'item' => $item, 'patient' => $this->patient));
     } else {
         $output = $this->renderPartial('/default/form_Element_OphDrPrescription_Details_Item', array('key' => $key, 'item' => $item), true);
         return $output;
     }
 }