Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
status.php
1
<?php
2
3
4
namespace
Bitrix\Sale\Controller
;
5
6
7
use
Bitrix\Main\Engine\Response\DataType\Page
;
8
use
Bitrix\Main\Error
;
9
use
Bitrix\Main\Localization\LanguageTable
;
10
use
Bitrix\Main\Localization\Loc
;
11
use
Bitrix\Main\TaskTable
;
12
use
Bitrix\Main\UI\PageNavigation
;
13
use
Bitrix\Sale\DeliveryStatus
;
14
use
Bitrix\Sale\Internals\StatusGroupTaskTable
;
15
use
Bitrix\Sale\Internals\StatusLangTable
;
16
use
Bitrix\Sale\Internals\StatusTable
;
17
use
Bitrix\Sale\OrderStatus
;
18
use
Bitrix\Sale\Result
;
19
use phpDocumentor\Reflection\Types\This;
20
21
class
Status
extends
ControllerBase
22
{
23
//region Actions
24
public
function
getFieldsAction
()
25
{
26
$view = $this->getViewManager()
27
->getView($this);
28
29
return
[
'STATUS'
=>$view->prepareFieldInfos(
30
$view->getFields()
31
)];
32
}
33
34
protected
function
getEntityTable
():
StatusTable
35
{
36
return
new
StatusTable
();
37
}
38
39
public
function
addAction
($fields)
40
{
41
$r =
new
Result
();
42
43
$res = $this->
exists
($fields[
'ID'
]);
44
if
($res->isSuccess() ==
false
)
45
{
46
$r = $this->
validate
($fields);
47
if
($r->isSuccess())
48
{
49
$fields = $this->
prepareFields
($fields);
50
$r = $this->
getEntityTable
()::add($fields);
51
}
52
}
53
else
54
{
55
$r->addError(
new
Error
(
'Duplicate entry for key [id]'
,201350000001));
56
}
57
58
if
(!$r->isSuccess())
59
{
60
$this->addErrors($r->getErrors());
61
return
null
;
62
}
63
else
64
{
65
return
[
'STATUS'
=>$this->
get
($fields[
'ID'
])];
66
}
67
}
68
69
public
function
updateAction
($id, array $fields)
70
{
71
$r = $this->
exists
($id);
72
if
($r->isSuccess())
73
{
74
$fields[
'ID'
] = $id;
75
76
$r = $this->
validate
($fields);
77
if
($r->isSuccess())
78
{
79
$r = $this->
getEntityTable
()::update($id, $fields);
80
}
81
}
82
83
if
($r->isSuccess())
84
{
85
return
[
'STATUS'
=>$this->
get
($id)];
86
}
87
else
88
{
89
$this->addErrors($r->getErrors());
90
return
null
;
91
}
92
}
93
94
public
function
deleteAction
($id)
95
{
96
$r = $this->
exists
($id);
97
if
($r->isSuccess())
98
{
99
if
(in_array($id, [
100
OrderStatus::getInitialStatus
(),
101
OrderStatus::getFinalStatus
(),
102
DeliveryStatus::getInitialStatus
(),
103
DeliveryStatus::getFinalStatus
()]))
104
{
105
$r->addError(
new
Error
(
'delete status type loced'
,201350000002));
106
}
107
108
if
($r->isSuccess())
109
{
110
$r = $this->
getEntityTable
()::
delete
($id);
111
}
112
}
113
114
if
($r->isSuccess())
115
{
116
return
true
;
117
}
118
else
119
{
120
$this->addErrors($r->getErrors());
121
return
null
;
122
}
123
}
124
125
public
function
getAction
($id)
126
{
127
$r = $this->
exists
($id);
128
if
($r->isSuccess())
129
{
130
return
[
'STATUS'
=>$this->
get
($id)];
131
}
132
else
133
{
134
$this->addErrors($r->getErrors());
135
return
null
;
136
}
137
}
138
139
public
function
listAction
(
PageNavigation
$pageNavigation, array $select = [], array $filter = [], array $order = []):
Page
140
{
141
$select = empty($select) ? [
'*'
] : $select;
142
$order = empty($order) ? [
'ID'
=>
'ASC'
] : $order;
143
144
$items = $this->
getEntityTable
()::getList(
145
[
146
'select'
=> $select,
147
'filter'
=> $filter,
148
'order'
=> $order,
149
'offset'
=> $pageNavigation->
getOffset
(),
150
'limit'
=> $pageNavigation->
getLimit
(),
151
]
152
)->fetchAll();
153
154
return
new
Page
(
'STATUSES'
, $items,
function
() use ($filter)
155
{
156
return
$this->
getEntityTable
()::getCount([$filter]);
157
});
158
}
159
//endregion
160
161
protected
function
prepareFields
($fields)
162
{
163
if
(!isset($fields[
'XML_ID'
]) && $fields[
'XML_ID'
] ==
''
)
164
{
165
$fields[
'XML_ID'
] = $this->
getEntityTable
()::generateXmlId();
166
}
167
168
$fields[
'COLOR'
] = isset($fields[
'COLOR'
]) ? $fields[
'COLOR'
]:
''
;
169
$fields[
'NOTIFY'
] = isset($fields[
'NOTIFY'
]) && $fields[
'NOTIFY'
]==
'Y'
?
'Y'
:
'N'
;
170
$fields[
'SORT'
] = isset($fields[
'SORT'
]) ? $fields[
'SORT'
]:0;
171
172
return
$fields;
173
}
174
175
protected
function
validate
(array $fields)
176
{
177
$r =
new
Result
();
178
179
if
(!in_array($fields[
'TYPE'
], [
180
OrderStatus::TYPE
,
181
DeliveryStatus::TYPE
182
]))
183
{
184
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_TYPE_OUT_OF_RANGE'
), 201350000003));
185
}
186
187
if
(trim($fields[
'ID'
])==
''
)
188
{
189
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_TYPE_ID_EMPTY'
), 201350000004));
190
}
191
elseif(mb_strlen($fields[
'ID'
]) > 2)
192
{
193
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_TYPE_STRLEN'
), 201350000005));
194
}
195
196
/* TODO: check is_latin()
197
* if(!is_latin($fields['TYPE']))
198
{
199
$r->addError(new Error('', 'ERROR_STATUS_TYPE_LATIN_ONLY'));
200
}*/
201
202
if
($r->isSuccess())
203
{
204
if
($status = $this->
get
($fields[
'ID'
]))
205
{
206
$lockedType = $this->
getLockedStatusType
($fields[
'ID'
]);
207
if
($lockedType<>
''
&& $lockedType!=$fields[
'TYPE'
])
208
{
209
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_WRONG_TYPE'
),201350000006));
210
}
211
212
if
($status[
'TYPE'
] != $fields[
'TYPE'
])
213
{
214
if
($status[
'TYPE'
] == \
Bitrix
\
Sale
\
OrderStatus::TYPE
)
215
{
216
if
(\
Bitrix
\
Sale
\Internals\OrderTable::getList([
217
'select'
=>[
'ID'
],
218
'filter'
=>[
'STATUS_ID'
=>$status[
'ID'
]],
219
'limit'
=>1
220
])->fetch())
221
{
222
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_TYPE_ORDER_EXISTS'
),201350000007));
223
}
224
}
225
else
226
{
227
if
(\
Bitrix
\
Sale
\Internals\ShipmentTable::getList([
228
'select'
=>[
'ID'
],
229
'filter'
=>[
'STATUS_ID'
=>$status[
'ID'
]],
230
'limit'
=>1
231
])->fetch())
232
{
233
$r->addError(
new
Error
(
Loc::getMessage
(
'CONTROLLER_ERROR_STATUS_TYPE_SHIPMENT_EXISTS'
),201350000008));
234
}
235
}
236
}
237
}
238
}
239
240
return
$r;
241
}
242
243
protected
function
getLockedStatusType
($statusId)
244
{
245
$lockedStatusList = [
246
OrderStatus::TYPE
=>[
247
OrderStatus::getInitialStatus
(),
248
OrderStatus::getFinalStatus
()
249
],
250
DeliveryStatus::TYPE
=>[
251
DeliveryStatus::getInitialStatus
(),
252
DeliveryStatus::getFinalStatus
()
253
]
254
];
255
256
foreach
($lockedStatusList as $lockStatusType=>$lockStatusIdList)
257
{
258
foreach
($lockStatusIdList as $lockStatusId)
259
{
260
if
($lockStatusId == $statusId)
261
{
262
return
$lockStatusType;
263
}
264
}
265
}
266
return
''
;
267
}
268
269
protected
function
get
($id)
270
{
271
return
$this->
getEntityTable
()::getById($id)->fetch();
272
}
273
274
protected
function
exists
($id)
275
{
276
$r =
new
Result
();
277
if
(isset($this->
get
($id)[
'ID'
]) ==
false
)
278
$r->addError(
new
Error
(
'status is not exists'
, 201340400001));
279
280
return
$r;
281
}
282
283
protected
function
checkModifyPermissionEntity
():
Result
284
{
285
$r =
new
Result
();
286
287
$saleModulePermissions =
self::getApplication
()->GetGroupRight(
"sale"
);
288
if
($saleModulePermissions <
"W"
)
289
{
290
$r->addError(
new
Error
(
'Access Denied'
, 200040300020));
291
}
292
return
$r;
293
}
294
295
protected
function
checkReadPermissionEntity
():
Result
296
{
297
$r =
new
Result
();
298
299
$saleModulePermissions =
self::getApplication
()->GetGroupRight(
"sale"
);
300
if
($saleModulePermissions ==
"D"
)
301
{
302
$r->addError(
new
Error
(
'Access Denied'
, 200040300010));
303
}
304
return
$r;
305
}
306
}
Bitrix\Main\DB\Result
Definition
result.php:19
Bitrix\Main\Engine\Response\DataType\Page
Definition
page.php:8
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Localization\LanguageTable
Definition
language.php:21
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\TaskTable
Definition
task.php:24
Bitrix\Main\UI\PageNavigation
Definition
pagenavigation.php:27
Bitrix\Main\UI\PageNavigation\getLimit
getLimit()
Definition
pagenavigation.php:247
Bitrix\Main\UI\PageNavigation\getOffset
getOffset()
Definition
pagenavigation.php:234
Bitrix\Sale\Controller\ControllerBase
Definition
controllerbase.php:15
Bitrix\Sale\Controller\ControllerBase\getApplication
static getApplication()
Definition
controllerbase.php:39
Bitrix\Sale\Controller\Status
Definition
status.php:22
Bitrix\Sale\Controller\Status\checkModifyPermissionEntity
checkModifyPermissionEntity()
Definition
status.php:283
Bitrix\Sale\Controller\Status\validate
validate(array $fields)
Definition
status.php:175
Bitrix\Sale\Controller\Status\checkReadPermissionEntity
checkReadPermissionEntity()
Definition
status.php:295
Bitrix\Sale\Controller\Status\exists
exists($id)
Definition
status.php:274
Bitrix\Sale\Controller\Status\getLockedStatusType
getLockedStatusType($statusId)
Definition
status.php:243
Bitrix\Sale\Controller\Status\getEntityTable
getEntityTable()
Definition
status.php:34
Bitrix\Sale\Controller\Status\updateAction
updateAction($id, array $fields)
Definition
status.php:69
Bitrix\Sale\Controller\Status\getFieldsAction
getFieldsAction()
Definition
status.php:24
Bitrix\Sale\Controller\Status\listAction
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition
status.php:139
Bitrix\Sale\Controller\Status\prepareFields
prepareFields($fields)
Definition
status.php:161
Bitrix\Sale\Controller\Status\deleteAction
deleteAction($id)
Definition
status.php:94
Bitrix\Sale\Controller\Status\getAction
getAction($id)
Definition
status.php:125
Bitrix\Sale\Controller\Status\addAction
addAction($fields)
Definition
status.php:39
Bitrix\Sale\DeliveryStatus
Definition
deliverystatus.php:13
Bitrix\Sale\DeliveryStatus\getInitialStatus
static getInitialStatus()
Definition
deliverystatus.php:19
Bitrix\Sale\DeliveryStatus\TYPE
const TYPE
Definition
deliverystatus.php:14
Bitrix\Sale\DeliveryStatus\getFinalStatus
static getFinalStatus()
Definition
deliverystatus.php:27
Bitrix\Sale\Internals\StatusGroupTaskTable
Definition
status_grouptask.php:29
Bitrix\Sale\Internals\StatusLangTable
Definition
status_lang.php:32
Bitrix\Sale\Internals\StatusTable
Definition
status.php:30
Bitrix\Sale\OrderStatus
Definition
orderstatus.php:13
Bitrix\Sale\OrderStatus\getInitialStatus
static getInitialStatus()
Definition
orderstatus.php:111
Bitrix\Sale\OrderStatus\TYPE
const TYPE
Definition
orderstatus.php:14
Bitrix\Sale\OrderStatus\getFinalStatus
static getFinalStatus()
Definition
orderstatus.php:119
Bitrix\Sale\Controller
Bitrix
Sale
modules
sale
lib
controller
status.php
Создано системой
1.10.0