public function testCompareStartdateWithWeekday() { $att = array('consultant' => '1', 'paediatric' => '0', 'anaesthetist' => '0', 'general_anaesthetic' => '0', 'last_generate_date' => '1901-01-01 00:00:00', 'last_modified_user_id' => '1', 'last_modified_date' => '1901-01-01 00:00:00', 'created_user_id' => '1', 'created_date' => '1901-01-01 00:00:00', 'deleted' => 0, 'firm_id' => '', 'theatre_id' => '1', 'start_date' => '20 Mar 2014', 'end_date' => null, 'weekday' => '3', 'start_time' => '01:00', 'end_time' => '02:00', 'default_admission_time' => '', 'interval_id' => '2', 'week_selection' => null, 'id' => null); $this->operationSequence->attributes = $att; $this->operationSequence->save(); $errors = $this->operationSequence->getErrors(); $this->assertGreaterThan(0, count($errors)); $this->assertTrue(array_key_exists('start_date', $errors)); $this->assertGreaterThan(0, $errors['start_date']); $this->assertEquals($errors['start_date'][0], 'Start date and weekday must be on the same day of the week'); $opSequence = new OphTrOperationbooking_Operation_Sequence(); $att['start_date'] = '19 Mar 2014'; $opSequence->attributes = $att; $opSequence->save(); $errors = $opSequence->getErrors(); $this->assertFalse(array_key_exists('start_date', $errors)); }
public function generateSessions($args = array()) { $output = ''; // Get sequences $today = date('Y-m-d'); $initialEndDate = empty($args) ? strtotime('+13 months') : strtotime($args[0]); $sequences = OphTrOperationbooking_Operation_Sequence::model()->findAll('start_date <= :end_date AND (end_date IS NULL or end_date >= :today)', array(':end_date' => date('Y-m-d', $initialEndDate), ':today' => $today)); foreach ($sequences as $sequence) { $criteria = new CDbCriteria(); $criteria->addCondition('sequence_id = :sequence_id'); $criteria->params[':sequence_id'] = $sequence->id; $criteria->order = 'date desc'; $session = OphTrOperationbooking_Operation_Session::model()->find($criteria); // The date of the most recent session for this sequence plus one day, or the sequence start date if no sessions for this sequence yet $startDate = empty($session) ? strtotime($sequence->start_date) : strtotime($session->date) + 60 * 60 * 24; // Sessions should be generated up to the smaller of initialEndDate (+13 months or command line) and sequence end_date if ($sequence->end_date && strtotime($sequence->end_date) < $initialEndDate) { $endDate = strtotime($sequence->end_date); } else { $endDate = $initialEndDate; } $dateList = array(); if ($sequence->interval_id == 1) { // NO REPEAT (single session) // If a session already exists for this one off there's no point creating another if (empty($session)) { $dateList[] = $sequence->start_date; } } elseif ($sequence->interval_id == 6 && $sequence->week_selection) { // MONTHLY REPEAT (weeks x,y of month) $date = date('Y-m-d', $startDate); $time = $startDate; // Get the next occurrence of the sequence on/after the start date while (date('N', $time) != date('N', strtotime($sequence->start_date))) { $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time))); $time = strtotime($date); } $dateList = $sequence->getWeekOccurrences($sequence->weekday, $sequence->week_selection, $time, $endDate, $date, date('Y-m-d', $endDate)); } else { // WEEKLY REPEAT (every x weeks) // There is a repeat interval, e.g. once every two weeks. In the instance of two weeks, the // function below returns 60 * 60 * 24 * 14, i.e. two weeks $interval = $sequence->interval->getInteger($endDate); // The number of days in the interval - 14 in the case of two week interval $days = $interval / 24 / 60 / 60; // IF there's no session use the sequence start date. If there is use the most recent // session date plus the interval (e.g. two weeks) if (empty($session)) { $nextStartDate = $startDate; } else { $nextStartDate = $startDate + $interval - 86400; } // Convert $nextStartDate (a timestamp of the seqence start date or the most recent session date plus the interval to a date. $date = date('Y-m-d', $nextStartDate); // The timestamp of the start date $time = $nextStartDate; // get the next occurrence of the sequence on/after the start date // Check to see if the day of the week for the time is the same day of the week as the sequence start date // Process loop if it isn't while (date('N', $time) != date('N', strtotime($sequence->start_date))) { // Set the date to $time + 1 day $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time))); // Set the time to the timstamp for the date + 1 day $time = strtotime($date); } while ($time <= $endDate) { $dateList[] = $date; $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + $days, date('Y', $time))); $time = strtotime($date); } } if (!empty($dateList)) { // Process dateList into sessions foreach ($dateList as $date) { // TODO: Check for collisions, maybe in Session validation code $new_session = new OphTrOperationbooking_Operation_Session(); foreach (array('start_time', 'end_time', 'consultant', 'anaesthetist', 'paediatric', 'general_anaesthetic', 'theatre_id', 'default_admission_time') as $attribute) { $new_session->{$attribute} = $sequence->{$attribute}; } $new_session->date = $date; $new_session->sequence_id = $sequence->id; $new_session->firm_id = $sequence->firm_id; if (Yii::app()->params['sessions_unavailable_past_date'] && $date >= Yii::app()->params['sessions_unavailable_past_date']) { $new_session->available = 0; } $new_session->save(); } $output .= "Sequence ID {$sequence->id}: Created " . count($dateList) . " session(s).\n"; } } if (!empty($args[1])) { return $output; } }
public function actionDeleteSequences() { if (!empty($_POST['sequence'])) { $criteria = new CDbCriteria(); $criteria->addInCondition('id', $_POST['sequence']); $sequences = OphTrOperationbooking_Operation_Sequence::model()->findAll($criteria); } elseif (@$_POST['use_filters']) { $sequences = $this->getSequences(true); } foreach ($sequences as $sequence) { foreach ($sequence->sessions as $session) { if (!$session->delete()) { throw new Exception('Unable to delete session: ' . print_r($session->getErrors(), true)); } } if (!$sequence->delete()) { throw new Exception('Unable to delete sequence: ' . print_r($sequence->getErrors(), true)); } Audit::add('admin', 'delete', $sequence->id, null, array('module' => 'OphTrOperationbooking', 'model' => 'OphTrOperationbooking_Operation_Sequence')); } echo '1'; }