Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
paymentitembasket.php
1<?php
2
4
7use Bitrix\Main\Entity\ExpressionField;
19
21{
22 public function getPrimaryAutoWiredParameter(): ExactParameter
23 {
24 return new ExactParameter(
25 PayableBasketItem::class,
26 'paymentItem',
27 function($className, $id) {
28
30
32 $payableItemCollection = $registry->get(Registry::ENTITY_PAYABLE_ITEM_COLLECTION);
33 $pi = $payableItemCollection::getList([
34 'select'=>['PAYMENT_ID'],
35 'filter'=>[
36 'ID'=>$id,
38 ],
39 ]);
40
41 if($piRow = $pi->fetch())
42 {
45 $paymentClass = $registry->getPaymentClassName();
46
47 $r = $paymentClass::getList([
48 'select'=>['ORDER_ID'],
49 'filter'=>['ID'=>$piRow['PAYMENT_ID']]
50 ]);
51
52 if($row = $r->fetch())
53 {
55 $orderClass = $registry->getOrderClassName();
56
57 $order = $orderClass::load($row['ORDER_ID']);
58 $payment = $order->getPaymentCollection()->getItemById($piRow['PAYMENT_ID']);
59 $payableItemCollection = $payment->getPayableItemCollection();
61 foreach ($payableItemCollection as $item)
62 {
63 if($item->getId() == $id)
64 {
65 return $item;
66 }
67 }
68 }
69 else
70 {
71 $this->addError(new Error('payment is not exists', 200640400001));
72 }
73 }
74
75 $this->addError(new Error('payment item is not exists', 201240400001));
76 return null;
77 }
78 );
79 }
80
81 //region Actions
82 public function getFieldsAction(): array
83 {
84 $view = $this->getViewManager()
85 ->getView($this);
86
87 return ['PAYMENT_ITEM_BASKET'=>$view->prepareFieldInfos(
88 $view->getFields()
89 )];
90 }
91
92 public function getAction(PayableBasketItem $paymentItem): array
93 {
94 return ['PAYMENT_ITEM_BASKET'=>$paymentItem->toArray()];
95 }
96
97 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
98 {
99 $select = empty($select) ? ['*'] : $select;
100 $order = empty($order) ? ['ID'=>'ASC'] : $order;
101
102 $filter['ENTITY_TYPE'] = PayableBasketItem::getEntityType();
103
106 $payableItemCollection = $registry->get(Registry::ENTITY_PAYABLE_ITEM_COLLECTION);
107 $paymentItems = $payableItemCollection::getList(
108 [
109 'select' => $select,
110 'filter' => $filter,
111 'order' => $order,
112 'offset' => $pageNavigation->getOffset(),
113 'limit' => $pageNavigation->getLimit(),
114 ]
115 )->fetchAll();
116
117 return new Page('PAYMENT_ITEMS_BASKET', $paymentItems, function() use ($filter)
118 {
121 $payableItemCollection = $registry->get(Registry::ENTITY_PAYABLE_ITEM_COLLECTION);
122
123 return (int) $payableItemCollection::getList([
124 'select' => ['CNT'],
125 'filter'=>$filter,
126 'runtime' => [
127 new ExpressionField('CNT', 'COUNT(ID)')
128 ]
129 ])->fetch()['CNT'];
130 });
131 }
132
133 public function addAction(array $fields): ?array
134 {
135 $result = new Result();
136 $paymentItem = null;
137
139
140 $basketId = $fields['BASKET_ID'];
141 $paymentId = $fields['PAYMENT_ID'];
142
143 unset($fields['PAYMENT_ID'], $fields['BASKET_ID']);
144
146 $basketClass = $registry->getBasketClassName();
147
148 $r = $basketClass::getList([
149 'select'=>['ORDER_ID'],
150 'filter'=>['ID'=>$basketId]
151 ]);
152
153 if($row = $r->fetch())
154 {
156 $orderClass = $registry->getOrderClassName();
157
158 $order = $orderClass::load($row['ORDER_ID']);
159 $basketItem = $order->getBasket()->getItemByBasketCode($basketId);
160 if($basketItem instanceof BasketItem)
161 {
163 $collection = $order->getPaymentCollection();
164 $payment = $collection->getItemById($paymentId);
165 if($payment instanceof \Bitrix\Sale\Payment)
166 {
167 $paymentItems = $payment->getPayableItemCollection()->getBasketItems();
168 if($this->isExistBasketItem($paymentItems, $basketItem) == false)
169 {
171 $paymentItem = $payment->getPayableItemCollection()->createItemByBasketItem($basketItem);
172 $result = $paymentItem->setFields($fields);
173 if($result->isSuccess() && $result->hasWarnings() == false)
174 {
175 $r = $this->save($paymentItem);
176 if(!$r->isSuccess())
177 {
178 $result->addErrors($r->getErrors());
179 }
180 }
181 }
182 else
183 {
184 $result->addError(new Error('Duplicate entry for key [basketId, paymentId]', 201250000001));
185 }
186 }
187 else
188 {
189 $result->addError(new Error('payment not exists', 201240400002));
190 }
191 }
192 }
193 else
194 {
195 $result->addError(new Error('basket item not exists', 201240400003));
196 }
197
198 if(!$result->isSuccess())
199 {
200 $this->addErrors($result->getErrors());
201 return null;
202 }
203 elseif($result->hasWarnings())
204 {
205 $this->addErrors($result->getWarnings());
206 return null;
207 }
208 elseif($paymentItem instanceof PayableItem)
209 {
210 return ['PAYMENT_ITEM_BASKET'=>$paymentItem->toArray()];
211 }
212 else
213 {
214 return ['PAYMENT_ITEM_BASKET'=>$paymentItem];
215 }
216 }
217
218 public function updateAction(PayableBasketItem $paymentItem, array $fields): ?array
219 {
220 $r = $paymentItem->setFields($fields);
221
222 if($r->isSuccess() == false)
223 {
224 $this->addErrors($r->getErrors());
225 return null;
226 }
227 elseif($r->hasWarnings())
228 {
229 $this->addErrors($r->getWarnings());
230 return null;
231 }
232 else
233 {
234 $r = $this->save($paymentItem);
235 if(!$r->isSuccess())
236 {
237 $this->addErrors($r->getErrors());
238 return null;
239 }
240 else
241 {
242 return ['PAYMENT_ITEM_BASKET'=>$paymentItem->toArray()];
243 }
244 }
245 }
246
247 public function deleteAction(PayableBasketItem $paymentItem): ?bool
248 {
249 $r = $paymentItem->delete();
250
251 if($r->isSuccess() == false)
252 {
253 $this->addErrors($r->getErrors());
254 return null;
255 }
256 elseif($r->hasWarnings())
257 {
258 $this->addErrors($r->getWarnings());
259 return null;
260 }
261 else
262 {
263 $r = $this->save($paymentItem);
264 if(!$r->isSuccess())
265 {
266 $this->addErrors($r->getErrors());
267 return null;
268 }
269 else
270 {
271 return true;
272 }
273 }
274 }
275 //endregion
276
277 private function save(PayableBasketItem $paymentItem): Result
278 {
279 $result = new Result();
281 $collectionPaymentItems = $paymentItem->getCollection();
283 $payment = $collectionPaymentItems->getPayment();
285 $collectionPayments = $payment->getCollection();
286 $order = $collectionPayments->getOrder();
287
288 $r = $order->save();
289 if(!$r->isSuccess())
290 {
291 $result->addErrors($r->getErrors());
292 }
293 elseif($r->hasWarnings())
294 {
295 $result->addErrors($r->getWarnings());
296 }
297 return $result;
298 }
299
300 protected function isExistBasketItem(CollectionFilterIterator $paymentItems, BasketItem $basketItem): bool
301 {
302 foreach ($paymentItems as $paymentItem)
303 {
305 $entityBasketItem = $paymentItem->getEntityObject();
306 if($entityBasketItem->getBasketCode() === $basketItem->getBasketCode())
307 {
308 return true;
309 }
310 }
311 return false;
312 }
313
315 {
316 $r = new Result();
317
318 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
319 if ($saleModulePermissions < "W")
320 {
321 $r->addError(new Error('Access Denied', 200040300020));
322 }
323 return $r;
324 }
325
326 protected function checkReadPermissionEntity(): Result
327 {
328 $r = new Result();
329
330 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
331 if ($saleModulePermissions == "D")
332 {
333 $r->addError(new Error('Access Denied', 200040300010));
334 }
335 return $r;
336 }
337}
updateAction(PayableBasketItem $paymentItem, array $fields)
getAction(PayableBasketItem $paymentItem)
deleteAction(PayableBasketItem $paymentItem)
static getInstance($type)
Definition registry.php:183
const ENTITY_PAYABLE_ITEM_COLLECTION
Definition registry.php:25
addErrors(array $errors)
Definition error.php:41