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();
 }
示例#2
0
 public function afterSaveEvent()
 {
     //Обновление цены у родительского элемента если она есть
     if ($this->product->cmsContentElement->parent_content_element_id) {
         $parentProduct = $this->product->cmsContentElement->parentContentElement->shopProduct;
         if ($parentProduct) {
             $minPriceValue = $this->price;
             $minPriceCurrency = $this->currency_code;
             //У родительского элемента уже есть предложения
             if ($offers = $parentProduct->tradeOffers) {
                 //Все цены оферов этого типа
                 $minPrice = ShopProductPrice::find()->where(['product_id' => ArrayHelper::map($offers, 'id', 'id')])->andWhere(['type_price_id' => $this->type_price_id])->orderBy(['price' => SORT_ASC])->one();
                 if ($minPrice) {
                     $minPriceValue = $minPrice->price;
                     $minPriceCurrency = $minPrice->currency_code;
                 }
             }
             $query = $parentProduct->getShopProductPrices()->andWhere(['type_price_id' => $this->type_price_id]);
             /**
              * @var $price self
              */
             if ($price = $query->one()) {
                 $price->price = $minPriceValue;
                 $price->currency_code = $minPriceCurrency;
                 $price->save();
             }
         }
     }
 }
示例#3
0
 /**
  * Просмотр созданных бекапов баз данных
  */
 public function actionRemoveDublicatePrices()
 {
     /**
      * @var ShopProductPrice $price
      */
     foreach (ShopProductPrice::find()->each(1000) as $price) {
         $prices = ShopProductPrice::find()->where(['type_price_id' => $price->type_price_id])->andWhere(['product_id' => $price->product_id])->andWhere(["!=", 'id', $price->id])->all();
         $this->stdout("price: {$price->id}\n");
         if (!$prices) {
             continue;
         } else {
             $total = count($prices);
             $removePrices = ShopProductPrice::find()->where(['type_price_id' => $price->type_price_id])->andWhere(['product_id' => $price->product_id])->orderBy(['price' => SORT_ASC])->limit($total)->all();
             $forRemove = count($removePrices);
             foreach ($removePrices as $price) {
                 $price->delete();
             }
             $this->stdout("\tЕсть дубли, удалено {$forRemove}\n", Console::FG_RED);
         }
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getShopProductPrice()
 {
     return $this->hasOne(ShopProductPrice::className(), ['id' => 'shop_product_price_id']);
 }
示例#5
0
 /**
  *
  * Лучшая цена по которой может купить этот товар пользователь, среди всех доступных
  * Операемся на курс
  *
  * @param null $shopFuser
  * @return $this
  */
 public function getMinProductPrice($shopFuser = null)
 {
     if ($shopFuser === null) {
         $shopFuser = \Yii::$app->shop->shopFuser;
     }
     return $this->hasOne(ShopProductPrice::className(), ['product_id' => 'id'])->select(['shop_product_price.*', 'realPrice' => '( (SELECT course FROM `money_currency` WHERE `money_currency`.`code` = `shop_product_price`.`currency_code`) * `shop_product_price`.`price` )'])->leftJoin('money_currency', '`money_currency`.`code` = `shop_product_price`.`currency_code`')->orWhere(['and', ['>', 'price', 0], ['type_price_id' => ArrayHelper::map($shopFuser->buyTypePrices, 'id', 'id')]])->orWhere(['type_price_id' => \Yii::$app->shop->baseTypePrice->id])->orderBy(['realPrice' => SORT_ASC]);
 }
 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()]);
 }
 /**
  * @return \skeeks\cms\shop\models\ShopProductPrice
  */
 public function getMaxPrice()
 {
     $maxPrice = null;
     if ($this->elementIds && $this->typePrice) {
         $maxPrice = \skeeks\cms\shop\models\ShopProductPrice::find()->andWhere(['product_id' => $this->elementIds])->andWhere(['type_price_id' => $this->typePrice->id])->orderBy(['price' => SORT_DESC])->one();
     }
     return $maxPrice;
 }