ImportController.php
7.08 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
169
170
171
172
173
174
<?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;
}
public function goClear() {
foreach(Product::find()->all() as $product) {
if (empty($product->variant)) {
if (FALSE && !$product->delete()) {
$this->stdout("#$product->product_id '$product->name'\n");
exit;
} else {
$this->stdout("#$product->product_id '$product->name'\n");
}
}
}
}
public function goGo() {
$new_products = $linked_products = 0;
foreach(RemoteProducts::find()->all() as $product) {
// $product->Brand = trim($product->Brand);
if (empty($product->remoteCategory) || empty($product->remoteCategory->category)) {
continue;
}
if (!empty($product->product))
{
$linked_products++;
$_productVariant = ProductVariant::findOne($product->product->product_variant_id);
$_product = Product::findOne($product->product->product_id);
// $brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one();
if (
$_product->name != $product->Name ||
$_product->categories[0]->category_id != $product->remoteCategory->category->category_id
/*|| ($product->Brand && $brand !== null && $_product->brand_id != $brand->brand_id)*/
) {
$_product->name = $product->Name;
$_product->categories = [$product->remoteCategory->category->category_id];
/*if ( $product->Brand ) {
if ( $brand !== null ) {
$_product->brand_id = $brand->brand_id;
} else {
// Create brand
$brand = new Brand();
$brand->name = $product->Brand;
$brand->remote_id = $product->Brand;
$brand->save();
$_product->brand_id = $brand->brand_id;
}
}*/
$_product->save();
}
$price = floatval($product->Price);
$price_old = floatval($product->Price_old);
$stock = (bool)$product->In_stock ? NULL : 0;
if (
$_productVariant->price != $price ||
$_productVariant->price_old != $price_old ||
(!empty($product->Article) && $_productVariant->sku != $product->Article) ||
$_productVariant->stock !== $stock
) {
$_productVariant->price = floatval($product->Price);
$_productVariant->price_old = floatval($product->Price_old);
$_productVariant->sku = empty($product->Article) ? uniqid('gds_') : $product->Article;
$_productVariant->stock = $stock;
// if (!$_productVariant->price) {
// $_productVariant->stock = 0;
// }
$_productVariant->save();
}
}
elseif (!empty($product->remoteCategory->category->category_id)) {
$new_products++;
$_product = new Product();
$_productVariant = new ProductVariant();
$_product->name = $product->Name;
$_product->categories = [$product->remoteCategory->category->category_id];
/*if ( $product->Brand ) {
if ( ($brand = Brand::find()->filterWhere(['ilike', 'remote_id', trim($product->Brand)])->one()) !== null ) {
$_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;
}
}*/
$_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;
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;
}
}
}
// $product->delete();
}
// $this->goStat();
}
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() {
}
}