Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vat.php
1<?php
3
7
8class Vat extends Entity
9{
15 public static function getTabletClassName(): string
16 {
17 return '\Bitrix\Catalog\VatTable';
18 }
19
25 protected static function getDefaultCachedFieldList(): array
26 {
27 return [
28 'ID',
29 'RATE',
30 'EXCLUDE_VAT',
31 'ACTIVE',
32 ];
33 }
34
43 protected static function prepareForAdd(ORM\Data\AddResult $result, $id, array &$data): void
44 {
45 $fields = $data['fields'];
46 parent::prepareForAdd($result, $id, $fields);
47 if (!$result->isSuccess())
48 {
49 return;
50 }
51
52 static $defaultValues = null,
53 $blackList = null;
54
55 if ($defaultValues === null)
56 {
57 $defaultValues = [
58 'ACTIVE' => 'Y',
59 'SORT' => 100,
60 'NAME' => null,
61 'EXCLUDE_VAT' => 'N',
62 'RATE' => null,
63 'XML_ID' => null,
64 ];
65
66 $blackList = [
67 'ID' => true
68 ];
69 }
70
71 $fields = array_merge(
72 $defaultValues,
73 array_diff_key($fields, $blackList)
74 );
75
76 if ($fields['ACTIVE'] !== 'N')
77 {
78 $fields['ACTIVE'] = $defaultValues['ACTIVE'];
79 }
80
81 $fields['SORT'] = static::prepareIntValue($fields['SORT']);
82 if ($fields['SORT'] === null || $fields['SORT'] <= 0)
83 {
84 $fields['SORT'] = $defaultValues['SORT'];
85 }
86
87 $fields['NAME'] = static::prepareStringValue($fields['NAME']);
88 if ($fields['NAME'] === null)
89 {
90 $result->addError(new ORM\EntityError(
91 Loc::getMessage('BX_CATALOG_MODEL_VAT_ERR_WRONG_NAME')
92 ));
93 }
94
95 if ($fields['EXCLUDE_VAT'] !== 'Y')
96 {
97 $fields['EXCLUDE_VAT'] = $defaultValues['EXCLUDE_VAT'];
98 }
99 if ($fields['EXCLUDE_VAT'] === 'Y')
100 {
101 $fields['RATE'] = null;
102 }
103 else
104 {
105 $fields['RATE'] = static::prepareFloatValue($fields['RATE']);
106 if ($fields['RATE'] === null || $fields['RATE'] < 0)
107 {
108 $result->addError(new ORM\EntityError(
109 Loc::getMessage('BX_CATALOG_MODEL_VAT_ERR_WRONG_RATE')
110 ));
111 }
112 }
113
114 $fields['XML_ID'] = static::prepareStringValue($fields['XML_ID']);
115 if ($fields['XML_ID'] !== null)
116 {
117 $fields['XML_ID'] = mb_substr($fields['XML_ID'], 0, 255);
118 }
119
120 if ($result->isSuccess())
121 {
122 $fields['TIMESTAMP_X'] = new Main\Type\DateTime();
123 $data['fields'] = $fields;
124 }
125 unset($fields);
126 }
127
136 protected static function prepareForUpdate(ORM\Data\UpdateResult $result, $id, array &$data): void
137 {
138 $id = (int)$id;
139 if ($id <= 0)
140 {
141 $result->addError(new ORM\EntityError(
142 Loc::getMessage('BX_CATALOG_MODEL_VAT_ERR_WRONG_VAT_ID')
143 ));
144 return;
145 }
146
147 $fields = $data['fields'];
148 parent::prepareForUpdate($result, $id, $fields);
149 if (!$result->isSuccess())
150 {
151 return;
152 }
153
154 $blackList = [
155 'ID' => true
156 ];
157
158 $fields = array_diff_key($fields, $blackList);
159
160 if (array_key_exists('ACTIVE', $fields))
161 {
162 if (
163 $fields['ACTIVE'] !== 'Y'
164 && $fields['ACTIVE'] !== 'N'
165 )
166 {
167 unset($fields['ACTIVE']);
168 }
169 }
170
171 if (array_key_exists('NAME', $fields))
172 {
173 $value = static::prepareStringValue($fields['NAME']);
174 if ($value === null)
175 {
176 $result->addError(new ORM\EntityError(
177 Loc::getMessage('BX_CATALOG_MODEL_VAT_ERR_WRONG_NAME')
178 ));
179 }
180 }
181
182 if (array_key_exists('SORT', $fields))
183 {
184 $fields['SORT'] = static::prepareIntValue($fields['SORT']);
185 if ($fields['SORT'] === null || $fields['SORT'] <= 0)
186 {
187 unset($fields['SORT']);
188 }
189 }
190
191 if (array_key_exists('EXCLUDE_VAT', $fields))
192 {
193 if (
194 $fields['EXCLUDE_VAT'] !== 'Y'
195 && $fields['EXCLUDE_VAT'] !== 'N'
196 )
197 {
198 unset($fields['EXCLUDE_VAT']);
199 }
200 }
201
202
203 if (array_key_exists('RATE', $fields))
204 {
205 $excludeVat = 'N';
206 if (isset($fields['EXCLUDE_VAT']))
207 {
208 $excludeVat = $fields['EXCLUDE_VAT'];
209 }
210 else
211 {
212 $cache = static::getCacheItem($id, true);
213 if (!empty($cache))
214 {
215 $excludeVat = $cache['EXCLUDE_VAT'];
216 }
217 unset($cache);
218 }
219
220 if ($excludeVat === 'Y')
221 {
222 $fields['RATE'] = null;
223 }
224 else
225 {
226 $fields['RATE'] = static::prepareFloatValue($fields['RATE']);
227 if ($fields['RATE'] === null || $fields['RATE'] < 0)
228 {
229 $result->addError(new ORM\EntityError(
230 Loc::getMessage('BX_CATALOG_MODEL_VAT_ERR_WRONG_RATE')
231 ));
232 }
233 }
234 unset($excludeVat);
235 }
236 else
237 {
238 if (
239 isset($fields['EXCLUDE_VAT'])
240 && $fields['EXCLUDE_VAT'] === 'Y'
241 )
242 {
243 $fields['RATE'] = null;
244 }
245 }
246
247 if (array_key_exists('XML_ID', $fields))
248 {
249 $fields['XML_ID'] = static::prepareStringValue($fields['XML_ID']);
250 if ($fields['XML_ID'] !== null)
251 {
252 $fields['XML_ID'] = mb_substr($fields['XML_ID'], 0, 255);
253 }
254 }
255
256 if ($result->isSuccess())
257 {
258 $fields['TIMESTAMP_X'] = new Main\Type\DateTime();
259 $data['fields'] = $fields;
260 }
261 unset($fields);
262 }
263}
static prepareForUpdate(ORM\Data\UpdateResult $result, $id, array &$data)
Definition vat.php:136
static getDefaultCachedFieldList()
Definition vat.php:25
static prepareForAdd(ORM\Data\AddResult $result, $id, array &$data)
Definition vat.php:43
static getTabletClassName()
Definition vat.php:15
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29