Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reservequantitycollection.php
1<?php
2
3namespace Bitrix\Sale;
4
8
10{
12 protected $basketItem;
13
17 protected function getEntityParent()
18 {
19 return $this->getBasketItem();
20 }
21
22 protected function setBasketItem(BasketItemBase $item)
23 {
24 $this->basketItem = $item;
25 }
26
27 public function getBasketItem()
28 {
29 return $this->basketItem;
30 }
31
35 public static function getRegistryType()
36 {
38 }
39
45 protected static function createCollectionObject() : self
46 {
47 $registry = Registry::getInstance(static::getRegistryType());
48 $className = $registry->get(Registry::ENTITY_BASKET_RESERVE_COLLECTION);
49
50 return new $className;
51 }
52
59 public function create()
60 {
61 $registry = Registry::getInstance(static::getRegistryType());
62
64 $reservedItemClassName = $registry->getReservedItemClassName();
65
66 $reservedItem = $reservedItemClassName::create($this);
67 $this->addItem($reservedItem);
68
69 return $reservedItem;
70 }
71
79 public static function load(BasketItemBase $basketItem) : self
80 {
81 if (!$basketItem->isReservableItem())
82 {
83 throw new Main\SystemException('Basket item is not available for reservation');
84 }
85
86 $collection = static::createCollectionObject();
87 $collection->setBasketItem($basketItem);
88
89 if ($basketItem->getId() > 0)
90 {
91 $registry = Registry::getInstance(static::getRegistryType());
92
94 $reservedItemClassName = $registry->getReservedItemClassName();
95
96 $reservedQuantityList = $reservedItemClassName::loadForBasketItem($basketItem->getId());
97 foreach ($reservedQuantityList as $item)
98 {
99 $item->setCollection($collection);
100 $collection->addItem($item);
101 }
102 }
103
104 return $collection;
105 }
106
115 public function save()
116 {
117 $result = new Result();
118
119 if (!$this->isChanged())
120 {
121 return $result;
122 }
123
124 $basketItem = $this->getEntityParent();
125
126 if ($basketItem->getId() > 0)
127 {
128 $itemsFromDbList = static::getList([
129 'filter' => ["BASKET_ID" => $basketItem->getId()],
130 ]);
131 while ($item = $itemsFromDbList->fetch())
132 {
133 if (!$this->getItemById($item['ID']))
134 {
135 static::deleteInternal($item['ID']);
136 }
137 }
138 }
139
141 foreach ($this->collection as $entity)
142 {
143 $r = $entity->save();
144 if (!$r->isSuccess())
145 {
146 $result->addErrors($r->getErrors());
147 }
148 }
149
150 $this->clearChanged();
151
152 return $result;
153 }
154
164 public static function deleteNoDemand($basketId)
165 {
166 $result = new Result();
167
168 $dbRes = static::getList([
169 'select' => ['ID'],
170 'filter' => ['=BASKET_ID' => $basketId],
171 ]);
172
173 while ($entity = $dbRes->fetch())
174 {
175 $r = static::deleteInternal($entity['ID']);
176 if (!$r->isSuccess())
177 {
178 $result->addErrors($r->getErrors());
179 }
180 }
181
182 return $result;
183 }
184
185 public function getQuantity() : float
186 {
187 $quantity = 0;
188
190 foreach ($this->collection as $reservation)
191 {
192 $quantity += $reservation->getQuantity();
193 }
194
195 return $quantity;
196 }
197
198 public function getQuantityByStoreId(int $storeId) : float
199 {
200 $quantity = 0;
201
203 foreach ($this->collection as $reservation)
204 {
205 if ($reservation->getStoreId() === $storeId)
206 {
207 $quantity += $reservation->getQuantity();
208 }
209 }
210
211 return $quantity;
212 }
213
219 protected static function deleteInternal($primary)
220 {
222 $service = ServiceLocator::getInstance()->get('sale.basketReservation');
223 return $service->delete($primary);
224 }
225
233 public static function getList(array $parameters = [])
234 {
235 return Reservation\Internals\BasketReservationTable::getList($parameters);
236 }
237}
const ENTITY_BASKET_RESERVE_COLLECTION
Definition registry.php:35
static getInstance($type)
Definition registry.php:183