Blame view

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