Ejemplo n.º 1
0
 public function actionLogWork($remote = false)
 {
     $model = new Schedule(['scenario' => Schedule::SCENARIO_REMOTE]);
     $model->loadDefaultValues();
     $model->duration = null;
     $model->tech_id = Yii::$app->user->id;
     $note = new Note(['scenario' => Note::SCENARIO_REMOTE]);
     $ticket = new Ticket(['scenario' => Ticket::SCENARIO_REMOTE]);
     if ($model->load(Yii::$app->request->post()) && $model->validate() && $note->load(Yii::$app->request->post()) && $note->validate()) {
         if (empty($note->ticket_id)) {
             // new ticket
             if (!($ticket->load(Yii::$app->request->post()) && !empty(trim($ticket->title)) && $ticket->validate())) {
                 $ticket->addError('title', 'Select a ticket or enter a title to create a new one');
             } else {
                 $ticket->invoice_id = $model->invoice_id;
                 $ticket->status_id = $ticket::STATUS_IN_PROGRESS;
                 if ($ticket->save(false) && $model->save(false)) {
                     $note->link('ticket', $ticket);
                     $note->link('schedule', $model, ['hours' => $model->duration / 60]);
                     return $this->redirect(['ticket/view', 'id' => $note->ticket_id]);
                 }
             }
         } elseif ($model->save(false) && $note->save(false)) {
             $note->link('schedule', $model, ['hours' => $model->duration / 60]);
             if ($note->ticket->status_id == $ticket::STATUS_SUBMITTED) {
                 $note->ticket->status_id = $ticket::STATUS_IN_PROGRESS;
                 $note->ticket->save(false);
             }
             return $this->redirect(['ticket/view', 'id' => $note->ticket_id]);
         }
     } elseif (!Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->get());
         $note->load(Yii::$app->request->get());
         $ticket->load(Yii::$app->request->get());
     }
     if (empty($model->start_time)) {
         $minutes = time() / 60;
         $minutes = $minutes - $minutes % 5;
         // round down to 5 min increment
         $model->start_time = date('Y-m-d H:i:00', $minutes * 60);
     }
     if (Yii::$app->request->isAjax) {
         return $this->renderAjax('log-work', ['model' => $model, 'note' => $note, 'ticket' => $ticket, 'remote' => $remote]);
     }
     return $this->render('log-work', ['model' => $model, 'note' => $note, 'ticket' => $ticket, 'remote' => $remote]);
 }