diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index b3ca7cc..6cc697f 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -127,8 +127,8 @@ class Product extends \yii\db\ActiveRecord */ public function getImageUrl() { - // return a default image placeholder if your source image is not found - return !empty($this->image) ? $this->image->imageUrl : '/images/no_photo.png'; + $image = empty($this->variant) ? null : $this->variant->image; + return !empty($image) ? $image->imageUrl : '/images/no_photo.png'; } /** @@ -136,7 +136,7 @@ class Product extends \yii\db\ActiveRecord */ public function getImages() { - return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']); + return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id'])->filterWhere(['is', 'product_variant_id', NULL]); } /** @@ -144,7 +144,7 @@ class Product extends \yii\db\ActiveRecord */ public function getVariant() { - return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']); + return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id'])->orderBy('product_variant.product_variant_id', SORT_ASC); } public function getVariantPrice() { @@ -215,27 +215,29 @@ class Product extends \yii\db\ActiveRecord // // } - $todel = []; - foreach ($this->variants ? : [] as $_variant) { - $todel[$_variant->product_variant_id] = $_variant->product_variant_id; - } - foreach ($this->_variants as $_variant) { - if (!is_array($_variant)) { - return; + if (!empty($this->_variants)) { + $todel = []; + foreach ($this->variants ?: [] as $_variant) { + $todel[$_variant->product_variant_id] = $_variant->product_variant_id; } - if (!empty($_variant['product_variant_id'])) { - unset($todel[$_variant['product_variant_id']]); - $model = ProductVariant::findOne($_variant['product_variant_id']); - } else { - $model = new ProductVariant(); + foreach ($this->_variants as $_variant) { + if (!is_array($_variant)) { + return; + } + if (!empty($_variant['product_variant_id'])) { + unset($todel[$_variant['product_variant_id']]); + $model = ProductVariant::findOne($_variant['product_variant_id']); + } else { + $model = new ProductVariant(); + } + $_variant['product_id'] = $this->product_id; + $model->load(['ProductVariant' => $_variant]); + $model->product_id = $this->product_id; + $model->save(); + } + if (!empty($todel)) { + ProductVariant::deleteAll(['product_variant_id' => $todel]); } - $_variant['product_id'] = $this->product_id; - $model->load(['ProductVariant' => $_variant]); - $model->product_id = $this->product_id; - $model->save(); - } - if (!empty($todel)) { - ProductVariant::deleteAll(['product_variant_id' => $todel]); } } diff --git a/common/modules/product/models/ProductSearch.php b/common/modules/product/models/ProductSearch.php index 44a49e6..314d941 100755 --- a/common/modules/product/models/ProductSearch.php +++ b/common/modules/product/models/ProductSearch.php @@ -14,6 +14,8 @@ use yii\web\NotFoundHttpException; class ProductSearch extends Product { public $brand_name; + public $brand_id; + public $category_id; public $category_name; public $variant_sku; @@ -23,7 +25,7 @@ class ProductSearch extends Product public function rules() { return [ - [['name', 'brand_name', 'category_name', 'variant_sku'], 'safe'], + [['name', 'brand_name', 'brand_id', 'category_id', 'category_name', 'variant_sku'], 'safe'], [['tax_brand_id', 'product_id'], 'integer'], [['is_top', 'is_new', 'akciya'], 'boolean'], ]; @@ -49,17 +51,16 @@ class ProductSearch extends Product { $query = Product::find(); - // add conditions that should always apply here + $query->joinWith(['brand', 'brand.brandNames', 'categories', 'categories.categoryNames', 'variant']); + + $query->groupBy(['product.product_id']); + $query->orderBy('product.product_id', 'DESC'); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); - $this->load($params); - - if (!$this->validate()) { - // uncomment the following line if you do not want to return any records when validation fails - // $query->where('0=1'); + if ( !($this->load($params) && $this->validate()) ) { return $dataProvider; } @@ -77,8 +78,6 @@ class ProductSearch extends Product ] ]); - $query->joinWith(['brand', 'brand.brandNames', 'categories', 'categories.categoryNames', 'variant']); - if (isset($this->is_top)) { $query->andFilterWhere([ 'is_top' => (bool)$this->is_top, @@ -95,18 +94,16 @@ class ProductSearch extends Product ]); } $query->andFilterWhere([ - 'tax_brand_id' => $this->tax_brand_id, - 'product_id' => $this->product_id, + 'product.brand_id' => $this->brand_id, + 'product.product_id' => $this->product_id, + 'product_category.category_id' => $this->category_id ]); - $query->andFilterWhere(['ilike', 'name', $this->name]); + $query->andFilterWhere(['ilike', 'product.name', $this->name]); $query->andFilterWhere(['ilike', 'brand_name.value', $this->brand_name]); $query->andFilterWhere(['ilike', 'category_name.value', $this->category_name]); $query->andFilterWhere(['ilike', 'product_variant.sku', $this->variant_sku]); - $query->groupBy(['product.product_id']); - $query->orderBy('product.product_id', 'DESC'); - return $dataProvider; } diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index feecd22..bddce79 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -111,7 +111,7 @@ class ProductVariant extends \yii\db\ActiveRecord } public function getStock_caption() { - return is_null($this->stock) ? '∞' : intval($this->stock); + return is_null($this->stock) ? '∞' : ($this->stock > 0 ? Yii::t('product', 'Enable') : Yii::t('product', 'Disable')); // intval($this->stock); } /** diff --git a/common/modules/product/views/manage/index.php b/common/modules/product/views/manage/index.php index b21de4a..5fc4527 100755 --- a/common/modules/product/views/manage/index.php +++ b/common/modules/product/views/manage/index.php @@ -2,6 +2,11 @@ use yii\helpers\Html; use yii\grid\GridView; +use yii\helpers\ArrayHelper; +use kartik\select2\Select2; +use common\components\artboxtree\ArtboxTreeHelper; +use common\modules\product\helpers\ProductHelper; + /* @var $this yii\web\View */ /* @var $searchModel common\modules\product\models\ProductSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ @@ -28,11 +33,45 @@ $this->params['breadcrumbs'][] = $this->title; 'label' => Yii::t('product', 'Brand'), 'attribute' => 'brand_name', 'value' => 'brand.name', + 'format' => 'raw', + 'filter' => Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'brand_id', + 'data' => ArrayHelper::map(ProductHelper::getBrands()->all(), 'brand_id', 'name'), + 'language' => 'ru', + 'options' => [ + 'placeholder' => Yii::t('product', 'Select brand'), + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]) ], [ 'label' => Yii::t('product', 'Category'), 'attribute' => 'category_name', - 'value' => 'category.name', + 'value' => function($model) { + $categories = []; + foreach ($model->categories as $category) { + $categories[] = $category->name; + } + return implode(", ", $categories); + }, + 'format' => 'raw', + 'filter' => Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'category_id', + 'data' => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'name'), + 'language' => 'ru', + 'options' => [ + 'placeholder' => Yii::t('product', 'Select category'), + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]) ], [ 'label' => Yii::t('product', 'SKU'), @@ -40,9 +79,12 @@ $this->params['breadcrumbs'][] = $this->title; 'value' => 'variant.sku', ], 'variant.price', - 'variant.stock_caption', - - + 'variant.price_old', + [ + 'label' => Yii::t('product', 'Stock'), + 'attribute' => 'variant_stock', + 'value' => 'variant.stock_caption', + ], [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view} |{is_top} {is_new} {akciya} | {update} {delete}', diff --git a/common/modules/product/widgets/views/product_smart.php b/common/modules/product/widgets/views/product_smart.php index d0ed55f..3f061a1 100755 --- a/common/modules/product/widgets/views/product_smart.php +++ b/common/modules/product/widgets/views/product_smart.php @@ -12,6 +12,19 @@ use yii\helpers\Url; = \common\components\artboximage\ArtboxImageHelper::getImage($product->imageUrl, 'list')?> + is_top) || !empty($product->is_new) || !empty($product->akciya)) :?> +