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 array|\yii\web\Response
  */
 public function actionCreateOrder()
 {
     $rr = new RequestResponse();
     if ($rr->isRequestAjaxPost()) {
         try {
             $fuser = \Yii::$app->shop->shopFuser;
             if (!$fuser->shopBaskets) {
                 throw new Exception(\skeeks\cms\shop\Module::t('app', 'Your basket is empty'));
             }
             if ($fuser->load(\Yii::$app->request->post()) && $fuser->save()) {
                 $rr->success = true;
                 $rr->message = "";
                 $fuser->scenario = ShopFuser::SCENARIO_CREATE_ORDER;
                 if ($fuser->validate()) {
                     $order = ShopOrder::createOrderByFuser($fuser);
                     if (!$order->isNewRecord) {
                         $rr->message = \skeeks\cms\shop\Module::t('app', 'The order #{order_id} created successfully', ['order_id' => $order->id]);
                         $rr->success = true;
                         $rr->redirect = Url::to(['/shop/order/view', 'id' => $order->id]);
                         $rr->data = ['order' => $order];
                     } else {
                         throw new Exception(\skeeks\cms\shop\Module::t('app', 'Incorrect data of the new order') . ": " . array_shift($order->getFirstErrors()));
                     }
                 } else {
                     throw new Exception(\skeeks\cms\shop\Module::t('app', 'Not enogh data for ordering') . ": " . array_shift($fuser->getFirstErrors()));
                 }
             } else {
                 throw new Exception(\skeeks\cms\shop\Module::t('app', 'Not enogh data for ordering') . ": " . array_shift($fuser->getFirstErrors()));
             }
         } catch (Exception $e) {
             $rr->message = $e->getMessage();
             $rr->success = false;
         }
         $rr->data = \Yii::$app->shop->shopFuser->toArray([], \Yii::$app->shop->shopFuser->extraFields());
         return (array) $rr;
     } else {
         return $this->goBack();
     }
 }
Пример #3
0
 public function createOrder()
 {
     $cmsUser = null;
     if ($userId = \Yii::$app->request->get('cmsUserId')) {
         $cmsUser = CmsUser::findOne($userId);
     }
     if ($cmsUser) {
         /**
          * @var $shopFuser ShopFuser
          */
         $shopFuser = ShopFuser::getInstanceByUser($cmsUser);
         $model = $shopFuser;
         $rr = new RequestResponse();
         if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
             $model->scenario = ShopFuser::SCENARIO_CREATE_ORDER;
             return $rr->ajaxValidateForm($model);
         }
         if ($rr->isRequestPjaxPost()) {
             try {
                 if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                     $model->scenario = ShopFuser::SCENARIO_CREATE_ORDER;
                     if ($model->validate()) {
                         $order = ShopOrder::createOrderByFuser($model);
                         if (!$order->isNewRecord) {
                             \Yii::$app->getSession()->setFlash('success', \Yii::t('skeeks/shop/app', 'The order #{order_id} created successfully', ['order_id' => $order->id]));
                             if (\Yii::$app->request->post('submit-btn') == 'apply') {
                                 return $this->redirect(UrlHelper::constructCurrent()->setCurrentRef()->enableAdmin()->setRoute($this->modelDefaultAction)->normalizeCurrentRoute()->addData([$this->requestPkParamName => $order->id])->toString());
                             } else {
                                 return $this->redirect($this->indexUrl);
                             }
                         } else {
                             throw new Exception(\Yii::t('skeeks/shop/app', 'Incorrect data of the new order') . ": " . array_shift($order->getFirstErrors()));
                         }
                     } else {
                         throw new Exception(\Yii::t('skeeks/shop/app', 'Not enogh data for ordering') . ": " . array_shift($model->getFirstErrors()));
                     }
                 } else {
                     throw new Exception(\Yii::t('skeeks/shop/app', 'Could not save'));
                 }
             } catch (\Exception $e) {
                 \Yii::$app->getSession()->setFlash('error', $e->getMessage());
             }
         }
         return $this->render($this->action->id, ['cmsUser' => $cmsUser, 'shopFuser' => $model]);
     } else {
         return $this->render($this->action->id . "-select-user");
     }
 }