diff --git a/backend/config/bootstrap.php b/backend/config/bootstrap.php index 3408edf..293a0af 100755 --- a/backend/config/bootstrap.php +++ b/backend/config/bootstrap.php @@ -1,5 +1,9 @@ request->isPost) { + if ($model->load(Yii::$app->request->post())) { $file = UploadedFile::getInstances($model, 'file'); - if (!empty($file[0]) && $model->validate() && $file[0]->saveAs(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'))) { - $model->go(); + $method = 'go'. ucfirst($model->type); + if (!empty($file) && $model->validate()) { + $file[0]->saveAs(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFile'. ucfirst($model->type))); + $model->$method(); } } + return $this->render('import', [ 'model' => $model, ]); diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 2bb9b38..111bb54 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -188,6 +188,10 @@ class Product extends \yii\db\ActiveRecord return $this->getRelations('product_option'); } + public function getStocks() { + return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id']); + } + /** * @inheritdoc * @return ProductQuery the active query used by this AR class. diff --git a/common/modules/product/models/ProductStock.php b/common/modules/product/models/ProductStock.php new file mode 100644 index 0000000..c667ada --- /dev/null +++ b/common/modules/product/models/ProductStock.php @@ -0,0 +1,78 @@ + true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], + [['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']], + [['stock_id'], 'exist', 'skipOnError' => true, 'targetClass' => Stock::className(), 'targetAttribute' => ['stock_id' => 'stock_id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product_id' => 'Product ID', + 'stock_id' => 'Stock ID', + 'quantity' => 'Quantity', + 'product_variant_id' => 'Product Variant ID', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne(Product::className(), ['product_id' => 'product_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProductVariant() + { + return $this->hasOne(ProductVariant::className(), ['product_variant_id' => 'product_variant_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getStock() + { + return $this->hasOne(Stock::className(), ['stock_id' => 'stock_id']); + } +} diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index bddce79..7bbbfc8 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -3,6 +3,7 @@ namespace common\modules\product\models; use Yii; +use yii\helpers\ArrayHelper; /** * This is the model class for table "product_variant". @@ -33,6 +34,7 @@ class ProductVariant extends \yii\db\ActiveRecord public $translit; public $translit_rubric; private $data; + public $stocks = []; /** @var array $_images */ // public $imagesUpload = []; @@ -106,8 +108,12 @@ class ProductVariant extends \yii\db\ActiveRecord return $this->hasOne(Product::className(), ['product_id' => 'product_id']); } - public function getEnabled() { - return $this->stock !== 0; + public function getQuantity() { + return ProductStock::find() + ->where(['product_variant_id' => $this->product_variant_id]) + ->sum('quantity'); +// return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id'])->sum(ProductStock::tableName() .'.quantity') > 0; +// return $this->stock !== 0; } public function getStock_caption() { @@ -157,7 +163,36 @@ class ProductVariant extends \yii\db\ActiveRecord return $this->product_variant_id; } + public function setStocks($stocks) { + $this->stocks = (array) $stocks; + } + + /*public function getStocks() { + return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_variant_id' => 'product_variant_id']); + } + + public function getStocksIds() { + return ArrayHelper::getColumn($this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_variant_id' => 'product_variant_id'])->all(), 'stock_id'); + }*/ + + public function afterSave($insert, $changedAttributes) + { + if (!empty($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]); + ProductStock::deleteAll(['product_variant_id' => $this->product_variant_id]); } } diff --git a/common/modules/product/models/Stock.php b/common/modules/product/models/Stock.php new file mode 100644 index 0000000..0d02c32 --- /dev/null +++ b/common/modules/product/models/Stock.php @@ -0,0 +1,54 @@ + 150], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'stock_id' => Yii::t('product', 'Stock ID'), + 'name' => Yii::t('product', 'Name'), + ]; + } + + /** + * @inheritdoc + * @return StockQuery the active query used by this AR class. + */ + public static function find() + { + return new StockQuery(get_called_class()); + } +} diff --git a/common/modules/product/models/StockQuery.php b/common/modules/product/models/StockQuery.php new file mode 100644 index 0000000..2650415 --- /dev/null +++ b/common/modules/product/models/StockQuery.php @@ -0,0 +1,34 @@ +andWhere('[[status]]=1'); + }*/ + + /** + * @inheritdoc + * @return Stock[]|array + */ + public function all($db = null) + { + return parent::all($db); + } + + /** + * @inheritdoc + * @return Stock|array|null + */ + public function one($db = null) + { + return parent::one($db); + } +} diff --git a/common/modules/product/models/import.php b/common/modules/product/models/import.php index e24d8dc..2d72124 100644 --- a/common/modules/product/models/import.php +++ b/common/modules/product/models/import.php @@ -18,6 +18,7 @@ use yii\base\Model; class Import extends Model { public $file; + public $type; public $errors = []; public $output = []; @@ -28,8 +29,10 @@ class Import extends Model { public function rules() { return [ -// [['file'], 'safe'], - [['file'], 'file'], + [['type'], 'required'], + [['type'], 'string'], + [['file'], 'safe'], + [['file'], 'file', 'extensions' => 'csv'], ]; } @@ -43,13 +46,18 @@ class Import extends Model { ]; } - public function go() { + public function getType() { + if (!$this->type) { + $this->type = 'products'; + } + return $this->type; + } + public function goProducts() { + set_time_limit(0); $new_products = $linked_products = 0; - $db = yii::$app->db; - - if ( !($handle = $this->getProductsFile()) ) { + if ( !($handle = $this->getProductsFile('uploadFileProducts')) ) { $this->errors[] = 'File not found'; return FALSE; } @@ -351,12 +359,108 @@ class Import extends Model { return TRUE; } - private function getProductsFile() { - $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'); + public function goPrices() { + set_time_limit(0); + $new_products = $linked_products = 0; + + if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) { + $this->errors[] = 'File not found'; + return FALSE; + } + + $j = 0; + + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) { + $j++; +// if ($j > 10) { +// return TRUE; +// } + + foreach ($data as &$value) + { + //$value = mb_convert_encoding ($value, "UTF-8", mb_detect_encoding ($value)); + $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); + $value = trim ($value); + } + + // данные строк + $modification_code = @$data[0]; + $price = floatval(@$data[1]); + $price_promo = floatval(@$data[2]); + $count = intval(@$data[3]); + $city_name = @$data[4]; + $product_title = @$data[5]; + + if (empty ($modification_code)) { + CONTINUE; + } + // товары в пути + if (empty ($city_name)) + { + $this->saveNotFoundRecord ( + [$modification_code, $product_title], + Yii::getAlias('@uploadFilePricesAway') + ); + + $this->output[] = 'Товар '. $product_title . ' в пути'; + + CONTINUE; + } + + if ( ($productVariant = ProductVariant::find()->filterWhere(['ilike', 'sku', trim($modification_code)])->one()) === null ) { + // 'Нет даной модификации в базе'; + $this->saveNotFoundRecord ( + [$modification_code, $product_title], + Yii::getAlias('@uploadFilePricesNoVariant') + ); + + $this->output[] = 'Для товара '. $product_title . ' не найдено соотвествия'; + + CONTINUE; + } + + $quantity = 0; + + // ===== Set stock ==== + if ( $city_name ) { + if ( ($stock = Stock::find()->filterWhere(['ilike', 'name', trim($city_name)])->one()) === null ) { + // Create stock + $stock = new Stock(); + $stock->name = trim($city_name); + $stock->save(); + } + + $productVariant->stocks[$stock->stock_id] = $count; + $quantity = $quantity + $count; + } + + $productVariant->price = $price; + $productVariant->price_old = $price_promo; + $productVariant->stock = $quantity; + + $productVariant->save(); + + $this->output[] = 'Товар '. $product_title .' успешно сохранен'; + } + fclose ($handle); + } + + private function getProductsFile($file_type) { + $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); if (!is_file($filename)) { - $this->stderr("File $filename not found"); + $this->errors[] = "File $filename not found"; return FALSE; } return fopen ($filename, 'r'); } + + private function saveNotFoundRecord (array $line, $filename) + { + $str = implode (';', $line)."\n"; + $str = iconv ("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str); + + $fg = fopen (Yii::getAlias('@uploadDir') .'/'. $filename, 'a+'); + fputs ($fg, $str); + fclose ($fg); + } } \ No newline at end of file diff --git a/common/modules/product/views/manage/import.php b/common/modules/product/views/manage/import.php index ba9868a..81f9a31 100644 --- a/common/modules/product/views/manage/import.php +++ b/common/modules/product/views/manage/import.php @@ -6,6 +6,7 @@ use yii\widgets\ActiveForm;