Skip to content

Commit 3b52c68

Browse files
dev
1 parent fa72101 commit 3b52c68

File tree

1 file changed

+204
-20
lines changed

1 file changed

+204
-20
lines changed

src/console/controllers/SkeeksSuppliersController.php

Lines changed: 204 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use skeeks\cms\shop\models\ShopCollection;
2323
use skeeks\cms\shop\models\ShopProduct;
2424
use skeeks\cms\shop\models\ShopStore;
25+
use skeeks\cms\shop\models\ShopStoreProduct;
2526
use yii\base\Exception;
2627
use yii\console\Controller;
2728
use yii\helpers\ArrayHelper;
@@ -244,13 +245,26 @@ public function actionUpdateMeasures()
244245
* @return void
245246
* @throws Exception
246247
*/
247-
public function actionUpdateCategories()
248+
public function actionUpdateCategories($page = 1)
248249
{
249-
$response = \Yii::$app->skeeksSuppliersApi->methodCategories();
250+
$response = \Yii::$app->skeeksSuppliersApi->methodCategories([
251+
'page' => $page
252+
]);
250253

251-
$this->stdout("Обновление категорий [{$response->time} сек]", Console::BG_BLUE);
254+
$this->stdout("Обновление категорий, страница {$page} [{$response->time} сек]", Console::BG_BLUE);
252255
$this->stdout("\n");
253256

257+
$total = $response->headers->get("x-pagination-total-count");
258+
$pageCount = $response->headers->get("x-pagination-page-count");
259+
260+
if ($page == 1) {
261+
$this->stdout("Всего категорий к обновлению: {$total}\n");
262+
$this->stdout("Страниц: {$pageCount}\n");
263+
}
264+
265+
$this->stdout("Страница: {$page}\n");
266+
267+
254268
$updated = 0;
255269
$created = 0;
256270
if ($response->isOk) {
@@ -286,19 +300,35 @@ public function actionUpdateCategories()
286300
} else {
287301
throw new Exception("Ошибка ответа API {$response->request_url}; code: {$response->code}; code: {$response->content}");
288302
}
303+
304+
if ($page < $pageCount) {
305+
$this->actionUpdateCategories($page + 1);
306+
}
289307
}
290308

291309
/**
292310
* @return void
293311
* @throws Exception
294312
*/
295-
public function actionUpdateProperties()
313+
public function actionUpdateProperties($page = 1)
296314
{
297-
$response = \Yii::$app->skeeksSuppliersApi->methodProperties();
315+
$response = \Yii::$app->skeeksSuppliersApi->methodProperties([
316+
'page' => $page
317+
]);
298318

299-
$this->stdout("Обновление характеристик [{$response->time} сек]", Console::BG_BLUE);
319+
$this->stdout("Обновление характеристик, страница {$page} [{$response->time} сек]", Console::BG_BLUE);
300320
$this->stdout("\n");
301321

322+
$total = $response->headers->get("x-pagination-total-count");
323+
$pageCount = $response->headers->get("x-pagination-page-count");
324+
325+
if ($page == 1) {
326+
$this->stdout("Всего характеристик к обновлению: {$total}\n");
327+
$this->stdout("Страниц: {$pageCount}\n");
328+
}
329+
330+
$this->stdout("Страница: {$page}\n");
331+
302332
$updated = 0;
303333
$created = 0;
304334

@@ -333,19 +363,35 @@ public function actionUpdateProperties()
333363
} else {
334364
throw new Exception("Ошибка ответа API {$response->request_url}; code: {$response->code}; code: {$response->content}");
335365
}
366+
367+
if ($page < $pageCount) {
368+
$this->actionUpdateProperties($page + 1);
369+
}
336370
}
337371

338372
/**
339373
* @return void
340374
* @throws Exception
341375
*/
342-
public function actionUpdateBrands()
376+
public function actionUpdateBrands($page = 1)
343377
{
344-
$response = \Yii::$app->skeeksSuppliersApi->methodBrands();
378+
$response = \Yii::$app->skeeksSuppliersApi->methodBrands([
379+
'page' => $page
380+
]);
345381

346-
$this->stdout("Обновление брендов [{$response->time} сек]", Console::BG_BLUE);
382+
$this->stdout("Обновление брендов, страница {$page} [{$response->time} сек]", Console::BG_BLUE);
347383
$this->stdout("\n");
348384

385+
$total = $response->headers->get("x-pagination-total-count");
386+
$pageCount = $response->headers->get("x-pagination-page-count");
387+
388+
if ($page == 1) {
389+
$this->stdout("Всего брендов к обновлению: {$total}\n");
390+
$this->stdout("Страниц: {$pageCount}\n");
391+
}
392+
393+
$this->stdout("Страница: {$page}\n");
394+
349395
$updated = 0;
350396
$created = 0;
351397

@@ -380,20 +426,36 @@ public function actionUpdateBrands()
380426
} else {
381427
throw new Exception("Ошибка ответа API {$response->request_url}; code: {$response->code}; code: {$response->content}");
382428
}
429+
430+
if ($page < $pageCount) {
431+
$this->actionUpdateBrands($page + 1);
432+
}
383433
}
384434

