Blame view

common/components/parsers/CustomConverter.php 4.67 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;
78684ed4   Mihail   add multiply pric...
18
  
a0d1ac87   Mihail   add articul filte...
19
      public static function convertToDetails(array $row)
ef41533d   Mihail   temp commit - wor...
20
21
22
23
24
25
26
27
      {
          // присвоим полный артикул
          $row['FULL_ARTICLE'] = $row['ARTICLE'];
  
          $details_model = new Details();
          // проверим все ли обязательные колонки были указаны пользователем
          $details_model->load(['Details' => $row]);
  
a0d1ac87   Mihail   add articul filte...
28
          if (!$details_model->validate()) {
ef41533d   Mihail   temp commit - wor...
29
              $errors = '';
a0d1ac87   Mihail   add articul filte...
30
31
              foreach ($details_model->errors as $key => $arr_errors) {
                  $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
ef41533d   Mihail   temp commit - wor...
32
              }
a0d1ac87   Mihail   add articul filte...
33
              throw new \ErrorException($errors);
ef41533d   Mihail   temp commit - wor...
34
35
36
37
          }
          return $row;
      }
  
7776ca75   Mihail   add form, model a...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      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;
      }
a0d1ac87   Mihail   add articul filte...
54
      public function ConvertToMultiply(array $row)
78684ed4   Mihail   add multiply pric...
55
      {
a0d1ac87   Mihail   add articul filte...
56
          $PRICE = $row['PRICE'];
78684ed4   Mihail   add multiply pric...
57
58
59
60
61
62
63
64
          $sign = self::$sign;
          $multiplier = self::$multiplier;
          //CustomVarDamp::dumpAndDie(self);
          if (isset($sign)) {
              if ($sign == '+') {
                  if ($multiplier > 0) {
                      $PRICE += $multiplier;
                  }
a0d1ac87   Mihail   add articul filte...
65
              } else if ($sign == '-') {
78684ed4   Mihail   add multiply pric...
66
67
68
                  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
77
78
79
                  if ($multiplier > 0) {
                      $PRICE /= $multiplier;
                  }
              }
          }
  
a0d1ac87   Mihail   add articul filte...
80
81
82
83
84
85
          $row['PRICE'] = $PRICE;
  
          return $row;
  
      }
  
67adc788   Mihail   add converter int...
86
      public static function convertToArticle( $value )
a0d1ac87   Mihail   add articul filte...
87
      {
7776ca75   Mihail   add form, model a...
88
89
  
          if (is_array($value)) {
a0d1ac87   Mihail   add articul filte...
90
  
7776ca75   Mihail   add form, model a...
91
              $row = $value;
a0d1ac87   Mihail   add articul filte...
92
93
94
95
96
97
              // 1. Уберем префикс который разделен пробелом (если он есть)
              $words = explode(" ", $row['ARTICLE']);
              if (count($words) > 1) {
                  array_shift($words);
                  $row['ARTICLE'] = implode(" ", $words);
              }
7776ca75   Mihail   add form, model a...
98
99
100
  
              if( isset( $row['BRAND'] ) && isset( self::$importer_id ) ){
                  // 2. Уберем брендовый префикс (если он есть)
3be5e214   Mihail   fixed issues with...
101
                  self::$brand = $row['BRAND'];
7776ca75   Mihail   add form, model a...
102
103
                  $prefix = '';
                  // запрос закешируем
3be5e214   Mihail   fixed issues with...
104
                  $prefix = ImportersPrefix::getDb()->cache( function ($db) {
7776ca75   Mihail   add form, model a...
105
                      return ImportersPrefix::find()->where([ 'importer_id' => self::$importer_id,
3be5e214   Mihail   fixed issues with...
106
                          'brand' => self::$brand ])->one();
a0d1ac87   Mihail   add articul filte...
107
                  });
78684ed4   Mihail   add multiply pric...
108
  
7776ca75   Mihail   add form, model a...
109
110
111
                  if ($prefix) {
                      $row['BRAND'] = str_replace($prefix, "", $row['BRAND']);
                  }
a0d1ac87   Mihail   add articul filte...
112
              }
7776ca75   Mihail   add form, model a...
113
114
115
116
117
118
119
120
121
122
123
  
              return $row;
  
          } else {
              $words = explode( " ", $value );
              if ( count( $words ) > 1) {
                  array_shift( $words );
                  $value = implode( " ", $words );
              }
  
              return $value;
a0d1ac87   Mihail   add articul filte...
124
          }
a0d1ac87   Mihail   add articul filte...
125
126
127
128
      }
  
      public static function convertToBrand($value)
      {
67adc788   Mihail   add converter int...
129
          $res = self::convertToEncode($value);;
a0d1ac87   Mihail   add articul filte...
130
131
132
133
134
135
136
          $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 ...
137
138
139
140
141
142
143
144
145
146
147
      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;
      }
  
ef41533d   Mihail   temp commit - wor...
148
  
33d902b8   Mihail   add converter as ...
149
  }