/** * @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; }
/** * @return Entity\AddResult|Entity\UpdateResult * @throws Main\ArgumentOutOfRangeException * @throws Main\ObjectNotFoundException * @throws \Exception */ public function save() { $id = $this->getId(); $fields = $this->fields->getValues(); if ($id > 0) { $fields = $this->fields->getChangedValues(); if (!empty($fields) && is_array($fields)) { // $fields['DATE_UPDATE'] = new Main\Type\DateTime(); $r = Internals\ShipmentTable::update($id, $fields); if (!$r->isSuccess()) { return $r; } } $result = new Entity\UpdateResult(); if (!empty($fields['TRACKING_NUMBER'])) { $oldEntityValues = $this->fields->getOriginalValues(); /** @var Main\Event $event */ $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_TRACKING_NUMBER_CHANGE, array('ENTITY' => $this, 'VALUES' => $oldEntityValues)); $event->send(); } } else { $fields['ORDER_ID'] = $this->getParentOrderId(); $fields['DATE_INSERT'] = new Main\Type\DateTime(); $fields['SYSTEM'] = $fields['SYSTEM'] ? 'Y' : 'N'; $r = Internals\ShipmentTable::add($fields); if (!$r->isSuccess()) { return $r; } $id = $r->getId(); $this->setFieldNoDemand('ID', $id); $result = new Entity\AddResult(); /** @var ShipmentItemCollection $shipmentItemCollection */ if (!($shipmentItemCollection = $this->getShipmentItemCollection())) { 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 ($order->getId() > 0 && !$this->isSystem()) { OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ADDED', $id, $this); } } if ($result->isSuccess() && !$this->isSystem()) { $this->saveExtraServices(); $this->saveStoreId(); } /** @var ShipmentItemCollection $shipmentItemCollection */ if (!($shipmentItemCollection = $this->getShipmentItemCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found'); } $r = $shipmentItemCollection->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if ($result->isSuccess()) { /** @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 (!$this->isSystem()) { OrderHistory::collectEntityFields('SHIPMENT', $order->getId(), $id); } } return $result; }
/** * @return Entity\AddResult|Entity\UpdateResult * @throws ArgumentException * @throws ArgumentNullException * @throws \Exception */ public function save() { $id = $this->getId(); $changedFields = $this->fields->getChangedValues(); if ($id == 0 || !empty($changedFields)) { /** @var Event $event */ $event = new Event('sale', EventActions::EVENT_ON_BASKET_ITEM_BEFORE_SAVED, array($this)); $event->send(); if ($event->getResults()) { $result = new Result(); /** @var EventResult $eventResult */ foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == EventResult::ERROR) { $errorMsg = new ResultError(Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED'); if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) { $errorMsg = $eventResultData['ERROR']; } $result->addError($errorMsg); } } if (!$result->isSuccess()) { return $result; } } } $fields = $this->fields->getValues(); $newItem = $id > 0 ? false : true; if ($this->isBundleParent()) { $bundleBasketCollection = $this->getBundleCollection(); } if ($id > 0) { $fields = $changedFields; if (!isset($fields["ORDER_ID"]) || intval($fields["ORDER_ID"]) == 0) { $orderId = null; if ($this->getParentOrderId() > 0) { $orderId = $this->getParentOrderId(); } if ($this->isBundleChild() && $orderId === null) { /** @var BasketItem $parentBasket */ if (!($parentBasket = $this->getParentBasketItem())) { throw new ObjectNotFoundException('Entity parent "BasketItem" not found'); } $orderId = $parentBasket->getParentOrderId(); } if (intval($orderId) > 0) { $fields['ORDER_ID'] = $orderId; } } if (!empty($fields) && is_array($fields)) { if (isset($fields["QUANTITY"]) && floatval($fields["QUANTITY"]) == 0) { return new Entity\UpdateResult(); } $fields['DATE_UPDATE'] = new DateTime(); $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']); $r = Internals\BasketTable::update($id, $fields); if (!$r->isSuccess()) { return $r; } } $result = new Entity\UpdateResult(); } else { $fields['ORDER_ID'] = $this->getParentOrderId(); $fields['DATE_INSERT'] = new DateTime(); $fields['DATE_UPDATE'] = new DateTime(); $this->setFieldNoDemand('DATE_INSERT', $fields['DATE_INSERT']); $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']); if (!$this->isBundleChild() && (!isset($fields["FUSER_ID"]) || intval($fields["FUSER_ID"]) <= 0)) { /** @var Basket $basket */ if (!($basket = $this->getCollection())) { throw new ObjectNotFoundException('Entity "Basket" not found'); } $fields["FUSER_ID"] = intval($basket->getFUserId()); } /** @var Basket $basket */ if (!($basket = $this->getCollection())) { throw new ObjectNotFoundException('Entity "Basket" not found'); } /** @var Order $order */ if ($order = $basket->getOrder()) { if (!isset($fields["LID"]) || strval($fields["LID"]) == '') { $fields['LID'] = $order->getField('LID'); } } else { if ($siteId = $basket->getSiteId()) { $fields['LID'] = $siteId; } } if ($this->isBundleChild()) { if (!($parentBasketItem = $this->getParentBasketItem())) { throw new ObjectNotFoundException('Entity parent "BasketItem" not found'); } $fields['LID'] = $parentBasketItem->getField('LID'); if (!isset($fields["FUSER_ID"]) || intval($fields["FUSER_ID"]) <= 0) { $fields['FUSER_ID'] = intval($parentBasketItem->getField('FUSER_ID')); } } if (!isset($fields["LID"]) || strval(trim($fields["LID"])) == '') { throw new ArgumentNullException('lid'); } if ($this->isBundleChild() && (!isset($fields["SET_PARENT_ID"]) || intval($fields["QUANTITY"]) <= 0)) { $fields["SET_PARENT_ID"] = $this->getParentBasketItemId(); $this->setFieldNoDemand('SET_PARENT_ID', $fields['SET_PARENT_ID']); } if (!isset($fields["QUANTITY"]) || floatval($fields["QUANTITY"]) == 0) { return new Entity\AddResult(); } if (!isset($fields["CURRENCY"]) || strval(trim($fields["CURRENCY"])) == '') { throw new ArgumentNullException('currency'); } $r = Internals\BasketTable::add($fields); if (!$r->isSuccess()) { return $r; } $id = $r->getId(); $this->setFieldNoDemand('ID', $id); $this->setFieldNoDemand('LID', $fields['LID']); $this->setFieldNoDemand('FUSER_ID', $fields['FUSER_ID']); if ($basket->getOrder() && $basket->getOrderId() > 0) { OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_ADDED', $id, array('QUANTITY' => $this->getQuantity(), 'PRODUCT_ID' => $this->getProductId(), 'NAME' => $this->getField('NAME'))); } $result = new Entity\AddResult(); } // bundle if ($this->isBundleParent()) { if (!empty($bundleBasketCollection)) { if (!($order = $bundleBasketCollection->getOrder())) { /** @var Basket $basketCollection */ $basketCollection = $this->getCollection(); if ($order = $basketCollection->getOrder()) { $bundleBasketCollection->setOrder($order); } } $itemsFromDb = array(); if (!$newItem) { $itemsFromDbList = Internals\BasketTable::getList(array("filter" => array("SET_PARENT_ID" => $id), "select" => array("ID"))); while ($itemsFromDbItem = $itemsFromDbList->fetch()) { if ($itemsFromDbItem["ID"] == $id) { continue; } $itemsFromDb[$itemsFromDbItem["ID"]] = true; } } /** @var BasketItem $bundleBasketItem */ foreach ($bundleBasketCollection as $bundleBasketItem) { $r = $bundleBasketItem->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } if (isset($itemsFromDb[$bundleBasketItem->getId()])) { unset($itemsFromDb[$bundleBasketItem->getId()]); } } foreach ($itemsFromDb as $k => $v) { Internals\BasketTable::delete($k); } } } /** @var BasketPropertiesCollection $basketPropertyCollection */ $basketPropertyCollection = $this->getPropertyCollection(); $r = $basketPropertyCollection->save(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } return $result; }