1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
discount_convert.php
См. документацию.
1<?php
2
5
7{
8 public static $intConvertPerStep = 0;
9 public static $intNextConvertPerStep = 0;
10 public static $intConverted = 0;
11 public static $intLastConvertID = 0;
12 public static $boolEmptyList = false;
13 public static $intErrors = 0;
14 public static $arErrors = array();
15 public static $strSessID = '';
16
17 public function __construct()
18 {
19
20 }
21
22 public static function InitStep()
23 {
24 if (self::$strSessID == '')
25 self::$strSessID = 'DC'.time();
26 if (isset($_SESSION[self::$strSessID]) && is_array($_SESSION[self::$strSessID]))
27 {
28 if (isset($_SESSION[self::$strSessID]['ERRORS_COUNT']) && (int)$_SESSION[self::$strSessID]['ERRORS_COUNT'] > 0)
29 self::$intErrors = (int)$_SESSION[self::$strSessID]['ERRORS_COUNT'];
30 if (isset($_SESSION[self::$strSessID]['ERRORS']) && is_array($_SESSION[self::$strSessID]['ERRORS']))
31 self::$arErrors = $_SESSION[self::$strSessID]['ERRORS'];
32 }
33 }
34
35 public static function SaveStep()
36 {
37 if (self::$strSessID == '')
38 self::$strSessID = 'DC'.time();
39 if (!isset($_SESSION[self::$strSessID]) || !is_array($_SESSION[self::$strSessID]))
40 $_SESSION[self::$strSessID] = array();
41 if (self::$intErrors > 0)
42 {
43 $_SESSION[self::$strSessID]['ERRORS_COUNT'] = self::$intErrors;
44 }
45 if (!empty(self::$arErrors))
46 {
47 $_SESSION[self::$strSessID]['ERRORS'] = self::$arErrors;
48 }
49 }
50
51 public static function GetErrors()
52 {
53 return self::$arErrors;
54 }
55
56 public static function ConvertDiscount($intStep = 100, $intMaxExecutionTime = 15)
57 {
58 global $DB;
59 global $APPLICATION;
60
62
63 $intStep = (int)$intStep;
64 if ($intStep <= 0)
65 $intStep = 100;
66 $startConvertTime = microtime(true);
67
68 $obDiscount = new CCatalogDiscount();
69
70 $strQueryPriceTypes = '';
71 $strQueryUserGroups = '';
72 $strTableName = '';
73
74 $strQueryPriceTypes = 'select CATALOG_GROUP_ID from b_catalog_discount2cat where DISCOUNT_ID = #ID#';
75 $strQueryUserGroups = 'select GROUP_ID from b_catalog_discount2group where DISCOUNT_ID = #ID#';
76 $strTableName = 'b_catalog_discount';
77
78 CTimeZone::Disable();
79
80 $rsDiscounts = CCatalogDiscount::GetList(
81 array('ID' => 'ASC'),
82 array(
85 ),
86 false,
87 array('nTopCount' => $intStep),
88 array('ID', 'MODIFIED_BY', 'TIMESTAMP_X', 'NAME', 'ACTIVE')
89 );
90 while ($arDiscount = $rsDiscounts->Fetch())
91 {
92 $boolActive = true;
93 $arSrcEntity = array();
94
96 'MODIFIED_BY' => $arDiscount['MODIFIED_BY'],
97 'ACTIVE' => $arDiscount['ACTIVE']
98 );
99
100 $arPriceTypes = array();
101 $arUserGroups = array();
102
103 $rsPriceTypes = $DB->Query(str_replace('#ID#', $arDiscount['ID'], $strQueryPriceTypes));
104 while ($arPrice = $rsPriceTypes->Fetch())
105 {
106 $arPrice['CATALOG_GROUP_ID'] = (int)$arPrice['CATALOG_GROUP_ID'];
107 if ($arPrice['CATALOG_GROUP_ID'] > 0)
108 $arPriceTypes[] = $arPrice['CATALOG_GROUP_ID'];
109 }
110 if (!empty($arPriceTypes))
111 {
112 $arPriceTypes = array_values(array_unique($arPriceTypes));
113 }
114 else
115 {
116 $arPriceTypes = array(-1);
117 }
118
119 $rsUserGroups = $DB->Query(str_replace('#ID#', $arDiscount['ID'], $strQueryUserGroups));
120 while ($arGroup = $rsUserGroups->Fetch())
121 {
122 $arGroup['GROUP_ID'] = (int)$arGroup['GROUP_ID'];
123 if ($arGroup['GROUP_ID'] > 0)
124 $arUserGroups[] = $arGroup['GROUP_ID'];
125 }
126 if (!empty($arUserGroups))
127 {
128 $arUserGroups = array_values(array_unique($arUserGroups));
129 }
130 else
131 {
132 $arUserGroups = array(-1);
133 }
134
135 $arFields['CATALOG_GROUP_IDS'] = $arPriceTypes;
136 $arFields['GROUP_IDS'] = $arUserGroups;
137
138 $arIBlockList = array();
139 $arSectionList = array();
140 $arElementList = array();
141 $arConditions = array(
142 'CLASS_ID' => 'CondGroup',
143 'DATA' => array(
144 'All' => 'AND',
145 'True' => 'True',
146 ),
147 'CHILDREN' => array(),
148 );
149 $intEntityCount = 0;
150
151 $boolEmpty = true;
152 $arSrcList = array();
153 $rsIBlocks = CCatalogDiscount::GetDiscountIBlocksList(array(), array('DISCOUNT_ID' => $arDiscount['ID']), false, false, array('IBLOCK_ID'));
154 while ($arIBlock = $rsIBlocks->Fetch())
155 {
156 $boolEmpty = false;
157 $arSrcList[] = $arIBlock['IBLOCK_ID'];
158 $arIBlock['IBLOCK_ID'] = (int)$arIBlock['IBLOCK_ID'];
159 if ($arIBlock['IBLOCK_ID'] > 0)
160 {
161 $strName = CIBlock::GetArrayByID($arIBlock['IBLOCK_ID'], 'NAME');
162 if ($strName !== false && $strName !== null)
163 {
164 $arIBlockList[] = $arIBlock['IBLOCK_ID'];
165 }
166 }
167 }
168 if (!empty($arIBlockList))
169 {
170 $arIBlockList = array_values(array_unique($arIBlockList));
171 $intEntityCount++;
172 }
173 else
174 {
175 if (!$boolEmpty)
176 {
177 $boolActive = false;
178 $arSrcEntity[] = str_replace('#IDS#', implode(', ', $arSrcList), Loc::getMessage('BT_MOD_CAT_DSC_CONV_ENTITY_IBLOCK_ERR'));
179 }
180 }
181
182 $boolEmpty = true;
183 $arSrcList = array();
184 $rsSections = CCatalogDiscount::GetDiscountSectionsList(array(), array('DISCOUNT_ID' => $arDiscount['ID']), false, false, array('SECTION_ID'));
185 while ($arSection = $rsSections->Fetch())
186 {
187 $boolEmpty = false;
188 $arSrcList[] = $arSection['SECTION_ID'];
189 $arSection['SECTION_ID'] = (int)$arSection['SECTION_ID'];
190 if ($arSection['SECTION_ID'] > 0)
191 $arSectionList[] = $arSection['SECTION_ID'];
192 }
193 if (!empty($arSectionList))
194 {
195 $arSectionList = array_values(array_unique($arSectionList));
196 $rsSections = CIBlockSection::GetList(array(), array('ID' => $arSectionList), false, array('ID'));
197 $arCheckResult = array();
198 while ($arSection = $rsSections->Fetch())
199 {
200 $arCheckResult[] = (int)$arSection['ID'];
201 }
202 if (!empty($arCheckResult))
203 {
204 $arSectionList = $arCheckResult;
205 $intEntityCount++;
206 }
207 else
208 {
209 $arSectionList = array();
210 }
211 }
212
213 if (empty($arSectionList))
214 {
215 if (!$boolEmpty)
216 {
217 $boolActive = false;
218 $arSrcEntity[] = str_replace('#IDS#', implode(', ', $arSrcList), Loc::getMessage('BT_MOD_CAT_DSC_CONV_ENTITY_SECTION_ERR'));
219 }
220 }
221
222 $boolEmpty = true;
223 $arSrcList = array();
224 $rsElements = CCatalogDiscount::GetDiscountProductsList(array(), array('DISCOUNT_ID' => $arDiscount['ID']), false, false, array('PRODUCT_ID'));
225 while ($arElement = $rsElements->Fetch())
226 {
227 $boolEmpty = false;
228 $arSrcList[] = $arElement['PRODUCT_ID'];
229 $arElement['PRODUCT_ID'] = (int)$arElement['PRODUCT_ID'];
230 if ($arElement['PRODUCT_ID'] > 0)
231 $arElementList[] = $arElement['PRODUCT_ID'];
232 }
233 if (!empty($arElementList))
234 {
235 $arElementList = array_values(array_unique($arElementList));
236 $rsItems = CIBlockElement::GetList(array(), array('ID' => $arElementList), false, false, array('ID'));
237 $arCheckResult = array();
238 while ($arItem = $rsItems->Fetch())
239 {
240 $arCheckResult[] = (int)$arItem['ID'];
241 }
242 if (!empty($arCheckResult))
243 {
244 $arElementList = $arCheckResult;
245 $intEntityCount++;
246 }
247 else
248 {
249 $arElementList = array();
250 }
251 }
252
253 if (empty($arElementList))
254 {
255 if (!$boolEmpty)
256 {
257 $boolActive = false;
258 $arSrcEntity[] = str_replace('#IDS#', implode(', ', $arSrcList), Loc::getMessage('BT_MOD_CAT_DSC_CONV_ENTITY_ELEMENT_ERR'));
259 }
260 }
261
262 if (!empty($arIBlockList))
263 {
264 if (1 < count($arIBlockList))
265 {
266 $arList = array();
267 foreach ($arIBlockList as &$intItemID)
268 {
269 $arList[] = array(
270 'CLASS_ID' => 'CondIBIBlock',
271 'DATA' => array(
272 'logic' => 'Equal',
273 'value' => $intItemID
274 ),
275 );
276 }
277 if (isset($intItemID))
278 unset($intItemID);
279 if (1 == $intEntityCount)
280 {
281 $arConditions = array(
282 'CLASS_ID' => 'CondGroup',
283 'DATA' => array(
284 'All' => 'OR',
285 'True' => 'True',
286 ),
287 'CHILDREN' => $arList,
288 );
289 }
290 else
291 {
292 $arConditions['CHILDREN'][] = array(
293 'CLASS_ID' => 'CondGroup',
294 'DATA' => array(
295 'All' => 'OR',
296 'True' => 'True',
297 ),
298 'CHILDREN' => $arList,
299 );
300 }
301 }
302 else
303 {
304 $arConditions['CHILDREN'][] = array(
305 'CLASS_ID' => 'CondIBIBlock',
306 'DATA' => array(
307 'logic' => 'Equal',
308 'value' => current($arIBlockList)
309 ),
310 );
311 }
312 }
313
314 if (!empty($arSectionList))
315 {
316 if (1 < count($arSectionList))
317 {
318 $arList = array();
319 foreach ($arSectionList as &$intItemID)
320 {
321 $arList[] = array(
322 'CLASS_ID' => 'CondIBSection',
323 'DATA' => array(
324 'logic' => 'Equal',
325 'value' => $intItemID
326 ),
327 );
328 }
329 if (isset($intItemID))
330 unset($intItemID);
331 if (1 == $intEntityCount)
332 {
333 $arConditions = array(
334 'CLASS_ID' => 'CondGroup',
335 'DATA' => array(
336 'All' => 'OR',
337 'True' => 'True',
338 ),
339 'CHILDREN' => $arList,
340 );
341 }
342 else
343 {
344 $arConditions['CHILDREN'][] = array(
345 'CLASS_ID' => 'CondGroup',
346 'DATA' => array(
347 'All' => 'OR',
348 'True' => 'True',
349 ),
350 'CHILDREN' => $arList,
351 );
352 }
353 }
354 else
355 {
356 $arConditions['CHILDREN'][] = array(
357 'CLASS_ID' => 'CondIBSection',
358 'DATA' => array(
359 'logic' => 'Equal',
360 'value' => current($arSectionList)
361 ),
362 );
363 }
364 }
365
366 if (!empty($arElementList))
367 {
368 if (1 < count($arElementList))
369 {
370 $arList = array();
371 foreach ($arElementList as &$intItemID)
372 {
373 $arList[] = array(
374 'CLASS_ID' => 'CondIBElement',
375 'DATA' => array(
376 'logic' => 'Equal',
377 'value' => $intItemID
378 ),
379 );
380 }
381 if (isset($intItemID))
382 unset($intItemID);
383 if (1 == $intEntityCount)
384 {
385 $arConditions = array(
386 'CLASS_ID' => 'CondGroup',
387 'DATA' => array(
388 'All' => 'OR',
389 'True' => 'True',
390 ),
391 'CHILDREN' => $arList,
392 );
393 }
394 else
395 {
396 $arConditions['CHILDREN'][] = array(
397 'CLASS_ID' => 'CondGroup',
398 'DATA' => array(
399 'All' => 'OR',
400 'True' => 'True',
401 ),
402 'CHILDREN' => $arList,
403 );
404 }
405 }
406 else
407 {
408 $arConditions['CHILDREN'][] = array(
409 'CLASS_ID' => 'CondIBElement',
410 'DATA' => array(
411 'logic' => 'Equal',
412 'value' => current($arElementList)
413 ),
414 );
415 }
416 }
417
418 $arFields['CONDITIONS'] = $arConditions;
419
420 if (!$boolActive)
421 {
422 $arFields['ACTIVE'] = 'N';
423 self::$intErrors++;
424 self::$arErrors[] = array(
425 'ID' => $arDiscount['ID'],
426 'NAME' => $arDiscount['NAME'],
427 'ERROR' => Loc::getMessage('BT_MOD_CAT_DSC_CONV_INACTIVE').' '.implode('; ', $arSrcEntity),
428 );
429 }
430
431 $mxRes = $obDiscount->Update($arDiscount['ID'], $arFields);
432 if (!$mxRes)
433 {
434 self::$intErrors++;
435 $strError = '';
436 if ($ex = $APPLICATION->GetException())
437 {
438 $strError = $ex->GetString();
439 }
440 if (empty($strError))
441 $strError = Loc::getMessage('BT_MOD_CAT_DSC_FORMAT_ERR');
442 self::$arErrors[] = array(
443 'ID' => $arDiscount['ID'],
444 'NAME' => $arDiscount['NAME'],
445 'ERROR' => $strError,
446 );
447 }
448 else
449 {
450 $arTimeFields = array('~TIMESTAMP_X' => $DB->CharToDateFunction($arDiscount['TIMESTAMP_X'], "FULL"));
451 $strUpdate = $DB->PrepareUpdate($strTableName, $arTimeFields);
452 if (!empty($strUpdate))
453 {
454 $strQuery = "UPDATE ".$strTableName." SET ".$strUpdate." WHERE ID = ".$arDiscount['ID']." AND TYPE = ".CCatalogDiscount::ENTITY_ID;
455 $DB->Query($strQuery);
456 }
457
458 self::$intConverted++;
459 self::$intConvertPerStep++;
460 }
461
462 if ($intMaxExecutionTime > 0 && (microtime(true) - $startConvertTime > $intMaxExecutionTime))
463 break;
464 }
465
466 CTimeZone::Enable();
467
468 if ($intMaxExecutionTime > (2*(microtime(true) - $startConvertTime)))
469 self::$intNextConvertPerStep = $intStep*2;
470 else
471 self::$intNextConvertPerStep = $intStep;
472
474 }
475
476 public static function ConvertFormatDiscount($intStep = 20, $intMaxExecutionTime = 15)
477 {
478 global $DB;
479 global $APPLICATION;
480
482
483 $intStep = (int)$intStep;
484 if ($intStep <= 0)
485 $intStep = 20;
486 $startConvertTime = microtime(true);
487
488 $obDiscount = new CCatalogDiscount();
489
490 $strTableName = 'b_catalog_discount';
491
493 {
494 return false;
495 }
496
497 if (self::$intLastConvertID <= 0)
498 self::$intLastConvertID = CCatalogDiscountConvertTmp::GetLastID();
499
500 CTimeZone::Disable();
501
502 self::$boolEmptyList = true;
503
504 $rsDiscounts = CCatalogDiscount::GetList(
505 array('ID' => 'ASC'),
506 array(
507 '>ID' => self::$intLastConvertID,
510 ),
511 false,
512 array('nTopCount' => $intStep),
513 array('ID', 'MODIFIED_BY', 'TIMESTAMP_X', 'CONDITIONS', 'NAME', 'ACTIVE')
514 );
515 while ($arDiscount = $rsDiscounts->Fetch())
516 {
517 $mxExist = CCatalogDiscountConvertTmp::IsExistID($arDiscount['ID']);
518 if (false === $mxExist)
519 {
520 self::$intErrors++;
521 return false;
522 }
523 self::$boolEmptyList = false;
524 if (0 < $mxExist)
525 {
526 self::$intConverted++;
527 self::$intConvertPerStep++;
528 self::$intLastConvertID = $arDiscount['ID'];
529 continue;
530 }
531
532 $iterator = Catalog\DiscountCouponTable::getList(array(
533 'select' => array('DISCOUNT_ID'),
534 'filter' => array('=DISCOUNT_ID' => (int)$arDiscount['ID']),
535 'limit' => 1
536 ));
537 $existRow = $iterator->fetch();
538 unset($iterator);
540 'MODIFIED_BY' => $arDiscount['MODIFIED_BY'],
541 'CONDITIONS' => $arDiscount['CONDITIONS'],
542 'ACTIVE' => $arDiscount['ACTIVE'],
543 'USE_COUPONS' => (!empty($existRow) ? 'Y' : 'N')
544 );
545 unset($existRow);
546
547 $mxRes = $obDiscount->Update($arDiscount['ID'], $arFields);
548 if (!$mxRes)
549 {
550 self::$intErrors++;
551 $strError = '';
552 if ($ex = $APPLICATION->GetException())
553 {
554 $strError = $ex->GetString();
555 }
556 if (empty($strError))
557 $strError = Loc::getMessage('BT_MOD_CAT_DSC_FORMAT_ERR');
558 self::$arErrors[] = array(
559 'ID' => $arDiscount['ID'],
560 'NAME' => $arDiscount['NAME'],
561 'ERROR' => $strError,
562 );
563 if (!CCatalogDiscountConvertTmp::SetID($arDiscount['ID']))
564 {
565 return false;
566 }
567
568 self::$intConverted++;
569 self::$intConvertPerStep++;
570 self::$intLastConvertID = $arDiscount['ID'];
571 }
572 else
573 {
574 $arTimeFields = array('~TIMESTAMP_X' => $DB->CharToDateFunction($arDiscount['TIMESTAMP_X'], "FULL"));
575 $strUpdate = $DB->PrepareUpdate($strTableName, $arTimeFields);
576 if (!empty($strUpdate))
577 {
578 $strQuery = "UPDATE ".$strTableName." SET ".$strUpdate." WHERE ID = ".$arDiscount['ID']." AND TYPE = ".CCatalogDiscount::ENTITY_ID;
579 $DB->Query($strQuery);
580 }
581 if (!CCatalogDiscountConvertTmp::SetID($arDiscount['ID']))
582 {
583 return false;
584 }
585
586 self::$intConverted++;
587 self::$intConvertPerStep++;
588 self::$intLastConvertID = $arDiscount['ID'];
589 }
590
591 if ($intMaxExecutionTime > 0 && (microtime(true) - $startConvertTime > $intMaxExecutionTime))
592 break;
593
594 }
595 CTimeZone::Enable();
596
597 if ($intMaxExecutionTime > (2*(microtime(true) - $startConvertTime)))
598 self::$intNextConvertPerStep = $intStep*2;
599 else
600 self::$intNextConvertPerStep = $intStep;
601
603
604 return true;
605 }
606
607 public static function GetCountOld()
608 {
609 global $DB;
610
611 $strSql = "SELECT COUNT(*) CNT FROM b_catalog_discount WHERE TYPE=".CCatalogDiscount::ENTITY_ID." AND VERSION=".CCatalogDiscount::OLD_FORMAT;
612
613 $res = $DB->Query($strSql);
614 if (!$res)
615 return 0;
616
617 if ($row = $res->Fetch())
618 return (int)$row['CNT'];
619 return 0;
620 }
621
622 public static function GetCountFormat()
623 {
625 return false;
626 return CCatalogDiscountConvertTmp::GetNeedConvert(self::$intLastConvertID);
627 }
628
629 public static function FormatComplete()
630 {
632 }
633}
global $APPLICATION
Определения include.php:80
static getMessage($code, $replace=null, $language=null)
Определения loc.php:30
const ENTITY_ID
Определения discount.php:22
const OLD_FORMAT
Определения discount.php:24
const CURRENT_FORMAT
Определения discount.php:23
static GetCountOld()
Определения discount_convert.php:607
static ConvertFormatDiscount($intStep=20, $intMaxExecutionTime=15)
Определения discount_convert.php:476
static $boolEmptyList
Определения discount_convert.php:12
static $arErrors
Определения discount_convert.php:14
static $intErrors
Определения discount_convert.php:13
static ConvertDiscount($intStep=100, $intMaxExecutionTime=15)
Определения discount_convert.php:56
static SaveStep()
Определения discount_convert.php:35
static FormatComplete()
Определения discount_convert.php:629
static $intLastConvertID
Определения discount_convert.php:11
static GetErrors()
Определения discount_convert.php:51
static InitStep()
Определения discount_convert.php:22
static $intConverted
Определения discount_convert.php:10
static GetCountFormat()
Определения discount_convert.php:622
static $strSessID
Определения discount_convert.php:15
static $intConvertPerStep
Определения discount_convert.php:8
static $intNextConvertPerStep
Определения discount_convert.php:9
static CreateTable()
Определения discount_convert.php:10
static GetLastID()
Определения discount_convert.php:83
static DropTable()
Определения discount_convert.php:39
static GetNeedConvert($intMinProduct)
Определения discount_convert.php:118
static IsExistID($intID)
Определения discount_convert.php:60
static SetID($intID)
Определения discount_convert.php:102
Определения discount.php:9
static GetDiscountProductsList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения discount.php:369
static GetDiscountIBlocksList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения discount.php:547
static GetDiscountSectionsList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения discount.php:458
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения discount.php:190
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$strError
Определения options_user_settings.php:4
global $DB
Определения cron_frame.php:29
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$rsIBlocks
Определения yandex_detail.php:58
$arIBlock['PROPERTY']
Определения yandex_detail.php:172
$iterator
Определения yandex_run.php:610