Commit 02a42412008421f2ca7714e026f089b24c556bd9

Authored by Administrator
1 parent 9495311a

14.06.16

common/modules/product/models/Import.php
... ... @@ -131,16 +131,22 @@ class Import extends Model {
131 131 $stock->save();
132 132 }
133 133  
134   - $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $stock->stock_id])->one();
  134 + $productStock = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id, 'stock_id' => $stock->stock_id])->one();
  135 + if(!$productStock instanceof ProductStock) {
  136 + $productStock = new ProductStock;
  137 + $productStock->product_variant_id = $productVariant->product_variant_id;
  138 + $productStock->stock_id = $stock->stock_id;
  139 + $productStock->product_id = $productVariant->product_id;
  140 + }
135 141 $productStock->quantity = $count;
136 142 $productStock->save();
137 143  
138   - $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all();
  144 + $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all();
139 145  
140 146 $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
141 147 } else {
142 148  
143   - $productStocks = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id])->all();
  149 + $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->all();
144 150  
145 151 if($productStocks instanceof ProductStock){
146 152 $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
... ...
common/modules/product/models/ProductVariant.php
... ... @@ -169,6 +169,19 @@ 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 + $productStock = ProductStock::find()->where(['product_variant_id' => $this->product_variant_id, 'stock_id' => $id])->one();
  179 + $productStock->quantity = $quantity;
  180 + $productStock->save();
  181 + }
  182 + }
  183 + parent::afterSave($insert, $changedAttributes);
  184 + }
172 185  
173 186 public function beforeDelete() {
174 187 ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]);
... ...