public function run()
 {
     /**
      * @var $contentElement CmsContentElement
      */
     $contentElement = $this->controller->model;
     $model = ShopProduct::find()->where(['id' => $contentElement->id])->one();
     $productPrices = [];
     if (!$model) {
         $model = new ShopProduct(['id' => $contentElement->id]);
     } else {
         if ($typePrices = ShopTypePrice::find()->where(['!=', 'def', Cms::BOOL_Y])->all()) {
             foreach ($typePrices as $typePrice) {
                 $productPrice = ShopProductPrice::find()->where(['product_id' => $model->id, 'type_price_id' => $typePrice->id])->one();
                 if (!$productPrice) {
                     $productPrice = new ShopProductPrice(['product_id' => $model->id, 'type_price_id' => $typePrice->id]);
                 }
                 if ($post = \Yii::$app->request->post()) {
                     $data = ArrayHelper::getValue($post, 'prices.' . $typePrice->id);
                     $productPrice->load($data, "");
                 }
                 $productPrices[] = $productPrice;
             }
         }
     }
     $rr = new RequestResponse();
     if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
         return $rr->ajaxValidateForm($model);
     }
     if ($rr->isRequestPjaxPost()) {
         /**
          * @var $productPrice ShopProductPrice
          */
         foreach ($productPrices as $productPrice) {
             if ($productPrice->save()) {
             } else {
                 \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Check the correctness of the prices'));
             }
         }
         if ($model->load(\Yii::$app->request->post()) && $model->save()) {
             \Yii::$app->getSession()->setFlash('success', 'Saved');
             if (\Yii::$app->request->post('submit-btn') == 'apply') {
             } else {
                 return $this->controller->redirect($this->controller->indexUrl);
             }
             $model->refresh();
         } else {
             \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Failed to save'));
         }
     }
     $this->viewParams = ['model' => $model, 'productPrices' => $productPrices];
     return parent::run();
 }
Example #2
0
 /**
  * @param $event
  */
 public function afterSaveEvent($event)
 {
     if ($this->_baseProductPriceCurrency || $this->_baseProductPriceValue) {
         $baseProductPrice = $this->getBaseProductPrice()->one();
         if (!$baseProductPrice) {
             $baseProductPrice = new ShopProductPrice(['product_id' => $this->id]);
             $baseProductPrice->type_price_id = \Yii::$app->shop->baseTypePrice->id;
         }
         if ($this->_baseProductPriceValue) {
             $baseProductPrice->price = $this->_baseProductPriceValue;
         }
         if ($this->_baseProductPriceCurrency) {
             $baseProductPrice->currency_code = $this->_baseProductPriceCurrency;
         }
         $baseProductPrice->save();
     } else {
         if (!$this->baseProductPrice) {
             $baseProductPrice = new ShopProductPrice(['product_id' => $this->id]);
             $baseProductPrice->type_price_id = \Yii::$app->shop->baseTypePrice->id;
             $baseProductPrice->save();
         }
     }
 }
