d8c1a2e0
Yarik
Big commit artbox
|
1
|
<?php
|
e8ccb1b4
Yarik
Import beta
|
2
3
4
5
6
7
8
9
10
11
12
|
namespace common\modules\product\models;
use common\modules\language\models\Language;
use common\modules\rubrication\models\TaxGroup;
use common\modules\rubrication\models\TaxOption;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
class Import extends Model
|
d8c1a2e0
Yarik
Big commit artbox
|
13
|
{
|
e8ccb1b4
Yarik
Import beta
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
public $file;
public $type;
public $lang;
public $errors = [];
public $output = [];
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'type',
'lang',
],
'required',
],
[
[ 'lang' ],
'integer',
],
[
[ 'type' ],
'string',
],
[
[ 'file' ],
'file',
'extensions' => 'csv',
],
];
|
d8c1a2e0
Yarik
Big commit artbox
|
52
|
}
|
e8ccb1b4
Yarik
Import beta
|
53
54
55
56
57
58
59
60
61
|
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'file' => Yii::t('product', 'File'),
];
|
d8c1a2e0
Yarik
Big commit artbox
|
62
|
}
|
e8ccb1b4
Yarik
Import beta
|
63
64
|
public function getType()
|
d8c1a2e0
Yarik
Big commit artbox
|
65
|
{
|
e8ccb1b4
Yarik
Import beta
|
66
67
|
if(!$this->type) {
$this->type = 'products';
|
d8c1a2e0
Yarik
Big commit artbox
|
68
|
}
|
e8ccb1b4
Yarik
Import beta
|
69
70
71
72
73
74
75
76
77
78
|
return $this->type;
}
public function goPrices($from = 0, $limit = NULL)
{
set_time_limit(0);
if(!( $handle = $this->getProductsFile('uploadFilePrices') )) {
$this->errors[] = 'File not found';
return false;
|
d8c1a2e0
Yarik
Big commit artbox
|
79
|
}
|
e8ccb1b4
Yarik
Import beta
|
80
81
82
83
|
$filesize = filesize(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices'));
if($from) {
fseek($handle, $from);
|
d8c1a2e0
Yarik
Big commit artbox
|
84
|
}
|
e8ccb1b4
Yarik
Import beta
|
85
86
87
88
89
90
91
92
93
94
95
|
$j = 0;
$is_utf = ( preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices'), NULL, NULL, NULL, 1000000)) );
while(( empty( $limit ) || $j++ < $limit ) && ( $data = fgetcsv($handle, 10000, ";") ) !== false) {
foreach($data as &$value) {
if(!$is_utf) {
$value = iconv('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value);
}
$value = trim($value);
|
d8c1a2e0
Yarik
Big commit artbox
|
96
|
}
|
e8ccb1b4
Yarik
Import beta
|
97
98
99
100
101
102
103
104
105
106
107
|
// данные строк
$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;
|
d8c1a2e0
Yarik
Big commit artbox
|
108
|
}
|
e8ccb1b4
Yarik
Import beta
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// товары в пути
if(empty ( $city_name )) {
$this->output[] = 'Товар ' . $product_title . ' в пути';
continue;
}
if(( $productVariant = ProductVariant::find()
->filterWhere([ 'sku' => $modification_code ])
->one() ) === NULL
) {
$this->output[] = 'Для товара ' . $product_title . ' не найдено соотвествие';
continue;
}
// ===== 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();
}
$productStock = ProductStock::find()
->where([
'product_variant_id' => $productVariant->product_variant_id,
'stock_id' => $stock->stock_id,
])
->one();
if(!$productStock instanceof ProductStock) {
$productStock = new ProductStock;
$productStock->product_variant_id = $productVariant->product_variant_id;
$productStock->stock_id = $stock->stock_id;
$productStock->product_id = $productVariant->product_id;
}
$productStock->quantity = $count;
$productStock->save();
$productStocks = ProductStock::find()
->where([ 'product_variant_id' => $productVariant->product_variant_id ])
->andWhere([
'<>',
'stock_id',
$stock->stock_id,
])
->all();
|
d8c1a2e0
Yarik
Big commit artbox
|
158
159
|
$quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
} else {
|
e8ccb1b4
Yarik
Import beta
|
160
161
162
163
164
165
166
167
168
169
170
|
$productStocks = ProductStock::find()
->where([ 'product_variant_id' => $productVariant->product_variant_id ])
->all();
if($productStocks instanceof ProductStock) {
$quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
} else {
$quantity = 0;
}
|
d8c1a2e0
Yarik
Big commit artbox
|
171
|
}
|
e8ccb1b4
Yarik
Import beta
|
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
if($price_promo) {
$productVariant->price_old = $price;
$productVariant->price = $price_promo;
} else {
$productVariant->price = $price;
$productVariant->price_old = $price_promo;
}
$productVariant->stock = $quantity;
$productVariant->save();
$this->output[] = '<font style="color:blue">Товар ' . $product_title . ' успешно сохранен</font>';
|
d8c1a2e0
Yarik
Big commit artbox
|
186
|
}
|
e8ccb1b4
Yarik
Import beta
|
187
188
189
190
191
192
193
194
195
196
197
198
199
|
$result = [
'end' => feof($handle),
'from' => ftell($handle),
'totalsize' => $filesize,
'items' => $this->output,
];
fclose($handle);
if($result[ 'end' ]) {
unlink(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices'));
|
d8c1a2e0
Yarik
Big commit artbox
|
200
|
}
|
e8ccb1b4
Yarik
Import beta
|
201
202
|
return $result;
|
d8c1a2e0
Yarik
Big commit artbox
|
203
|
}
|
36d1807a
Yarik
Big commit.
|
204
|
|
e8ccb1b4
Yarik
Import beta
|
205
206
207
208
209
210
|
/**
* @param string $name
*
* @return array
*/
private function parseName(string $name):array
|
d8c1a2e0
Yarik
Big commit artbox
|
211
|
{
|
e8ccb1b4
Yarik
Import beta
|
212
213
214
215
|
$pattern = '/^(?P<name>.*)(?:\(#(?P<remote_id>\w+)#\))?$/U';
$name = trim($name);
$matches = [];
if(preg_match($pattern, $name, $matches)) {
|
b30494bf
Yarik
Import browser beta
|
216
217
|
if(!isset( $matches[ 'remote_id' ] )) {
$matches[ 'remote_id' ] = '';
|
d8c1a2e0
Yarik
Big commit artbox
|
218
|
}
|
e8ccb1b4
Yarik
Import beta
|
219
|
return $matches;
|
d8c1a2e0
Yarik
Big commit artbox
|
220
|
}
|
e8ccb1b4
Yarik
Import beta
|
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
return [
'name' => $name,
'remote_id' => '',
];
}
/**
* @param array $catalog_names
*
* @return array
* @throws \Exception
*/
private function saveCatalog(array $catalog_names):array
{
|
36d1807a
Yarik
Big commit.
|
235
236
|
$category_id = [];
|
e8ccb1b4
Yarik
Import beta
|
237
|
foreach($catalog_names as $catalog_name) {
|
d8c1a2e0
Yarik
Big commit artbox
|
238
|
// ==== Set category ====
|
e8ccb1b4
Yarik
Import beta
|
239
|
$parsed_name = $this->parseName($catalog_name);
|
b30494bf
Yarik
Import browser beta
|
240
241
242
243
|
if(!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find()
->joinWith('lang')
->andFilterWhere([ 'remote_id' => $parsed_name[ 'remote_id' ] ])
->one() ) !== NULL
|
e8ccb1b4
Yarik
Import beta
|
244
245
246
247
248
249
250
251
252
|
) {
if(!empty( $category->lang )) {
$category->lang->name = $parsed_name[ 'name' ];
$category->lang->save();
} else {
throw new \Exception('Category with ID ' . $category->category_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
} else {
|
d8c1a2e0
Yarik
Big commit artbox
|
253
254
|
// Create category
$category = new Category();
|
e8ccb1b4
Yarik
Import beta
|
255
256
257
258
259
260
|
$category->generateLangs();
$category_langs = $category->model_langs;
foreach($category_langs as $category_lang) {
$category_lang->name = $parsed_name[ 'name' ];
}
$category->remote_id = $parsed_name[ 'remote_id' ];
|
d8c1a2e0
Yarik
Big commit artbox
|
261
262
|
$category->save();
}
|
d8c1a2e0
Yarik
Big commit artbox
|
263
264
|
$category_id[] = $category->category_id;
}
|
e8ccb1b4
Yarik
Import beta
|
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
return $category_id;
}
/**
* @param string|NULL $brand_name
*
* @return int|null
* @throws \Exception
*/
private function saveBrand(string $brand_name = NULL):int
{
$parsed_name = $this->parseName($brand_name);
if(!empty( $brand_name )) {
|
36d1807a
Yarik
Big commit.
|
279
280
281
|
/**
* @var Brand $brand
*/
|
b30494bf
Yarik
Import browser beta
|
282
283
284
285
|
if(!empty( $parsed_name[ 'remote_id' ] ) && ( $brand = Brand::find()
->joinWith('lang')
->andFilterWhere([ 'remote_id' => $parsed_name[ 'remote_id' ] ])
->one() ) !== NULL
|
e8ccb1b4
Yarik
Import beta
|
286
287
288
289
290
291
292
293
|
) {
if(!empty( $brand->lang )) {
$brand->lang->name = $parsed_name[ 'name' ];
$brand->lang->save();
} else {
throw new \Exception('Brand with ID ' . $brand->brand_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
return $brand->brand_id;
|
d8c1a2e0
Yarik
Big commit artbox
|
294
295
296
|
} else {
// Create brand
$brand = new Brand();
|
e8ccb1b4
Yarik
Import beta
|
297
298
299
300
301
302
|
$brand->generateLangs();
$brand_langs = $brand->model_langs;
foreach($brand_langs as $brand_lang) {
$brand_lang->name = $parsed_name[ 'name' ];
}
$brand->remote_id = $parsed_name[ 'remote_id' ];
|
d8c1a2e0
Yarik
Big commit artbox
|
303
|
$brand->save();
|
e8ccb1b4
Yarik
Import beta
|
304
|
return $brand->brand_id;
|
d8c1a2e0
Yarik
Big commit artbox
|
305
306
|
}
}
|
e8ccb1b4
Yarik
Import beta
|
307
308
309
310
311
312
313
314
315
316
317
|
return NULL;
}
/**
* @param array $fotos
* @param int $product_id
* @param int $product_variant_id
*/
private function saveFotos(array $fotos, int $product_id, int $product_variant_id = NULL)
{
if(!empty( $fotos )) {
|
d8c1a2e0
Yarik
Big commit artbox
|
318
|
foreach($fotos as $foto) {
|
e8ccb1b4
Yarik
Import beta
|
319
320
321
322
323
324
325
326
|
$source_image = Yii::getAlias('@uploadDir') . '/product_images/' . urlencode($foto);
if(file_exists($source_image)) {
if(( $productImage = ProductImage::find()
->andWhere([ 'image' => $foto ])
->andWhere([ 'product_id' => $product_id ])
->andFilterWhere([ 'product_variant_id' => $product_variant_id ])
->one() ) === NULL
) {
|
d8c1a2e0
Yarik
Big commit artbox
|
327
328
|
copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto);
$productImage = new ProductImage();
|
e8ccb1b4
Yarik
Import beta
|
329
330
|
$productImage->product_id = $product_id;
$productImage->product_variant_id = $product_variant_id;
|
d8c1a2e0
Yarik
Big commit artbox
|
331
332
333
334
335
336
|
$productImage->image = $foto;
$productImage->save();
}
}
}
}
|
e8ccb1b4
Yarik
Import beta
|
337
338
339
340
341
342
343
344
345
346
347
348
349
|
}
/**
* @param array $data
* @param float $product_cost_old
* @param float $product_cost
* @param int $product_id
* @param array $category_id
*
* @return array
*/
private function saveVariants(array $data, float $product_cost_old, int $product_id, array $category_id, float $product_cost = NULL):array
{
|
d8c1a2e0
Yarik
Big commit artbox
|
350
|
$MOD_ARRAY = [];
|
e8ccb1b4
Yarik
Import beta
|
351
352
353
354
355
356
357
358
359
360
361
362
|
for($i = 13; $i < count($data); $i++) {
if(!empty ( $data[ $i ] )) {
$mod_arr = explode('=', $data[ $i ]);
$mod_art = $mod_arr[ 0 ];
$variant_filters = explode('*', $mod_arr[ 1 ]);
$mod_name = $mod_arr[ 2 ];
if(empty( $mod_name )) {
$mod_name = $mod_art;
}
$mod_image = $mod_arr[ 3 ];
$mod_stock = isset( $mod_arr[ 4 ] ) ? $mod_arr[ 4 ] : 1;
$mod_cost = isset( $product_cost ) ? floatval($product_cost) : 0;
|
d8c1a2e0
Yarik
Big commit artbox
|
363
|
$mod_old_cost = floatval($product_cost_old);
|
d8c1a2e0
Yarik
Big commit artbox
|
364
|
// Check product variant
|
e8ccb1b4
Yarik
Import beta
|
365
366
367
368
369
370
371
372
373
|
/**
* @var ProductVariant $_productVariant
*/
if(( $_productVariant = ProductVariant::find()
->joinWith('lang')
->andFilterWhere([ 'sku' => $mod_art ])
->andFilterWhere([ 'product_variant.product_id' => $product_id ])
->one() ) === NULL
) {
|
d8c1a2e0
Yarik
Big commit artbox
|
374
|
$_productVariant = new ProductVariant();
|
e8ccb1b4
Yarik
Import beta
|
375
376
377
378
379
380
381
382
383
384
385
386
387
|
$_productVariant->product_id = $product_id;
$_productVariant->generateLangs();
$product_variant_langs = $_productVariant->model_langs;
foreach($product_variant_langs as $product_variant_lang) {
$product_variant_lang->name = $mod_name;
}
} else {
if(!empty( $_productVariant->lang )) {
$_productVariant->lang->name = $mod_name;
$_productVariant->lang->save();
} else {
throw new \Exception('Product variant with ID ' . $_productVariant->product_variant_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
|
d8c1a2e0
Yarik
Big commit artbox
|
388
389
|
}
$_productVariant->product_unit_id = 1;
|
d8c1a2e0
Yarik
Big commit artbox
|
390
391
392
393
|
$_productVariant->sku = $mod_art;
$_productVariant->price = $mod_cost;
$_productVariant->price_old = $mod_old_cost;
$_productVariant->stock = $mod_stock;
|
e8ccb1b4
Yarik
Import beta
|
394
395
396
|
if(!empty ( $variant_filters )) {
$variants_options = $this->saveFilters($variant_filters, 1, $category_id);
|
d8c1a2e0
Yarik
Big commit artbox
|
397
|
}
|
e8ccb1b4
Yarik
Import beta
|
398
399
|
if(isset( $variants_options ) && !empty( $variants_options )) {
|
d8c1a2e0
Yarik
Big commit artbox
|
400
401
|
$_productVariant->options = $variants_options;
}
|
b30494bf
Yarik
Import browser beta
|
402
|
|
e8ccb1b4
Yarik
Import beta
|
403
404
405
|
/**
* @todo set to false
*/
|
d8c1a2e0
Yarik
Big commit artbox
|
406
|
$_productVariant->save(false);
|
e8ccb1b4
Yarik
Import beta
|
407
|
|
d8c1a2e0
Yarik
Big commit artbox
|
408
|
$MOD_ARRAY[] = $_productVariant->product_variant_id;
|
e8ccb1b4
Yarik
Import beta
|
409
410
411
412
413
414
415
|
$this->saveFotos([ $mod_image ], $product_id, $_productVariant->product_variant_id);
}
}
return $MOD_ARRAY;
}
|
b30494bf
Yarik
Import browser beta
|
416
417
418
|
// private function debug($start_time, $message) {
// echo $message.': '.(time()-$start_time).'s passed';
// }
|
e8ccb1b4
Yarik
Import beta
|
419
420
421
|
public function goProducts($from = 0, $limit = NULL)
{
|
e8ccb1b4
Yarik
Import beta
|
422
423
424
425
426
427
|
set_time_limit(0);
if(!( $handle = $this->getProductsFile('uploadFileProducts') )) {
$this->errors[] = 'File not found';
return false;
}
|
b30494bf
Yarik
Import browser beta
|
428
|
|
e8ccb1b4
Yarik
Import beta
|
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
$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)) );
$result_items = [];
while(( empty( $limit ) || $j++ < $limit ) && ( $data = fgetcsv($handle, 10000, ";") ) !== false) {
try {
foreach($data as &$value) {
if(!$is_utf) {
$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_names = explode(',', $data[ 0 ]);
if(empty ( $catalog_names )) {
$result_items[] = "Не указана категория (строка $j)";
continue;
}
// 2 Бренд
$brand_name = $data[ 1 ];
// if(empty ( $brand_name )) {
// $result_items[] = "Не указан бренд (строка $j)";
// continue;
// }
// 3 Название товара
$product_name = $data[ 2 ];
if(empty ( $product_name )) {
$result_items[] = "Не указано наименование товара (строка $j)";
continue;
}
// 5 Описание товара
$product_body = $data[ 3 ];
// 6 Фильтр
$filters = explode('*', $data[ 4 ]);
// 11 Цена акция
$product_cost_old = floatval($data[ 6 ]);
$product_cost = NULL;
// 10 Цена
if($product_cost_old) {
$product_cost_old = floatval($data[ 5 ]);
$product_cost = floatval($data[ 6 ]);
}
// 12 Акция
$product_akciya = (bool) $data[ 7 ];
// 13 Сопуд. Тов.
$similar = explode(',', $data[ 8 ]);
// 14 Новинки
$product_new = (bool) $data[ 9 ];
// 15 Топ продаж
$product_top = (bool) $data[ 10 ];
// 17 ВИДЕО КОД
$product_video = $data[ 11 ];
// 18 Галлерея фото
$fotos = [];
if(trim($data[ 12 ])) {
$fotos = explode(',', trim($data[ 12 ]));
}
|
b30494bf
Yarik
Import browser beta
|
514
515
516
517
518
519
520
521
522
|
// $lang = \Yii::$app->session->get('export_lang', Language::getDefaultLanguage()->language_id);
// /**
// * @var Language $language
// */
// $language = Language::find()
// ->where([ 'language_id' => $lang ])
// ->one();
// Language::setCurrent($language->url);
|
e8ccb1b4
Yarik
Import beta
|
523
|
$categories = $this->saveCatalog($catalog_names);
|
b30494bf
Yarik
Import browser beta
|
524
|
|
e8ccb1b4
Yarik
Import beta
|
525
|
$brand_id = $this->saveBrand($brand_name);
|
e8ccb1b4
Yarik
Import beta
|
526
527
528
|
$options = [];
if(!empty ( $filters )) {
|
b30494bf
Yarik
Import browser beta
|
529
|
|
e8ccb1b4
Yarik
Import beta
|
530
|
$options = $this->saveFilters($filters, 0, $categories);
|
e8ccb1b4
Yarik
Import beta
|
531
532
533
534
535
|
}
$parsed_name = $this->parseName($product_name);
/**
* @var Product $_product
*/
|
b30494bf
Yarik
Import browser beta
|
536
537
538
539
540
|
if(!empty( $parsed_name[ 'remote_id' ] ) && ( $_product = Product::find()
->joinWith('lang')
->andFilterWhere([ 'remote_id' => $parsed_name[ 'remote_id' ] ])
->one() ) !== NULL
|
e8ccb1b4
Yarik
Import beta
|
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
) {
if(!empty( $_product->lang )) {
$_product->lang->name = $parsed_name[ 'name' ];
$_product->lang->description = $product_body;
$_product->lang->save();
} else {
throw new \Exception('Product with ID ' . $_product->product_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
} else {
$_product = new Product();
$_product->generateLangs();
$product_langs = $_product->model_langs;
foreach($product_langs as $product_lang) {
$product_lang->name = $parsed_name[ 'name' ];
$product_lang->description = $product_body;
}
}
|
b30494bf
Yarik
Import browser beta
|
558
|
|
e8ccb1b4
Yarik
Import beta
|
559
560
561
562
563
564
565
566
567
568
|
$is_new_product = empty( $_product->product_id );
$_product->categories = $categories;
$_product->brand_id = $brand_id;
$_product->video = $product_video;
$_product->is_top = $product_top;
$_product->akciya = $product_akciya;
$_product->is_new = $product_new;
|
9f8be5c9
Yarik
Import browser be...
|
569
570
571
572
573
574
575
576
577
578
579
|
if(!empty( $options )) {
$_product->options = $options;
}
if(!empty( $_product->lang )) {
$product_name_inserted = $_product->lang->name;
} else {
$product_name_inserted = $_product->model_langs[ Language::$current->language_id ]->name;
}
if(!$_product->save() || !$_product->transactionStatus) {
|
e8ccb1b4
Yarik
Import beta
|
580
581
|
$result_items[] = 'Product #' . $product_name_inserted . ' not saved' . " (line $j)";
continue;
|
d8c1a2e0
Yarik
Big commit artbox
|
582
|
}
|
b30494bf
Yarik
Import browser beta
|
583
|
|
e8ccb1b4
Yarik
Import beta
|
584
|
$this->saveFotos($fotos, $_product->product_id);
|
e8ccb1b4
Yarik
Import beta
|
585
|
// нужно для проставления характеристик относящихся к модификациям
|
b30494bf
Yarik
Import browser beta
|
586
|
|
e8ccb1b4
Yarik
Import beta
|
587
|
$this->saveVariants($data, $product_cost_old, $_product->product_id, $_product->categories, $product_cost);
|
e8ccb1b4
Yarik
Import beta
|
588
|
|
d387e999
Yarik
Import browser be...
|
589
|
$_product->save();
|
e8ccb1b4
Yarik
Import beta
|
590
|
|
9f8be5c9
Yarik
Import browser be...
|
591
|
$result_items[] = "Product {$product_name_inserted} #{$_product->product_id} saved (" . ( $is_new_product ? 'new product' : 'exists product' ) . ")" . " (line $j)";
|
e8ccb1b4
Yarik
Import beta
|
592
593
|
} catch(\Exception $e) {
|
b30494bf
Yarik
Import browser beta
|
594
|
$result_items[] = $e->getMessage() . '(line ' . $j . ')';
|
d8c1a2e0
Yarik
Big commit artbox
|
595
|
}
|
e8ccb1b4
Yarik
Import beta
|
596
|
|
d8c1a2e0
Yarik
Big commit artbox
|
597
|
}
|
e8ccb1b4
Yarik
Import beta
|
598
599
600
601
602
603
604
|
$result = [
'end' => feof($handle),
'from' => ftell($handle),
'totalsize' => $filesize,
'items' => $result_items,
];
|
b30494bf
Yarik
Import browser beta
|
605
|
|
e8ccb1b4
Yarik
Import beta
|
606
|
fclose($handle);
|
b30494bf
Yarik
Import browser beta
|
607
|
|
e8ccb1b4
Yarik
Import beta
|
608
|
if($result[ 'end' ]) {
|
b30494bf
Yarik
Import browser beta
|
609
|
// unlink(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFileProducts'));
|
d8c1a2e0
Yarik
Big commit artbox
|
610
|
}
|
e8ccb1b4
Yarik
Import beta
|
611
612
|
return $result;
|
d8c1a2e0
Yarik
Big commit artbox
|
613
|
}
|
e8ccb1b4
Yarik
Import beta
|
614
615
616
617
618
619
620
|
private function getProductsFile($file_type)
{
$filename = Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@' . $file_type);
if(!is_file($filename)) {
$this->errors[] = "File $filename not found";
return false;
|
d8c1a2e0
Yarik
Big commit artbox
|
621
|
}
|
e8ccb1b4
Yarik
Import beta
|
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
return fopen($filename, 'r');
}
/**
* @param $filters array of filters like [['pol'='мужской'],['god' =
* '2013'],['volume'='25 л']*['size'='49 x 30 x
* 20см'],['composition'='600D полиэстер']]
* @param int $level 0 for products and 1 for product variant
* @param $catalog_names array catalogs id
*
* @return array
* @throws \Exception
*/
private function saveFilters(array $filters, int $level, array $catalog_names):array
{
$options = [];
foreach($filters as $filter) {
preg_match_all('/\[(.*):(.*)\]/', $filter, $filter);
if(empty( $filter[ 1 ][ 0 ] )) {
continue;
}
$filter_name = trim($filter[ 1 ][ 0 ]);
$parsed_group_name = $this->parseName($filter_name);
|
b30494bf
Yarik
Import browser beta
|
647
|
|
e8ccb1b4
Yarik
Import beta
|
648
649
650
|
/**
* @var TaxGroup $taxGroup
*/
|
b30494bf
Yarik
Import browser beta
|
651
652
653
654
655
|
if(!empty( $parsed_group_name[ 'remote_id' ] ) && ( $taxGroup = TaxGroup::find()
->joinWith('lang')
->andFilterWhere([ 'remote_id' => $parsed_group_name[ 'remote_id' ] ])
->one() ) !== NULL
) {
|
e8ccb1b4
Yarik
Import beta
|
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
if(!empty( $taxGroup->lang )) {
$taxGroup->lang->name = $parsed_group_name[ 'name' ];
$taxGroup->lang->save();
} else {
throw new \Exception('Tax group with ID ' . $taxGroup->tax_group_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
} else {
$taxGroup = new TaxGroup();
$taxGroup->generateLangs();
$tax_group_langs = $taxGroup->model_langs;
foreach($tax_group_langs as $tax_group_lang) {
$tax_group_lang->name = $parsed_group_name[ 'name' ];
}
$taxGroup->level = $level;
$taxGroup->categories = $catalog_names;
$taxGroup->is_filter = false;
$taxGroup->save();
}
|
e8ccb1b4
Yarik
Import beta
|
674
675
676
677
678
679
|
$filters_options = explode(',', $filter[ 2 ][ 0 ]);
foreach($filters_options as $filter_options) {
$parsed_option_name = $this->parseName($filter_options);
/**
* @var TaxOption $option
*/
|
b30494bf
Yarik
Import browser beta
|
680
681
682
683
684
685
686
|
if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find()
->joinWith('lang')
->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ])
->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ])
->one() ) !== NULL
) {
|
e8ccb1b4
Yarik
Import beta
|
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
if(!empty( $option->lang )) {
$option->lang->value = $parsed_option_name[ 'name' ];
$option->lang->save();
} else {
throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
}
} else {
// Create option
$option = new TaxOption();
$option->generateLangs();
$option_langs = $option->model_langs;
foreach($option_langs as $option_lang) {
$option_lang->value = $parsed_option_name[ 'name' ];
}
$option->tax_group_id = $taxGroup->tax_group_id;
$option->save();
}
$options[] = $option->tax_option_id;
|
d8c1a2e0
Yarik
Big commit artbox
|
705
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
706
|
}
|
e8ccb1b4
Yarik
Import beta
|
707
|
return $options;
|
d8c1a2e0
Yarik
Big commit artbox
|
708
|
}
|
e8ccb1b4
Yarik
Import beta
|
709
|
}
|