From 37c2918b6f510183d7126de7b9d7759b71e17a91 Mon Sep 17 00:00:00 2001 From: Karnovsky A Date: Tue, 7 Jun 2016 15:21:05 +0300 Subject: [PATCH] - --- common/modules/product/models/import.php | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------- 1 file changed, 153 insertions(+), 98 deletions(-) diff --git a/common/modules/product/models/import.php b/common/modules/product/models/import.php index f1de8f5..be0f528 100644 --- a/common/modules/product/models/import.php +++ b/common/modules/product/models/import.php @@ -54,7 +54,113 @@ class Import extends Model { return $this->type; } - public function goProducts() { + public function goPrices($from = 0, $limit = null) { + set_time_limit(0); + $new_products = $linked_products = 0; + + if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) { + $this->errors[] = 'File not found'; + return FALSE; + } + + $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); + + if ($from) { + fseek($handle, $from); + } + + $j = 0; + + $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'), null, null, null, 1000000))); + + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit)) + { + foreach ($data as &$value) + { + if (!$is_utf) { + $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 .' успешно сохранен'; + } + + $result = [ + 'end' => feof($handle), + 'from' => ftell($handle), + 'totalsize' => $filesize, + 'items' => $this->output, + + ]; + + fclose ($handle); + + if ($result['end']) { + unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices')); + } + + return $result; + } + + public function goProducts($from = 0, $limit = null) { set_time_limit(0); $new_products = $linked_products = 0; @@ -63,17 +169,20 @@ class Import extends Model { return FALSE; } + $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); + + if ($from) { + fseek($handle, $from); + } + $j = 0; $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'), null, null, null, 1000000))); - while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) - { - $j++; - /*if ($j > 10) { - return TRUE; - }*/ + $result_items = []; + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit)) + { foreach ($data as &$value) { if (!$is_utf) { @@ -95,6 +204,7 @@ class Import extends Model { $catalog_name = $data[0]; if (empty ($catalog_name)) { + $result_items[] = "Не указана категория (строка $j)"; CONTINUE; } @@ -102,6 +212,7 @@ class Import extends Model { $brand_name = $data[1]; if (empty ($brand_name)) { + $result_items[] = "Не указан бренд (строка $j)"; CONTINUE; } @@ -109,6 +220,7 @@ class Import extends Model { $product_name = $data[2]; if (empty ($product_name)) { + $result_items[] = "Не указано наименование товара (строка $j)"; CONTINUE; } @@ -155,7 +267,9 @@ class Import extends Model { $product_video = $data[16]; // 18 Галлерея фото - $fotos = explode (',', $data[17]); + if (trim($data[17])) { + $fotos = explode (',', trim($data[17])); + } // 19 Штрих код товара. // расшифровал - это модификации товара! @@ -199,10 +313,26 @@ class Import extends Model { $_product->is_new = $product_new; if (!$_product->save()) { - $this->errors[] = 'Product #'. $_product->name .' not saved'; + $result_items[] = 'Product #'. $_product->name .' not saved' . " (строка $j)"; continue; } + if (!empty($fotos)) { + foreach($fotos as $foto) { + $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto); + + if (file_exists($source_image)) { + if (($productImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $foto])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null) { + copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto); + $productImage = new ProductImage(); + $productImage->product_id = $_product->product_id; + $productImage->image = $foto; + $productImage->save(); + } + } + } + } + // нужно для проставления характеристик относящихся к модификациям $MOD_ARRAY = []; @@ -256,11 +386,10 @@ class Import extends Model { $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) { + $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($mod_image); + if (file_exists($source_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); + copy($source_image, Yii::getAlias('@productsDir') . "/" . $mod_image); $variantImage = new ProductImage(); $variantImage->product_id = $_product->product_id; $variantImage->product_variant_id = $_productVariant->product_variant_id; @@ -355,98 +484,24 @@ class Import extends Model { $_product->save(); - $this->output[] = "$j: Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")"; + $result_items[] = "Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")" . " (строка $j)"; } - fclose ($handle); + $result = [ + 'end' => feof($handle), + 'from' => ftell($handle), + 'totalsize' => $filesize, + 'items' => $result_items, - return TRUE; - } + ]; - public function goPrices() { - set_time_limit(0); - $new_products = $linked_products = 0; + fclose ($handle); - if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) { - $this->errors[] = 'File not found'; - return FALSE; + if ($result['end']) { + unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); } - $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); + return $result; } private function getProductsFile($file_type) { -- libgit2 0.21.4