/** * @return Main\Entity\AddResult|Main\Entity\UpdateResult * @throws Main\ArgumentNullException * @throws Main\ArgumentOutOfRangeException * @throws \Exception */ public function save() { global $USER; $result = new Result(); $id = $this->getId(); $fields = $this->fields->getValues(); /** @var ShipmentItemStoreCollection $collection */ $collection = $this->getCollection(); /** @var Result $r */ $r = $collection->checkAvailableQuantity($this); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); return $result; } /** @var BasketItem $basketItem */ $basketItem = $this->getBasketItem(); if ($id > 0) { $fields = $this->fields->getChangedValues(); if (!empty($fields) && is_array($fields)) { if (isset($fields["QUANTITY"]) && floatval($fields["QUANTITY"]) <= 0) { throw new Main\ArgumentNullException('quantity'); } $fields['DATE_MODIFY'] = new Main\Type\DateTime(); $fields['MODIFIED_BY'] = $USER->GetID(); $r = Internals\ShipmentItemStoreTable::update($id, $fields); if (!$r->isSuccess()) { return $r; } } $result = new Main\Entity\UpdateResult(); } else { if (!isset($fields["ORDER_DELIVERY_BASKET_ID"])) { $fields['ORDER_DELIVERY_BASKET_ID'] = $this->getParentShipmentItemId(); } if (!isset($fields["BASKET_ID"])) { $fields['BASKET_ID'] = $basketItem->getId(); } $fields['DATE_CREATE'] = new Main\Type\DateTime(); if (!isset($fields["QUANTITY"]) || floatval($fields["QUANTITY"]) == 0) { return new Main\Entity\AddResult(); } if ($basketItem->isBarcodeMulti() && isset($fields['BARCODE']) && strval(trim($fields['BARCODE'])) == "") { $result->addError(new ResultError(Loc::getMessage('SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY', array('#PRODUCT_NAME#' => $basketItem->getField('NAME'), '#STORE_ID#' => $fields['STORE_ID'])), 'SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY')); return $result; } $r = Internals\ShipmentItemStoreTable::add($fields); if (!$r->isSuccess()) { return $r; } $id = $r->getId(); $this->setFieldNoDemand('ID', $id); $result = new Main\Entity\AddResult(); } return $result; }