Commit 02a42412008421f2ca7714e026f089b24c556bd9
1 parent
9495311a
14.06.16
Showing
2 changed files
with
22 additions
and
3 deletions
Show diff stats
common/modules/product/models/Import.php
@@ -131,16 +131,22 @@ class Import extends Model { | @@ -131,16 +131,22 @@ class Import extends Model { | ||
131 | $stock->save(); | 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 | $productStock->quantity = $count; | 141 | $productStock->quantity = $count; |
136 | $productStock->save(); | 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 | $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; | 146 | $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; |
141 | } else { | 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 | if($productStocks instanceof ProductStock){ | 151 | if($productStocks instanceof ProductStock){ |
146 | $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; | 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,6 +169,19 @@ class ProductVariant extends \yii\db\ActiveRecord | ||
169 | $this->stocks = (array) $stocks; | 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 | public function beforeDelete() { | 186 | public function beforeDelete() { |
174 | ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]); | 187 | ProductImage::deleteAll(['product_variant_id' => $this->product_variant_id]); |