Blame view

common/components/parsers/CustomConverter.php 5.17 KB
28253169   Mihail   add converter as ...
1
  <?php
bedb55fe   Mihail   fixed Image class
2
  namespace common\components\parsers;
ffd4b834   Mihail   add articul filte...
3
  
34d480b7   Mihail   temp commit - wor...
4
  use common\components\CustomVarDamp;
28253169   Mihail   add converter as ...
5
  use yii\multiparser\Converter;
34d480b7   Mihail   temp commit - wor...
6
  use backend\models\Details;
706a1491   Mihail   add form, model a...
7
  use backend\models\DetailsCrosses;
ffd4b834   Mihail   add articul filte...
8
  use backend\models\ImportersPrefix;
28253169   Mihail   add converter as ...
9
  
ffd4b834   Mihail   add articul filte...
10
11
  class CustomConverter extends Converter
  {
28253169   Mihail   add converter as ...
12
  
d3cf6647   Mihail   add multiply pric...
13
14
      public static $sign;
      public static $multiplier;
ffd4b834   Mihail   add articul filte...
15
      public static $importer_id;
3da8b25f   Mihail   fixed issues with...
16
      public static $brand;
512cc271   Mihail   add delete price ...
17
      public static $begin_of_the_day = true;
49bb0055   Mihail   add round for koe...
18
19
      // точность для округления десятичных чисел
      public static $precision = 2;
d3cf6647   Mihail   add multiply pric...
20
  
ffd4b834   Mihail   add articul filte...
21
      public static function convertToDetails(array $row)
34d480b7   Mihail   temp commit - wor...
22
23
24
25
26
27
28
29
      {
          // присвоим полный артикул
          $row['FULL_ARTICLE'] = $row['ARTICLE'];
  
          $details_model = new Details();
          // проверим все ли обязательные колонки были указаны пользователем
          $details_model->load(['Details' => $row]);
  
ffd4b834   Mihail   add articul filte...
30
          if (!$details_model->validate()) {
34d480b7   Mihail   temp commit - wor...
31
              $errors = '';
ffd4b834   Mihail   add articul filte...
32
33
              foreach ($details_model->errors as $key => $arr_errors) {
                  $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
34d480b7   Mihail   temp commit - wor...
34
              }
ffd4b834   Mihail   add articul filte...
35
              throw new \ErrorException($errors);
34d480b7   Mihail   temp commit - wor...
36
37
38
39
          }
          return $row;
      }
  
706a1491   Mihail   add form, model a...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
      public static function convertToCrosses( array $row )
      {
  
          $details_model = new DetailsCrosses();
          // проверим все ли обязательные колонки были указаны пользователем
          $details_model->load(['DetailsCrosses' => $row]);
  
          if (!$details_model->validate()) {
              $errors = '';
              foreach ($details_model->errors as $key => $arr_errors) {
                  $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
              }
              throw new \ErrorException($errors);
          }
          return $row;
      }
ffd4b834   Mihail   add articul filte...
56
      public function ConvertToMultiply(array $row)
d3cf6647   Mihail   add multiply pric...
57
      {
ffd4b834   Mihail   add articul filte...
58
          $PRICE = $row['PRICE'];
d3cf6647   Mihail   add multiply pric...
59
60
61
62
63
64
65
66
          $sign = self::$sign;
          $multiplier = self::$multiplier;
          //CustomVarDamp::dumpAndDie(self);
          if (isset($sign)) {
              if ($sign == '+') {
                  if ($multiplier > 0) {
                      $PRICE += $multiplier;
                  }
ffd4b834   Mihail   add articul filte...
67
              } else if ($sign == '-') {
d3cf6647   Mihail   add multiply pric...
68
69
70
                  if ($multiplier > 0) {
                      $PRICE -= $multiplier;
                  }
ffd4b834   Mihail   add articul filte...
71
              } else if ($sign == '*') {
d3cf6647   Mihail   add multiply pric...
72
73
74
                  if ($multiplier > 0) {
                      $PRICE *= $multiplier;
                  }
ffd4b834   Mihail   add articul filte...
75
              } else if ($sign == '/') {
d3cf6647   Mihail   add multiply pric...
76
77
78
79
80
81
                  if ($multiplier > 0) {
                      $PRICE /= $multiplier;
                  }
              }
          }
  
ffd4b834   Mihail   add articul filte...
82
83
84
85
86
87
          $row['PRICE'] = $PRICE;
  
          return $row;
  
      }
  
6215a30d   Mihail   add converter int...
88
      public static function convertToArticle( $value )
ffd4b834   Mihail   add articul filte...
89
      {
706a1491   Mihail   add form, model a...
90
91
  
          if (is_array($value)) {
ffd4b834   Mihail   add articul filte...
92
  
706a1491   Mihail   add form, model a...
93
              $row = $value;
ffd4b834   Mihail   add articul filte...
94
95
96
97
98
99
              // 1. Уберем префикс который разделен пробелом (если он есть)
              $words = explode(" ", $row['ARTICLE']);
              if (count($words) > 1) {
                  array_shift($words);
                  $row['ARTICLE'] = implode(" ", $words);
              }
706a1491   Mihail   add form, model a...
100
101
102
  
              if( isset( $row['BRAND'] ) && isset( self::$importer_id ) ){
                  // 2. Уберем брендовый префикс (если он есть)
3da8b25f   Mihail   fixed issues with...
103
                  self::$brand = $row['BRAND'];
706a1491   Mihail   add form, model a...
104
105
                  $prefix = '';
                  // запрос закешируем
3da8b25f   Mihail   fixed issues with...
106
                  $prefix = ImportersPrefix::getDb()->cache( function ($db) {
706a1491   Mihail   add form, model a...
107
                      return ImportersPrefix::find()->where([ 'importer_id' => self::$importer_id,
3da8b25f   Mihail   fixed issues with...
108
                          'brand' => self::$brand ])->one();
ffd4b834   Mihail   add articul filte...
109
                  });
d3cf6647   Mihail   add multiply pric...
110
  
706a1491   Mihail   add form, model a...
111
112
113
                  if ($prefix) {
                      $row['BRAND'] = str_replace($prefix, "", $row['BRAND']);
                  }
ffd4b834   Mihail   add articul filte...
114
              }
706a1491   Mihail   add form, model a...
115
116
117
118
119
120
121
122
123
124
125
  
              return $row;
  
          } else {
              $words = explode( " ", $value );
              if ( count( $words ) > 1) {
                  array_shift( $words );
                  $value = implode( " ", $words );
              }
  
              return $value;
ffd4b834   Mihail   add articul filte...
126
          }
ffd4b834   Mihail   add articul filte...
127
128
129
130
      }
  
      public static function convertToBrand($value)
      {
6215a30d   Mihail   add converter int...
131
          $res = self::convertToEncode($value);;
ffd4b834   Mihail   add articul filte...
132
133
134
135
136
137
138
          $res = trim(strtoupper($res));
          $res = str_replace("Ä", "A", str_replace("Ö", "O", str_replace("Ü", "U", str_replace("Ë", "E", str_replace("Ò", "O", $res)))));
          $res = str_replace(array('@', '#', '~', '"', "'", "?", "!"), '', $res);
  
          return $res;
      }
  
512cc271   Mihail   add delete price ...
139
140
141
142
143
144
145
146
147
148
149
      public static function convertToTimestamp($value)
      {
          if ( self::$begin_of_the_day ){
              $res = mktime(0,0,0,(int)substr($value,3,2),(int)substr($value,0,2),(int)substr($value,6,4));
          } else {
              $res = mktime(23,59,59,(int)substr($value,3,2),(int)substr($value,0,2),(int)substr($value,6,4));
          }
  
          return $res;
      }
  
49bb0055   Mihail   add round for koe...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      public static function convertToFloat( $value )
      {
          if ($value == '') {
              $value = 0;
          }
          $value = trim(str_replace(",", ".", $value));
          $value = preg_replace("/[^0-9.]+/", "", strtoupper($value));
  
          if ($value == '') {
              return '';
          }
          $value = round( (float)$value, self::$precision );
  
          return $value;
      }
34d480b7   Mihail   temp commit - wor...
165
  
28253169   Mihail   add converter as ...
166
  }