private function findOrCreateItem(ShipmentInterface $shipment, SaleItemInterface $saleItem, $expected, $available = null)
    {
        $item = null;

        if (0 >= $expected) {
            return $item;
        }

        // Existing item lookup
        foreach ($shipment->getItems() as $i) {
            if ($i->getSaleItem() === $saleItem) {
                $item = $i;
                break;
            }
        }

        // Not found, create it
        if (null === $item) {
            $item = $this->factory->createItemForShipment($shipment);
            $item->setShipment($shipment);
            $item->setSaleItem($saleItem);
        }

        // Set expected quantity
        $item->setExpected($expected);

        if ($shipment->isReturn()) {
            // Set expected quantity as available
            $item->setAvailable($expected);
        } else {
            if (null === $available) {
                $available = $this->calculator->calculateAvailableQuantity($saleItem, $shipment);
            }

            // Set available quantity
            $item->setAvailable($available);

            // Set default quantity for new non return shipment items
            if (null === $shipment->getId()) {
                $item->setQuantity(min($expected, $available));
            }
        }

        return $item;
    }
private function findOrCreateItem(ShipmentInterface $shipment, SaleItemInterface $saleItem, $expected, $available = null)
    {
        $item = null;

        if (0 >= $expected) {
            return $item;
        }

        // Existing item lookup
        foreach ($shipment->getItems() as $i) {
            if ($i->getSaleItem() === $saleItem) {
                $item = $i;
                break;
            }
        }

        // Not found, create it
        if (null === $item) {
            $item = $this->factory->createItemForShipment($shipment);
            $item->setShipment($shipment);
            $item->setSaleItem($saleItem);
        }

        // Set expected quantity
        $item->setExpected($expected);

        if ($shipment->isReturn()) {
            // Set expected quantity as available
            $item->setAvailable($expected);
        } else {
            if (null === $available) {
                $available = $this->calculator->calculateAvailableQuantity($saleItem, $shipment);
            }

            // Set available quantity
            $item->setAvailable($available);

            // Set default quantity for new non return shipment items
            if (null === $shipment->getId()) {
                $item->setQuantity(min($expected, $available));
            }
        }

        return $item;
    }
private function findOrCreateItem ( ShipmentInterface $ shipment , SaleItemInterface $ saleItem , $ expected , $ available = null ) { $ item = null ; if ( 0 >= $ expected ) { return $ item ; } // Existing item lookup foreach ( $ shipment -> getItems ( ) as $ i ) { if ( $ i -> getSaleItem ( ) === $ saleItem ) { $ item = $ i ; break ; } } // Not found, create it if ( null === $ item ) { $ item = $ this -> factory -> createItemForShipment ( $ shipment ) ; $ item -> setShipment ( $ shipment ) ; $ item -> setSaleItem ( $ saleItem ) ; } // Set expected quantity $ item -> setExpected ( $ expected ) ; if ( $ shipment -> isReturn ( ) ) { // Set expected quantity as available $ item -> setAvailable ( $ expected ) ; } else { if ( null === $ available ) { $ available = $ this -> calculator -> calculateAvailableQuantity ( $ saleItem , $ shipment ) ; } // Set available quantity $ item -> setAvailable ( $ available ) ; // Set default quantity for new non return shipment items if ( null === $ shipment -> getId ( ) ) { $ item -> setQuantity ( min ( $ expected , $ available ) ) ; } } return $ item ; }
Finds or create the shipment item.

@param ShipmentInterface $shipment
@param SaleItemInterface $saleItem
@param float             $expected
@param float             $available

@return ShipmentItemInterface
Finds or create the shipment item .