/**
  * Updates an existing Booths model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelsBoothSchedules = $model->boothSchedules;
     if ($model->load(Yii::$app->request->post())) {
         //Load data from post
         $oldIDs = ArrayHelper::map($modelsBoothSchedules, 'id', 'id');
         $modelsBoothSchedules = Model::createMultiple(BoothSchedules::classname(), $modelsBoothSchedules);
         Model::loadMultiple($modelsBoothSchedules, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsBoothSchedules, 'id', 'id')));
         $model->date = \DateTime::createFromFormat('m-d-Y', $model->date)->format('Y-m-d');
         // validate all models
         $valid = $model->validate();
         if ($valid || !$valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         BoothSchedules::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($modelsBoothSchedules as $modelBoothSchedule) {
                         if (!empty($modelBoothSchedule->attendee_id)) {
                             $attendeeDetails = $modelBoothSchedule->attendee;
                             if (empty($modelBoothSchedule->booth_id)) {
                                 $modelBoothSchedule->booth_id = $model->id;
                                 //Below two lines can be removed
                                 $modelBoothSchedule->function_id = $attendeeDetails->function_Id;
                                 $modelBoothSchedule->cost_center_id = $attendeeDetails->group_Id;
                                 $modelBoothSchedule->isNewRecord = true;
                             }
                             $flag = $modelBoothSchedule->save(false);
                             if (!$flag) {
                                 //If data is not saved then rollback the transaction and break the loop
                                 $transaction->rollBack();
                                 break;
                             }
                         }
                     }
                 }
                 if ($flag) {
                     //If data is not saved then rollback the transaction  and redirect to view page
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (\Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         //Booth date should be between congress start date and end date
         $congressDetail = CongressDetails::findOne($model->congress_id);
         $congressStartDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->start_date)->format('m-d-Y');
         $congressEndDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->end_date)->format('m-d-Y');
         return $this->render('update', ['model' => $model, 'modelsBoothSchedules' => empty($modelsBoothSchedules) ? [new BoothSchedules()] : $modelsBoothSchedules, 'congressStartDate' => $congressStartDate, 'congressEndDate' => $congressEndDate]);
     }
 }