Commit 37c2918b6f510183d7126de7b9d7759b71e17a91

Authored by Karnovsky A
1 parent 402f0738

-

Showing 1 changed file with 153 additions and 98 deletions   Show diff stats
common/modules/product/models/import.php
... ... @@ -54,7 +54,113 @@ class Import extends Model {
54 54 return $this->type;
55 55 }
56 56  
57   - public function goProducts() {
  57 + public function goPrices($from = 0, $limit = null) {
  58 + set_time_limit(0);
  59 + $new_products = $linked_products = 0;
  60 +
  61 + if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) {
  62 + $this->errors[] = 'File not found';
  63 + return FALSE;
  64 + }
  65 +
  66 + $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
  67 +
  68 + if ($from) {
  69 + fseek($handle, $from);
  70 + }
  71 +
  72 + $j = 0;
  73 +
  74 + $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'), null, null, null, 1000000)));
  75 +
  76 + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit))
  77 + {
  78 + foreach ($data as &$value)
  79 + {
  80 + if (!$is_utf) {
  81 + $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value);
  82 + }
  83 + $value = trim ($value);
  84 + }
  85 +
  86 + // данные строк
  87 + $modification_code = @$data[0];
  88 + $price = floatval(@$data[1]);
  89 + $price_promo = floatval(@$data[2]);
  90 + $count = intval(@$data[3]);
  91 + $city_name = @$data[4];
  92 + $product_title = @$data[5];
  93 +
  94 + if (empty ($modification_code)) {
  95 + CONTINUE;
  96 + }
  97 + // товары в пути
  98 + if (empty ($city_name))
  99 + {
  100 + $this->saveNotFoundRecord (
  101 + [$modification_code, $product_title],
  102 + Yii::getAlias('@uploadFilePricesAway')
  103 + );
  104 +
  105 + $this->output[] = 'Товар '. $product_title . ' в пути';
  106 +
  107 + CONTINUE;
  108 + }
  109 +
  110 + if ( ($productVariant = ProductVariant::find()->filterWhere(['ilike', 'sku', trim($modification_code)])->one()) === null ) {
  111 + // 'Нет даной модификации в базе';
  112 + $this->saveNotFoundRecord (
  113 + [$modification_code, $product_title],
  114 + Yii::getAlias('@uploadFilePricesNoVariant')
  115 + );
  116 +
  117 + $this->output[] = 'Для товара '. $product_title . ' не найдено соотвествие';
  118 +
  119 + CONTINUE;
  120 + }
  121 +
  122 + $quantity = 0;
  123 +
  124 + // ===== Set stock ====
  125 + if ( $city_name ) {
  126 + if ( ($stock = Stock::find()->filterWhere(['ilike', 'name', trim($city_name)])->one()) === null ) {
  127 + // Create stock
  128 + $stock = new Stock();
  129 + $stock->name = trim($city_name);
  130 + $stock->save();
  131 + }
  132 +
  133 + $productVariant->stocks[$stock->stock_id] = $count;
  134 + $quantity = $quantity + $count;
  135 + }
  136 +
  137 + $productVariant->price = $price;
  138 + $productVariant->price_old = $price_promo;
  139 + $productVariant->stock = $quantity;
  140 +
  141 + $productVariant->save();
  142 +
  143 + $this->output[] = '<font style="color:blue">Товар '. $product_title .' успешно сохранен</font>';
  144 + }
  145 +
  146 + $result = [
  147 + 'end' => feof($handle),
  148 + 'from' => ftell($handle),
  149 + 'totalsize' => $filesize,
  150 + 'items' => $this->output,
  151 +
  152 + ];
  153 +
  154 + fclose ($handle);
  155 +
  156 + if ($result['end']) {
  157 + unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'));
  158 + }
  159 +
  160 + return $result;
  161 + }
  162 +
  163 + public function goProducts($from = 0, $limit = null) {
58 164 set_time_limit(0);
59 165 $new_products = $linked_products = 0;
60 166  
... ... @@ -63,17 +169,20 @@ class Import extends Model {
63 169 return FALSE;
64 170 }
65 171  
  172 + $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
  173 +
  174 + if ($from) {
  175 + fseek($handle, $from);
  176 + }
  177 +
66 178 $j = 0;
67 179  
68 180 $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'), null, null, null, 1000000)));
69 181  
70   - while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE)
71   - {
72   - $j++;
73   - /*if ($j > 10) {
74   - return TRUE;
75   - }*/
  182 + $result_items = [];
76 183  
  184 + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit))
  185 + {
77 186 foreach ($data as &$value)
78 187 {
79 188 if (!$is_utf) {
... ... @@ -95,6 +204,7 @@ class Import extends Model {
95 204 $catalog_name = $data[0];
96 205 if (empty ($catalog_name))
97 206 {
  207 + $result_items[] = "Не указана категория (строка $j)";
98 208 CONTINUE;
99 209 }
100 210  
... ... @@ -102,6 +212,7 @@ class Import extends Model {
102 212 $brand_name = $data[1];
103 213 if (empty ($brand_name))
104 214 {
  215 + $result_items[] = "Не указан бренд (строка $j)";
105 216 CONTINUE;
106 217 }
107 218  
... ... @@ -109,6 +220,7 @@ class Import extends Model {
109 220 $product_name = $data[2];
110 221 if (empty ($product_name))
111 222 {
  223 + $result_items[] = "Не указано наименование товара (строка $j)";
112 224 CONTINUE;
113 225 }
114 226  
... ... @@ -155,7 +267,9 @@ class Import extends Model {
155 267 $product_video = $data[16];
156 268  
157 269 // 18 Галлерея фото
158   - $fotos = explode (',', $data[17]);
  270 + if (trim($data[17])) {
  271 + $fotos = explode (',', trim($data[17]));
  272 + }
159 273  
160 274 // 19 Штрих код товара.
161 275 // расшифровал - это модификации товара!
... ... @@ -199,10 +313,26 @@ class Import extends Model {
199 313 $_product->is_new = $product_new;
200 314  
201 315 if (!$_product->save()) {
202   - $this->errors[] = 'Product #'. $_product->name .' not saved';
  316 + $result_items[] = 'Product #'. $_product->name .' not saved' . " (строка $j)";
203 317 continue;
204 318 }
205 319  
  320 + if (!empty($fotos)) {
  321 + foreach($fotos as $foto) {
  322 + $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto);
  323 +
  324 + if (file_exists($source_image)) {
  325 + if (($productImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $foto])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null) {
  326 + copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto);
  327 + $productImage = new ProductImage();
  328 + $productImage->product_id = $_product->product_id;
  329 + $productImage->image = $foto;
  330 + $productImage->save();
  331 + }
  332 + }
  333 + }
  334 + }
  335 +
206 336 // нужно для проставления характеристик относящихся к модификациям
207 337 $MOD_ARRAY = [];
208 338  
... ... @@ -256,11 +386,10 @@ class Import extends Model {
256 386 $MOD_ARRAY[] = $_productVariant->product_variant_id;
257 387  
258 388 if ($mod_image) {
259   - $url = 'http://rukzachok.com.ua/upload/mod/' . urlencode($mod_image);
260   - $image = @file_get_contents($url);
261   - if ($image) {
  389 + $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($mod_image);
  390 + if (file_exists($source_image)) {
262 391 if (($variantImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $mod_image])->andFilterWhere(['product_variant_id' => $_productVariant->product_variant_id])->one()) === null) {
263   - file_put_contents(Yii::getAlias('@productsDir') . "/" . $mod_image, $image);
  392 + copy($source_image, Yii::getAlias('@productsDir') . "/" . $mod_image);
264 393 $variantImage = new ProductImage();
265 394 $variantImage->product_id = $_product->product_id;
266 395 $variantImage->product_variant_id = $_productVariant->product_variant_id;
... ... @@ -355,98 +484,24 @@ class Import extends Model {
355 484  
356 485 $_product->save();
357 486  
358   - $this->output[] = "$j: Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")";
  487 + $result_items[] = "Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")" . " (строка $j)";
359 488 }
360 489  
361   - fclose ($handle);
  490 + $result = [
  491 + 'end' => feof($handle),
  492 + 'from' => ftell($handle),
  493 + 'totalsize' => $filesize,
  494 + 'items' => $result_items,
362 495  
363   - return TRUE;
364   - }
  496 + ];
365 497  
366   - public function goPrices() {
367   - set_time_limit(0);
368   - $new_products = $linked_products = 0;
  498 + fclose ($handle);
369 499  
370   - if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) {
371   - $this->errors[] = 'File not found';
372   - return FALSE;
  500 + if ($result['end']) {
  501 + unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
373 502 }
374 503  
375   - $j = 0;
376   -
377   - while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) {
378   - $j++;
379   -// if ($j > 10) {
380   -// return TRUE;
381   -// }
382   -
383   - foreach ($data as &$value)
384   - {
385   - //$value = mb_convert_encoding ($value, "UTF-8", mb_detect_encoding ($value));
386   - $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value);
387   - $value = trim ($value);
388   - }
389   -
390   - // данные строк
391   - $modification_code = @$data[0];
392   - $price = floatval(@$data[1]);
393   - $price_promo = floatval(@$data[2]);
394   - $count = intval(@$data[3]);
395   - $city_name = @$data[4];
396   - $product_title = @$data[5];
397   -
398   - if (empty ($modification_code)) {
399   - CONTINUE;
400   - }
401   - // товары в пути
402   - if (empty ($city_name))
403   - {
404   - $this->saveNotFoundRecord (
405   - [$modification_code, $product_title],
406   - Yii::getAlias('@uploadFilePricesAway')
407   - );
408   -
409   - $this->output[] = 'Товар '. $product_title . ' в пути';
410   -
411   - CONTINUE;
412   - }
413   -
414   - if ( ($productVariant = ProductVariant::find()->filterWhere(['ilike', 'sku', trim($modification_code)])->one()) === null ) {
415   - // 'Нет даной модификации в базе';
416   - $this->saveNotFoundRecord (
417   - [$modification_code, $product_title],
418   - Yii::getAlias('@uploadFilePricesNoVariant')
419   - );
420   -
421   - $this->output[] = 'Для товара '. $product_title . ' не найдено соотвествия';
422   -
423   - CONTINUE;
424   - }
425   -
426   - $quantity = 0;
427   -
428   - // ===== Set stock ====
429   - if ( $city_name ) {
430   - if ( ($stock = Stock::find()->filterWhere(['ilike', 'name', trim($city_name)])->one()) === null ) {
431   - // Create stock
432   - $stock = new Stock();
433   - $stock->name = trim($city_name);
434   - $stock->save();
435   - }
436   -
437   - $productVariant->stocks[$stock->stock_id] = $count;
438   - $quantity = $quantity + $count;
439   - }
440   -
441   - $productVariant->price = $price;
442   - $productVariant->price_old = $price_promo;
443   - $productVariant->stock = $quantity;
444   -
445   - $productVariant->save();
446   -
447   - $this->output[] = '<font style="color:blue">Товар '. $product_title .' успешно сохранен</font>';
448   - }
449   - fclose ($handle);
  504 + return $result;
450 505 }
451 506  
452 507 private function getProductsFile($file_type) {
... ...