Commit 9495311a45a7e128ba654497db17c8a47a637855

Authored by Administrator
1 parent a4b231f8

14.06.16

common/modules/product/models/Import.php
... ... @@ -16,6 +16,7 @@ use common\modules\product\models\Product;
16 16 use common\modules\product\models\ProductVariant;
17 17 use common\modules\product\models\RemoteProducts;
18 18 use yii\base\Model;
  19 +use yii\helpers\ArrayHelper;
19 20  
20 21 class Import extends Model {
21 22 public $file;
... ... @@ -119,7 +120,7 @@ class Import extends Model {
119 120 }
120 121  
121 122  
122   - $quantity = 0;
  123 +
123 124  
124 125 // ===== Set stock ====
125 126 if ( $city_name ) {
... ... @@ -130,8 +131,23 @@ class Import extends Model {
130 131 $stock->save();
131 132 }
132 133  
133   - $productVariant->stocks[$stock->stock_id] = $count;
134   - $quantity = $quantity + $count;
  134 + $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $stock->stock_id])->one();
  135 + $productStock->quantity = $count;
  136 + $productStock->save();
  137 +
  138 + $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all();
  139 +
  140 + $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
  141 + } else {
  142 +
  143 + $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->all();
  144 +
  145 + if($productStocks instanceof ProductStock){
  146 + $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
  147 + } else {
  148 + $quantity = 0;
  149 + }
  150 +
135 151 }
136 152  
137 153 if ($price_promo) {
... ...
common/modules/product/models/ProductVariant.php
... ... @@ -169,21 +169,6 @@ class ProductVariant extends \yii\db\ActiveRecord
169 169 $this->stocks = (array) $stocks;
170 170 }
171 171  
172   - public function afterSave($insert, $changedAttributes)
173   - {
174   - if (!is_null($this->stocks)) {
175   - ProductStock::deleteAll(['product_variant_id' => $this->product_variant_id]);
176   - $values = [];
177   - foreach ($this->stocks as $id => $quantity) {
178   - $values[] = [$this->product_id, $this->product_variant_id, $id, $quantity];
179   - }
180   - if ($values) {
181   - self::getDb()->createCommand()
182   - ->batchInsert(ProductStock::tableName(), ['product_id', 'product_variant_id', 'stock_id', 'quantity'], $values)->execute();
183   - }
184   - }
185   - parent::afterSave($insert, $changedAttributes);
186   - }
187 172  
188 173 public function beforeDelete() {
189 174 ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]);
... ...