Blame view

common/modules/product/models/Export.php 3.19 KB
c7852657   Karnovsky A   -
1
2
3
4
5
6
7
8
9
10
11
12
13
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  <?php
  
  namespace common\modules\product\models;
  
  use common\modules\product\helpers\ProductHelper;
  use yii\base\Model;
  
  class Export extends Model {
      public $errors = [];
      public $output = [];
  
      public function process($dirName, $filename = null, $use_not_enables = false) {
          if (is_null($filename)) {
              $filename = 'products_'. date('d_m_Y_H_i') .'.csv';
          }
          setlocale(LC_ALL, 'ru_RU.CP1251');
          $handle = fopen($dirName .'/'. $filename, "w");
          $products = Product::find()->joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all();
          $i = 0;
          foreach ($products as $product_id)
          {
              $product = Product::findOne($product_id);
              $i++;
              /*if ($i>1e2) {
                  break;
              }*/
              $mods = [];
  
              foreach ($product->enabledVariants as $variant)
              {
                  $size = $color = '';
                  if ($product->product_variant_type_id) {
                      $productVariantType = ProductVariantType::findOne($product->product_variant_type_id);
                      if ($productVariantType) {
                          if ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_COLOR) {
                              $color = $product->name;
                          } elseif ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_SIZE) {
                              $size = $product->name;
                          }
                      }
                  }
                  $mods[] = $variant->sku . '=' . $size . '=' . $color . '=' . $variant->imageUrl;
              }
  
              $fotos = [];
  
              foreach ($product->images as $image)
              {
                  $fotos[] = $image->imageUrl;
              }
  
              $filters = $product->properties;
  
              $list = [
                  $product->category->name,
                  $product->brand->name,
                  $product->name,
                  '',
                  ((! empty($product->description)) ? $product->description : ''),
                  ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET]) : ''),
                  '',
                  ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX]) : ''),
                  ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR]) : ''),
                  $product->price_old,
                  $product->price,
                  intval($product->akciya),
                  '',
                  intval($product->new),
                  intval($product->top),
                  '',
                  $product->video,
                  implode (',', $fotos),
              ];
  
              $to_write = array_merge ($list, $mods);
              foreach($to_write as &$cell) {
                  $cell = iconv("UTF-8", "WINDOWS-1251", $cell);
              }
  
              fputcsv($handle, $to_write, ';');
          }
  
          fclose ($handle);
  
          return $dirName .'/'. $filename;
      }
  }