Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketproperties.php
1<?php
2
3
5
6
11use Bitrix\Sale;
15
17{
18 public function getPrimaryAutoWiredParameter()
19 {
20 return new ExactParameter(
21 Sale\BasketPropertyItem::class,
22 'basketProperty',
23 function($className, $id) {
24 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
25
27 $basketPropertyClass = $registry->getBasketPropertyItemClassName();
28
29 $b = $basketPropertyClass::getList([
30 'select'=>['BASKET_ID'],
31 'filter'=>['ID'=>$id]
32 ]);
33
34 if($bRow = $b->fetch())
35 {
37 $basketClass = $registry->getBasketClassName();
38
39 $r = $basketClass::getList([
40 'select'=>['ORDER_ID'],
41 'filter'=>['ID'=>$bRow['BASKET_ID']]
42 ]);
43
44 if($row = $r->fetch())
45 {
47 $orderClass = $registry->getOrderClassName();
48
49 $order = $orderClass::load($row['ORDER_ID']);
50 $basket = $order->getBasket()->getItemByBasketCode($bRow['BASKET_ID']);
51 if ($basket)
52 {
53 $property = $basket->getPropertyCollection()->getItemById($id);
54 if ($property)
55 {
56 return $property;
57 }
58 }
59 }
60 }
61
62 $this->addError(new Error('basket property is not exists', 200240400003));
63 return null;
64 }
65 );
66 }
67
68 //region Actions
69 public function getFieldsAction()
70 {
71 $entity = new \Bitrix\Sale\Rest\Entity\BasketProperties();
72 return ['BASKET_PROPERTIES'=>$entity->prepareFieldInfos(
73 $entity->getFields()
74 )];
75 }
76
77 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
78 {
79 $select = empty($select) ? ['*'] : $select;
80 $order = empty($order) ? ['ID' => 'ASC'] : $order;
81
82 $items = \Bitrix\Sale\Internals\BasketPropertyTable::getList(
83 [
84 'select' => $select,
85 'filter' => $filter,
86 'order' => $order,
87 'offset' => $pageNavigation->getOffset(),
88 'limit' => $pageNavigation->getLimit(),
89 ]
90 )->fetchAll();
91
92 return new Page('BASKET_PROPERTIES', $items, function() use ($filter)
93 {
94 return count(
95 \Bitrix\Sale\Internals\BasketPropertyTable::getList(['filter'=>$filter])->fetchAll()
96 );
97 });
98 }
99
100 public function getAction(\Bitrix\Sale\BasketPropertyItem $basketProperty)
101 {
102 return ['BASKET_PROPERTY'=>$this->get($basketProperty)];
103 }
104
105 public function addAction(array $fields)
106 {
107 $result = new Result();
108
109 $basketId = $fields['BASKET_ID'];
110
111 unset($fields['BASKET_ID']);
112
113 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
114
116 $basketClass = $registry->getBasketClassName();
117
118 $r = $basketClass::getList([
119 'select'=>['ORDER_ID'],
120 'filter'=>['ID'=>$basketId]
121 ]);
122
123 if($row = $r->fetch())
124 {
126 $orderClass = $registry->getOrderClassName();
127
128 $order = $orderClass::load($row['ORDER_ID']);
129 $basketItem = $order->getBasket()->getItemByBasketCode($basketId);
130 if($basketItem instanceof \Bitrix\Sale\BasketItem)
131 {
133 $propertyCollection = $basketItem->getPropertyCollection();
135 $basketProperty = $propertyCollection->createItem();
136 $result = $basketProperty->setFields($fields);
137
138 if($result->isSuccess() && $result->hasWarnings() == false)
139 {
140 $r = $this->save($basketProperty);
141 if(!$r->isSuccess())
142 {
143 $result->addErrors($r->getErrors());
144 }
145 }
146 }
147 else
148 {
149 $result->addError(new Error('basket item not exists', 200240400001));
150 }
151 }
152 else
153 {
154 $result->addError(new Error('basket item not exists', 200240400002));
155 }
156
157 if(!$result->isSuccess())
158 {
159 $this->addErrors($result->getErrors());
160 return null;
161 }
162 elseif($result->hasWarnings())
163 {
164 $this->addErrors($result->getWarnings());
165 return null;
166 }
167 else
168 {
169 return ['BASKET_PROPERTY'=>$this->get($basketProperty)];
170 }
171 }
172
173 public function updateAction(\Bitrix\Sale\BasketPropertyItem $basketProperty, array $fields)
174 {
175 $r = $basketProperty->setFields($fields);
176
177 if($r->isSuccess() == false)
178 {
179 $this->addErrors($r->getErrors());
180 return null;
181 }
182 elseif($r->hasWarnings())
183 {
184 $this->addErrors($r->getWarnings());
185 return null;
186 }
187 else
188 {
189 $r = $this->save($basketProperty);
190 if(!$r->isSuccess())
191 {
192 $this->addErrors($r->getErrors());
193 return null;
194 }
195 else
196 {
197 return ['BASKET_PROPERTY'=>$this->get($basketProperty)];
198 }
199 }
200 }
201
202 public function deleteAction(\Bitrix\Sale\BasketPropertyItem $basketProperty)
203 {
204 $r = $basketProperty->delete();
205
206 if($r->isSuccess() == false)
207 {
208 $this->addErrors($r->getErrors());
209 return null;
210 }
211 elseif($r->hasWarnings())
212 {
213 $this->addErrors($r->getWarnings());
214 return null;
215 }
216 else
217 {
218 $r = $this->save($basketProperty);
219 if(!$r->isSuccess())
220 {
221 $this->addErrors($r->getErrors());
222 return null;
223 }
224 else
225 {
226 return true;
227 }
228 }
229 }
230 //endregion
231
232 protected function get(\Bitrix\Sale\BasketPropertyItem $basketProperty, array $fields=[])
233 {
235 $properties = $basketProperty->getCollection();
236 $basketItem = $properties->getBasketItem();
238 $basket = $basketItem->getCollection();
240 $order = $basket->getOrder();
241
242 $basketItems = $this->toArray($order, $fields)['ORDER']['BASKET_ITEMS'];
243 foreach ($basketItems as $item)
244 {
245 foreach ($item['PROPERTIES'] as $property)
246 {
247 if($property['ID']==$basketProperty->getId())
248 {
249 return $property;
250 }
251 }
252 }
253 return [];
254 }
255
256 private function save(\Bitrix\Sale\BasketPropertyItem $basketProperty)
257 {
258 $result = new Result();
260 $properties = $basketProperty->getCollection();
261 $basketItem = $properties->getBasketItem();
263 $basket = $basketItem->getCollection();
265 $order = $basket->getOrder();
266
267 $r = $order->save();
268 if(!$r->isSuccess())
269 {
270 $result->addErrors($r->getErrors());
271 }
272 elseif($r->hasWarnings())
273 {
274 $result->addErrors($r->getWarnings());
275 }
276 return $result;
277 }
278}
updateAction(\Bitrix\Sale\BasketPropertyItem $basketProperty, array $fields)
getAction(\Bitrix\Sale\BasketPropertyItem $basketProperty)
deleteAction(\Bitrix\Sale\BasketPropertyItem $basketProperty)
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])