/** * @return Entity\Result|bool * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\ArgumentNullException */ public function save() { $result = new Result(); /** @var Order $order */ $order = $this->getOrder(); $itemsFromDb = array(); $filter = array(); if (!$order) { /** @var Main\Entity\Event $event */ $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_BEFORE_SAVED, array('ENTITY' => $this)); $event->send(); if ($event->getResults()) { $result = new Result(); /** @var Main\EventResult $eventResult */ foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == Main\EventResult::ERROR) { $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_SAVED'); if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) { $errorMsg = $eventResultData['ERROR']; } $result->addError($errorMsg); } } if (!$result->isSuccess()) { return $result; } } } $isNew = $order && $order->isNew() ? true : false; if ($order && !$isNew) { $filter['ORDER_ID'] = $order->getId(); } else { if ($this->isLoadForFuserId()) { $filter = array('FUSER_ID' => $this->getFUserId(), 'ORDER_ID' => null, 'LID' => $this->getSiteId()); } if ($isNew) { $fUserId = $this->getFUserId(true); if ($fUserId <= 0) { $userId = $order->getUserId(); if (intval($userId) > 0) { $fUserId = Fuser::getIdByUserId($userId); if ($fUserId > 0) { $this->setFUserId($fUserId); } } } } } if (!empty($filter)) { $itemsFromDbList = Internals\BasketTable::getList(array("filter" => $filter, "select" => array("ID", 'TYPE', 'SET_PARENT_ID', 'PRODUCT_ID', 'NAME', 'QUANTITY'))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { if (intval($itemsFromDbItem['SET_PARENT_ID']) > 0 && intval($itemsFromDbItem['SET_PARENT_ID']) != $itemsFromDbItem['ID']) { continue; } $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem; } } /** @var BasketItem $basketItem */ foreach ($this->collection as $index => $basketItem) { $r = $basketItem->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$basketItem->getId()]) && $basketItem->getQuantity() > 0) { unset($itemsFromDb[$basketItem->getId()]); } } if (!empty($filter)) { foreach ($itemsFromDb as $k => $v) { if ($v['TYPE'] == static::TYPE_SET) { Internals\BasketTable::deleteBundle($k); } else { Internals\BasketTable::deleteWithItems($k); } /** @var Order $order */ if ($order && $order->getId() > 0) { OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_REMOVED', $k, null, array('NAME' => $v['NAME'], 'QUANTITY' => $v['QUANTITY'], 'PRODUCT_ID' => $v['PRODUCT_ID'])); } } } if ($order && $order->getId() > 0) { OrderHistory::collectEntityFields('BASKET', $order->getId()); } if (!$order) { /** @var Main\Entity\Event $event */ $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_SAVED, array('ENTITY' => $this)); $event->send(); if ($event->getResults()) { $result = new Result(); /** @var Main\EventResult $eventResult */ foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == Main\EventResult::ERROR) { $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BASKET_SAVED'), 'SALE_EVENT_ON_BASKET_SAVED'); if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) { $errorMsg = $eventResultData['ERROR']; } $result->addError($errorMsg); } } if (!$result->isSuccess()) { return $result; } } } return $result; }
/** * @return Entity\Result|bool * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\ArgumentNullException */ public function save() { $result = new Result(); /** @var Order $order */ $order = $this->getOrder(); $itemsFromDb = array(); $filter = array(); if (!$order) { /** @var Main\Entity\Event $event */ $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_BEFORE_SAVED, array('ENTITY' => $this)); $event->send(); if ($event->getResults()) { /** @var Main\EventResult $eventResult */ foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == Main\EventResult::ERROR) { $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_SAVED'); if ($eventResultData = $eventResult->getParameters()) { if (isset($eventResultData) && $eventResultData instanceof ResultError) { /** @var ResultError $errorMsg */ $errorMsg = $eventResultData; } } $result->addError($errorMsg); } } if (!$result->isSuccess()) { return $result; } } } $isNew = $order && $order->isNew() ? true : false; if ($order && !$isNew) { $filter['ORDER_ID'] = $order->getId(); } else { if ($this->isLoadForFuserId()) { $filter = array('FUSER_ID' => $this->getFUserId(), 'ORDER_ID' => null, 'LID' => $this->getSiteId()); } if ($isNew) { $fUserId = $this->getFUserId(true); if ($fUserId <= 0) { $userId = $order->getUserId(); if (intval($userId) > 0) { $fUserId = Fuser::getIdByUserId($userId); if ($fUserId > 0) { $this->setFUserId($fUserId); } } } } } if (!empty($filter)) { $itemsFromDbList = Internals\BasketTable::getList(array("filter" => $filter, "select" => array("ID", 'TYPE', 'SET_PARENT_ID', 'PRODUCT_ID', 'NAME', 'QUANTITY', 'FUSER_ID', 'ORDER_ID'))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { if (intval($itemsFromDbItem['SET_PARENT_ID']) > 0 && intval($itemsFromDbItem['SET_PARENT_ID']) != $itemsFromDbItem['ID']) { continue; } $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem; } } if ($order && ($order->getId() > 0 && $this->getOrderId() == 0)) { $this->orderId = $order->getId(); } $changeMeaningfulFields = array("PRODUCT_ID", "QUANTITY", "PRICE", "DISCOUNT_VALUE", "VAT_RATE", "NAME"); /** @var BasketItem $basketItem */ foreach ($this->collection as $index => $basketItem) { $isNew = (bool) ($basketItem->getId() <= 0); $isChanged = $basketItem->isChanged(); if ($order && $order->getId() > 0 && $isChanged) { $logFields = array(); $fields = $basketItem->getFields(); $originalValues = $fields->getOriginalValues(); foreach ($originalValues as $originalFieldName => $originalFieldValue) { if (in_array($originalFieldName, $changeMeaningfulFields) && $basketItem->getField($originalFieldName) != $originalFieldValue) { $logFields[$originalFieldName] = $basketItem->getField($originalFieldName); $logFields['OLD_' . $originalFieldName] = $originalFieldValue; } } } $r = $basketItem->save(); if ($r->isSuccess()) { if ($order && $order->getId() > 0) { if ($isChanged) { OrderHistory::addLog('BASKET', $order->getId(), $isNew ? "BASKET_ITEM_ADD" : "BASKET_ITEM_UPDATE", $basketItem->getId(), $basketItem, $logFields, OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); OrderHistory::addAction('BASKET', $order->getId(), "BASKET_SAVED", $basketItem->getId(), $basketItem); } } } else { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$basketItem->getId()]) && $basketItem->getQuantity() > 0) { unset($itemsFromDb[$basketItem->getId()]); } } if (!empty($filter)) { $itemEventName = BasketItem::getEntityEventName(); foreach ($itemsFromDb as $k => $v) { /** @var Main\Event $event */ $event = new Main\Event('sale', "OnBefore" . $itemEventName . "Deleted", array('VALUES' => $v)); $event->send(); if ($v['TYPE'] == static::TYPE_SET) { if ($order && $order->getId() > 0) { OrderHistory::addLog('BASKET', $order->getId(), 'BASKET_ITEM_DELETE_BUNDLE', $k, null, array("PRODUCT_ID" => $v["PRODUCT_ID"], "NAME" => $v["NAME"], "QUANTITY" => $v["QUANTITY"]), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); } Internals\BasketTable::deleteBundle($k); } else { if ($order && $order->getId() > 0) { OrderHistory::addLog('BASKET', $order->getId(), 'BASKET_ITEM_DELETED', $k, null, array("PRODUCT_ID" => $v["PRODUCT_ID"], "NAME" => $v["NAME"], "QUANTITY" => $v["QUANTITY"]), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); } Internals\BasketTable::deleteWithItems($k); } /** @var Main\Event $event */ $event = new Main\Event('sale', "On" . $itemEventName . "Deleted", array('VALUES' => $v)); $event->send(); /** @var Order $order */ if ($order && $order->getId() > 0) { OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_REMOVED', $k, null, array('NAME' => $v['NAME'], 'QUANTITY' => $v['QUANTITY'], 'PRODUCT_ID' => $v['PRODUCT_ID'])); } } } if ($order && $order->getId() > 0) { OrderHistory::collectEntityFields('BASKET', $order->getId()); } if (!$order) { /** @var Main\Entity\Event $event */ $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_SAVED, array('ENTITY' => $this)); $event->send(); if ($event->getResults()) { /** @var Main\EventResult $eventResult */ foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == Main\EventResult::ERROR) { $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BASKET_SAVED'), 'SALE_EVENT_ON_BASKET_SAVED'); if ($eventResultData = $eventResult->getParameters()) { if (isset($eventResultData) && $eventResultData instanceof ResultError) { /** @var ResultError $errorMsg */ $errorMsg = $eventResultData; } } $result->addError($errorMsg); } } if (!$result->isSuccess()) { return $result; } } } return $result; }