Exemplo n.º 1
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");
     }
 }
Exemplo n.º 2
0
 /**
  * После создания заказа, пробуем создать все что нужно в cms но это уже не обязательно, поэтому если что то, где то не сработает не столь важно
  *
  * @param $e
  */
 public function _afterCreateOrder($e)
 {
     try {
         //Если пользователя не было, пробуем создать
         if (!$this->user_id) {
             if (!($user = CmsUser::findOne(['email' => $this->email]))) {
                 $user = $this->_createCmsUser();
             }
             //create user
             $this->user_id = $user->id;
             $this->save();
             $this->refresh();
         }
     } catch (\Exception $e) {
         \Yii::error($e->getMessage(), V3toysModule::className());
     }
 }