/** * @param BasketItem $basketItem * @return static * @throws \Bitrix\Main\ArgumentException */ public static function load(BasketItem $basketItem) { $basketPropertyCollection = new static(); $basketPropertyCollection->basketItem = $basketItem; if ($basketItem->getId() <= 0) { return $basketPropertyCollection; } $res = Internals\BasketPropertyTable::getList(array('order' => array("SORT" => "ASC", "ID" => "ASC"), 'filter' => array("BASKET_ID" => $basketItem->getId()))); while ($property = $res->fetch()) { $basketPropertyItem = BasketPropertyItem::create($basketPropertyCollection); $basketPropertyItem->initFields($property); $basketPropertyCollection->addItem($basketPropertyItem); } return $basketPropertyCollection; }
public static function create(ShipmentItemStoreCollection $collection, BasketItem $basketItem) { $fields = array('BASKET_ID' => $basketItem->getId()); $shipmentItemStore = new static($fields); $shipmentItemStore->setCollection($collection); $shipmentItemStore->basketItem = $basketItem; return $shipmentItemStore; }
/** * @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(); if ($order && $order->getId() > 0) { OrderHistory::collectEntityFields('SHIPMENT_ITEM_STORE', $order->getId(), $id); } } 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(); if (!$shipment->isSystem()) { OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ITEM_BASKET_ADDED', $shipment->getId(), $this->basketItem, array('QUANTITY' => $this->getQuantity())); } } if ($eventName = static::getEntityEventName()) { $oldEntityValues = $this->fields->getOriginalValues(); if (!empty($oldEntityValues)) { /** @var Main\Event $event */ $event = new Main\Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues)); $event->send(); } } $shipmentItemStoreCollection = $this->getShipmentItemStoreCollection(); $r = $shipmentItemStoreCollection->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if ($result->isSuccess()) { OrderHistory::collectEntityFields('SHIPMENT_ITEM', $order->getId(), $id); } $this->fields->clearChanged(); return $result; }
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; }