Blame view

console/controllers/ImportController.php 6.8 KB
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  <?php
  
  namespace console\controllers;
  
  use common\modules\product\models\Brand;
  use common\modules\product\models\BrandName;
  use common\modules\product\models\Product;
  use common\modules\product\models\ProductVariant;
  use common\modules\product\models\RemoteProducts;
  use yii\console\Controller;
  use yii\helpers\Console;
  
  class ImportController extends Controller {
  
      public function actionIndex($section) {
          $method = 'go'. ucfirst(strtolower($section));
          if (!method_exists($this, $method)) {
              print $this->stdout("What is $section?\n");
              return Controller::EXIT_CODE_ERROR;
          }
  
          $this->$method();
  
          return Controller::EXIT_CODE_NORMAL;
      }
  
1c1c6545   Karnovsky A   ---
27
28
29
30
31
32
33
34
35
36
37
38
39
      public function goClear() {
          foreach(Product::find()->all() as $product) {
              if (empty($product->variant)) {
                  if (!$product->delete()) {
                      $this->stdout("#$product->product_id '$product->name'\n");
                      exit;
                  } else {
                      $this->stdout("#$product->product_id '$product->name'\n");
                  }
              }
          }
      }
  
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
40
41
42
      public function goGo() {
          $new_products = $linked_products = 0;
          foreach(RemoteProducts::find()->all() as $product) {
dc50f607   Karnovsky A   Some fixes
43
              $product->Brand = trim($product->Brand);
55b984ad   Karnovsky A   ---
44
45
46
              if (empty($product->remoteCategory) || empty($product->remoteCategory->category)) {
                  continue;
              }
1c1c6545   Karnovsky A   ---
47
              if (!empty($product->product))
55b984ad   Karnovsky A   ---
48
              {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
49
50
                  $linked_products++;
  
dc50f607   Karnovsky A   Some fixes
51
                  $_productVariant = ProductVariant::findOne($product->product->product_variant_id);
55b984ad   Karnovsky A   ---
52
  
dc50f607   Karnovsky A   Some fixes
53
54
55
                  $_product = Product::findOne($product->product->product_id);
  
                  $brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
56
57
58
  
                  if (
                  $_product->name != $product->Name ||
55b984ad   Karnovsky A   ---
59
                  $_product->categories[0]->category_id != $product->remoteCategory->category->category_id ||
dc50f607   Karnovsky A   Some fixes
60
                  ($product->Brand && $brand !== null && $_product->brand_id != $brand->brand_id)
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
61
62
63
64
                  ) {
                      $_product->name = $product->Name;
                      $_product->categories = [$product->remoteCategory->category->category_id];
                      if ( $product->Brand ) {
dc50f607   Karnovsky A   Some fixes
65
                          if ( $brand !== null ) {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
66
67
68
69
                              $_product->brand_id = $brand->brand_id;
                          } else {
                              // Create brand
                              $brand = new Brand();
dc50f607   Karnovsky A   Some fixes
70
71
                              $brand->name = $product->Brand;
                              $brand->remote_id = $product->Brand;
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
                              $brand->save();
                              $_product->brand_id = $brand->brand_id;
                          }
                      }
                      $_product->save();
                  }
  
                  if (
                  $_productVariant->price != floatval($product->Price) ||
                  $_productVariant->price_old != floatval($product->Price_old) ||
                  (!empty($product->Article) && $_productVariant->sku != $product->Article)
                  ) {
                      $_productVariant->price = floatval($product->Price);
                      $_productVariant->price_old = floatval($product->Price_old);
                      $_productVariant->sku = empty($product->Article) ? uniqid('gds_') : $product->Article;
                      if (!$_productVariant->price) {
                          $_productVariant->stock = 0;
                      }
                      $_productVariant->save();
                  }
55b984ad   Karnovsky A   ---
92
93
              }
              elseif (!empty($product->remoteCategory->category->category_id)) {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
94
95
96
97
98
99
100
101
                  $new_products++;
  
                  $_product = new Product();
                  $_productVariant = new ProductVariant();
                  $_product->name = $product->Name;
                  $_product->categories = [$product->remoteCategory->category->category_id];
  
                  if ( $product->Brand ) {
dc50f607   Karnovsky A   Some fixes
102
                      if ( ($brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one()) !== null ) {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
103
104
105
106
107
108
109
110
111
                          $_product->brand_id = $brand->brand_id;
                      } else {
                          // Create brand
                          $brand = new Brand();
                          $brand->name = trim($product->Brand);
                          $brand->save();
                          $_product->brand_id = $brand->brand_id;
                      }
                  }
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
112
113
114
115
116
117
118
                  $_productVariant->price = floatval($product->Price);
                  $_productVariant->price_old = floatval($product->Price_old);
                  $_productVariant->sku = empty($product->Article) ? uniqid('gds_') : $product->Article;
                  $_productVariant->product_unit_id = 1;
                  $_productVariant->remote_id = $product->ID;
                  $_productVariant->stock = $_productVariant->price > 0 ? null : 0;
  
1c1c6545   Karnovsky A   ---
119
120
121
122
123
124
125
                  if ($_product->save()) {
                      $_productVariant->product_id = $_product->product_id;
                      if (!$_productVariant->save()) {
                          print $this->stdout("Saved error for variant of the {$_product->name} {$_product->product_id}?\n");
  //                    return Controller::EXIT_CODE_ERROR;
                      }
  
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
126
127
                  }
              }
1c1c6545   Karnovsky A   ---
128
              $product->delete();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
129
130
          }
  
1c1c6545   Karnovsky A   ---
131
  //        $this->goStat();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      }
  
      public function goStat() {
          $all_products = $new_products = $linked_products = $orpahed_products = 0;
          $remoteProducts = RemoteProducts::find()->all();
  
          $not_linked_cats = [];
  
          foreach($remoteProducts as $product) {
              if (!empty($product->product->product_id)) {
                  $linked_products++;
              } elseif (!empty($product->remoteCategory) && !empty($product->remoteCategory->category) && !empty($product->remoteCategory->category->category_id)) {
                  $new_products++;
              } else {
                  if (!empty($product->remoteCategory)) {
                      if (empty($not_linked_cats[$product->remoteCategory->ID])) {
                          $not_linked_cats[$product->remoteCategory->ID] = $product->remoteCategory->Name ." (". $product->remoteCategory->ID .")\n";
                      }
                  }
                  $orpahed_products++;
              }
              $all_products++;
          }
  
          $this->stdout("Всего $all_products товаров, $new_products новых и $linked_products уже связанных.\n");
          if (!empty($not_linked_cats)) {
              $this->stdout("$orpahed_products товаров не привязаны к категориям:\n");
              foreach ($not_linked_cats as $not_linked_cat) {
                  $this->stdout("$not_linked_cat");
              }
          }
      }
  
      public function goProducts() {
  
      }
  }