1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
shipmentpropertyvalue.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Controller;
4
5use Bitrix\Main\DB\SqlExpression;
6use Bitrix\Main\Engine\Response\DataType\Page;
7use Bitrix\Main\Entity\ExpressionField;
8use Bitrix\Main\Error;
9use Bitrix\Sale;
10use Bitrix\Main\UI\PageNavigation;
11use Bitrix\Sale\Result;
12use Bitrix\Main\Entity\ReferenceField;
13
15{
16 //region Actions
17 public function getFieldsAction()
18 {
19 $view = $this->getViewManager()->getView($this);
20
21 return [
22 'PROPERTY_VALUE' => $view->prepareFieldInfos($view->getFields()),
23 ];
24 }
25
26 public function modifyAction(array $fields)
27 {
28 $shipmentId = isset($fields['SHIPMENT']['ID']) ? (int)$fields['SHIPMENT']['ID'] : 0;
29 if (!$shipmentId)
30 {
31 $this->addError(new Error('Shipment identifier is not specified', 201040400006));
32
33 return null;
34 }
35
36 $shipment = Sale\Repository\ShipmentRepository::getInstance()->getById($shipmentId);
37 if (!$shipment)
38 {
39 $this->addError(new Error('Shipment has not been found', 201040400007));
40
41 return null;
42 }
43
44 $propertyValuesList =
45 (
46 isset($fields['SHIPMENT']['PROPERTY_VALUES'])
47 && is_array($fields['SHIPMENT']['PROPERTY_VALUES'])
48 )
49 ? $fields['SHIPMENT']['PROPERTY_VALUES']
50 : []
51 ;
52 $propertyCollection = $shipment->getPropertyCollection();
53
54 foreach ($propertyValuesList as $propertyValueItem)
55 {
56 $propertyValue = $propertyCollection->getItemByOrderPropertyId($propertyValueItem['SHIPMENT_PROPS_ID']);
57 if (!$propertyValue)
58 {
59 continue;
60 }
61
62 $propertyValue->setValue($propertyValueItem['VALUE']);
63 }
64
65 $orderSaveResult = $shipment->getOrder()->save();
66 if (!$orderSaveResult->isSuccess())
67 {
68 $this->addError(new Error('Order save error', 201040400008));
69
70 return null;
71 }
72
73 return [
74 'PROPERTY_VALUES' => $shipment->toArray()['PROPERTIES'],
75 ];
76 }
77
78 public function deleteAction(int $id): ?bool
79 {
80 $propertyValue = $this->getShipmentPropertyValueById($id);
81 if (!$propertyValue)
82 {
83 return null;
84 }
85
86 $propertyValueDeleteResult = $propertyValue->delete();
87 if (!$propertyValueDeleteResult->isSuccess())
88 {
89 $this->addError(new Error('Property value delete error', 201040400004));
90
91 return null;
92 }
93
94 $orderSaveResult = $propertyValue->getCollection()->getOrder()->save();
95 if (!$orderSaveResult)
96 {
97 $this->addError(new Error('Order save error', 201040400005));
98
99 return null;
100 }
101
102 return true;
103 }
104
105 public function getAction(int $id): ?array
106 {
107 $propertyValue = $this->getShipmentPropertyValueById($id);
108 if (!$propertyValue)
109 {
110 return null;
111 }
112
113 return [
114 'PROPERTY_VALUE' => current(
115 array_filter(
116 $propertyValue->getCollection()->toArray(),
117 static function ($item) use ($propertyValue)
118 {
119 return (int)$propertyValue->getPropertyId() === (int)$item['ORDER_PROPS_ID'];
120 }
121 )
122 ),
123 ];
124 }
125
126 public function listAction(
127 PageNavigation $pageNavigation,
128 array $select = [],
129 array $filter = [],
130 array $order = []
131 ): Page
132 {
133 $select = empty($select) ? ['*'] : $select;
134 $order = empty($order) ? ['ID'=>'ASC'] : $order;
135 $runtime = [
136 new ReferenceField(
137 'ORDER_PROPS',
138 '\Bitrix\Sale\Internals\OrderPropsTable',
139 [
140 '=this.ORDER_PROPS_ID' => 'ref.ID',
141 '=ref.ENTITY_TYPE' => new SqlExpression('?s', Sale\Registry::ENTITY_SHIPMENT),
142 ],
143 [
144 'join_type' => 'INNER',
145 ]
146 )
147 ];
148
149 $propertyValues = Sale\ShipmentPropertyValue::getList(
150 [
151 'select' => $select,
152 'filter' => $filter,
153 'order' => $order,
154 'offset' => $pageNavigation->getOffset(),
155 'limit' => $pageNavigation->getLimit(),
156 'runtime' => $runtime,
157 ]
158 )->fetchAll();
159
160 return new Page('PROPERTY_VALUES', $propertyValues, static function() use ($select, $filter, $runtime)
161 {
163 'select' => ['CNT'],
164 'filter' => $filter,
165 'runtime' => [new ExpressionField('CNT', 'COUNT(ID)')]
166 ])->fetch()['CNT'];
167 });
168 }
169 //end region
170
171 protected function checkModifyPermissionEntity()
172 {
173 $r = new Result();
174
175 $saleModulePermissions = self::getApplication()->GetGroupRight('sale');
176 if ($saleModulePermissions < 'W')
177 {
178 $r->addError(new Error('Access Denied', 200040300020));
179 }
180
181 return $r;
182 }
183
184 protected function checkReadPermissionEntity()
185 {
186 $r = new Result();
187
188 $saleModulePermissions = self::getApplication()->GetGroupRight('sale');
189 if ($saleModulePermissions === 'D')
190 {
191 $r->addError(new Error('Access Denied', 200040300010));
192 }
193
194 return $r;
195 }
196
197 protected function checkPermissionEntity($name, $arguments = [])
198 {
199 $name = mb_strtolower($name);
200
201 if ($name === 'modify')
202 {
203 return $this->checkModifyPermissionEntity();
204 }
205
206 return parent::checkPermissionEntity($name);
207 }
208
209 private function getShipmentPropertyValueById(int $id): ?Sale\ShipmentPropertyValue
210 {
212
214 $propertyValueClass = $registry->getShipmentPropertyValueClassName();
215
216 $shipmentPropertiesList = $propertyValueClass::getList([
217 'select' => [
218 'ORDER_ID',
219 'ENTITY_ID',
220 ],
221 'filter' => [
222 'ID' => $id,
223 'ENTITY_TYPE' => Sale\Registry::ENTITY_SHIPMENT,
224 ],
225 ]);
226
227 if (!$shipmentPropertyRow = $shipmentPropertiesList->fetch())
228 {
229 $this->addError(new Error('Property value has not been found', 201040400001));
230
231 return null;
232 }
233
235 $orderClass = $registry->getOrderClassName();
236
238 $shipment = $orderClass::load($shipmentPropertyRow['ORDER_ID'])
239 ->getShipmentCollection()
240 ->getItemById($shipmentPropertyRow['ENTITY_ID'])
241 ;
242
243 if (!$shipment)
244 {
245 $this->addError(new Error('Shipment has not been found', 20104040002));
246
247 return null;
248 }
249
251 $propertyValue = $shipment->getPropertyCollection()->getItemById($id);
252 if (!$propertyValue)
253 {
254 $this->addError(new Error('Property value has not been found', 201040400003));
255
256 return null;
257 }
258
259 return $propertyValue;
260 }
261}
static getList(array $parameters)
Определения entity.php:78
Определения error.php:15
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Определения shipmentpropertyvalue.php:126
checkPermissionEntity($name, $arguments=[])
Определения shipmentpropertyvalue.php:197
static getInstance($type)
Определения registry.php:183
const ENTITY_SHIPMENT
Определения registry.php:19
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
$name
Определения menu_edit.php:35
Определения aliases.php:54
$order
Определения payment.php:8
$saleModulePermissions
Определения tools.php:21
$fields
Определения yandex_run.php:501