Пример #1
0
 /**
  * @param $id
  * @return Main\Entity\DeleteResult
  * @throws Main\ArgumentException
  * @throws Main\ArgumentNullException
  */
 public static function deleteWithItems($id)
 {
     $id = intval($id);
     if ($id <= 0) {
         throw new Main\ArgumentNullException("id");
     }
     $itemsList = ShipmentItemTable::getList(array("filter" => array("ORDER_DELIVERY_ID" => $id), "select" => array("ID")));
     while ($item = $itemsList->fetch()) {
         ShipmentItemTable::deleteWithItems($item["ID"]);
     }
     return ShipmentTable::delete($id);
 }
 /**
  * @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;
 }