Blame view

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