public function run()
    {
        $rr = new RequestResponse();
        $errors = [];
        if ($post = \Yii::$app->request->post()) {
            $this->shopFuser->load($post);
            $this->shopFuser->save();
        }
        $this->shopBuyer = $this->shopFuser->personType->createModelShopBuyer();
        $shopBuyer = $this->shopBuyer;
        if ($shopBuyer) {
            if ($post = \Yii::$app->request->post()) {
                $this->shopBuyer->load($post);
                $this->shopBuyer->relatedPropertiesModel->load($post);
            }
        }
        if ($rr->isRequestPjaxPost()) {
            if (!\Yii::$app->request->post($this->notSubmitParam)) {
                if ($this->shopFuser->validate() && $this->shopBuyer->validate() && $this->shopBuyer->relatedPropertiesModel->validate()) {
                    if ($this->shopBuyer->isNewRecord) {
                        if (!$this->shopBuyer->save()) {
                            throw new Exception('Not save buyer');
                        }
                    }
                    if (!$this->shopBuyer->relatedPropertiesModel->save()) {
                        throw new Exception('Not save buyer data');
                    }
                    $this->shopFuser->buyer_id = $this->shopBuyer->id;
                    $newOrder = ShopOrder::createOrderByFuser($this->shopFuser);
                    $orderUrl = $newOrder->publicUrl;
                    $this->view->registerJs(<<<JS
location.href='{$orderUrl}';
JS
);
                } else {
                    /*print_r($this->shopFuser->firstErrors);
                      print_r($this->shopBuyer->firstErrors);
                      print_r($this->shopBuyer->relatedPropertiesModel->firstErrors);*/
                }
            }
        }
        return $this->render($this->viewFile);
    }
示例#2
0
 /**
  * Если нет будет создан
  *
  * @return ShopFuser
  */
 public function getShopFuser()
 {
     if ($this->_shopFuser instanceof ShopFuser) {
         return $this->_shopFuser;
     }
     //Если пользователь гость
     if (\Yii::$app->user->isGuest) {
         //Проверка сессии
         if (\Yii::$app->getSession()->offsetExists($this->sessionFuserName)) {
             $fuserId = \Yii::$app->getSession()->get($this->sessionFuserName);
             $shopFuser = ShopFuser::find()->where(['id' => $fuserId])->one();
             //Поиск юзера
             if ($shopFuser) {
                 $this->_shopFuser = $shopFuser;
             }
         }
         if (!$this->_shopFuser) {
             $shopFuser = new ShopFuser();
             $shopFuser->save();
             \Yii::$app->getSession()->set($this->sessionFuserName, $shopFuser->id);
             $this->_shopFuser = $shopFuser;
         }
     } else {
         $this->_shopFuser = ShopFuser::find()->where(['user_id' => \Yii::$app->user->identity->id])->one();
         //Если у авторизовнного пользоывателя уже есть пользователь корзины
         if ($this->_shopFuser) {
             //Проверка сессии, а было ли чего то в корзине
             if (\Yii::$app->getSession()->offsetExists($this->sessionFuserName)) {
                 $fuserId = \Yii::$app->getSession()->get($this->sessionFuserName);
                 $shopFuser = ShopFuser::find()->where(['id' => $fuserId])->one();
                 /**
                  * @var $shopFuser ShopFuser
                  */
                 if ($shopFuser) {
                     $this->_shopFuser->addBaskets($shopFuser->shopBaskets);
                     $shopFuser->delete();
                 }
                 //Эти данные в сессии больше не нужны
                 \Yii::$app->getSession()->remove($this->sessionFuserName);
             }
         } else {
             //Проверка сессии, а было ли чего то в корзине
             if (\Yii::$app->getSession()->offsetExists($this->sessionFuserName)) {
                 $fuserId = \Yii::$app->getSession()->get($this->sessionFuserName);
                 $shopFuser = ShopFuser::find()->where(['id' => $fuserId])->one();
                 //Поиск юзера
                 /**
                  * @var $shopFuser ShopFuser
                  */
                 if ($shopFuser) {
                     $shopFuser->user_id = \Yii::$app->user->identity->id;
                     $shopFuser->save();
                 }
                 $this->_shopFuser = $shopFuser;
                 \Yii::$app->getSession()->remove($this->sessionFuserName);
             } else {
                 $shopFuser = new ShopFuser(['user_id' => \Yii::$app->user->identity->id]);
                 $shopFuser->save();
                 $this->_shopFuser = $shopFuser;
             }
         }
     }
     return $this->_shopFuser;
 }