Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tradebindingcollection.php
1<?php
2
3namespace Bitrix\Sale;
4
8
14{
15 protected $order = null;
16
20 protected function getEntityParent()
21 {
22 return $this->order;
23 }
24
28 public function getOrder()
29 {
30 return $this->order;
31 }
32
36 private static function createCollectionObject()
37 {
38 $registry = Registry::getInstance(static::getRegistryType());
39 $className = $registry->get(Registry::ENTITY_TRADE_BINDING_COLLECTION);
40
41 return new $className();
42 }
43
47 public static function getRegistryType()
48 {
50 }
51
55 public function setOrder(Order $order)
56 {
57 $this->order = $order;
58 }
59
64 public static function load(Order $order)
65 {
66 $collection = static::createCollectionObject();
67 $collection->setOrder($order);
68
69 if (!$order->isNew())
70 {
71 $registry = Registry::getInstance(static::getRegistryType());
72
74 $entity = $registry->get(Registry::ENTITY_TRADE_BINDING_ENTITY);
75
76 $bindingList = $entity::loadForOrder($order->getId());
78 foreach ($bindingList as $item)
79 {
80 $item->setCollection($collection);
81 $collection->addItem($item);
82 }
83 }
84
85 return $collection;
86 }
87
92 public static function getList(array $parameters = array())
93 {
94 return TradingPlatform\OrderTable::getList($parameters);
95 }
96
101 public function createItem(TradingPlatform\Platform $platform = null)
102 {
103 $registry = Registry::getInstance(static::getRegistryType());
104
106 $tradeBindingEntity = $registry->get(Registry::ENTITY_TRADE_BINDING_ENTITY);
107
108 $tradeBinding = $tradeBindingEntity::create($this, $platform);
109 $this->addItem($tradeBinding);
110
111 return $tradeBinding;
112 }
113
114 public function onItemModify(CollectableEntity $item, $name = null, $oldValue = null, $value = null)
115 {
117 $order = $this->getOrder();
118
119 if ($item instanceof TradeBindingEntity)
120 {
121 $order->onTradeBindingCollectionModify(EventActions::UPDATE, $item, $name, $oldValue, $value);
122 }
123
124 return parent::onItemModify($item, $name, $oldValue, $value);
125 }
126
132 public function addItem(Internals\CollectableEntity $item)
133 {
134 if (!($item instanceof TradeBindingEntity))
135 {
136 throw new Main\NotSupportedException();
137 }
138
140 $entity = parent::addItem($item);
141
142 $order = $this->getOrder();
143 $order->onTradeBindingCollectionModify(EventActions::ADD, $entity);
144
145 return $entity;
146 }
147
152 public function deleteItem($index)
153 {
155 $oldItem = parent::deleteItem($index);
156
157 $order = $this->getOrder();
158 $order->onTradeBindingCollectionModify(EventActions::DELETE, $oldItem);
159 }
160
166 public function save()
167 {
168 $result = new Result();
169
171 if (!$order = $this->getEntityParent())
172 {
173 throw new Main\ObjectNotFoundException('Entity "Order" not found');
174 }
175
176 if (!$order->isNew())
177 {
178 $itemsFromDbList = static::getList(
179 array(
180 "filter" => array("ORDER_ID" => $order->getId())
181 )
182 );
183
184 while ($item = $itemsFromDbList->fetch())
185 {
186 if (!$this->getItemById($item['ID']))
187 {
188 static::deleteInternal($item['ID']);
189 }
190 }
191 }
192
194 foreach ($this->collection as $entity)
195 {
196 $r = $entity->save();
197 if (!$r->isSuccess())
198 {
199 $result->addErrors($r->getErrors());
200 }
201 }
202
203 $this->clearChanged();
204
205 return $result;
206 }
207
214 public static function deleteNoDemand($idOrder)
215 {
216 $result = new Result();
217
218 $dbRes = static::getList(
219 array(
220 "filter" => array("=ORDER_ID" => $idOrder),
221 "select" => array("ID")
222 )
223 );
224
225 while ($entity = $dbRes->fetch())
226 {
227 $r = static::deleteInternal($entity['ID']);
228 if (!$r->isSuccess())
229 {
230 $result->addErrors($r->getErrors());
231 }
232 }
233
234 return $result;
235 }
236
241 protected static function deleteInternal($primary)
242 {
243 return TradingPlatform\OrderTable::delete($primary);
244 }
245
252 public function createClone(\SplObjectStorage $cloneEntity)
253 {
254 if ($this->isClone() && $cloneEntity->contains($this))
255 {
256 return $cloneEntity[$this];
257 }
258
260 $tradeBindingCollection = parent::createClone($cloneEntity);
261
262 if ($this->order)
263 {
264 if ($cloneEntity->contains($this->order))
265 {
266 $tradeBindingCollection->order = $cloneEntity[$this->order];
267 }
268 }
269
270 return $tradeBindingCollection;
271 }
272
280 public function hasTradingPlatform(string $platformCode, string $type = null): bool
281 {
282 return $this->getTradingPlatform($platformCode, $type) ? true : false;
283 }
284
292 public function getTradingPlatform(string $platformCode, string $type = null)
293 {
294 foreach ($this->collection as $item)
295 {
296 $tradingPlatform = $item->getTradePlatform();
297 if (!$tradingPlatform || $tradingPlatform::TRADING_PLATFORM_CODE !== $platformCode)
298 {
299 continue;
300 }
301
302 if (!is_null($type) && !$tradingPlatform->isOfType($type))
303 {
304 continue;
305 }
306
307 return $tradingPlatform;
308 }
309
310 return null;
311 }
312
313 public function getTradingPlatformIdList() : array
314 {
315 $result = [];
316
317 foreach ($this->collection as $item)
318 {
319 $result[] = $item->getField('TRADING_PLATFORM_ID');
320 }
321
322 return $result;
323 }
324}
onItemModify(CollectableEntity $item, $name=null, $oldValue=null, $value=null)
const ENTITY_TRADE_BINDING_COLLECTION
Definition registry.php:55
static getInstance($type)
Definition registry.php:183
const ENTITY_TRADE_BINDING_ENTITY
Definition registry.php:56
static getList(array $parameters=array())
hasTradingPlatform(string $platformCode, string $type=null)
getTradingPlatform(string $platformCode, string $type=null)