From ab917b7436f86e52d80f204b5ec2a14e41312c25 Mon Sep 17 00:00:00 2001 From: Karnovsky A Date: Tue, 7 Jun 2016 14:58:40 +0300 Subject: [PATCH] Import --- common/modules/product/controllers/ManageController.php | 36 +++++++++++++++++++++++++++++++++--- common/modules/product/views/manage/import-process.php | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/controllers/ImportController.php | 468 ++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 3 files changed, 111 insertions(+), 465 deletions(-) create mode 100644 common/modules/product/views/manage/import-process.php diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index 495ae00..130506c 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -268,15 +268,23 @@ class ManageController extends Controller if ($model->load(Yii::$app->request->post())) { $file = UploadedFile::getInstances($model, 'file'); $method = 'go'. ucfirst($model->type); + $target = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFile'. ucfirst($model->type)); if (empty($file)) { $model->errors[] = 'File not upload'; } elseif ($method == 'goPrices' && $file[0]->name != 'file_1.csv') { $model->errors[] = 'File need "file_1.csv"'; } elseif ($method == 'goProducts' && $file[0]->name == 'file_1.csv') { $model->errors[] = 'File can not "file_1.csv"'; - } elseif ($model->validate()) { - $file[0]->saveAs(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFile'. ucfirst($model->type))); - $model->$method(); + } elseif ($model->validate() && $file[0]->saveAs($target)) { + // PROCESS PAGE + return $this->render('import-process', [ + 'model' => $model, + 'method' => $model->type, + 'target' => $target, + ]); +// $model->$method(); + } else { + $model->errors[] = 'File can not be upload or other error'; } } @@ -285,6 +293,28 @@ class ManageController extends Controller ]); } + public function actionProducts() { + $from = Yii::$app->request->get('from', 0); + + $model = new Import(); + + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + return $model->goProducts($from, 10); + } + } + + public function actionPrices() { + $from = Yii::$app->request->get('from', 0); + + $model = new Import(); + + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + return $model->goPrices($from, 10); + } + } + public function actionExport() { $model = new Export(); if (($file = $model->process(Yii::getAlias('@uploadDir')))) { diff --git a/common/modules/product/views/manage/import-process.php b/common/modules/product/views/manage/import-process.php new file mode 100644 index 0000000..206ae65 --- /dev/null +++ b/common/modules/product/views/manage/import-process.php @@ -0,0 +1,72 @@ +registerJs(" + +"); +?> + + + +
+

Импорт товаров

+ + [ + 'value' => 100, + 'label' => 'ddd' + ], + 'options' => [ + 'id' => 'progressbar' + ], + ]);?> + +
diff --git a/console/controllers/ImportController.php b/console/controllers/ImportController.php index 9e77457..78efc93 100755 --- a/console/controllers/ImportController.php +++ b/console/controllers/ImportController.php @@ -4,6 +4,7 @@ namespace console\controllers; use common\modules\product\models\Category; use common\modules\product\models\CategoryName; +use common\modules\product\models\Import; use common\modules\product\models\ProductImage; use common\modules\product\models\ProductVariantType; use common\modules\product\models\Stock; @@ -28,473 +29,16 @@ class ImportController extends Controller { return fopen ($filename, 'r'); } - public function actionIndex() { - $new_products = $linked_products = 0; - - $db = yii::$app->db; - - if ( !($handle = $this->getProductsFile()) ) { - return Controller::EXIT_CODE_ERROR; - } - - $j = 0; - - while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) - { - $j++; - - 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); - } - - // будет всегда 19 элементов - for ($i = 0; $i <= 18; $i++) - { - if (! isset ($data[$i])) - { - $data[$i] = null; - } - } - - // 1 Группа (категория) - $catalog_name = $data[0]; - if (empty ($catalog_name)) - { - CONTINUE; - } - - // 2 Бренд - $brand_name = $data[1]; - if (empty ($brand_name)) - { - CONTINUE; - } - - // 3 Название товара - $product_name = $data[2]; - if (empty ($product_name)) - { - CONTINUE; - } - - // 4 Описание Укр - $product_body_uk = $data[3]; - - // 5 Описание Рус - $product_body_ru = $data[4]; - - // 6 Фильтр (через запятую) - $filters = explode (',', $data[5]); - - // 7 Доп фильтр через запятую - $filters_extra = explode (',', $data[6]); - - // 8 Пол череззапятую (мужской, женский, детский, унисекс) - $gender = explode (',', $data[7]); - - // 9 Год - $years = explode (',', $data[8]); - - // 10 Цена старая - $product_cost_old = $data[10]; - - // 11 Цена - $product_cost = $data[9]; - - // 12 Акция - $product_akciya = (bool)$data[11]; - - // 13 Сопуд. Тов. - $similar = explode (',', $data[12]); - - // 14 Новинки - $product_new = (bool)$data[13]; - - // 15 Топ продаж - $product_top = (bool)$data[14]; - - // 16 Сетка Характеристик - $feature = explode ('=', $data[15]); - - // 17 ВИДЕО КОД - $product_video = $data[16]; - - // 18 Галлерея фото - $fotos = explode (',', $data[17]); - - // 19 Штрих код товара. - // расшифровал - это модификации товара! - - $product_image = explode ('=', $data[18]); - $product_image = @$product_image[3]; - - if ( ($_product = Product::find()->filterWhere(['ilike', 'name', trim($product_name)])->one()) === null ) { - $_product = new Product(); - } - - $is_new_product = empty($_product->product_id); - - // ==== Set category ==== - if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) { - // Create category - $category = new Category(); - $category->name = trim($catalog_name); - $category->save(); - } - $_product->categories = [$category->category_id]; - - // ===== Set brand ==== - if ( $brand_name ) { - if ( ($brand = BrandName::find()->filterWhere(['ilike', 'value', trim($brand_name)])->one()) !== null ) { - $_product->brand_id = $brand->brand_id; - } else { - // Create brand - $brand = new Brand(); - $brand->name = trim($brand_name); - $brand->save(); - $_product->brand_id = $brand->brand_id; - } - } - - $_product->name = $product_name; - $_product->video = $product_video; - $_product->description = $product_body_ru; - $_product->is_top = $product_top; - $_product->akciya = $product_akciya; - $_product->is_new = $product_new; - - if (!$_product->save()) { - var_dump($category->category_id, $_product->categories);exit; - } - - - // @todo Set image - /*// картинки - if (is_file ($dir.'/mod/' . $product_image)) - { - $resizeObj = new resize ($dir.'/mod/' . $product_image); - $resizeObj->resizeImage (135, 200, 'auto'); - $resizeObj->saveImage ($dir.'/products/ico/' . $product_image, 100); - $resizeObj->resizeImage (370, 370, 'auto'); - $resizeObj->saveImage ($dir.'/products/big/' . $product_image, 100); - }*/ - - // ==== mods ==== - - // (типа штрих код товара) - - // нужно для проставления характеристик относящихся к модификациям - $MOD_ARRAY = []; - - for ($i = 18; $i < count ($data); $i ++) - { - if (! empty ($data[$i])) - { - $mod_arr = explode ('=', $data[$i]); - $mod_art = $mod_arr[0]; - $mod_size = $mod_arr[1]; - $mod_color = $mod_arr[2]; - $mod_image = $mod_arr[3]; - $mod_cost = floatval($product_cost); - $mod_old_cost = floatval($product_cost_old); - - // Check product variant - if ( ($_productVariant = ProductVariant::find()->andFilterWhere(['ilike', 'sku', $mod_art])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null ) { - $_productVariant = new ProductVariant(); - $_productVariant->product_id = $_product->product_id; - } - $_productVariant->product_unit_id = 1; - - $_productVariant->sku = $mod_art; - $_productVariant->price = $mod_cost; - $_productVariant->price_old = $mod_old_cost; - $_productVariant->stock = 1; - - $product_variant_type_name = ''; - if (! empty ($mod_color)) { - $product_variant_type_name = 'Цвет'; - $_productVariant->name = $mod_color; - } - elseif (! empty ($mod_size)) { - $product_variant_type_name = 'Размер'; - $_productVariant->name = $mod_size; - } - - // ===== Set variant type ==== - if ( $product_variant_type_name ) { - if ( ($product_variant_type = ProductVariantType::find()->filterWhere(['ilike', 'name', $product_variant_type_name])->one()) !== null ) { - $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; - } else { - $product_variant_type = new ProductVariantType(); - $product_variant_type->name = $product_variant_type_name; - $product_variant_type->save(); - $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; - } - } - $_productVariant->save(false); -// if (!$_productVariant->save(false)) { -//// $this->stdout("$j: Product {$_product->name} #{$_product->product_id} NOT saved (". ($is_new_product ? 'new product' : 'exists product') .")\n"); -//// var_dump($_productVariant);exit; -// continue; -// } - -// if ($mod_art == '610934725148') { -// var_dump($_productVariant); -// exit; -// } - - $MOD_ARRAY[] = $_productVariant->product_variant_id; - - if ($mod_image) { - $url = 'http://rukzachok.com.ua/upload/mod/' . urlencode($mod_image); - $image = @file_get_contents($url); - if ($image) { - if (($variantImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $mod_image])->andFilterWhere(['product_variant_id' => $_productVariant->product_variant_id])->one()) === null) { - file_put_contents(Yii::getAlias('@productsDir') . "/" . $mod_image, $image); - $variantImage = new ProductImage(); - $variantImage->product_id = $_product->product_id; - $variantImage->product_variant_id = $_productVariant->product_variant_id; - $variantImage->image = $mod_image; - $variantImage->save(); - } - } - } - } - } - - /*// ==== fotos ==== - - foreach ($fotos as $foto) - { - $fields = [ - 'product_id' => $product_id, - 'image' => $foto - ]; - - $modelFotos = Fotos::find () - ->where ('image=:image', [ - ':image' => $foto - ]) - ->one (); - - if (empty ($modelFotos->id)) - { - $db->createCommand () - ->insert ('products_fotos', $fields) - ->execute (); - } - else - { - $db->createCommand () - ->update ('products_fotos', $fields, 'id = ' . $modelFotos->id) - ->execute (); - } - - if (is_file ($dir.'/fotos/' . $foto)) - { - $resizeObj = new resize ($dir.'/fotos/' . $foto); - $resizeObj->resizeImage (100, 100, 'crop'); - $resizeObj->saveImage ($dir.'/fotos/ico/' . $foto, 100); - $resizeObj->resizeImage (400, 400, 'crop'); - $resizeObj->saveImage ($dir.'/fotos/big/' . $foto, 100); - } - }*/ - - $options = []; - - if (! empty ($filters)) { - // Set Naznachenie (tax_group_id = 20) - foreach($filters as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 20])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 20; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } - } - - if (! empty ($years)) { - // Set God (tax_group_id = 21) - foreach($years as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 21])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 21; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } - } - - if (! empty ($gender)) { - // Set Pol (tax_group_id = 22) - foreach($gender as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 22])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 22; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } - } - - if (!empty($options)) { - $_product->options = $options; - } - - $_product->save(); - - $this->stdout("$j: Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")\n"); - - /*if ($j > 100) { - $this->stdout("Dev OK"); - exit; - }*/ - - } - - fclose ($handle); + public function actionProducts() { + $model = new Import(); + $data = $model->goProducts(0, null); return Controller::EXIT_CODE_NORMAL; } - public function goProducts() { - - } - public function actionPrices() { - $new_products = $linked_products = 0; - - if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) { - $this->stdout("File not found\n"); - return Controller::EXIT_CODE_ERROR; - } - - $j = 0; - - while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) { - $j++; -// if ($j > 1) { -// 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]; - - // @todo refactory this code - $price = $_price_promo > 0 ? $_price_promo : $_price; - $price_promo = $_price_promo > 0 ? $_price : $_price_promo; - - $modification_code = trim($modification_code); - - if (empty ($modification_code)) { - continue; - } - // товары в пути - if (empty ($city_name)) - { - $this->saveNotFoundRecord ( - [$modification_code, $product_title], - Yii::getAlias('@uploadFilePricesAway') - ); - - $this->stdout("~ Товар $product_title в пути\n"); - - continue; - } - - if ( ($productVariant = ProductVariant::find()->filterWhere(['sku' => $modification_code])->one()) === null ) { - // 'Нет даной модификации в базе'; - $this->saveNotFoundRecord ( - [$modification_code, $product_title], - Yii::getAlias('@uploadFilePricesNoVariant') - ); - - $this->stdout("- Для товара $product_title (#$modification_code) не найдено соотвествия\n"); - - continue; - } - - $quantity = 0; - - // ===== Set stock ==== - if ( $city_name ) { - if ( ($stock = Stock::find()->filterWhere(['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->stdout("+ Товар $product_title успешно сохранен\n"); - } - fclose ($handle); - - unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices')); + $model = new Import(); + $data = $model->goPrices(0, null); return Controller::EXIT_CODE_NORMAL; } -- libgit2 0.21.4