Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
vat.php
1
<?php
2
namespace
Bitrix\Catalog
;
3
4
use
Bitrix\Main\Localization\Loc
;
5
use
Bitrix\Main\ORM
;
6
use
Bitrix\Main\Type
;
7
37
class
VatTable
extends
ORM\Data\DataManager
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
[
102
new
ORM\Fields\Validators\LengthValidator
(
null
, 50),
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
[
133
new
ORM\Fields\Validators\LengthValidator
(
null
, 255),
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
{
150
$result =
new
ORM\EventResult;
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
{
180
$result =
new
ORM\EventResult;
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
($rate < 0 || $rate > 100)
216
{
217
return
null
;
218
}
219
$row = static::getList([
220
'select'
=> [
221
'ID'
,
222
],
223
'filter'
=> [
224
'=ACTIVE'
=>
'Y'
,
225
'=RATE'
=> $rate,
226
],
227
])->fetch();
228
if
(!empty($row))
229
{
230
return
(
int
)$row[
'ID'
];
231
}
232
233
if
($create)
234
{
235
$result = static::add([
236
'ACTIVE'
=>
'Y'
,
237
'NAME'
=> $rate .
'%'
,
238
'RATE'
=> $rate,
239
]);
240
241
return
$result->isSuccess() ? (int)$result->getId() :
null
;
242
}
243
244
return
null
;
245
}
246
252
public
static
function
isExistsExcludeVat
(): bool
253
{
254
return
(static::getExcludeVatId() !==
null
);
255
}
256
262
public
static
function
getExcludeVatId
(): ?int
263
{
264
$iterator = static::getList([
265
'select'
=> [
266
'ID'
,
267
],
268
'filter'
=> [
269
'=EXCLUDE_VAT'
=>
'Y'
,
270
],
271
'limit'
=> 1,
272
]);
273
$row = $iterator->fetch();
274
unset($iterator);
275
276
return
(!empty($row) ? (
int
)$row[
'ID'
] :
null
);
277
}
278
}
Bitrix\Catalog\Model\Event
Definition
event.php:11
Bitrix\Catalog\Model\EventResult
Definition
eventresult.php:7
Bitrix\Catalog\Model\EventResult\modifyFields
modifyFields(array $fields)
Definition
eventresult.php:23
Bitrix\Catalog\VatTable
Definition
vat.php:38
Bitrix\Catalog\VatTable\getMap
static getMap()
Definition
vat.php:54
Bitrix\Catalog\VatTable\getExcludeVatId
static getExcludeVatId()
Definition
vat.php:262
Bitrix\Catalog\VatTable\onBeforeAdd
static onBeforeAdd(ORM\Event $event)
Definition
vat.php:148
Bitrix\Catalog\VatTable\getActiveVatIdByRate
static getActiveVatIdByRate(float $rate, bool $create=false)
Definition
vat.php:213
Bitrix\Catalog\VatTable\onBeforeUpdate
static onBeforeUpdate(ORM\Event $event)
Definition
vat.php:178
Bitrix\Catalog\VatTable\isExistsExcludeVat
static isExistsExcludeVat()
Definition
vat.php:252
Bitrix\Catalog\VatTable\getTableName
static getTableName()
Definition
vat.php:44
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\DataManager
Definition
datamanager.php:33
Bitrix\Main\ORM\EntityError
Definition
entityerror.php:12
Bitrix\Main\ORM\Fields\BooleanField
Definition
booleanfield.php:20
Bitrix\Main\ORM\Fields\DatetimeField
Definition
datetimefield.php:22
Bitrix\Main\ORM\Fields\FloatField
Definition
floatfield.php:20
Bitrix\Main\ORM\Fields\IntegerField
Definition
integerfield.php:20
Bitrix\Main\ORM\Fields\StringField
Definition
stringfield.php:20
Bitrix\Main\ORM\Fields\Validators\LengthValidator
Definition
lengthvalidator.php:19
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Sale\Internals\Fields
Definition
fields.php:6
Bitrix\Catalog
Bitrix\Main\ORM
Bitrix\Main\Type
Definition
collection.php:2
modules
catalog
lib
vat.php
Создано системой
1.10.0