Example #3
0
 /**
  * После сохранения следим за ценами создаем если нет
  * @param $event
  */
 public function _afterSaveEvent($event)
 {
     //Prices update
     if ($this->_baseProductPriceCurrency || $this->_baseProductPriceValue) {
         /**
          * @var $baseProductPrice ShopProductPrice
          */
         $baseProductPrice = $this->getBaseProductPrice()->one();
         if (!$baseProductPrice) {
             $baseProductPrice = new ShopProductPrice(['product_id' => $this->id]);
             $baseProductPrice->type_price_id = \Yii::$app->shop->baseTypePrice->id;
             if ($this->_baseProductPriceValue) {
                 $baseProductPrice->price = $this->_baseProductPriceValue;
             }
             if ($this->_baseProductPriceCurrency) {
                 $baseProductPrice->currency_code = $this->_baseProductPriceCurrency;
             }
             $baseProductPrice->save();
         } else {
             $isChanged = false;
             //Установка и сохранение только если что то изменилось
             if ($this->_baseProductPriceValue && $this->_baseProductPriceValue != $baseProductPrice->price) {
                 $baseProductPrice->price = $this->_baseProductPriceValue;
                 $isChanged = true;
             }
             if ($this->_baseProductPriceCurrency && $this->_baseProductPriceCurrency != $baseProductPrice->currency_code) {
                 $baseProductPrice->currency_code = $this->_baseProductPriceCurrency;
                 $isChanged = true;
             }
             if ($isChanged) {
                 $baseProductPrice->save();
             }
         }
     } else {
         if (!$this->baseProductPrice) {
             $baseProductPrice = new ShopProductPrice(['product_id' => $this->id]);
             $baseProductPrice->type_price_id = \Yii::$app->shop->baseTypePrice->id;
             $baseProductPrice->save();
         }
     }
 }
 public function update(AdminAction $adminAction)
 {
     /**
      * @var $model CmsContentElement
      */
     $model = $this->model;
     $relatedModel = $model->relatedPropertiesModel;
     $shopProduct = ShopProduct::find()->where(['id' => $model->id])->one();
     $productPrices = [];
     if (!$shopProduct) {
         $shopProduct = new ShopProduct(['id' => $model->id]);
         $shopProduct->save();
     } else {
         if ($typePrices = ShopTypePrice::find()->where(['!=', 'def', Cms::BOOL_Y])->all()) {
             foreach ($typePrices as $typePrice) {
                 $productPrice = ShopProductPrice::find()->where(['product_id' => $shopProduct->id, 'type_price_id' => $typePrice->id])->one();
                 if (!$productPrice) {
                     $productPrice = new ShopProductPrice(['product_id' => $shopProduct->id, 'type_price_id' => $typePrice->id]);
                 }
                 if ($post = \Yii::$app->request->post()) {
                     $data = ArrayHelper::getValue($post, 'prices.' . $typePrice->id);
                     $productPrice->load($data, "");
                 }
                 $productPrices[] = $productPrice;
             }
         }
     }
     $rr = new RequestResponse();
     if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
         $model->load(\Yii::$app->request->post());
         $relatedModel->load(\Yii::$app->request->post());
         $shopProduct->load(\Yii::$app->request->post());
         return \yii\widgets\ActiveForm::validateMultiple([$model, $relatedModel, $shopProduct]);
     }
     if ($rr->isRequestPjaxPost()) {
         $model->load(\Yii::$app->request->post());
         $relatedModel->load(\Yii::$app->request->post());
         $shopProduct->load(\Yii::$app->request->post());
         /**
          * @var $productPrice ShopProductPrice
          */
         foreach ($productPrices as $productPrice) {
             if ($productPrice->save()) {
             } else {
                 \Yii::$app->getSession()->setFlash('error', \Yii::t('skeeks/shop/app', 'Check the correctness of the prices'));
             }
         }
         if ($model->save() && $relatedModel->save() && $shopProduct->save()) {
             \Yii::$app->getSession()->setFlash('success', \Yii::t('skeeks/shop/app', 'Saved'));
             if (\Yii::$app->request->post('submit-btn') == 'apply') {
             } else {
                 return $this->redirect($this->indexUrl);
             }
             $model->refresh();
         } else {
             $errors = [];
             if ($model->getErrors()) {
                 foreach ($model->getErrors() as $error) {
                     $errors[] = implode(', ', $error);
                 }
             }
             \Yii::$app->getSession()->setFlash('error', \Yii::t('skeeks/shop/app', 'Could not save') . $errors);
         }
     }
     if (!$shopProduct->baseProductPrice) {
         $baseProductPrice = new ShopProductPrice(['type_price_id' => \Yii::$app->shop->baseTypePrice->id, 'currency_code' => \Yii::$app->money->currencyCode, 'product_id' => $model->id]);
         $baseProductPrice->save();
     }
     return $this->render('_form', ['model' => $model, 'relatedModel' => $relatedModel, 'shopProduct' => $shopProduct, 'productPrices' => $productPrices, 'baseProductPrice' => $shopProduct->getBaseProductPrice()->one()]);
 }