/** * @param Order $order */ public static function resetEvents(Order $order = null) { if ($order !== null) { unset(static::$events[$order->getInternalId()]); } else { static::$events = array(); } }
/** * @param Order $order * @return Result * @throws NotImplementedException * @throws NotSupportedException * @throws SystemException */ private static function applyPoolReservation(Order $order) { if (!isset(static::$reservationPool[$order->getInternalId()])) { return new Result(); } $result = new Result(); $pool = static::getReservationPool($order->getInternalId()); $poolQuantities = $pool->getQuantities(); $poolItems = $pool->getItems(); $shipmentCollection = $order->getShipmentCollection(); foreach ($poolQuantities as $basketCode => $quantity) { $quantity = round($quantity, 4); if ($quantity == 0) { continue; } if (!isset($poolItems[$basketCode])) { throw new SystemException(); } /** @var BasketItem $basketItem */ $basketItem = $poolItems[$basketCode]; $canReserve = false; /** @var Result $r */ $r = Provider::reserveBasketItem($basketItem, $quantity); if ($r->isSuccess()) { $reserveBasketItemResult = $r->getData(); $reservedQuantity = null; if (!empty($reserveBasketItemResult) && is_array($reserveBasketItemResult)) { if (array_key_exists('QUANTITY', $reserveBasketItemResult)) { $reservedQuantity = round($reserveBasketItemResult['QUANTITY'], 4); } if (array_key_exists('HAS_PROVIDER', $reserveBasketItemResult)) { $canReserve = $reserveBasketItemResult['HAS_PROVIDER']; } } if ($reservedQuantity === null) { $result->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_RESERVE_BASKET_ITEM_WRONG_QUANTITY'), 'SALE_PROVIDER_RESERVE_BASKET_ITEM_WRONG_QUANTITY')); } } else { $result->addErrors($r->getErrors()); } if (!$result->isSuccess()) { return $result; } $pool->set($basketItem, 0); if ($quantity > 0 && $reservedQuantity > $quantity || $quantity < 0 && $reservedQuantity < $quantity) { $result->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_RESERVE_BASKET_ITEM_QUANTITY_NOT_ENOUGH'), 'SALE_PROVIDER_RESERVE_BASKET_ITEM_QUANTITY_NOT_ENOUGH')); return $result; } // not implemented yet if ($quantity < 0 && $reservedQuantity != $quantity) { throw new NotImplementedException(); } if ($canReserve && Configuration::isEnabledReservation() && $quantity != $reservedQuantity) { $systemShipment = $shipmentCollection->getSystemShipment(); /** @var ShipmentItemCollection $systemShipmentItemCollection */ $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection(); if ($shipmentItem = $systemShipmentItemCollection->getItemByBasketCode($basketCode)) { if ($shipmentItem->getReservedQuantity() > 0) { $needQuantity = $quantity - $reservedQuantity; if ($shipmentItem->getReservedQuantity() >= $needQuantity) { $setQuantity = $shipmentItem->getReservedQuantity() - $needQuantity; } else { $setQuantity = 0; $needQuantity = $shipmentItem->getReservedQuantity(); } $reservedQuantity += $needQuantity; $shipmentItem->setField('RESERVED_QUANTITY', $setQuantity); } } if ($quantity != $reservedQuantity) { $diffQuantity = $quantity - $reservedQuantity; /** @var Shipment $shipment */ foreach ($shipmentCollection as $shipment) { if ($shipment->isSystem()) { continue; } /** @var ShipmentItemCollection $shipmentItemCollection */ $shipmentItemCollection = $shipment->getShipmentItemCollection(); if ($shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketCode)) { if ($shipmentItem->getReservedQuantity() >= $diffQuantity) { $shipmentItem->setField('RESERVED_QUANTITY', $shipmentItem->getReservedQuantity() - $diffQuantity); $diffQuantity = 0; break; } else { $diffQuantity -= $shipmentItem->getReservedQuantity(); $shipmentItem->setField('RESERVED_QUANTITY', 0); } } } if ($diffQuantity > 0 && $reservedQuantity > 0) { $result->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_RESERVE_BASKET_ITEM_QUANTITY_WRONG_RESIDUE'), 'SALE_PROVIDER_RESERVE_BASKET_ITEM_QUANTITY_WRONG_RESIDUE')); return $result; // throw new SystemException("diffQuantity"); } } } if ($canReserve) { $order->setFieldNoDemand('RESERVED', $shipmentCollection->isReserved() ? "Y" : "N"); } } return $result; }