code: private function buildSaleItemRemaining(
          Common\SaleItemInterface $saleItem,
          Shipment\RemainingList $list,
          array $shipments
      ) {
  
          // Not for compound item with only public children
          if (!($saleItem->isCompound() && !$saleItem->hasPrivateChildren())) {
              $quantity = $saleItem->getTotalQuantity();
  
              foreach ($shipments as $shipment) {
                  foreach ($shipment->getItems() as $item) {
                      if ($item->getSaleItem() === $saleItem) {
                          $quantity += $shipment->isReturn() ? $item->getQuantity() : -$item->getQuantity();
  
                          continue 2;
                      }
                  }
              }
  
              if (0 < $quantity) {
                  $entry = new Shipment\RemainingEntry();
                  $entry
                      ->setSaleItem($saleItem)
                      ->setQuantity($quantity);
  
                  $list->addEntry($entry);
              }
          }
  
          foreach ($saleItem->getChildren() as $child) {
              $this->buildSaleItemRemaining($child, $list, $shipments);
          }
      }
docs: Calculate the sale item remaining quantity.
  
  @param Common\SaleItemInterface     $saleItem
  @param Shipment\RemainingList       $list
  @param Shipment\ShipmentInterface[] $shipments