public function testGetStatus() { $this->specify("Status must be correct", function ($duration, $possession, $complete, $expected) { $model = new Trip(); $model['duration'] = $duration; $model['numberPossession'] = $possession; $model['complete'] = $complete; expect($model->getStatus())->equals($expected); }, ['examples' => [['duration' => ['to' => new MongoDate(strtotime('08.03.2015'))], 'possession' => ['to' => new MongoDate(strtotime('07.03.2015'))], 'complete' => true, 'expected' => Trip::STATUS_COMPLETE], ['duration' => ['to' => new MongoDate(strtotime('08.03.2015'))], 'possession' => ['to' => new MongoDate(strtotime('11.03.2015'))], 'complete' => true, 'expected' => Trip::STATUS_COMPLETE], ['duration' => ['to' => new MongoDate(strtotime('08.03.2015'))], 'possession' => ['to' => new MongoDate(strtotime('12.03.2015'))], 'complete' => true, 'expected' => Trip::STATUS_EXPIRED], ['duration' => ['to' => new MongoDate()], 'possession' => ['to' => null], 'complete' => false, 'expected' => Trip::STATUS_INCOMPLETE], ['duration' => ['to' => new MongoDate(time() - Trip::TIME_TO_RETURN_NUMBER - 1)], 'possession' => ['to' => null], 'complete' => false, 'expected' => Trip::STATUS_EXPIRED]]]); }
public function search(array $params) { $query = Trip::find(); $query->with('number', 'employee'); $query->orderBy(['numberPossession.from' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if (!empty($this->mobileNumber)) { $numberIds = array_map(function ($item) { return $item['_id']; }, Number::find()->select(['_id'])->asArray()->where(['like', 'number', $this->mobileNumber])->all()); $query->andWhere(['in', 'numberId', $numberIds]); } if (!empty($this->employeeName)) { $employeeIds = array_map(function ($item) { return (int) $item['id']; }, Employee::findByName($this->employeeName, true)->select('id')->asArray()->all()); $query->andWhere(['in', 'employeeId', $employeeIds]); } if ($this->complete === self::INCOMPLETE) { $query->andWhere(['complete' => false]); } if ($this->rentNumberFrom !== $this->rentNumberTo) { $query->andWhere(['numberPossession.from' => ['$gt' => new MongoDate(strtotime($this->rentNumberFrom)), '$lte' => new MongoDate(strtotime($this->rentNumberTo))]]); } $query->andFilterWhere(['like', 'destination', $this->destination]); return $dataProvider; }
/** * @param $id * @return Trip * @throws NotFoundHttpException */ private function findModel($id) { if (($model = Trip::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Запрашиваемая страница не найдена.'); } }
public function actionTrip() { $this->stdout("Загружаю командировки\n", Console::FG_BLUE, Console::BOLD); $query = (new Query())->from($this->tripTableName); $this->stdout("Источник: таблица '{$this->tripTableName}' " . $query->count() . " записей.\nЦелевая коллекция: '" . Trip::collectionName() . "'\n"); $this->cleanCollection(Trip::collectionName()); $successCounter = 0; foreach ($query->all() as $item) { $trip = new Trip(); $number = (new Query())->select(['number'])->from($this->numberTableName)->where(['id' => $item['mobile_id']])->one(); $trip['numberId'] = Number::findOne(['number' => $number['number']])->getPrimaryKey(); $trip['employeeId'] = (int) $item['employee_id']; $trip['duration'] = ['from' => new MongoDate(strtotime("+1 day", (int) $item['rent_date'])), 'to' => new MongoDate((int) $item['return_date'])]; $trip['numberPossession'] = ['from' => new MongoDate((int) $item['rent_date']), 'to' => new MongoDate(strtotime($item['actual_return_date']))]; if ($trip['numberPossession']['to'] !== null) { $trip['complete'] = true; } $trip['destination'] = $item['destination']; if ($trip->save(false)) { $successCounter++; } } $this->stdout("Успешно загружено {$successCounter} записей\n", Console::BOLD, Console::FG_GREEN); $this->stdout("\n"); return Controller::EXIT_CODE_NORMAL; }