1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
store.php
См. документацию.
1<?php
2
6
8
10{
11 protected static function CheckFields($action, &$arFields)
12 {
13 global $DB;
14 global $USER;
15
16 if ($action !== 'ADD' && $action !== 'UPDATE')
17 {
18 return false;
19 }
20
21 $currentUserId = false;
22 if (isset($USER) && $USER instanceof CUser)
23 {
24 $currentUserId = (int)$USER->GetID();
25 if ($currentUserId <= 0)
26 {
27 $currentUserId = false;
28 }
29 }
30
31 if ($action === 'ADD')
32 {
33 $arFields += [
34 'ACTIVE' => 'Y',
35 'IMAGE_ID' => false,
36 'LOCATION_ID' => false,
37 'ISSUING_CENTER' => 'N',
38 'SHIPPING_CENTER' => 'N',
39 'SITE_ID' => false,
40 'CODE' => false,
41 'IS_DEFAULT' => 'N',
42 ];
43
44 $allowList = [
45 'TITLE' => true,
46 'ACTIVE' => true,
47 'ADDRESS' => true,
48 'DESCRIPTION' => true,
49 'GPS_N' => true,
50 'GPS_S' => true,
51 'IMAGE_ID' => true,
52 'LOCATION_ID' => true,
53 'USER_ID' => true,
54 'MODIFIED_BY' => true,
55 'PHONE' => true,
56 'SCHEDULE' => true,
57 'XML_ID' => true,
58 'SORT' => true,
59 'EMAIL' => true,
60 'ISSUING_CENTER' => true,
61 'SHIPPING_CENTER' => true,
62 'SITE_ID' => true,
63 'CODE' => true,
64 'IS_DEFAULT' => true,
65 ];
66 }
67 else
68 {
69 $allowList = [
70 'TITLE' => true,
71 'ACTIVE' => true,
72 'ADDRESS' => true,
73 'DESCRIPTION' => true,
74 'GPS_N' => true,
75 'GPS_S' => true,
76 'IMAGE_ID' => true,
77 'LOCATION_ID' => true,
78 'MODIFIED_BY' => true,
79 'PHONE' => true,
80 'SCHEDULE' => true,
81 'XML_ID' => true,
82 'SORT' => true,
83 'EMAIL' => true,
84 'ISSUING_CENTER' => true,
85 'SHIPPING_CENTER' => true,
86 'SITE_ID' => true,
87 'CODE' => true,
88 'IS_DEFAULT' => true,
89 ];
90 }
91
92 $arFields = array_intersect_key($arFields, $allowList);
93 $arFields['~DATE_MODIFY'] = $DB->GetNowFunction();
94 $arFields['MODIFIED_BY'] = $arFields['MODIFIED_BY'] ?? $currentUserId;
95 if ($action === 'ADD')
96 {
97 $arFields['~DATE_CREATE'] = $DB->GetNowFunction();
98 $arFields['USER_ID'] = $arFields['USER_ID'] ?? $currentUserId;
99 }
100
101 if (array_key_exists("ADDRESS", $arFields) && (string)$arFields["ADDRESS"] == '')
102 {
103 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("CS_EMPTY_ADDRESS"));
104 return false;
105 }
106
107 if (array_key_exists('IMAGE_ID', $arFields))
108 {
109 self::prepareImage($arFields, 'IMAGE_ID');
110 }
111
112 if(isset($arFields["ACTIVE"]) && ($arFields["ACTIVE"]) !== 'Y')
113 {
114 $arFields["ACTIVE"] = 'N';
115 }
116 if(isset($arFields["ISSUING_CENTER"]) && ($arFields["ISSUING_CENTER"]) !== 'Y')
117 {
118 $arFields["ISSUING_CENTER"] = 'N';
119 }
120 if(isset($arFields["SHIPPING_CENTER"]) && ($arFields["SHIPPING_CENTER"]) !== 'Y')
121 {
122 $arFields["SHIPPING_CENTER"] = 'N';
123 }
124 if(isset($arFields["SITE_ID"]) && ($arFields["SITE_ID"] === '0' || $arFields["SITE_ID"] === ''))
125 {
126 $arFields["SITE_ID"] = false;
127 }
128 if (isset($arFields['CODE']) && $arFields['CODE'] === '')
129 {
130 $arFields['CODE'] = false;
131 }
132 if (isset($arFields['IS_DEFAULT']) && ($arFields['IS_DEFAULT']) !== 'Y')
133 {
134 $arFields['IS_DEFAULT'] = 'N';
135 }
136
137 return true;
138 }
139
140 private static function prepareImage(array &$fields, $fieldName): void
141 {
142 if (
143 $fields[$fieldName] === null
144 || $fields[$fieldName] === 'null'
145 || $fields[$fieldName] === ''
146 || $fields[$fieldName] === false
147 )
148 {
149 $fields[$fieldName] = false;
150 }
151 elseif (
152 is_string($fields[$fieldName])
153 || is_int($fields[$fieldName])
154 )
155 {
156 $fields[$fieldName] = (int)$fields[$fieldName];
157 if ($fields[$fieldName] <= 0)
158 {
159 unset($fields[$fieldName]);
160 }
161 }
162 elseif (is_array($fields[$fieldName]))
163 {
164 self::prepareImageArray($fields, $fieldName);
165 }
166 else
167 {
168 unset($fields[$fieldName]);
169 }
170 }
171
172 private static function prepareImageArray(array &$fields, $fieldName): void
173 {
174 if (empty($fields[$fieldName]))
175 {
176 unset($fields[$fieldName]);
177
178 return;
179 }
180
181 if (!isset($fields[$fieldName]['name']) && !isset($fields[$fieldName]['del']))
182 {
183 unset($fields[$fieldName]);
184
185 return;
186 }
187
188 $fields[$fieldName]['MODULE_ID'] = 'catalog';
189 }
190
191 public static function Update($id, $arFields)
192 {
193 global $DB;
194 $id = (int)$id;
195 if ($id <= 0)
196 return false;
197
198 $store = Catalog\StoreTable::getRow([
199 'select' => [
200 'ID',
201 'IMAGE_ID',
202 'ACTIVE',
203 ],
204 'filter' => [
205 '=ID' => $id,
206 ]
207 ]);
208 if (empty($store))
209 {
210 return false;
211 }
212
213 if ($store['IMAGE_ID'] !== null)
214 {
215 $store['IMAGE_ID'] = (int)$store['IMAGE_ID'];
216 if ($store['IMAGE_ID'] <= 0)
217 {
218 $store['IMAGE_ID'] = null;
219 }
220 }
221
222 foreach (GetModuleEvents("catalog", "OnBeforeCatalogStoreUpdate", true) as $arEvent)
223 {
224 if (ExecuteModuleEventEx($arEvent, array($id, &$arFields))===false)
225 return false;
226 }
227
228 if (!self::CheckFields('UPDATE', $arFields))
229 return false;
230
231 if (isset($arFields['IMAGE_ID']))
232 {
233 if (is_array($arFields['IMAGE_ID']))
234 {
235 $arFields['IMAGE_ID']['old_file'] = $store['IMAGE_ID'];
236 CFile::SaveForDB($arFields, 'IMAGE_ID', 'catalog');
237 }
238 elseif ($store['IMAGE_ID'] !== null)
239 {
240 CFile::Delete($store['IMAGE_ID']);
241 }
242 }
243 $strUpdate = $DB->PrepareUpdate("b_catalog_store", $arFields);
244
245 $bNeedConversion = false;
246 if (!empty($strUpdate))
247 {
248 if (isset($arFields['ACTIVE']))
249 {
250 $bNeedConversion = ($store['ACTIVE'] !== $arFields['ACTIVE']);
251 }
252
253 $strSql = "update b_catalog_store set ".$strUpdate." where ID = ".$id;
254 if(!$DB->Query($strSql))
255 return false;
257
258 Catalog\StoreTable::cleanCache();
259 }
260
261 if($bNeedConversion)
262 {
264 }
265
266 foreach(GetModuleEvents("catalog", "OnCatalogStoreUpdate", true) as $arEvent)
267 ExecuteModuleEventEx($arEvent, array($id, $arFields));
268
269 return $id;
270 }
271
272 public static function Delete($id)
273 {
274 global $DB, $USER_FIELD_MANAGER;
275 $id = (int)$id;
276 if ($id > 0)
277 {
278 $store = Catalog\StoreTable::getRow([
279 'select' => [
280 'ID',
281 'IMAGE_ID'
282 ],
283 'filter' => [
284 '=ID' => $id,
285 ]
286 ]);
287 if (empty($store))
288 {
289 return false;
290 }
291
292 if ($store['IMAGE_ID'] !== null)
293 {
294 $store['IMAGE_ID'] = (int)$store['IMAGE_ID'];
295 if ($store['IMAGE_ID'] <= 0)
296 {
297 $store['IMAGE_ID'] = null;
298 }
299 }
300
301 foreach (GetModuleEvents("catalog", "OnBeforeCatalogStoreDelete", true) as $arEvent)
302 {
303 if(ExecuteModuleEventEx($arEvent, array($id))===false)
304 return false;
305 }
306
307 $dbDocs = $DB->Query("select ID from b_catalog_docs_element where STORE_FROM = ".$id." or STORE_TO = ".$id, true);
308 if($bStoreHaveDocs = $dbDocs->Fetch())
309 {
310 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("CS_STORE_HAVE_DOCS"));
311 return false;
312 }
313
314 $DB->Query("delete from b_catalog_store_product where STORE_ID = ".$id, true);
315 $DB->Query("delete from b_catalog_store where ID = ".$id, true);
316 if ($store['IMAGE_ID'] !== null)
317 {
318 CFile::Delete($store['IMAGE_ID']);
319 }
320
322
323 Catalog\StoreTable::cleanCache();
324
325 foreach(GetModuleEvents("catalog", "OnCatalogStoreDelete", true) as $arEvent)
326 ExecuteModuleEventEx($arEvent, array($id));
327
330 return true;
331 }
332 return false;
333 }
334
338 public static function recalculateStoreBalances(int $storeId): void
339 {
340 if ($storeId <= 0)
341 {
342 return;
343 }
344
345 if (!Catalog\Config\State::isUsedInventoryManagement())
346 {
347 return;
348 }
349
350 $store = Catalog\StoreTable::getList([
351 'select' => ['ID'],
352 'filter' => ['=ID' => $storeId],
353 ])->fetch();
354 if (!$store)
355 {
356 return;
357 }
358
361
362 $iblockIds = [];
363 $startId = 0;
364 do
365 {
366 $hasMore = false;
367 $productIds = [];
368
369 $storeProductList = Catalog\StoreProductTable::getList([
370 'select' => ['ID', 'PRODUCT_ID'],
371 'filter' => [
372 '>ID' => $startId,
373 '=STORE_ID' => $storeId,
374 '!=AMOUNT' => 0,
375 ],
376 'order' => ['ID' => 'ASC'],
377 'limit' => 200,
378 ]);
379 while ($row = $storeProductList->fetch())
380 {
381 $hasMore = true;
382 $startId = (int)$row['ID'];
383 $productIds[] = (int)$row['PRODUCT_ID'];
384 }
385
386 if (!empty($productIds))
387 {
389 }
390
391 self::recalculateProductsBalancesInternal($productIds, $iblockIds);
392 } while ($hasMore);
393
396
398 if (!empty($iblockIds))
399 {
400 foreach ($iblockIds as $iblock)
401 {
403 }
404 }
405
407 }
408
412 public static function recalculateProductsBalances(array $productIds): void
413 {
414 if (!Catalog\Config\State::isUsedInventoryManagement())
415 {
416 return;
417 }
418
421
422 $iblockIds = [];
423 self::recalculateProductsBalancesInternal($productIds, $iblockIds);
424
427
429 if (!empty($iblockIds))
430 {
431 foreach ($iblockIds as $iblock)
432 {
434 }
435 }
436
438 }
439
440 private static function recalculateProductsBalancesInternal(array $productIds, array &$iblockIds): void
441 {
442 if (empty($productIds))
443 {
444 return;
445 }
446
447 $products = [];
448 $foundedProductIds = [];
449
450 $productsList = Catalog\Model\Product::getList([
451 'select' => [
452 'ID',
453 'QUANTITY_RESERVED',
454 'IBLOCK_ID' => 'IBLOCK_ELEMENT.IBLOCK_ID',
455 ],
456 'filter' => ['@ID' => $productIds],
457 'order' => ['ID' => 'ASC'],
458 ]);
459 while ($row = $productsList->fetch())
460 {
461 if ($row['IBLOCK_ID'] === null)
462 {
463 continue;
464 }
465
466 $id = (int)$row['ID'];
467
468 $iblockId = (int)$row['IBLOCK_ID'];
469 $iblockIds[$iblockId] = $iblockId;
470 $products[$id] = [
471 'QUANTITY_RESERVED' => (float)$row['QUANTITY_RESERVED'],
472 'IBLOCK_ID' => $iblockId,
473 ];
474 $foundedProductIds[] = $id;
475 }
476 unset($row, $productsList);
477
478 if (empty($products))
479 {
480 return;
481 }
482
483 $storeProductQuantityList = Main\Application::getConnection()->query("
484 SELECT
485 SUM(CSP.AMOUNT) AS PRODUCT_QUANTITY,
486 CSP.PRODUCT_ID
487 FROM b_catalog_store_product CSP
488 INNER JOIN b_catalog_store CS ON CS.ID = CSP.STORE_ID
489 WHERE
490 CSP.PRODUCT_ID IN (" . implode(',', $foundedProductIds) . ")
491 AND CS.ACTIVE = 'Y'
492 GROUP BY CSP.PRODUCT_ID
493 ");
494 while ($row = $storeProductQuantityList->fetch())
495 {
496 $productId = (int)$row['PRODUCT_ID'];
497
498 Catalog\Model\Product::update(
499 $productId,
500 [
501 'fields' => [
502 'QUANTITY' => (float)$row['PRODUCT_QUANTITY'] - $products[$productId]['QUANTITY_RESERVED'],
503 ],
504 'external_fields' => [
505 'IBLOCK_ID' => $products[$productId]['IBLOCK_ID'],
506 ],
507 ]
508 );
509
510 unset($products[$productId]);
511 }
512 unset($row, $storeProductQuantityList);
513 unset($foundedProductIds);
514
515 if (!empty($products))
516 {
517 foreach ($products as $rowId => $rowData)
518 {
519 Catalog\Model\Product::update(
520 $rowId,
521 [
522 'fields' => [
523 'QUANTITY' => ($rowData['QUANTITY_RESERVED'] != 0 ? -$rowData['QUANTITY_RESERVED'] : 0),
524 ],
525 'external_fields' => [
526 'IBLOCK_ID' => $rowData['IBLOCK_ID'],
527 ],
528 ]
529 );
530 }
531 }
532
533 Catalog\Model\Product::clearCache();
534 }
535}
static clearCache()
Определения entity.php:139
static enableDeferredCalculation()
Определения sku.php:87
static disableDeferredCalculation()
Определения sku.php:97
static calculate()
Определения sku.php:450
static getUfId()
Определения store.php:265
static disableDeferredIndexing()
Определения manager.php:300
static runDeferredIndexing($iblockId)
Определения manager.php:321
static enableDeferredIndexing()
Определения manager.php:290
static getList(array $parameters=array())
Определения datamanager.php:431
static normalizeArrayValuesByInt(&$map, $sorted=true)
Определения collection.php:150
Определения store.php:10
static recalculateProductsBalances(array $productIds)
Определения store.php:412
static Delete($id)
Определения store.php:272
static recalculateStoreBalances(int $storeId)
Определения store.php:338
static CheckFields($action, &$arFields)
Определения store.php:11
static Update($id, $arFields)
Определения store.php:191
static clearStoreName($storeId)
Определения store_utility.php:163
Определения user.php:6037
$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
global $USER_FIELD_MANAGER
Определения attempt.php:6
$iblockId
Определения iblock_catalog_edit.php:30
if(! $catalogEdit->isSuccess()) $iblock
Определения iblock_catalog_edit.php:38
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$GLOBALS['____1690880296']
Определения license.php:1
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501