/** * @return string|Response * @throws \yii\base\Exception * @throws \yii\web\ServerErrorHttpException */ public function actionCreate() { $model = Order::create(false, false, true); $model->setScenario('backend'); if (Yii::$app->request->isPost) { $data = Yii::$app->request->post(); if ($model->load($data) && $model->validate()) { if (User::USER_GUEST === intval($model->user_id)) { $model->customer_id = 0; } elseif (null === Customer::findOne(['user_id' => intval($model->user_id), 'id' => intval($model->customer_id)])) { $model->customer_id = 0; } if (0 === intval($model->customer_id)) { $customer = Customer::createEmptyCustomer(intval($model->user_id)); if ($customer->load($data) && $customer->save()) { if (!empty($customer->getPropertyGroup())) { $customer->getPropertyGroup()->appendToObjectModel($customer); $data[$customer->getAbstractModel()->formName()] = isset($data['CustomerNew']) ? $data['CustomerNew'] : []; } $customer->saveModelWithProperties($data); $customer->refresh(); $model->customer_id = $customer->id; } } else { $customer = Customer::findOne(['id' => $model->customer_id]); } if (0 === $model->contragent_id || null === Contragent::findOne(['id' => $model->contragent_id, 'customer_id' => $model->customer_id])) { $contragent = Contragent::createEmptyContragent($customer); if ($contragent->load($data) && $contragent->save()) { if (!empty($contragent->getPropertyGroup())) { $contragent->getPropertyGroup()->appendToObjectModel($contragent); $data[$contragent->getAbstractModel()->formName()] = isset($data['ContragentNew']) ? $data['ContragentNew'] : []; } $contragent->saveModelWithProperties($data); $contragent->refresh(); $model->contragent_id = $contragent->id; } } else { $contragent = Contragent::findOne(['id' => $model->contragent_id]); } if ($model->save()) { OrderDeliveryInformation::createNewOrderDeliveryInformation($model, false); DeliveryInformation::createNewDeliveryInformation($contragent, false); return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); } } } return $this->render('create', ['model' => $model]); }