/** * @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; }
/** * @param int $shipmentId * @param StatusResult $params * @param bool|false $isStatusChanged * @return Result * @throws ArgumentNullException * @throws \Exception */ public function updateShipment($shipmentId, StatusResult $params) { if ($shipmentId <= 0) { throw new ArgumentNullException('id'); } $dateTime = new \Bitrix\Main\Type\DateTime(); return ShipmentTable::update($shipmentId, array('TRACKING_STATUS' => $params->status, 'TRACKING_LAST_CHECK' => $dateTime, 'TRACKING_LAST_CHANGE' => \Bitrix\Main\Type\DateTime::createFromTimestamp($params->lastChangeTimestamp), 'TRACKING_DESCRIPTION' => $params->description, 'TRACKING_NUMBER' => $params->trackingNumber)); }