Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rounding.php
1<?php
2namespace Bitrix\Catalog;
3
14
50{
51 public const ROUND_MATH = 0x0001;
52 public const ROUND_UP = 0x0002;
53 public const ROUND_DOWN = 0x0004;
54
56 protected static int $clearCache = 0;
58 protected static array $priceTypeIds = [];
59
65 public static function getTableName(): string
66 {
67 return 'b_catalog_rounding';
68 }
69
75 public static function getMap(): array
76 {
77 return [
78 'ID' => new IntegerField(
79 'ID',
80 [
81 'primary' => true,
82 'autocomplete' => true,
83 'title' => Loc::getMessage('ROUNDING_ENTITY_ID_FIELD'),
84 ]
85 ),
86 'CATALOG_GROUP_ID' => new IntegerField(
87 'CATALOG_GROUP_ID',
88 [
89 'required' => true,
90 'title' => Loc::getMessage('ROUNDING_ENTITY_CATALOG_GROUP_ID_FIELD'),
91 ]
92 ),
93 'PRICE' => new FloatField(
94 'PRICE',
95 [
96 'required' => true,
97 'title' => Loc::getMessage('ROUNDING_ENTITY_PRICE_FIELD'),
98 ]
99 ),
100 'ROUND_TYPE' => new EnumField(
101 'ROUND_TYPE',
102 [
103 'required' => true,
104 'values' => [
105 self::ROUND_MATH,
106 self::ROUND_UP,
107 self::ROUND_DOWN,
108 ],
109 'title' => Loc::getMessage('ROUNDING_ENTITY_ROUND_TYPE_FIELD'),
110 ]
111 ),
112 'ROUND_PRECISION' => new FloatField(
113 'ROUND_PRECISION',
114 [
115 'required' => true,
116 'title' => Loc::getMessage('ROUNDING_ENTITY_ROUND_PRECISION_FIELD'),
117 ]
118 ),
119 'CREATED_BY' => new IntegerField(
120 'CREATED_BY',
121 [
122 'title' => Loc::getMessage('ROUNDING_ENTITY_CREATED_BY_FIELD'),
123 ]
124 ),
125 'DATE_CREATE' => new DatetimeField(
126 'DATE_CREATE',
127 [
128 'title' => Loc::getMessage('ROUNDING_ENTITY_DATE_CREATE_FIELD'),
129 ]
130 ),
131 'MODIFIED_BY' => new IntegerField(
132 'MODIFIED_BY',
133 [
134 'title' => Loc::getMessage('ROUNDING_ENTITY_MODIFIED_BY_FIELD'),
135 ]
136 ),
137 'DATE_MODIFY' => new DatetimeField(
138 'DATE_MODIFY',
139 [
140 'title' => Loc::getMessage('ROUNDING_ENTITY_TIMESTAMP_X_FIELD'),
141 ]
142 ),
143 'CREATED_BY_USER' => new Reference(
144 'CREATED_BY_USER',
145 '\Bitrix\Main\User',
146 ['=this.CREATED_BY' => 'ref.ID'],
147 ['join_type' => 'LEFT']
148 ),
149 'MODIFIED_BY_USER' => new Reference(
150 'MODIFIED_BY_USER',
151 '\Bitrix\Main\User',
152 ['=this.MODIFIED_BY' => 'ref.ID'],
153 ['join_type' => 'LEFT']
154 ),
155 ];
156 }
157
164 public static function onBeforeAdd(Event $event): EventResult
165 {
166 $result = new EventResult();
167 $data = $event->getParameter('fields');
168
169 $modifyFieldList = [];
170 static::setUserId(
171 $modifyFieldList,
172 $data,
173 [
174 'CREATED_BY',
175 'MODIFIED_BY',
176 ]
177 );
178 static::setTimestamp(
179 $modifyFieldList,
180 $data,
181 [
182 'DATE_CREATE',
183 'DATE_MODIFY',
184 ]
185 );
186
187 if (!empty($modifyFieldList))
188 {
189 $result->modifyFields($modifyFieldList);
190 }
191 unset($modifyFieldList);
192
193 return $result;
194 }
195
202 public static function onAfterAdd(Event $event): void
203 {
204 if (!static::isAllowedClearCache())
205 {
206 return;
207 }
208 $data = $event->getParameter('fields');
209 self::$priceTypeIds[$data['CATALOG_GROUP_ID']] = $data['CATALOG_GROUP_ID'];
210 unset($data);
211 static::clearCache();
212 }
213
220 public static function onBeforeUpdate(Event $event): EventResult
221 {
222 $result = new EventResult();
223 $data = $event->getParameter('fields');
224
225 $modifyFieldList = [];
226 static::setUserId(
227 $modifyFieldList,
228 $data,
229 [
230 'MODIFIED_BY',
231 ]
232 );
233 static::setTimestamp(
234 $modifyFieldList,
235 $data,
236 [
237 'DATE_MODIFY',
238 ]
239 );
240
241 if (!empty($modifyFieldList))
242 {
243 $result->modifyFields($modifyFieldList);
244 }
245 unset($modifyFieldList);
246
247 return $result;
248 }
249
256 public static function onUpdate(Event $event): void
257 {
258 if (!static::isAllowedClearCache())
259 {
260 return;
261 }
262 $data = $event->getParameter('fields');
263 $rule = static::getRow([
264 'select' => [
265 'ID',
266 'CATALOG_GROUP_ID',
267 ],
268 'filter' => [
269 '=ID' => $event->getParameter('id'),
270 ],
271 ]);
272 if (!empty($rule))
273 {
274 self::$priceTypeIds[$rule['CATALOG_GROUP_ID']] = $rule['CATALOG_GROUP_ID'];
275 if (isset($data['CATALOG_GROUP_ID']))
276 {
277 self::$priceTypeIds[$data['CATALOG_GROUP_ID']] = $data['CATALOG_GROUP_ID'];
278 }
279 }
280 unset($rule, $data);
281 }
282
289 public static function onAfterUpdate(Event $event): void
290 {
291 static::clearCache();
292 }
293
300 public static function onDelete(Event $event): void
301 {
302 if (!static::isAllowedClearCache())
303 {
304 return;
305 }
306 $rule = static::getRow([
307 'select' => [
308 'ID',
309 'CATALOG_GROUP_ID',
310 ],
311 'filter' => [
312 '=ID' => $event->getParameter('id'),
313 ],
314 ]);
315 if (!empty($rule))
316 {
317 self::$priceTypeIds[$rule['CATALOG_GROUP_ID']] = $rule['CATALOG_GROUP_ID'];
318 }
319 unset($rule);
320 }
321
328 public static function onAfterDelete(Event $event): void
329 {
330 static::clearCache();
331 }
332
338 public static function isAllowedClearCache(): bool
339 {
340 return (self::$clearCache >= 0);
341 }
342
348 public static function allowClearCache(): void
349 {
350 self::$clearCache++;
351 }
352
358 public static function disallowClearCache(): void
359 {
360 self::$clearCache--;
361 }
362
368 public static function clearPriceTypeIds(): void
369 {
370 self::$priceTypeIds = array();
371 }
372
379 public static function setPriceTypeIds(string|int|array $priceTypes): void
380 {
381 if (!is_array($priceTypes))
382 {
383 $priceTypes = [$priceTypes => $priceTypes];
384 }
385
386 if (!empty($priceTypes) && is_array($priceTypes))
387 {
388 self::$priceTypeIds = (
389 empty(self::$priceTypeIds)
390 ? $priceTypes
391 : array_merge(self::$priceTypeIds, $priceTypes)
392 );
393 }
394 }
395
401 public static function clearCache(): void
402 {
403 if (!static::isAllowedClearCache() || empty(self::$priceTypeIds))
404 {
405 return;
406 }
407 foreach (self::$priceTypeIds as $priceType)
408 {
409 Product\Price::clearRoundRulesCache($priceType);
410 }
411 unset($priceType);
412 static::clearPriceTypeIds();
413 }
414
421 public static function deleteByPriceType(string|int $priceType): void
422 {
423 $priceType = (int)$priceType;
424 if ($priceType <= 0)
425 {
426 return;
427 }
428 $conn = Main\Application::getConnection();
429 $helper = $conn->getSqlHelper();
430 $conn->queryExecute(
431 'delete from '.$helper->quote(self::getTableName()).' where '.$helper->quote('CATALOG_GROUP_ID').' = '.$priceType
432 );
433 unset($helper, $conn);
434 Product\Price::clearRoundRulesCache($priceType);
435 }
436
443 public static function getRoundTypes(bool $full = false): array
444 {
445 if ($full)
446 {
447 return [
448 self::ROUND_MATH => Loc::getMessage('ROUNDING_TYPE_ROUND_MATH'),
449 self::ROUND_UP => Loc::getMessage('ROUNDING_TYPE_ROUND_UP'),
450 self::ROUND_DOWN => Loc::getMessage('ROUNDING_TYPE_ROUND_DOWN'),
451 ];
452 }
453 return [
457 ];
458 }
459
466 public static function getPresetRoundingValues(): array
467 {
468 return [
469 0.0001,
470 0.001,
471 0.005,
472 0.01,
473 0.02,
474 0.05,
475 0.1,
476 0.2,
477 0.5,
478 1,
479 2,
480 5,
481 10,
482 20,
483 50,
484 100,
485 200,
486 500,
487 1000,
488 5000
489 ];
490 }
491
500 protected static function setUserId(array &$result, array $data, array $keys): void
501 {
502 static $currentUserID = false;
503 if ($currentUserID === false)
504 {
505 global $USER;
507 $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() : null);
508 }
509 foreach ($keys as $index)
510 {
511 $setField = true;
512 if (array_key_exists($index, $data))
513 {
514 $setField = ($data[$index] !== null && (int)$data[$index] <= 0);
515 }
516
517 if ($setField)
518 {
519 $result[$index] = $currentUserID;
520 }
521 }
522 unset($index);
523 }
524
533 protected static function setTimestamp(array &$result, array $data, array $keys): void
534 {
535 foreach ($keys as $index)
536 {
537 $setField = true;
538 if (array_key_exists($index, $data))
539 {
540 $setField = ($data[$index] !== null && !is_object($data[$index]));
541 }
542
543 if ($setField)
544 {
545 $result[$index] = new Main\Type\DateTime();
546 }
547 }
548 unset($index);
549 }
550}
static onAfterUpdate(Event $event)
Definition rounding.php:289
static onDelete(Event $event)
Definition rounding.php:300
static onAfterAdd(Event $event)
Definition rounding.php:202
static onBeforeAdd(Event $event)
Definition rounding.php:164
static deleteByPriceType(string|int $priceType)
Definition rounding.php:421
static getRoundTypes(bool $full=false)
Definition rounding.php:443
static onAfterDelete(Event $event)
Definition rounding.php:328
static setUserId(array &$result, array $data, array $keys)
Definition rounding.php:500
static setTimestamp(array &$result, array $data, array $keys)
Definition rounding.php:533
static onBeforeUpdate(Event $event)
Definition rounding.php:220
static onUpdate(Event $event)
Definition rounding.php:256
static setPriceTypeIds(string|int|array $priceTypes)
Definition rounding.php:379
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29