/** * Modify basket. * * @param string $action Action. * @param BasketItem $basketItem Basket item. * @param null|string $name Field name. * @param null|string|int|float $oldValue Old value. * @param null|string|int|float $value New value. * @return Result * @throws Main\NotImplementedException * @throws Main\NotSupportedException * @throws Main\ObjectNotFoundException */ public function onBasketModify($action, BasketItem $basketItem, $name = null, $oldValue = null, $value = null) { if ($action != EventActions::UPDATE) { return new Result(); } if ($name == "QUANTITY") { if ($value < 0) { $result = new Result(); $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_BASKET_WRONG_QUANTITY', array('#PRODUCT_NAME#' => $basketItem->getField('NAME'))), 'SALE_ORDER_BASKET_WRONG_QUANTITY')); return $result; } /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $this->getShipmentCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } $result = $shipmentCollection->onBasketModify($action, $basketItem, $name, $oldValue, $value); if (!$result->isSuccess()) { return $result; } if ($value == 0) { /** @var Result $r */ $r = $this->refreshVat(); if (!$r->isSuccess()) { return $r; } if ($tax = $this->getTax()) { $tax->refreshData(); } } if ($basketItem->isBundleChild()) { return $result; } /** @var Result $result */ $result = $this->setField("PRICE", $this->getBasket()->getPrice() + $this->getShipmentCollection()->getPriceDelivery()); if ($this->getId() == 0 && !$this->isMathActionOnly()) { $shipmentCollection->refreshData(); } return $result; } elseif ($name == "PRICE") { /** @var Result $result */ $result = $this->setField("PRICE", $this->getBasket()->getPrice() + $this->getShipmentCollection()->getPriceDelivery()); /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $this->getShipmentCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } if ($this->getId() == 0 && !$this->isMathActionOnly()) { $shipmentCollection->refreshData(); } return $result; } elseif ($name == "CURRENCY") { if ($value != $this->getField("CURRENCY")) { throw new Main\NotSupportedException("CURRENCY"); } } elseif ($name == "DIMENSIONS") { /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $this->getShipmentCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } return $shipmentCollection->onBasketModify($action, $basketItem, $name, $oldValue, $value); } return new Result(); }
/** * @param \Bitrix\Sale\BasketItem $basketItem * @param string $reserved * @param array $basketStoreData * @param null $quantity * @return \Bitrix\Sale\Result */ public static function tryShipmentProduct(\Bitrix\Sale\BasketItem $basketItem, $reserved = 'N', array $basketStoreData = array(), $quantity = null) { $result = new \Bitrix\Sale\Result(); $storesList = array(); $strUseStoreControl = COption::GetOptionString('catalog', 'default_use_store_control'); $disableReservation = COption::GetOptionString("catalog", "enable_reservation") == "N" && COption::GetOptionString("sale", "product_reserve_condition", "O") != "S" && $strUseStoreControl != "Y"; if ($disableReservation) { return $result; } $productId = $basketItem->getProductId(); // if ((int)$arParams["PRODUCT_ID"] <= 0) // { // $APPLICATION->ThrowException(Loc::getMessage("RSRV_INCORRECT_ID"), "NO_ORDER_ID"); // $arRes["RESULT"] = false; // return $arRes; // } $rsProducts = CCatalogProduct::GetList(array(), array('ID' => $productId), false, false, array('ID', 'QUANTITY', 'QUANTITY_RESERVED', 'QUANTITY_TRACE', 'CAN_BUY_ZERO')); if ($arProduct = $rsProducts->Fetch()) { if ($strUseStoreControl == "Y") { if (empty($basketStoreData)) { $countStores = static::GetStoresCount(array('SITE_ID' => $basketItem->getField('LID'))); $defaultDeductionStore = Main\Config\Option::get("sale", "deduct_store_id", "", $basketItem->getField('LID')); if (($countStores == 1 || $countStores == -1 || $defaultDeductionStore > 0) && !$basketItem->isBarcodeMulti()) { if ($productStore = static::GetProductStores(array('PRODUCT_ID' => $productId, 'SITE_ID' => $basketItem->getField('LID')))) { $productStore = reset($productStore); $basketStoreData = array($productStore['STORE_ID'] => array('QUANTITY' => $quantity)); } } } if (!empty($basketStoreData)) { $totalAmount = 0; foreach ($basketStoreData as $storeId => $basketStore) { if (intval($storeId) < -1 || intval($storeId) == 0 || !isset($basketStore["QUANTITY"]) || intval($basketStore["QUANTITY"]) < 0) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_STORE_ERROR", self::GetProductCatalogInfo($productId)), "DDCT_DEDUCTION_STORE_ERROR")); return $result; } if (intval($storeId) == -1) { $totalAmount = intval($basketStore["QUANTITY"]); } else { $rsProps = CCatalogStoreProduct::GetList(array(), array("PRODUCT_ID" => $productId, "STORE_ID" => $storeId), false, false, array('ID', 'AMOUNT')); if ($arProp = $rsProps->Fetch()) { if ($arProp["AMOUNT"] < $basketStore["QUANTITY"]) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_QUANTITY_STORE_ERROR", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $storeId))), "DDCT_DEDUCTION_QUANTITY_STORE_ERROR")); return $result; } else { $storesList[$storeId] = $basketStore["QUANTITY"]; $totalAmount += $basketStore["QUANTITY"]; //check barcodes if (isset($basketStore["BARCODE"]) && is_array($basketStore["BARCODE"]) && count($basketStore["BARCODE"]) > 0) { foreach ($basketStore["BARCODE"] as $barcodeId => $barcodeValue) { if (strval(trim($barcodeValue)) == "") { if ($basketItem->isBarcodeMulti()) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_MULTI_BARCODE_EMPTY", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $basketStore['STORE_ID']))), "DDCT_DEDUCTION_MULTI_BARCODE_EMPTY")); } continue; } $arFields = array("STORE_ID" => static::CATALOG_PROVIDER_EMPTY_STORE_ID, "BARCODE" => $barcodeValue, "PRODUCT_ID" => $productId); if ($basketItem->isBarcodeMulti()) { $arFields['STORE_ID'] = $storeId; } $dbres = CCatalogStoreBarcode::GetList(array(), $arFields, false, false, array("ID", "STORE_ID", "BARCODE", "PRODUCT_ID")); if (!($arRes = $dbres->Fetch())) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_BARCODE_ERROR", array_merge(self::GetProductCatalogInfo($productId), array("#BARCODE#" => $barcodeValue))), "DDCT_DEDUCTION_BARCODE_ERROR")); } } } elseif ($basketItem->isBarcodeMulti()) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_MULTI_BARCODE_EMPTY", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $basketStore['STORE_ID']))), "DDCT_DEDUCTION_MULTI_BARCODE_EMPTY")); } } } } if (!$result->isSuccess(true)) { return $result; } if ($reserved == 'Y') { $reservedPoolQuantity = static::getProductPoolQuantityByBasketItem($basketItem); $reservedQuantity = $arProduct["QUANTITY_RESERVED"] + floatval($reservedPoolQuantity); } $productQuantity = $reserved == 'Y' ? $reservedQuantity : $arProduct["QUANTITY"]; /*if (($totalAmount > $productQuantity) || ($totalAmount > $reservedQuantity + $arProduct["QUANTITY"]))*/ if ($totalAmount > $arProduct["QUANTITY_RESERVED"] + $arProduct["QUANTITY"]) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH", self::GetProductCatalogInfo($productId)), "SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH")); return $result; } } } else { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_STORE_ERROR", self::GetProductCatalogInfo($productId)), "DEDUCTION_STORE_ERROR1")); return $result; } } else { $reservedPoolQuantity = static::getProductPoolQuantityByBasketItem($basketItem); $reservedQuantity = $arProduct["QUANTITY_RESERVED"] + floatval($reservedPoolQuantity); if ($quantity > $reservedQuantity + $arProduct["QUANTITY"]) { $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_QUANTITY_ERROR", self::GetProductCatalogInfo($productId)), "DDCT_DEDUCTION_QUANTITY_ERROR")); return $result; } // $arRes["RESULT"] = true; } //store control } 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; }
/** * @param BasketItem $item * * @return bool */ public function isItemExists(BasketItem $item) { $propertyList = array(); /** @var BasketPropertiesCollection $basketPropertyCollection */ if ($basketPropertyCollection = $item->getPropertyCollection()) { /** @var BasketPropertyItem $basketPropertyItem */ foreach ($basketPropertyCollection as $basketPropertyItem) { $propertyList[$basketPropertyItem->getId()] = array('ID' => $basketPropertyItem->getId(), 'NAME' => $basketPropertyItem->getField('NAME'), 'VALUE' => $basketPropertyItem->getField('VALUE'), 'CODE' => $basketPropertyItem->getField('CODE'), 'SORT' => $basketPropertyItem->getField('SORT')); } } return (bool) $this->getExistsItem($item->getField('MODULE'), $item->getProductId(), $propertyList); }
/** * @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; }