Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reservequantity.php
1<?php
2
3namespace Bitrix\Sale;
4
8
13{
17 public static function getRegistryType()
18 {
20 }
21
22 public static function getRegistryEntity()
23 {
25 }
26
30 public static function getAvailableFields()
31 {
32 return [
33 'QUANTITY', 'BASKET_ID', 'STORE_ID',
34 'DATE_RESERVE', 'DATE_RESERVE_END', 'RESERVED_BY',
35 ];
36 }
37
41 protected static function getMeaningfulFields()
42 {
43 return [];
44 }
45
49 protected static function getFieldsMap()
50 {
51 return Reservation\Internals\BasketReservationTable::getMap();
52 }
53
61 {
62 $basketItem = $collection->getBasketItem();
63 if (!$basketItem->isReservableItem())
64 {
65 throw new Main\SystemException('Basket item is not available for reservation');
66 }
67
68 $fields = [
70 ];
71
72 if ($basketItem->getId() > 0)
73 {
74 $fields['BASKET_ID'] = $basketItem->getId();
75 }
76
77 $reservedItem = static::createEntityObject($fields);
78 $reservedItem->setCollection($collection);
79
80 return $reservedItem;
81 }
82
89 private static function createEntityObject(array $fields = array())
90 {
91 $registry = Registry::getInstance(static::getRegistryType());
92 $entityClassName = $registry->get(static::getRegistryEntity());
93
94 return new $entityClassName($fields);
95 }
96
105 public static function loadForBasketItem(int $id)
106 {
107 if ($id <= 0)
108 {
109 throw new Main\ArgumentNullException('id');
110 }
111
112 $entityList = [];
113
114 $registry = Registry::getInstance(static::getRegistryType());
115
117 $reserveCollection = $registry->get(Registry::ENTITY_BASKET_RESERVE_COLLECTION);
118 $dbRes = $reserveCollection::getList([
119 'filter' => [
120 '=BASKET_ID' => $id,
121 ]
122 ]);
123
124 while ($data = $dbRes->fetch())
125 {
126 $entityList[] = static::createEntityObject($data);
127 }
128
129 return $entityList;
130 }
131
132 public function getQuantity() : float
133 {
134 return (float)$this->getField('QUANTITY');
135 }
136
137 public function setQuantity($quantity) : Result
138 {
139 return $this->setField('QUANTITY', $quantity);
140 }
141
142 public function getStoreId() : int
143 {
144 return (int)$this->getField('STORE_ID');
145 }
146
147 public function setStoreId(int $storeId) : Result
148 {
149 return $this->setField('STORE_ID', $storeId);
150 }
151
152 protected function onFieldModify($name, $oldValue, $value)
153 {
154 if ($name === 'QUANTITY')
155 {
156 $collection = $this->getCollection();
157
158 if ($collection->getQuantity() > $collection->getBasketItem()->getQuantity())
159 {
160 $result = new Result();
161
162 return $result->addError(
163 new Main\Error(
164 Main\Localization\Loc::getMessage('SALE_RESERVE_QUANTITY_EXCEEDING_ERROR')
165 )
166 );
167 }
168
169 $result = Internals\Catalog\Provider::tryReserve($this);
170 if (!$result->isSuccess())
171 {
172 return $result;
173 }
174 }
175 else if (
176 $name === 'STORE_ID'
177 && $this->getQuantity() > 0
178 )
179 {
180 throw new Main\SystemException(
181 Main\Localization\Loc::getMessage('SALE_RESERVE_QUANTITY_CHANGE_STORE_ERROR')
182 );
183 }
184
185 return parent::onFieldModify($name, $oldValue, $value);
186 }
187
192 protected function onBeforeSetFields(array $values)
193 {
194 if (isset($values['QUANTITY']))
195 {
196 $quantity = $values['QUANTITY'];
197
198 // move to the end of array
199 unset($values['QUANTITY']);
200 $values['QUANTITY'] = $quantity;
201 }
202
203 return $values;
204 }
205
210 public function save()
211 {
212 $result = new Result();
213
214 if (!$this->isChanged())
215 {
216 return $result;
217 }
218
219 $id = $this->getId();
220
221 if ($id > 0)
222 {
223 $fields = $this->getFields()->getChangedValues();
224 $r = $this->updateInternal($id, $fields);
225 }
226 else
227 {
228 if (!$this->getField('BASKET_ID'))
229 {
230 $basketItem = $this->getCollection()->getBasketItem();
231
232 $fields['BASKET_ID'] = $basketItem->getId();
233 $this->setFieldNoDemand('BASKET_ID', $fields['BASKET_ID']);
234 }
235
236 if (!$this->getField('DATE_RESERVE_END'))
237 {
238 $reserveClearPeriod = Configuration::getProductReserveClearPeriod();
239 $defaultDateReserveEnd = (new \Bitrix\Main\Type\Date())->add($reserveClearPeriod . 'D');
240 $this->setFieldNoDemand('DATE_RESERVE_END', $defaultDateReserveEnd);
241 }
242
243 $this->setFieldNoDemand('DATE_RESERVE', new Main\Type\DateTime());
244
245 $fields = $this->getFields()->getValues();
246
247 $r = $this->addInternal($fields);
248 if ($r->isSuccess())
249 {
250 $id = $r->getId();
251 $this->setFieldNoDemand('ID', $id);
252 }
253 }
254
255 if (!$r->isSuccess())
256 {
257 $result->addErrors($r->getErrors());
258 }
259
260 $result->setId($id);
261
262 return $result;
263 }
264
265 public function delete()
266 {
267 $result = $this->setQuantity(0);
268 if (!$result->isSuccess())
269 {
270 return $result;
271 }
272
273 return parent::delete();
274 }
275
276 public function deleteNoDemand()
277 {
278 return parent::delete();
279 }
280
287 protected function updateInternal($primary, array $data)
288 {
290 $service = ServiceLocator::getInstance()->get('sale.basketReservation');
291 return $service->update($primary, $data);
292 }
293
299 protected function addInternal(array $data)
300 {
302 $service = ServiceLocator::getInstance()->get('sale.basketReservation');
303 return $service->add($data);
304 }
305
306 public static function getEntityEventName()
307 {
308 return 'SaleReservedQuantity';
309 }
310}
static add(array $data)
Definition entity.php:150
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
const ENTITY_BASKET_RESERVE_COLLECTION
Definition registry.php:35
static getInstance($type)
Definition registry.php:183
const ENTITY_BASKET_RESERVE_COLLECTION_ITEM
Definition registry.php:36
onFieldModify($name, $oldValue, $value)
static create(ReserveQuantityCollection $collection)