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