385435

386436
/**
387437
* @return void
388438
* @throws Exception
389439
*/
390-
public function actionUpdateCollections()
440+
public function actionUpdateCollections($page = 1)
391441
{
392-
$response = \Yii::$app->skeeksSuppliersApi->methodCollections();
442+
$response = \Yii::$app->skeeksSuppliersApi->methodCollections([
443+
'page' => $page
444+
]);
393445

394-
$this->stdout("Обновление коллекций [{$response->time} сек]", Console::BG_BLUE);
446+
$this->stdout("Обновление коллекций, страница {$page} [{$response->time} сек]", Console::BG_BLUE);
395447
$this->stdout("\n");
396448

449+
$total = $response->headers->get("x-pagination-total-count");
450+
$pageCount = $response->headers->get("x-pagination-page-count");
451+
452+
if ($page == 1) {
453+
$this->stdout("Всего коллекций к обновлению: {$total}\n");
454+
$this->stdout("Страниц: {$pageCount}\n");
455+
}
456+
457+
$this->stdout("Страница: {$page}\n");
458+
397459
$updated = 0;
398460
$created = 0;
399461

@@ -428,19 +490,36 @@ public function actionUpdateCollections()
428490
} else {
429491
throw new Exception("Ошибка ответа API {$response->request_url}; code: {$response->code}; code: {$response->content}");
430492
}
493+
494+
if ($page < $pageCount) {
495+
$this->actionUpdateCollections($page + 1);
496+
}
431497
}
432498

433499
/**
434500
* @return void
435501
* @throws Exception
436502
*/
437-
public function actionUpdateStores()
503+
public function actionUpdateStores($page = 1)
438504
{
439-
$response = \Yii::$app->skeeksSuppliersApi->methodStores();
505+
$response = \Yii::$app->skeeksSuppliersApi->methodStores([
506+
'page' => $page
507+
]);
440508

441-
$this->stdout("Обновление складов [{$response->time} сек]", Console::BG_BLUE);
509+
$this->stdout("Обновление складов, страница {$page} [{$response->time} сек]", Console::BG_BLUE);
442510
$this->stdout("\n");
443511

512+
$total = $response->headers->get("x-pagination-total-count");
513+
$pageCount = $response->headers->get("x-pagination-page-count");
514+
515+
if ($page == 1) {
516+
$this->stdout("Всего складов к обновлению: {$total}\n");
517+
$this->stdout("Страниц: {$pageCount}\n");
518+
}
519+
520+
$this->stdout("Страница: {$page}\n");
521+
522+
444523
$updated = 0;
445524
$created = 0;
446525

@@ -475,12 +554,18 @@ public function actionUpdateStores()
475554
} else {
476555
throw new Exception("Ошибка ответа API {$response->request_url}; code: {$response->code}; code: {$response->content}");
477556
}
557+
558+
if ($page < $pageCount) {
559+
$this->actionUpdateStores($page + 1);
560+
}
478561
}
479562

480563

481564
/**
565+
* @param $page
482566
* @return void
483567
* @throws Exception
568+
* @throws \Throwable
484569
*/
485570
public function actionUpdateProducts($page = 1)
486571
{
@@ -894,7 +979,7 @@ private function _updateCollection($apiData = [], ShopCollection $model = null)
894979
$img = $this->_addImage($imgApiData);
895980
$imgIds[] = $img->id;
896981
}
897-
982+
898983
$model->setImageIds($imgIds);
899984
}
900985

@@ -938,10 +1023,10 @@ private function _updateCollection($apiData = [], ShopCollection $model = null)
9381023
$img = $this->_addImage($imgApiData);
9391024
$imgIds[] = $img->id;
9401025
}
941-
1026+
9421027
$model->setImageIds($imgIds);
9431028
}
944-
1029+
9451030
if ($model->save()) {
9461031

9471032
} else {
@@ -980,7 +1065,7 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
9801065
$result = false;
9811066

9821067
$content_id = \Yii::$app->shop->contentProducts->id;
983-
1068+
9841069
$t = \Yii::$app->db->beginTransaction();
9851070
try {
9861071
if ($model) {
@@ -1090,6 +1175,9 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
10901175
throw new Exception("Ошибка обновления товара {$model->id}: ".print_r($shopProduct->errors, true));
10911176
}
10921177

1178+
$store_items = (array)ArrayHelper::getValue($apiData, "store_items");
1179+
$this->_updateStoreItemsForProduct($shopProduct, $store_items);
1180+
10931181
$result = true;
10941182
}
10951183
} else {
@@ -1192,11 +1280,15 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
11921280

11931281

11941282
$shopProduct->id = $model->id;
1195-
1283+
11961284
if (!$shopProduct->save()) {
11971285
throw new Exception("Ошибка обновления товара {$model->id}: ".print_r($shopProduct->errors, true));
11981286
}
11991287

1288+
1289+
$store_items = (array)ArrayHelper::getValue($apiData, "store_items");
1290+
$this->_updateStoreItemsForProduct($shopProduct, $store_items);
1291+
12001292
$result = true;
12011293

12021294
}
@@ -1217,6 +1309,98 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
12171309
return $result;
12181310
}
12191311

