/** * @param ShipmentItem $sourceItem * @param $quantity * @return Result * @throws Main\ArgumentException * @throws Main\ArgumentOutOfRangeException * @throws Main\NotSupportedException */ private function transferItem2SystemShipment(ShipmentItem $sourceItem, $quantity) { /** @var ShipmentItemCollection $sourceItemCollection */ $sourceItemCollection = $sourceItem->getCollection(); if ($this !== $sourceItemCollection->getShipment()) { throw new Main\ArgumentException("item"); } $quantity = floatval($quantity); /** @var ShipmentCollection $shipmentCollection */ $shipmentCollection = $this->getCollection(); /** @var Shipment $systemShipment */ $systemShipment = $shipmentCollection->getSystemShipment(); /** @var BasketItem $basketItem */ $basketItem = $sourceItem->getBasketItem(); $shipmentItemCode = $sourceItem->getBasketCode(); if ($quantity === 0) { return new Result(); } /** @var ShipmentItemCollection $systemShipmentItemCollection */ $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection(); $systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($shipmentItemCode); if (is_null($systemShipmentItem)) { $systemShipmentItem = $systemShipmentItemCollection->createItem($basketItem); } $newSystemShipmentItemQuantity = $systemShipmentItem->getQuantity() + $quantity; if ($newSystemShipmentItemQuantity < 0) { $result = new Result(); $result->addError(new ResultError(str_replace(array("#NAME#", "#QUANTITY#"), array($sourceItem->getBasketItem()->getField("NAME"), abs($quantity)), Loc::getMessage('SALE_SHIPMENT_QUANTITY_MISMATCH')), 'SALE_SHIPMENT_QUANTITY_MISMATCH')); return $result; } $systemShipmentItem->setFieldNoDemand('QUANTITY', $newSystemShipmentItemQuantity); $affectedQuantity = 0; if ($quantity > 0) { if ($sourceItem->getReservedQuantity() > 0) { $affectedQuantity = $quantity; $originalQuantity = $sourceItem->getQuantity() + $quantity; if ($sourceItem->getReservedQuantity() < $originalQuantity) { $affectedQuantity -= $originalQuantity - $sourceItem->getReservedQuantity(); } } } elseif ($quantity < 0) { if ($systemShipmentItem->getReservedQuantity() > 0) { $affectedQuantity = $quantity; if ($systemShipmentItem->getReservedQuantity() < -$affectedQuantity) { $affectedQuantity = -1 * $systemShipmentItem->getReservedQuantity(); } } } if ($affectedQuantity != 0) { $result = $sourceItem->setField("RESERVED_QUANTITY", $sourceItem->getField('RESERVED_QUANTITY') - $affectedQuantity); // if (!$result->isSuccess(true)) // return $result; $systemShipmentItem->setFieldNoDemand('RESERVED_QUANTITY', $systemShipmentItem->getField('RESERVED_QUANTITY') + $affectedQuantity); } $tryReserveResult = null; if ($quantity > 0) { if ($systemShipment->needReservation()) { /** @var Result $tryReserveResult */ $tryReserveResult = Provider::tryReserveShipmentItem($systemShipmentItem); } else { /** @var Result $tryReserveResult */ $tryReserveResult = Provider::tryUnreserveShipmentItem($systemShipmentItem); } } elseif ($quantity < 0) { if ($sourceItemCollection->getShipment()->needReservation()) { /** @var Result $tryReserveResult */ $tryReserveResult = Provider::tryReserveShipmentItem($sourceItem); } } $canReserve = false; if ($tryReserveResult === null) { $canReserve = true; } if ($tryReserveResult !== null && ($tryReserveResult->isSuccess() && ($tryReserveResultData = $tryReserveResult->getData()))) { if (array_key_exists('CAN_RESERVE', $tryReserveResultData)) { $canReserve = $tryReserveResultData['CAN_RESERVE']; } } if ($systemShipment->needReservation() && $canReserve) { $systemShipment->updateReservedFlag(); } return new Result(); }
/** * @param \Bitrix\Sale\ShipmentItem $shipment * @return mixed */ protected static function prepareData($shipment) { $data['STATUS_ID'] = $shipment->getField('STATUS_ID'); return $data; }