Example #1
0
 public function applyAction()
 {
     if ($this->request->isPost()) {
         $user = $this->getAuth();
         $new_car = $this->request->getPost('new_car');
         if ($new_car == 0) {
             $car = Car::findFirst(array('conditions' => 'number = ?1', 'bind' => array(1 => $this->request->getPost('choose-car'))));
         } else {
             $car = new Car();
             $car->number = $this->request->getPost('number');
             $car->brand = $this->request->getPost('brand');
             $car->model = $this->request->getPost('model');
             $car->owner = $user['user_id'];
             if (!$car->save()) {
                 message($this, "d", "Данные об автомобиле указаны неверно");
                 return $this->response->redirect("applyForService");
             }
         }
         if ($car) {
             $date = $this->request->getPost('date');
             $time = $this->request->getPost('time');
             $apps = Application::find(array('conditions' => 'term = ?1', 'bind' => array(1 => $date)));
             if ($date) {
                 if ($time) {
                     if (count($apps) < 4 * 20) {
                         $services = $this->request->getPost('services');
                         if (count($services) > 0) {
                             $app = new Application();
                             $app->car = $car->number;
                             $app->term = $date . ' ' . $time;
                             $app->confirmed = 0;
                             $app->manager = NULL;
                             if ($app->save()) {
                                 foreach ($services as $service) {
                                     $as = new ApplicationService();
                                     $as->application_id = $app->id;
                                     $as->service_id = $service;
                                     if (!$as->save()) {
                                         message($this, "d", "Ошибка при сохранении выбранной услуги");
                                         return $this->response->redirect("applyForService");
                                     }
                                 }
                                 //todo! Обработка поля с доп. информацией
                                 message($this, "s", "Заявка отправлена. В ближайшее время наш менеджер свяжется с Вами для ее подтверждения");
                                 return $this->response->redirect("cabinet");
                             } else {
                                 message($this, "d", "Ошибка при сохранении заявки");
                                 foreach ($app->getMessages() as $message) {
                                     message($this, "d", "Ошибка: " . $message->getMessage() . " в поле " . $message->getField() . ". Тип: " . $message->getType());
                                 }
                                 return $this->response->redirect("applyForService");
                             }
                         } else {
                             message($this, "d", "Минимум одна услуга должна быть выбрана");
                             return $this->response->redirect("applyForService");
                         }
                     } else {
                         message($this, "d", "Выбранная дата занята. Пожалуйста, выберите другой день");
                         return $this->response->redirect("applyForService");
                     }
                 } else {
                     message($this, "d", "Время не выбрано!");
                     return $this->response->redirect("applyForService");
                 }
             } else {
                 message($this, "d", "Дата не выбрана!");
                 return $this->response->redirect("applyForService");
             }
         } else {
             message($this, "d", "Автомобиль не найден");
             return $this->response->redirect("applyForService");
         }
     }
 }