Blame view

console/controllers/ImportController.php 7.08 KB
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
1
2
3
4
  <?php
  
  namespace console\controllers;
  
e136edc8   Karnovsky A   ---
5
6
  //use common\modules\product\models\Brand;
  //use common\modules\product\models\BrandName;
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  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
      public function goClear() {
          foreach(Product::find()->all() as $product) {
              if (empty($product->variant)) {
e136edc8   Karnovsky A   ---
30
                  if (FALSE && !$product->delete()) {
1c1c6545   Karnovsky A   ---
31
32
33
34
35
36
37
38
39
                      $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) {
e136edc8   Karnovsky A   ---
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
                  $_product = Product::findOne($product->product->product_id);
  
e136edc8   Karnovsky A   ---
55
  //                $brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
56
57
58
  
                  if (
                  $_product->name != $product->Name ||
e136edc8   Karnovsky A   ---
59
60
                  $_product->categories[0]->category_id != $product->remoteCategory->category->category_id
                  /*|| ($product->Brand && $brand !== null && $_product->brand_id != $brand->brand_id)*/
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
61
62
63
                  ) {
                      $_product->name = $product->Name;
                      $_product->categories = [$product->remoteCategory->category->category_id];
e136edc8   Karnovsky A   ---
64
                      /*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
                              $brand->save();
                              $_product->brand_id = $brand->brand_id;
                          }
e136edc8   Karnovsky A   ---
75
                      }*/
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
76
77
78
                      $_product->save();
                  }
  
e136edc8   Karnovsky A   ---
79
80
81
82
                  $price = floatval($product->Price);
                  $price_old = floatval($product->Price_old);
                  $stock = (bool)$product->In_stock ? NULL : 0;
  
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
83
                  if (
e136edc8   Karnovsky A   ---
84
85
86
87
                  $_productVariant->price != $price ||
                  $_productVariant->price_old != $price_old ||
                  (!empty($product->Article) && $_productVariant->sku != $product->Article) ||
                  $_productVariant->stock !== $stock
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
88
89
90
91
                  ) {
                      $_productVariant->price = floatval($product->Price);
                      $_productVariant->price_old = floatval($product->Price_old);
                      $_productVariant->sku = empty($product->Article) ? uniqid('gds_') : $product->Article;
e136edc8   Karnovsky A   ---
92
93
94
95
                      $_productVariant->stock = $stock;
  //                    if (!$_productVariant->price) {
  //                        $_productVariant->stock = 0;
  //                    }
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
96
97
                      $_productVariant->save();
                  }
55b984ad   Karnovsky A   ---
98
99
              }
              elseif (!empty($product->remoteCategory->category->category_id)) {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
100
101
102
103
104
105
106
                  $new_products++;
  
                  $_product = new Product();
                  $_productVariant = new ProductVariant();
                  $_product->name = $product->Name;
                  $_product->categories = [$product->remoteCategory->category->category_id];
  
e136edc8   Karnovsky A   ---
107
                  /*if ( $product->Brand ) {
dc50f607   Karnovsky A   Some fixes
108
                      if ( ($brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one()) !== null ) {
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
109
110
111
112
113
114
115
116
                          $_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;
                      }
e136edc8   Karnovsky A   ---
117
                  }*/
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
118
119
120
121
122
123
124
                  $_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   ---
125
126
127
128
129
130
131
                  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...
132
133
                  }
              }
e136edc8   Karnovsky A   ---
134
  //            $product->delete();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
135
136
          }
  
1c1c6545   Karnovsky A   ---
137
  //        $this->goStat();
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
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
169
170
171
172
173
174
      }
  
      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() {
  
      }
  }