1312+
private $_stores = [];
1313+
1314+
/**
1315+
* @param int $sx_store_id
1316+
* @return ShopStore|null
1317+
*/
1318+
private function _getStore(int $sx_store_id)
1319+
{
1320+
1321+
$shopStore = ArrayHelper::getValue($this->_stores, $sx_store_id, null);
1322+
1323+
if (!$shopStore) {
1324+
$shopStore = ShopStore::find()->sxId($sx_store_id)->one();
1325+
if ($shopStore) {
1326+
$this->_stores[$sx_store_id] = $shopStore;
1327+
}
1328+
}
1329+
1330+
return $shopStore;
1331+
}
1332+
1333+
private function _updateStoreItemsForProduct(ShopProduct $shopProduct, array $apiData = [])
1334+
{
1335+
if ($apiData) {
1336+
foreach ($apiData as $store_item_data) {
1337+
$sx_store_id = (int)ArrayHelper::getValue($store_item_data, "store_id");
1338+
$supplier_code = trim((string)ArrayHelper::getValue($store_item_data, "supplier_code"));
1339+
$shopStore = $this->_getStore($sx_store_id);
1340+
1341+
if ($shopStore) {
1342+
/**
1343+
* @var $shopStoreItem ShopStoreProduct
1344+
*/
1345+
$shopStoreItem = $shopStore->getShopStoreProducts()->andWhere(['external_id' => $supplier_code])->one();
1346+
1347+
$api_supplier_name = trim((string)ArrayHelper::getValue($store_item_data, "supplier_name"));
1348+
$api_quantity = (float)ArrayHelper::getValue($store_item_data, "quantity");
1349+
$api_purchase_price = (float)ArrayHelper::getValue($store_item_data, "purchase_price");
1350+
$api_selling_price = (float)ArrayHelper::getValue($store_item_data, "selling_price");
1351+
1352+
if (!$shopStoreItem) {
1353+
$shopStoreItem = new ShopStoreProduct();
1354+
$shopStoreItem->shop_store_id = $shopStore->id;
1355+
$shopStoreItem->shop_product_id = $shopProduct->id;
1356+
$shopStoreItem->external_id = $supplier_code;
1357+
$shopStoreItem->name = $api_supplier_name;
1358+
$shopStoreItem->quantity = $api_quantity;
1359+
$shopStoreItem->purchase_price = $api_purchase_price;
1360+
$shopStoreItem->selling_price = $api_selling_price;
1361+
if (!$shopStoreItem->save()) {
1362+
throw new Exception(print_r($shopStoreItem->errors, true));
1363+
}
1364+
} else {
1365+
$changedAttrs = [];
1366+
1367+
if ($shopStoreItem->shop_product_id != $shopProduct->id) {
1368+
$shopStoreItem->shop_product_id = $shopProduct->id;
1369+
$changedAttrs[] = "shop_product_id";
1370+
}
1371+
1372+
if ($shopStoreItem->name != $api_supplier_name) {
1373+
$shopStoreItem->name = $api_supplier_name;
1374+
$changedAttrs[] = "name";
1375+
}
1376+
1377+
if ($shopStoreItem->quantity != $api_quantity) {
1378+
$shopStoreItem->quantity = $api_quantity;
1379+
$changedAttrs[] = "quantity";
1380+
}
1381+
1382+
if ($shopStoreItem->selling_price != $api_selling_price) {
1383+
$shopStoreItem->selling_price = $api_selling_price;
1384+
$changedAttrs[] = "selling_price";
1385+
}
1386+
1387+
if ($shopStoreItem->purchase_price != $api_purchase_price) {
1388+
$shopStoreItem->purchase_price = $api_purchase_price;
1389+
$changedAttrs[] = "purchase_price";
1390+
}
1391+
1392+
if ($changedAttrs) {
1393+
if (!$shopStoreItem->update(true, $changedAttrs)) {
1394+
throw new Exception(print_r($shopStoreItem->errors, true));;
1395+
}
1396+
}
1397+
}
1398+
1399+
}
1400+
1401+
}
1402+
}
1403+
}
12201404

12211405
/**
12221406
* @param $apiData

0 commit comments

Comments
 (0)