Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
payableitemcollection.php
1<?php
2
3namespace Bitrix\Sale;
4
6
7Main\Localization\Loc::loadMessages(__FILE__);
8
14{
15 protected $payment;
16
20 protected function getEntityParent()
21 {
22 return $this->getPayment();
23 }
24
28 public function getPayment() : Payment
29 {
30 return $this->payment;
31 }
32
38 protected static function createCollectionObject()
39 {
40 $registry = Registry::getInstance(static::getRegistryType());
41 $className = $registry->get(Registry::ENTITY_PAYABLE_ITEM_COLLECTION);
42
43 return new $className();
44 }
45
49 public static function getRegistryType()
50 {
52 }
53
57 public function setPayment(Payment $payment)
58 {
59 $this->payment = $payment;
60 }
61
69 public static function load(Payment $payment)
70 {
71 $collection = static::createCollectionObject();
72 $collection->setPayment($payment);
73
74 if ($payment->getId() > 0)
75 {
76 $registry = Registry::getInstance(static::getRegistryType());
77
79 $entity = $registry->get(Registry::ENTITY_PAYABLE_SHIPMENT);
80
81 $items = $entity::loadForPayment($payment->getId());
82 foreach ($items as $item)
83 {
84 $item->setCollection($collection);
85 $collection->addItem($item);
86 }
87
89 $entity = $registry->get(Registry::ENTITY_PAYABLE_BASKET_ITEM);
90
91 $items = $entity::loadForPayment($payment->getId());
92 foreach ($items as $item)
93 {
94 $item->setCollection($collection);
95 $collection->addItem($item);
96 }
97 }
98
99 return $collection;
100 }
101
109 public static function getList(array $parameters = array())
110 {
111 return Internals\PayableItemTable::getList($parameters);
112 }
113
114 protected function addItem(Internals\CollectableEntity $item)
115 {
116 if (!$item instanceof PayableItem)
117 {
118 throw new Main\SystemException(
119 Main\Localization\Loc::getMessage(
120 'SALE_PAYABLE_ITEM_COLLECTION_INCOMPATIBLE_ITEM_TYPE',
121 ['#CLASS#' => PayableItem::class]
122 )
123 );
124 }
125
126 return parent::addItem($item);
127 }
128
138 public function createItemByBasketItem(BasketItem $basketItem) : PayableBasketItem
139 {
141 foreach ($this->getBasketItems() as $item)
142 {
143 $payableBasketItem = $item->getEntityObject();
144 if (
145 $payableBasketItem
146 && $basketItem->getBasketCode() === $payableBasketItem->getBasketCode())
147 {
148 return $item;
149 }
150 }
151
152 $registry = Registry::getInstance(static::getRegistryType());
153
155 $payableItemClass = $registry->get(Registry::ENTITY_PAYABLE_BASKET_ITEM);
156
158 $payableItem = $payableItemClass::create($this, $basketItem);
159 $this->addItem($payableItem);
160
161 return $payableItem;
162 }
163
164 public function onBeforeBasketItemDelete(BasketItem $basketItem)
165 {
166 $result = new Result();
167
169 foreach ($this->getBasketItems() as $item)
170 {
172 $entity = $item->getEntityObject();
173 if ($entity->getBasketCode() === $basketItem->getBasketCode())
174 {
175 $r = $item->delete();
176 if (!$r->isSuccess())
177 {
178 $result->addErrors($r->getErrors());
179 }
180 }
181 }
182
183 return $result;
184 }
185
194 public function createItemByShipment(Shipment $shipment) : PayableShipmentItem
195 {
197 foreach ($this->getShipments() as $item)
198 {
199 if ($shipment->getInternalIndex() === $item->getEntityObject()->getInternalIndex())
200 {
201 return $item;
202 }
203 }
204
205 $registry = Registry::getInstance(static::getRegistryType());
206
208 $payableItemClass = $registry->get(Registry::ENTITY_PAYABLE_SHIPMENT);
209
211 $payableItem = $payableItemClass::create($this, $shipment);
212 $this->addItem($payableItem);
213
214 return $payableItem;
215 }
216
220 public function getBasketItems() : Internals\CollectionFilterIterator
221 {
222 $callback = function (PayableItem $entity)
223 {
224 return $entity instanceof PayableBasketItem;
225 };
226
227 return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
228 }
229
233 public function getShipments() : Internals\CollectionFilterIterator
234 {
235 $callback = function (PayableItem $entity)
236 {
237 return $entity instanceof PayableShipmentItem;
238 };
239
240 return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
241 }
242
250 public function save()
251 {
252 $result = new Result();
253
254 $dbRes = static::getList([
255 'filter' => ['PAYMENT_ID' => $this->getPayment()->getId()]
256 ]);
257
258 while ($item = $dbRes->fetch())
259 {
260 if (!$this->getItemById($item['ID']))
261 {
262 static::deleteInternal($item['ID']);
263 }
264 }
265
267 foreach ($this->collection as $entity)
268 {
269 $r = $entity->save();
270 if (!$r->isSuccess())
271 {
272 $result->addErrors($r->getErrors());
273 }
274 }
275
276 $this->clearChanged();
277
278 return $result;
279 }
280
290 public static function deleteNoDemand($paymentId)
291 {
292 $result = new Result();
293
294 $dbRes = static::getList([
295 "filter" => ["=PAYMENT_ID" => $paymentId],
296 "select" => ["ID"]
297 ]);
298
299 while ($entity = $dbRes->fetch())
300 {
301 $r = static::deleteInternal($entity['ID']);
302 if (!$r->isSuccess())
303 {
304 $result->addErrors($r->getErrors());
305 }
306 }
307
308 return $result;
309 }
310
316 protected static function deleteInternal($primary)
317 {
318 return Internals\PayableItemTable::delete($primary);
319 }
320
327 public function createClone(\SplObjectStorage $cloneEntity)
328 {
329 if ($this->isClone() && $cloneEntity->contains($this))
330 {
331 return $cloneEntity[$this];
332 }
333
335 $payableItemCollection = parent::createClone($cloneEntity);
336
337 if ($this->payment)
338 {
339 if ($cloneEntity->contains($this->payment))
340 {
341 $payableItemCollection->payment = $cloneEntity[$this->payment];
342 }
343 }
344
345 return $payableItemCollection;
346 }
347}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
addItem(Internals\CollectableEntity $item)
const ENTITY_PAYABLE_BASKET_ITEM
Definition registry.php:23
static getInstance($type)
Definition registry.php:183
const ENTITY_PAYABLE_SHIPMENT
Definition registry.php:24
const ENTITY_PAYABLE_ITEM_COLLECTION
Definition registry.php:25