/** * @param BasketItem $basketItem * @throws Main\ArgumentOutOfRangeException */ private function addBundleToCollection(BasketItem $basketItem) { /** @var Basket $bundleCollection */ $bundleCollection = $basketItem->getBundleCollection(); if ($bundleCollection->getOrder() === null) { /** @var Basket $basketCollection */ if ($basketCollection = $basketItem->getCollection()) { if ($order = $basketCollection->getOrder()) { $bundleCollection->setOrder($order); } } } /** @var Shipment $shipment */ $shipment = $this->getShipment(); $bundleBaseQuantity = $basketItem->getBundleBaseQuantity(); /** @var BasketItem $bundleBasketItem */ foreach ($bundleCollection as $bundleBasketItem) { if ($this->isExistsBasketItem($bundleBasketItem)) { continue; } $bundleProductId = $bundleBasketItem->getProductId(); if (!isset($bundleBaseQuantity[$bundleProductId])) { throw new Main\ArgumentOutOfRangeException("bundle product id"); } $quantity = $bundleBaseQuantity[$bundleProductId] * $basketItem->getQuantity(); if ($quantity == 0) { continue; } $shipmentItemBundle = ShipmentItem::create($this, $bundleBasketItem); $this->addItem($shipmentItemBundle); if ($shipment->isSystem()) { $shipmentItemBundle->setFieldNoDemand('QUANTITY', $quantity); } else { $shipmentItemBundle->setQuantity($quantity); } } }
public static function checkAvailableProductQuantity(BasketItem $basketItem, $deltaQuantity) { global $APPLICATION; $result = new Result(); $resultProductData = array(); $orderId = null; $userId = null; $siteId = null; /** @var Basket $basket */ $basket = $basketItem->getCollection(); if (($order = $basket->getOrder()) !== null) { $userId = $order->getUserId(); $siteId = $order->getSiteId(); } if ($userId === null || $siteId === null) { static $usersIds = array(); if (empty($usersIds[$basket->getFUserId()])) { $usersIds[$basket->getFUserId()] = \CSaleUser::GetUserID($basket->getFUserId()); } $userId = $usersIds[$basket->getFUserId()]; $siteId = $basket->getSiteId(); } if ($provider = $basketItem->getProvider()) { if ($provider instanceof Provider) { throw new NotSupportedException('provider not supported'); // $result = $provider::getProductData($siteId, $userId, $currency, $providerBasketItemList, $select); } elseif ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider))) { $needQuantity = $basketItem->getQuantity(); if ($order && $order->getId() > 0) { $needQuantity = $deltaQuantity; } $data = array("PRODUCT_ID" => $basketItem->getProductId(), "QUANTITY" => $needQuantity, "USER_ID" => $userId, "SITE_ID" => $siteId, "BASKET_ID" => $basketItem->getId(), "CHECK_QUANTITY" => "Y", 'CHECK_PRICE' => 'N', 'CHECK_COUPONS' => 'N', "SELECT_QUANTITY_TRACE" => "Y"); // TODO: ! if ($deltaQuantity <= 0) { $result->setData(array('AVAILABLE_QUANTITY' => $deltaQuantity)); return $result; } $APPLICATION->ResetException(); $resultProductData = $provider::GetProductData($data); if ($ex = $APPLICATION->GetException()) { $result->addError(new ResultError($ex->GetString(), $ex->GetID())); } } else { $APPLICATION->ResetException(); $resultProductData = \CSaleBasket::ExecuteCallbackFunction($basketItem->getField('CALLBACK_FUNC'), $basketItem->getField('MODULE'), $basketItem->getProductId(), $basketItem->getQuantity()); if ($ex = $APPLICATION->GetException()) { $result->addError(new ResultError($ex->GetString(), $ex->GetID())); } } } else { $availableQuantity = $basketItem->getQuantity(); if ($deltaQuantity <= 0) { $availableQuantity = $deltaQuantity; } $result->setData(array('AVAILABLE_QUANTITY' => $availableQuantity)); return $result; } $fields = array(); if (array_key_exists('QUANTITY', $resultProductData)) { $fields['AVAILABLE_QUANTITY'] = $resultProductData['QUANTITY']; } if (array_key_exists('QUANTITY_TRACE', $resultProductData)) { $fields['QUANTITY_TRACE'] = $resultProductData['QUANTITY_TRACE'] == "Y"; } if (!empty($fields)) { $result->setData($fields); } return $result; }
/** * @return Entity\AddResult|Entity\UpdateResult * @throws Main\ArgumentOutOfRangeException * @throws Main\ObjectNotFoundException * @throws \Exception */ public function save() { $id = $this->getId(); $fields = $this->fields->getValues(); /** @var ShipmentItemCollection $shipmentItemCollection */ if (!($shipmentItemCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found'); } /** @var Shipment $shipment */ if (!($shipment = $shipmentItemCollection->getShipment())) { throw new Main\ObjectNotFoundException('Entity "Shipment" not found'); } /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $shipment->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } /** @var Order $order */ if (!($order = $shipmentCollection->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } if ($id > 0) { $fields = $this->fields->getChangedValues(); if (!empty($fields) && is_array($fields)) { /** @var ShipmentItemCollection $shipmentItemCollection */ if (!($shipmentItemCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found'); } /** @var Shipment $shipment */ if (!($shipment = $shipmentItemCollection->getShipment())) { throw new Main\ObjectNotFoundException('Entity "Shipment" not found'); } if (!$shipment->isSystem()) { if (isset($fields["QUANTITY"]) && floatval($fields["QUANTITY"]) == 0) { return new Entity\UpdateResult(); } } //$fields['DATE_UPDATE'] = new Main\Type\DateTime(); $r = Internals\ShipmentItemTable::update($id, $fields); if (!$r->isSuccess()) { return $r; } } $result = new Entity\UpdateResult(); } else { $fields['ORDER_DELIVERY_ID'] = $this->getParentShipmentId(); $fields['DATE_INSERT'] = new Main\Type\DateTime(); $fields["BASKET_ID"] = $this->basketItem->getId(); if (!isset($fields["QUANTITY"]) || floatval($fields["QUANTITY"]) == 0) { return new Entity\UpdateResult(); } if (!isset($fields['RESERVED_QUANTITY'])) { $fields['RESERVED_QUANTITY'] = $this->getReservedQuantity() === null ? 0 : $this->getReservedQuantity(); } $r = Internals\ShipmentItemTable::add($fields); if (!$r->isSuccess()) { return $r; } $id = $r->getId(); $this->setFieldNoDemand('ID', $id); $result = new Entity\AddResult(); OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ITEM_BASKET_ADDED', $shipment->getId(), array('NAME' => $this->basketItem->getField('NAME'), 'QUANTITY' => $this->basketItem->getQuantity(), 'PRODUCT_ID' => $this->basketItem->getProductId())); } $shipmentItemStoreCollection = $this->getShipmentItemStoreCollection(); $r = $shipmentItemStoreCollection->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if ($result->isSuccess()) { OrderHistory::collectEntityFields('SHIPMENT_ITEM', $order->getId(), $id); } return $result; }