1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
vat.php
См. документацию.
1<?php
2namespace Bitrix\Catalog;
3
4use Bitrix\Main\Localization\Loc;
5use Bitrix\Main\ORM;
6use Bitrix\Main\Type;
7
36
38{
44 public static function getTableName(): string
45 {
46 return 'b_catalog_vat';
47 }
48
54 public static function getMap(): array
55 {
56 return [
57 'ID' => new ORM\Fields\IntegerField(
58 'ID',
59 [
60 'primary' => true,
61 'autocomplete' => true,
62 'title' => Loc::getMessage('VAT_ENTITY_ID_FIELD'),
63 ]
64 ),
65 'TIMESTAMP_X' => new ORM\Fields\DatetimeField(
66 'TIMESTAMP_X',
67 [
68 'required' => true,
69 'default_value' => function()
70 {
71 return new Type\DateTime();
72 },
73 'title' => Loc::getMessage('VAT_ENTITY_TIMESTAMP_X_FIELD'),
74 ]
75 ),
76 'ACTIVE' => new ORM\Fields\BooleanField(
77 'ACTIVE',
78 [
79 'values' => [
80 'N',
81 'Y',
82 ],
83 'default_value' => 'Y',
84 'title' => Loc::getMessage('VAT_ENTITY_ACTIVE_FIELD'),
85 ]
86 ),
87 'SORT' => new ORM\Fields\IntegerField(
88 'SORT',
89 [
90 'column_name' => 'C_SORT',
91 'default_value' => 100,
92 'title' => Loc::getMessage('VAT_ENTITY_SORT_FIELD'),
93 ]
94 ),
95 'NAME' => new ORM\Fields\StringField(
96 'NAME',
97 [
98 'required' => true,
99 'validation' => function()
100 {
101 return [
103 ];
104 },
105 'title' => Loc::getMessage('VAT_ENTITY_NAME_FIELD'),
106 ]
107 ),
108 'RATE' => new ORM\Fields\FloatField(
109 'RATE',
110 [
111 'nullable' => true,
112 'title' => Loc::getMessage('VAT_ENTITY_RATE_FIELD'),
113 ]
114 ),
115 'EXCLUDE_VAT' => new ORM\Fields\BooleanField(
116 'EXCLUDE_VAT',
117 [
118 'values' => [
119 'N',
120 'Y',
121 ],
122 'default_value' => 'N',
123 'title' => Loc::getMessage('VAT_ENTITY_ACTIVE_FIELD'),
124 ]
125 ),
126 'XML_ID' => new ORM\Fields\StringField(
127 'XML_ID',
128 [
129 'required' => false,
130 'validation' => function()
131 {
132 return [
134 ];
135 },
136 'title' => Loc::getMessage('VAT_ENTITY_XML_ID_FIELD'),
137 ]
138 ),
139 ];
140 }
141
148 public static function onBeforeAdd(ORM\Event $event): ORM\EventResult
149 {
151 $fields = $event->getParameter('fields');
152
153 if (isset($fields['EXCLUDE_VAT']) && $fields['EXCLUDE_VAT'] === 'Y')
154 {
155 if (static::isExistsExcludeVat())
156 {
157 $result->addError(
158 new ORM\EntityError(Loc::getMessage('VAT_ENTITY_ERR_EXCLUDE_VAT_ALREADY_EXISTS'))
159 );
160
161 return $result;
162 }
163
164 $result->modifyFields([
165 'RATE' => null,
166 ]);
167 }
168
169 return $result;
170 }
171
178 public static function onBeforeUpdate(ORM\Event $event): ORM\EventResult
179 {
181 $fields = $event->getParameter('fields');
182
183 if (isset($fields['EXCLUDE_VAT']) && $fields['EXCLUDE_VAT'] === 'Y')
184 {
185 $id = (int)$event->getParameter('primary')['ID'];
186
187 $excludeId = static::getExcludeVatId();
188
189 if ($excludeId !== null && $excludeId !== $id)
190 {
191 $result->addError(
192 new ORM\EntityError(Loc::getMessage('VAT_ENTITY_ERR_EXCLUDE_VAT_ALREADY_EXISTS'))
193 );
194
195 return $result;
196 }
197
198 $result->modifyFields([
199 'RATE' => null,
200 ]);
201 }
202
203 return $result;
204 }
205
213 public static function getActiveVatIdByRate(?float $rate, bool $create = false): ?int
214 {
215 if (
216 is_float($rate)
217 && ($rate < 0 || $rate > 100)
218 )
219 {
220 return null;
221 }
222 $row = static::getList([
223 'select' => [
224 'ID',
225 ],
226 'filter' => [
227 '=ACTIVE' => 'Y',
228 '=RATE' => $rate,
229 ],
230 ])->fetch();
231 if (!empty($row))
232 {
233 return (int)$row['ID'];
234 }
235
236 if ($create)
237 {
238 $result = static::add([
239 'ACTIVE' => 'Y',
240 'NAME' => $rate . '%',
241 'RATE' => $rate,
242 ]);
243
244 return $result->isSuccess() ? (int)$result->getId() : null;
245 }
246
247 return null;
248 }
249
255 public static function isExistsExcludeVat(): bool
256 {
257 return (static::getExcludeVatId() !== null);
258 }
259
265 public static function getExcludeVatId(): ?int
266 {
267 $iterator = static::getList([
268 'select' => [
269 'ID',
270 ],
271 'filter' => [
272 '=EXCLUDE_VAT' => 'Y',
273 ],
274 'limit' => 1,
275 ]);
276 $row = $iterator->fetch();
277 unset($iterator);
278
279 return (!empty($row) ? (int)$row['ID'] : null);
280 }
281}
Определения vat.php:38
static getMap()
Определения vat.php:54
static getExcludeVatId()
Определения vat.php:265
static onBeforeAdd(ORM\Event $event)
Определения vat.php:148
static getActiveVatIdByRate(?float $rate, bool $create=false)
Определения vat.php:213
static onBeforeUpdate(ORM\Event $event)
Определения vat.php:178
static isExistsExcludeVat()
Определения vat.php:255
static getTableName()
Определения vat.php:44
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения buffer.php:3
$event
Определения prolog_after.php:141
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501