Blame view

common/modules/product/models/Export.php 5.04 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
                                      '',
                                      ( ( !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);
959af82a   Alexey Boroda   -Trimed export
77
78
79
80
81
                                  
                  foreach ($to_write as $key => $value) {
                      $to_write[$key] = trim(preg_replace('/[^\S ]/', ' ', $value));
                  }
                  
8072159c   Alex Savenko   create proj
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                  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
110
111
112
113
114
115
      
          /**
           * @param TaxOption[] $filters
           *
           * @return string
           */
8072159c   Alex Savenko   create proj
116
117
118
119
120
121
          public function convertFilterToString($filters)
          {
              if(!empty($filters)){
                  $fittersArray = [];
                  foreach($filters as $filter) {
                      if($filter->taxGroup instanceof  TaxGroup){
59751210   Yarik   Import returned back
122
                          $fittersArray[ $filter->taxGroup->alias ][] = $filter->value;
8072159c   Alex Savenko   create proj
123
124
125
126
127
128
129
130
131
132
133
134
135
136
                      }
  
                  }
                  $filterString = [];
  
                  foreach($fittersArray as $filterName => $filterRows) {
                      $row = implode(',', $filterRows);
                      $filterString[] = "[{$filterName}:{$row}]";
                  }
                  return implode('*', $filterString);
              }
  
          }
      }