22
22
use skeeks \cms \shop \models \ShopCollection ;
23
23
use skeeks \cms \shop \models \ShopProduct ;
24
24
use skeeks \cms \shop \models \ShopStore ;
25
+ use skeeks \cms \shop \models \ShopStoreProduct ;
25
26
use yii \base \Exception ;
26
27
use yii \console \Controller ;
27
28
use yii \helpers \ArrayHelper ;
@@ -244,13 +245,26 @@ public function actionUpdateMeasures()
244
245
* @return void
245
246
* @throws Exception
246
247
*/
247
- public function actionUpdateCategories ()
248
+ public function actionUpdateCategories ($ page = 1 )
248
249
{
249
- $ response = \Yii::$ app ->skeeksSuppliersApi ->methodCategories ();
250
+ $ response = \Yii::$ app ->skeeksSuppliersApi ->methodCategories ([
251
+ 'page ' => $ page
252
+ ]);
250
253
251
- $ this ->stdout ("Обновление категорий [ {$ response ->time } сек] " , Console::BG_BLUE );
254
+ $ this ->stdout ("Обновление категорий, страница { $ page } [ {$ response ->time } сек] " , Console::BG_BLUE );
252
255
$ this ->stdout ("\n" );
253
256
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
+
254
268
$ updated = 0 ;
255
269
$ created = 0 ;
256
270
if ($ response ->isOk ) {
@@ -286,19 +300,35 @@ public function actionUpdateCategories()
286
300
} else {
287
301
throw new Exception ("Ошибка ответа API {$ response ->request_url }; code: {$ response ->code }; code: {$ response ->content }" );
288
302
}
303
+
304
+ if ($ page < $ pageCount ) {
305
+ $ this ->actionUpdateCategories ($ page + 1 );
306
+ }
289
307
}
290
308
291
309
/**
292
310
* @return void
293
311
* @throws Exception
294
312
*/
295
- public function actionUpdateProperties ()
313
+ public function actionUpdateProperties ($ page = 1 )
296
314
{
297
- $ response = \Yii::$ app ->skeeksSuppliersApi ->methodProperties ();
315
+ $ response = \Yii::$ app ->skeeksSuppliersApi ->methodProperties ([
316
+ 'page ' => $ page
317
+ ]);
298
318
299
- $ this ->stdout ("Обновление характеристик [ {$ response ->time } сек] " , Console::BG_BLUE );
319
+ $ this ->stdout ("Обновление характеристик, страница { $ page } [ {$ response ->time } сек] " , Console::BG_BLUE );
300
320
$ this ->stdout ("\n" );
301
321
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
+
302
332
$ updated = 0 ;
303
333
$ created = 0 ;
304
334
@@ -333,19 +363,35 @@ public function actionUpdateProperties()
333
363
} else {
334
364
throw new Exception ("Ошибка ответа API {$ response ->request_url }; code: {$ response ->code }; code: {$ response ->content }" );
335
365
}
366
+
367
+ if ($ page < $ pageCount ) {
368
+ $ this ->actionUpdateProperties ($ page + 1 );
369
+ }
336
370
}
337
371
338
372
/**
339
373
* @return void
340
374
* @throws Exception
341
375
*/
342
- public function actionUpdateBrands ()
376
+ public function actionUpdateBrands ($ page = 1 )
343
377
{
344
- $ response = \Yii::$ app ->skeeksSuppliersApi ->methodBrands ();
378
+ $ response = \Yii::$ app ->skeeksSuppliersApi ->methodBrands ([
379
+ 'page ' => $ page
380
+ ]);
345
381
346
- $ this ->stdout ("Обновление брендов [ {$ response ->time } сек] " , Console::BG_BLUE );
382
+ $ this ->stdout ("Обновление брендов, страница { $ page } [ {$ response ->time } сек] " , Console::BG_BLUE );
347
383
$ this ->stdout ("\n" );
348
384
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
+
349
395
$ updated = 0 ;
350
396
$ created = 0 ;
351
397
@@ -380,20 +426,36 @@ public function actionUpdateBrands()
380
426
} else {
381
427
throw new Exception ("Ошибка ответа API {$ response ->request_url }; code: {$ response ->code }; code: {$ response ->content }" );
382
428
}
429
+
430
+ if ($ page < $ pageCount ) {
431
+ $ this ->actionUpdateBrands ($ page + 1 );
432
+ }
383
433
}
384
434
385
435
386
436
/**
387
437
* @return void
388
438
* @throws Exception
389
439
*/
390
- public function actionUpdateCollections ()
440
+ public function actionUpdateCollections ($ page = 1 )
391
441
{
392
- $ response = \Yii::$ app ->skeeksSuppliersApi ->methodCollections ();
442
+ $ response = \Yii::$ app ->skeeksSuppliersApi ->methodCollections ([
443
+ 'page ' => $ page
444
+ ]);
393
445
394
- $ this ->stdout ("Обновление коллекций [ {$ response ->time } сек] " , Console::BG_BLUE );
446
+ $ this ->stdout ("Обновление коллекций, страница { $ page } [ {$ response ->time } сек] " , Console::BG_BLUE );
395
447
$ this ->stdout ("\n" );
396
448
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
+
397
459
$ updated = 0 ;
398
460
$ created = 0 ;
399
461
@@ -428,19 +490,36 @@ public function actionUpdateCollections()
428
490
} else {
429
491
throw new Exception ("Ошибка ответа API {$ response ->request_url }; code: {$ response ->code }; code: {$ response ->content }" );
430
492
}
493
+
494
+ if ($ page < $ pageCount ) {
495
+ $ this ->actionUpdateCollections ($ page + 1 );
496
+ }
431
497
}
432
498
433
499
/**
434
500
* @return void
435
501
* @throws Exception
436
502
*/
437
- public function actionUpdateStores ()
503
+ public function actionUpdateStores ($ page = 1 )
438
504
{
439
- $ response = \Yii::$ app ->skeeksSuppliersApi ->methodStores ();
505
+ $ response = \Yii::$ app ->skeeksSuppliersApi ->methodStores ([
506
+ 'page ' => $ page
507
+ ]);
440
508
441
- $ this ->stdout ("Обновление складов [ {$ response ->time } сек] " , Console::BG_BLUE );
509
+ $ this ->stdout ("Обновление складов, страница { $ page } [ {$ response ->time } сек] " , Console::BG_BLUE );
442
510
$ this ->stdout ("\n" );
443
511
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
+
444
523
$ updated = 0 ;
445
524
$ created = 0 ;
446
525
@@ -475,12 +554,18 @@ public function actionUpdateStores()
475
554
} else {
476
555
throw new Exception ("Ошибка ответа API {$ response ->request_url }; code: {$ response ->code }; code: {$ response ->content }" );
477
556
}
557
+
558
+ if ($ page < $ pageCount ) {
559
+ $ this ->actionUpdateStores ($ page + 1 );
560
+ }
478
561
}
479
562
480
563
481
564
/**
565
+ * @param $page
482
566
* @return void
483
567
* @throws Exception
568
+ * @throws \Throwable
484
569
*/
485
570
public function actionUpdateProducts ($ page = 1 )
486
571
{
@@ -894,7 +979,7 @@ private function _updateCollection($apiData = [], ShopCollection $model = null)
894
979
$ img = $ this ->_addImage ($ imgApiData );
895
980
$ imgIds [] = $ img ->id ;
896
981
}
897
-
982
+
898
983
$ model ->setImageIds ($ imgIds );
899
984
}
900
985
@@ -938,10 +1023,10 @@ private function _updateCollection($apiData = [], ShopCollection $model = null)
938
1023
$ img = $ this ->_addImage ($ imgApiData );
939
1024
$ imgIds [] = $ img ->id ;
940
1025
}
941
-
1026
+
942
1027
$ model ->setImageIds ($ imgIds );
943
1028
}
944
-
1029
+
945
1030
if ($ model ->save ()) {
946
1031
947
1032
} else {
@@ -980,7 +1065,7 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
980
1065
$ result = false ;
981
1066
982
1067
$ content_id = \Yii::$ app ->shop ->contentProducts ->id ;
983
-
1068
+
984
1069
$ t = \Yii::$ app ->db ->beginTransaction ();
985
1070
try {
986
1071
if ($ model ) {
@@ -1090,6 +1175,9 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
1090
1175
throw new Exception ("Ошибка обновления товара {$ model ->id }: " .print_r ($ shopProduct ->errors , true ));
1091
1176
}
1092
1177
1178
+ $ store_items = (array )ArrayHelper::getValue ($ apiData , "store_items " );
1179
+ $ this ->_updateStoreItemsForProduct ($ shopProduct , $ store_items );
1180
+
1093
1181
$ result = true ;
1094
1182
}
1095
1183
} else {
@@ -1192,11 +1280,15 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
1192
1280
1193
1281
1194
1282
$ shopProduct ->id = $ model ->id ;
1195
-
1283
+
1196
1284
if (!$ shopProduct ->save ()) {
1197
1285
throw new Exception ("Ошибка обновления товара {$ model ->id }: " .print_r ($ shopProduct ->errors , true ));
1198
1286
}
1199
1287
1288
+
1289
+ $ store_items = (array )ArrayHelper::getValue ($ apiData , "store_items " );
1290
+ $ this ->_updateStoreItemsForProduct ($ shopProduct , $ store_items );
1291
+
1200
1292
$ result = true ;
1201
1293
1202
1294
}
@@ -1217,6 +1309,98 @@ private function _updateProduct($apiData = [], ShopCmsContentElement $model = nu
1217
1309
return $ result ;
1218
1310
}
1219
1311
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
+ }
1220
1404
1221
1405
/**
1222
1406
* @param $apiData
0 commit comments