Blame view

common/components/parsers/CustomConverter.php 4.27 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;
d3cf6647   Mihail   add multiply pric...
17
  
34d480b7   Mihail   temp commit - wor...
18
  
ffd4b834   Mihail   add articul filte...
19
      public static function convertToDetails(array $row)
34d480b7   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]);
  
ffd4b834   Mihail   add articul filte...
28
          if (!$details_model->validate()) {
34d480b7   Mihail   temp commit - wor...
29
              $errors = '';
ffd4b834   Mihail   add articul filte...
30
31
              foreach ($details_model->errors as $key => $arr_errors) {
                  $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
34d480b7   Mihail   temp commit - wor...
32
              }
ffd4b834   Mihail   add articul filte...
33
              throw new \ErrorException($errors);
34d480b7   Mihail   temp commit - wor...
34
35
36
37
          }
          return $row;
      }
  
706a1491   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;
      }
ffd4b834   Mihail   add articul filte...
54
      public function ConvertToMultiply(array $row)
d3cf6647   Mihail   add multiply pric...
55
      {
ffd4b834   Mihail   add articul filte...
56
          $PRICE = $row['PRICE'];
d3cf6647   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;
                  }
ffd4b834   Mihail   add articul filte...
65
              } else if ($sign == '-') {
d3cf6647   Mihail   add multiply pric...
66
67
68
                  if ($multiplier > 0) {
                      $PRICE -= $multiplier;
                  }
ffd4b834   Mihail   add articul filte...
69
              } else if ($sign == '*') {
d3cf6647   Mihail   add multiply pric...
70
71
72
                  if ($multiplier > 0) {
                      $PRICE *= $multiplier;
                  }
ffd4b834   Mihail   add articul filte...
73
              } else if ($sign == '/') {
d3cf6647   Mihail   add multiply pric...
74
75
76
77
78
79
                  if ($multiplier > 0) {
                      $PRICE /= $multiplier;
                  }
              }
          }
  
ffd4b834   Mihail   add articul filte...
80
81
82
83
84
85
          $row['PRICE'] = $PRICE;
  
          return $row;
  
      }
  
6215a30d   Mihail   add converter int...
86
      public static function convertToArticle( $value )
ffd4b834   Mihail   add articul filte...
87
      {
706a1491   Mihail   add form, model a...
88
89
  
          if (is_array($value)) {
ffd4b834   Mihail   add articul filte...
90
  
706a1491   Mihail   add form, model a...
91
              $row = $value;
ffd4b834   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);
              }
706a1491   Mihail   add form, model a...
98
99
100
  
              if( isset( $row['BRAND'] ) && isset( self::$importer_id ) ){
                  // 2. Уберем брендовый префикс (если он есть)
3da8b25f   Mihail   fixed issues with...
101
                  self::$brand = $row['BRAND'];
706a1491   Mihail   add form, model a...
102
103
                  $prefix = '';
                  // запрос закешируем
3da8b25f   Mihail   fixed issues with...
104
                  $prefix = ImportersPrefix::getDb()->cache( function ($db) {
706a1491   Mihail   add form, model a...
105
                      return ImportersPrefix::find()->where([ 'importer_id' => self::$importer_id,
3da8b25f   Mihail   fixed issues with...
106
                          'brand' => self::$brand ])->one();
ffd4b834   Mihail   add articul filte...
107
                  });
d3cf6647   Mihail   add multiply pric...
108
  
706a1491   Mihail   add form, model a...
109
110
111
                  if ($prefix) {
                      $row['BRAND'] = str_replace($prefix, "", $row['BRAND']);
                  }
ffd4b834   Mihail   add articul filte...
112
              }
706a1491   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;
ffd4b834   Mihail   add articul filte...
124
          }
ffd4b834   Mihail   add articul filte...
125
126
127
128
      }
  
      public static function convertToBrand($value)
      {
6215a30d   Mihail   add converter int...
129
          $res = self::convertToEncode($value);;
ffd4b834   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;
      }
  
34d480b7   Mihail   temp commit - wor...
137
  
28253169   Mihail   add converter as ...
138
  }