Exemplo n.º 1
0
 /**
  * @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;
 }