diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index f92ab39..211d38d 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -133,7 +133,7 @@ class ManageController extends Controller return $this->redirect(['view', 'id' => $model->product_id]); } } else { - $groups = $model->category->getTaxGroups(); + $groups = $model->category-> getTaxGroupsByLevel(0); return $this->render('update', [ 'model' => $model, diff --git a/common/modules/product/controllers/VariantController.php b/common/modules/product/controllers/VariantController.php old mode 100644 new mode 100755 index 3bd3673..92bd8a5 --- a/common/modules/product/controllers/VariantController.php +++ b/common/modules/product/controllers/VariantController.php @@ -4,8 +4,10 @@ namespace common\modules\product\controllers; use common\modules\product\models\Product; use common\modules\product\models\ProductImage; +use common\modules\product\models\ProductStock; use common\modules\product\models\ProductVariant; use common\modules\product\models\ProductVariantListSearch; +use common\modules\product\models\Stock; use Yii; use yii\web\Controller; use yii\web\NotFoundHttpException; @@ -94,8 +96,12 @@ class VariantController extends Controller return $this->redirect(['index', 'product_id' => $product_id]); } } else { + $groups = $model->category->getTaxGroupsByLevel(1); + return $this->render('create', [ 'model' => $model, + 'groups' => $groups, + 'stocks' => [new ProductStock()], ]); } } @@ -112,14 +118,9 @@ class VariantController extends Controller $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { - - - - - - if ($model->save()) { + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { $imageModel = ProductImage::find()->where(['product_variant_id' => $model->product_variant_id])->one(); @@ -146,6 +147,31 @@ class VariantController extends Controller $image->saveAs(Yii::getAlias('@storage/products/' . $image->name)); } + + $ProductStocks = Yii::$app->request->post('ProductStock'); + $Stocks = Yii::$app->request->post('Stock'); + + foreach($ProductStocks as $index => $ProductStock){ + $stock = Stock::find()->where(['name'=>$Stocks[$index]['name']])->one(); + if(!$stock instanceof Stock){ + $stock = new Stock(); + $stock->name = $Stocks[$index]['name']; + $stock->save(); + } + $psModel = ProductStock::find()->where(['product_variant_id'=>$model->product_variant_id, 'stock_id' =>$stock->stock_id ]); + if(!$psModel instanceof ProductStock){ + $psModel = new ProductStock; + $psModel->load(['ProductStock'=>$ProductStock]); + $psModel->stock_id = $stock->stock_id; + $psModel->save(); + } else { + $psModel->load(['ProductStock'=>$ProductStock]); + $psModel->save(); + } + + } + + } return $this->redirect(['index', 'product_id'=>$product_id]); @@ -172,6 +198,7 @@ class VariantController extends Controller return $this->render('update', [ 'model' => $model, 'groups' => $groups, + 'stocks' => (!empty($model->variantStocks)) ? $model->variantStocks : [new ProductStock], ]); } } diff --git a/common/modules/product/models/ProductStock.php b/common/modules/product/models/ProductStock.php index 9e0ec1c..e2e15b2 100755 --- a/common/modules/product/models/ProductStock.php +++ b/common/modules/product/models/ProductStock.php @@ -47,7 +47,7 @@ class ProductStock extends \yii\db\ActiveRecord return [ 'product_id' => 'Product ID', 'stock_id' => 'Stock ID', - 'quantity' => 'Quantity', + 'quantity' => 'Количество', 'product_variant_id' => 'Product Variant ID', ]; } diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 9534e2b..f084f64 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -37,7 +37,7 @@ class ProductVariant extends \yii\db\ActiveRecord public $translit; public $translit_rubric; private $data; - public $stocks; + /** @var array $_images */ public $imagesUpload = []; @@ -142,6 +142,18 @@ class ProductVariant extends \yii\db\ActiveRecord return is_null($this->stock) ? '∞' : ($this->stock > 0 ? Yii::t('product', 'Enable') : Yii::t('product', 'Disable')); // intval($this->stock); } + public function getVariantStocks(){ + + return $this->hasMany(ProductStock::className(),['product_variant_id'=> 'product_variant_id'])->joinWith('stock'); + } + + public function getStocks(){ + + return $this->hasMany(Stock::className(),['stock_id'=>'stock_id']) + ->viaTable(ProductStock::tableName(),['product_variant_id'=> 'product_variant_id']); + } + + public function getFilters(){ return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id']) diff --git a/common/modules/product/models/ProductVariantListSearch.php b/common/modules/product/models/ProductVariantListSearch.php old mode 100644 new mode 100755 index 4593951..4593951 --- a/common/modules/product/models/ProductVariantListSearch.php +++ b/common/modules/product/models/ProductVariantListSearch.php diff --git a/common/modules/product/models/ProductVariantOption.php b/common/modules/product/models/ProductVariantOption.php old mode 100644 new mode 100755 index c1d30ae..c1d30ae --- a/common/modules/product/models/ProductVariantOption.php +++ b/common/modules/product/models/ProductVariantOption.php diff --git a/common/modules/product/models/Stock.php b/common/modules/product/models/Stock.php index 0d02c32..db3e191 100755 --- a/common/modules/product/models/Stock.php +++ b/common/modules/product/models/Stock.php @@ -29,6 +29,7 @@ class Stock extends \yii\db\ActiveRecord { return [ [['name'], 'string', 'max' => 150], + [['name'], 'required'], ]; } diff --git a/common/modules/product/views/variant/_form.php b/common/modules/product/views/variant/_form.php old mode 100644 new mode 100755 index abcbf05..bd74bf5 --- a/common/modules/product/views/variant/_form.php +++ b/common/modules/product/views/variant/_form.php @@ -1,23 +1,48 @@ +/* @var $stocks common\modules\product\models\Stock */ + + +$js = ' +$(".dynamicform_wrapper").on("beforeInsert", function(e, item) { + console.log("beforeInsert"); +}); + +$(".dynamicform_wrapper").on("afterInsert", function(e, item) { + console.log("afterInsert"); +}); + +$(".dynamicform_wrapper").on("beforeDelete", function(e, item) { + if (! confirm("Are you sure you want to delete this item?")) { + return false; + } + return true; +}); +$(".dynamicform_wrapper").on("afterDelete", function(e) { + console.log("Deleted item!"); +}); + +$(".dynamicform_wrapper").on("limitReached", function(e, item) { + alert("Limit reached"); +}); +'; + +$this->registerJs($js, View::POS_END); +?>
" . htmlspecialchars($variant->sku) . "
";
- print "