/** * @param string $name * @param null $oldValue * @param null $value * @throws ObjectNotFoundException */ protected function addChangesToHistory($name, $oldValue = null, $value = null) { if ($this->getId() > 0) { $fields = array(); /** @var Basket $basket */ if (!($basket = $this->getCollection())) { throw new ObjectNotFoundException('Entity "Basket" not found'); } if ($basket->getOrder() && $basket->getOrderId() > 0) { if ($name == "QUANTITY") { if (floatval($value) == 0) { return; } $fields = array('PRODUCT_ID' => $this->getProductId(), 'QUANTITY' => $this->getQuantity(), 'NAME' => $this->getField('NAME')); } OrderHistory::addField('BASKET', $basket->getOrderId(), $name, $oldValue, $value, $this->getId(), $this, $fields); } } }
/** * Save field modify to history. * * @param string $name Field name. * @param null|string $oldValue Old value. * @param null|string $value New value. */ protected function addChangesToHistory($name, $oldValue = null, $value = null) { if ($this->getId() > 0) { $historyFields = array(); if ($name == "PRICE") { $historyFields = array('OLD_PRICE' => $oldValue, 'CURRENCY' => $this->getCurrency()); } OrderHistory::addField('ORDER', $this->getId(), $name, $oldValue, $value, $this->getId(), $this, $historyFields); } }
/** * @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; }
/** * @param string $name * @param null $oldValue * @param null $value * @throws Main\ObjectNotFoundException */ protected function addChangesToHistory($name, $oldValue = null, $value = null) { if ($this->getId() > 0 && !$this->isSystem()) { /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } /** @var Order $order */ if (($order = $shipmentCollection->getOrder()) && $order->getId() > 0) { OrderHistory::addField('SHIPMENT', $order->getId(), $name, $oldValue, $value, $this->getId(), $this); } } }
/** * Save field modify to history. * * @param string $name Field name. * @param null|string $oldValue Old value. * @param null|string $value New value. */ protected function addChangesToHistory($name, $oldValue = null, $value = null) { if ($this->getId() > 0) { OrderHistory::addField('ORDER', $this->getId(), $name, $oldValue, $value, $this->getId()); } }
/** * @param string $name * @param null|string $oldValue * @param null|string $value * @throws Main\ObjectNotFoundException */ protected function addChangesToHistory($name, $oldValue = null, $value = null) { if ($this->getId() > 0) { /** @var ShipmentItemStoreCollection $shipmentItemStoreCollection */ if (!($shipmentItemStoreCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItemStoreCollection" not found'); } /** @var ShipmentItem $shipmentItem */ if (!($shipmentItem = $shipmentItemStoreCollection->getShipmentItem())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItem" not found'); } /** @var ShipmentItemCollection $shipmentItemCollection */ if (!($shipmentItemCollection = $shipmentItem->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()) { return; } /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $shipment->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } /** @var Order $order */ if (($order = $shipmentCollection->getOrder()) && $order->getId() > 0) { $historyFields = array(); /** @var BasketItem $basketItem */ if ($basketItem = $shipmentItem->getBasketItem()) { $historyFields = array('NAME' => $basketItem->getField('NAME'), 'PRODUCT_ID' => $basketItem->getField('PRODUCT_ID')); } OrderHistory::addField('SHIPMENT_ITEM_STORE', $order->getId(), $name, $oldValue, $value, $this->getId(), $this, $historyFields); } } }
/** * @return Entity\Result * @throws Main\ArgumentException * @throws Main\ObjectNotFoundException * @throws \Exception */ public function save() { $result = new Entity\Result(); $itemsFromDb = array(); if ($this->getOrder()->getId() > 0) { $itemsFromDbList = Internals\PaymentTable::getList(array("filter" => array("ORDER_ID" => $this->getOrder()->getId()), "select" => array("ID"))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { $itemsFromDb[$itemsFromDbItem["ID"]] = true; } } /** @var Payment $payment */ foreach ($this->collection as $payment) { if ($payment->isInner() && $payment->getSum() == 0 && $payment->getId() == 0) { $payment->delete(); } } /** @var Payment $payment */ foreach ($this->collection as $payment) { $r = $payment->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$payment->getId()])) { unset($itemsFromDb[$payment->getId()]); } } foreach ($itemsFromDb as $k => $v) { Internals\PaymentTable::delete($k); /** @var Order $order */ if (!($order = $this->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } if ($order->getId() > 0) { OrderHistory::addAction('PAYMENT', $order->getId(), 'PAYMENT_REMOVE', $k); } } return $result; }
/** * @return Entity\Result * @throws Main\ArgumentException * @throws Main\ArgumentNullException * @throws Main\ObjectNotFoundException */ public function save() { $result = new Entity\Result(); $itemsFromDb = array(); if ($this->getOrder()->getId() > 0) { $itemsFromDbList = Internals\ShipmentTable::getList(array("filter" => array("ORDER_ID" => $this->getOrder()->getId()), "select" => array("ID", "DELIVERY_NAME"))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem; } } /** @var Shipment $shipment */ foreach ($this->collection as $shipment) { if ($shipment->isSystem()) { continue; } if (($systemShipment = $this->getSystemShipment()) && $systemShipment->getId() == 0) { /** @var Result $r */ $r = $this->cloneShipment($shipment, $systemShipment); if ($r->isSuccess()) { break; } else { $result->addErrors($r->getErrors()); } } } /** @var Shipment $shipment */ foreach ($this->collection as $shipment) { $r = $shipment->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$shipment->getId()])) { unset($itemsFromDb[$shipment->getId()]); } } foreach ($itemsFromDb as $k => $v) { Internals\ShipmentTable::deleteWithItems($k); /** @var Order $order */ if (!($order = $this->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } if ($order->getId() > 0) { OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_REMOVED', $k, null, array('ID' => $k, 'DELIVERY_NAME' => $v['DELIVERY_NAME'])); } } if ($this->getOrder()->getId() > 0) { OrderHistory::collectEntityFields('SHIPMENT', $this->getOrder()->getId()); } return $result; }
/** * @return Main\Entity\Result * @throws Main\ArgumentException * @throws Main\ArgumentNullException * @throws Main\ArgumentOutOfRangeException * @throws Main\ObjectNotFoundException */ public function save() { $result = new Main\Entity\Result(); $itemsFromDb = array(); if ($this->getShipment()->getId() > 0) { $itemsFromDbList = Internals\ShipmentItemTable::getList(array("filter" => array("ORDER_DELIVERY_ID" => $this->getShipment()->getId()), "select" => array("ID", 'BASKET_ID'))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem; } } /** @var ShipmentItem $shipmentItem */ foreach ($this->collection as $shipmentItem) { /** @var BasketItem $basketItem */ if (!($basketItem = $shipmentItem->getBasketItem())) { throw new Main\ObjectNotFoundException('Entity "BasketItem" not found'); } if ($basketItem->isBundleParent()) { $this->addBundleToCollection($basketItem); } } /** @var Shipment $shipment */ if (!($shipment = $this->getShipment())) { throw new Main\ObjectNotFoundException('Entity "Shipment" not found'); } /** @var ShipmentItem $shipmentItem */ foreach ($this->collection as $shipmentItem) { if ($shipment->isSystem() && $shipmentItem->getQuantity() == 0) { continue; } $r = $shipmentItem->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$shipmentItem->getId()])) { unset($itemsFromDb[$shipmentItem->getId()]); } } /** @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'); } /** @var Basket $basket */ if (!($basket = $order->getBasket())) { throw new Main\ObjectNotFoundException('Entity "Basket" not found'); } foreach ($itemsFromDb as $k => $v) { Internals\ShipmentItemTable::deleteWithItems($k); /** @var BasketItem $basketItem */ if ($basketItem = $basket->getItemById($k)) { OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ITEM_BASKET_REMOVED', $shipment->getId(), null, array('NAME' => $basketItem->getField('NAME'), 'QUANTITY' => $basketItem->getQuantity(), 'PRODUCT_ID' => $basketItem->getProductId())); } } return $result; }
static function DoSaveOrderTax($orderId, $taxList, &$arErrors) { $duplicateList = array(); $idList = array(); $res = CSaleOrderTax::GetList(array('ID' => 'ASC'), array("ORDER_ID" => $orderId), false, false, array("ID", "TAX_NAME", "CODE")); while ($data = $res->Fetch()) { $hash = $data["TAX_NAME"] . "|" . $data["CODE"]; if (!array_key_exists($hash, $idList)) { $idList[$data["TAX_NAME"] . "|" . $data["CODE"]] = $data["ID"]; } else { $duplicateList[$hash] = $data['ID']; } } $isChanged = false; if (is_array($taxList)) { foreach ($taxList as $itemData) { $fields = array("ORDER_ID" => $orderId, "TAX_NAME" => $itemData["NAME"], "IS_PERCENT" => $itemData["IS_PERCENT"], "VALUE" => $itemData["VALUE"], "VALUE_MONEY" => $itemData["VALUE_MONEY"], "APPLY_ORDER" => $itemData["APPLY_ORDER"], "IS_IN_PRICE" => $itemData["IS_IN_PRICE"], "CODE" => $itemData["CODE"]); $hash = $itemData["NAME"] . "|" . $itemData["CODE"]; $isNew = false; if (array_key_exists($hash, $idList)) { $taxId = CSaleOrderTax::Update($idList[$hash], $fields); unset($idList[$hash]); } elseif (!array_key_exists($hash, $duplicateList)) { $isNew = true; $taxId = CSaleOrderTax::Add($fields); } if ($orderId > 0) { \Bitrix\Sale\OrderHistory::addLog('TAX', $orderId, $isNew ? 'TAX_ADD' : 'TAX_UPDATE', $taxId, null, array("NAME" => $itemData["NAME"], "CODE" => $itemData["CODE"]), \Bitrix\Sale\OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); $isChanged = true; } } } foreach ($idList as $code => $id) { CSaleOrderTax::Delete($id); if ($orderId > 0) { \Bitrix\Sale\OrderHistory::addLog('TAX', $orderId, 'TAX_DELETED', $id, null, array(), \Bitrix\Sale\OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); } } if (!empty($duplicateList)) { foreach ($duplicateList as $hash => $id) { CSaleOrderTax::Delete($id); \Bitrix\Sale\OrderHistory::addLog('TAX', $orderId, 'TAX_DUPLICATE_DELETED', $id, null, array(), \Bitrix\Sale\OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1); } } if ($isChanged) { \Bitrix\Sale\OrderHistory::addAction('TAX', $orderId, "TAX_SAVED"); } }
/** * @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; }