Blame view

common/modules/product/models/Export.php 4.87 KB
8072159c   Alex Savenko   create proj
1
2
3
4
5
  <?php
      
      namespace common\modules\product\models;
      
      use common\modules\rubrication\models\TaxGroup;
1a17e5eb   Yarik   Import test
6
      use common\modules\rubrication\models\TaxOption;
8072159c   Alex Savenko   create proj
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
      use yii\base\Model;
      
      class Export extends Model
      {
          
          public $lang;
          
          public $file;
          
          public $errors = [];
          
          public $output = [];
          
          public function process($filename = NULL, $from = 0)
          {
              if(empty( $filename )) {
                  $filename = 'products_' . date('d_m_Y_H_i') . '.csv';
                  $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "w");
              } else {
                  $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "a");
              }
              
              
              $products = Product::find()
                                 ->with([
                                     'variantsWithFilters',
                                     'brand',
                                     'categories',
                                     'filters'])
                                 ->limit(100)
                                 ->offset($from)
                                 ->all();
              $filesize = Product::find()
                                 ->count();
              foreach($products as $product) {
                  
                                  $mods = [];
                                  $filterString = $this->convertFilterToString($product->filters);
                  
                                  foreach($product->variantsWithFilters as $variant) {
                                      $color = $variant->name;
                                      $mods[] = $variant->sku . '=' . $this->convertFilterToString($variant->filters) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock;
                                  }
                  
                                  $fotos = [];
                  
                                  $categories = [];
                                  foreach($product->categories as $value) {
                                      $categories[] = $value->name;
                                  }
                  
                                  $categories = implode(',', $categories);
                  
                                  $list = [
                                      $categories,
                                      !empty($product->brand) ? $product->brand ->name :'',
096128d9   Yarik   Import test
63
                                      $product->name . '(#' . $product->product_id .'#)',
8072159c   Alex Savenko   create proj
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                                      '',
                                      ( ( !empty( $product->description ) ) ? $product->description : '' ),
                                      $filterString,
                                      ( !empty( $product->variant ) ) ? $product->variant->price_old : '',
                                      ( !empty( $product->variant ) ) ? $product->variant->price : '',
                                      intval($product->akciya),
                                      '',
                                      intval($product->is_new),
                                      intval($product->is_top),
                                      $product->video,
                                      implode(',', $fotos),
                                  ];
                                  $to_write = array_merge($list, $mods);
                  fputcsv($handle, $to_write, ';');
                  unset( $product );
              }
      
              fclose($handle);
      
              $from += 100;
              $end = false;
              if($from > $filesize) {
                  $end = true;
              }
              
              $result = [
                  'end'       => $end,
                  'from'      => $from,
                  'totalsize' => $filesize,
                  'filename'  => $filename,
              ];
              
              if($end) {
                  $result = array_merge($result, [
                      'link' => '/storage/sync/'.$filename,
                  ]);
              }
              
              return $result;
              
          }
1a17e5eb   Yarik   Import test
105
106
107
108
109
110
      
          /**
           * @param TaxOption[] $filters
           *
           * @return string
           */
8072159c   Alex Savenko   create proj
111
112
113
114
115
116
          public function convertFilterToString($filters)
          {
              if(!empty($filters)){
                  $fittersArray = [];
                  foreach($filters as $filter) {
                      if($filter->taxGroup instanceof  TaxGroup){
1a17e5eb   Yarik   Import test
117
                          $fittersArray[ $filter->taxGroup->alias ][] = $filter->value . '(#' . $filter->alias . '#)';
8072159c   Alex Savenko   create proj
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                      }
  
                  }
                  $filterString = [];
  
                  foreach($fittersArray as $filterName => $filterRows) {
                      $row = implode(',', $filterRows);
                      $filterString[] = "[{$filterName}:{$row}]";
                  }
                  return implode('*', $filterString);
              }
  
          }
      }