From 02a42412008421f2ca7714e026f089b24c556bd9 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 17 Jun 2016 13:39:02 +0300 Subject: [PATCH] 14.06.16 --- common/modules/product/models/Import.php | 12 +++++++++--- common/modules/product/models/ProductVariant.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index d412a47..d3dc7ee 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -131,16 +131,22 @@ class Import extends Model { $stock->save(); } - $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $stock->stock_id])->one(); + $productStock = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id, 'stock_id' => $stock->stock_id])->one(); + if(!$productStock instanceof ProductStock) { + $productStock = new ProductStock; + $productStock->product_variant_id = $productVariant->product_variant_id; + $productStock->stock_id = $stock->stock_id; + $productStock->product_id = $productVariant->product_id; + } $productStock->quantity = $count; $productStock->save(); - $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all(); + $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all(); $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; } else { - $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->all(); + $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->all(); if($productStocks instanceof ProductStock){ $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 477c562..531854d 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -169,6 +169,19 @@ class ProductVariant extends \yii\db\ActiveRecord $this->stocks = (array) $stocks; } + public function afterSave($insert, $changedAttributes) + { + if (!is_null($this->stocks)) { + //ProductStock::deleteAll(['product_variant_id' => $this->product_variant_id]); + $values = []; + foreach ($this->stocks as $id => $quantity) { + $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $id])->one(); + $productStock->quantity = $quantity; + $productStock->save(); + } + } + parent::afterSave($insert, $changedAttributes); + } public function beforeDelete() { ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]); -- libgit2 0.21.4