diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index abbbc56..d412a47 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -16,6 +16,7 @@ use common\modules\product\models\Product; use common\modules\product\models\ProductVariant; use common\modules\product\models\RemoteProducts; use yii\base\Model; +use yii\helpers\ArrayHelper; class Import extends Model { public $file; @@ -119,7 +120,7 @@ class Import extends Model { } - $quantity = 0; + // ===== Set stock ==== if ( $city_name ) { @@ -130,8 +131,23 @@ class Import extends Model { $stock->save(); } - $productVariant->stocks[$stock->stock_id] = $count; - $quantity = $quantity + $count; + $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $stock->stock_id])->one(); + $productStock->quantity = $count; + $productStock->save(); + + $productStocks = ProductStock::find()->where(['product_variant_id' => $this->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(); + + if($productStocks instanceof ProductStock){ + $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; + } else { + $quantity = 0; + } + } if ($price_promo) { diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 0867687..477c562 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -169,21 +169,6 @@ 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) { - $values[] = [$this->product_id, $this->product_variant_id, $id, $quantity]; - } - if ($values) { - self::getDb()->createCommand() - ->batchInsert(ProductStock::tableName(), ['product_id', 'product_variant_id', 'stock_id', 'quantity'], $values)->execute(); - } - } - parent::afterSave($insert, $changedAttributes); - } public function beforeDelete() { ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]); -- libgit2 0.21.4