OrderLineItemCollection example

$lineItemC->setId(Uuid::randomHex());
        $lineItemC->setGood(true);

        $lineItemD = new OrderLineItemEntity();
        $lineItemD->setId(Uuid::randomHex());
        $lineItemD->setGood(false);

        $lineItemE = new OrderLineItemEntity();
        $lineItemE->setId(Uuid::randomHex());
        $lineItemE->setGood(true);

        $lineItemC->setChildren(new OrderLineItemCollection([$lineItemE$lineItemD]));

        $collection = new OrderLineItemCollection([$lineItemA$lineItemB$lineItemC]);

        $filtered = $collection->filterGoodsFlat();

        static::assertEquals([$lineItemA$lineItemC$lineItemE]$filtered);
    }

    public function testGetPayloadsProperty(): void
    {
        $lineItemA = new OrderLineItemEntity();
        
foreach ($orders as $order) {
            $orderId = $order->getId();

            try {
                $operation = $operations[$orderId] ?? null;

                if ($operation === null) {
                    continue;
                }

                $lineItems = $order->getLineItems();
                $creditItems = new OrderLineItemCollection();

                if ($lineItems) {
                    $creditItems = $lineItems->filterByType(LineItem::CREDIT_LINE_ITEM_TYPE);
                }

                if ($creditItems->count() === 0) {
                    throw new DocumentGenerationException(
                        'Can not generate credit note document because no credit line items exists. OrderId: ' . $operation->getOrderId()
                    );
                }

                
return 1;
            });

        return $connection;
    }

    private function createOrder(): OrderEntity
    {
        $order = new OrderEntity();

        $lineItems = new OrderLineItemCollection();
        $lineItems->add($this->buildOrderLineItem($this->ids->get('id1'), LineItem::PRODUCT_LINE_ITEM_TYPE, 1));
        $lineItems->add($this->buildOrderLineItem($this->ids->get('id2'), LineItem::PRODUCT_LINE_ITEM_TYPE, 2));
        $lineItems->add($this->buildOrderLineItem(null, LineItem::PRODUCT_LINE_ITEM_TYPE, 2));
        $lineItems->add($this->buildOrderLineItem($this->ids->get('discount'), LineItem::DISCOUNT_LINE_ITEM, 1));

        $order->setLineItems($lineItems);

        return $order;
    }

    private function buildOrderLineItem(?string $id, string $type, int $qty): OrderLineItemEntity
    {
$order = new OrderEntity();

        yield 'order without line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setGood(true);
        $lineItem->setId(Uuid::randomHex());

        $order->setLineItems(new OrderLineItemCollection([$lineItem]));

        yield 'order without downloadable line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setId(Uuid::randomHex());
        $lineItem->setGood(true);
        $lineItem->setStates([State::IS_DOWNLOAD]);

        $downloadId = Uuid::randomHex();
        
'orderNumber' => 'NUM-1',
            ],
        ];

        yield 'with entity' => [
            'entity' => self::createOrderEntity(),
            'expected' => self::getExpected(),
        ];

        yield 'with order empty line items' => [
            'entity' => self::createOrderEntity([
                'lineItems' => new OrderLineItemCollection(),
            ]),
            'expected' => self::getExpected(),
        ];

        yield 'with order line items' => [
            'entity' => self::createOrderEntity([
                'lineItems' => self::createLineItems(),
            ]),
            'expected' => self::getExpected([
                'lineItems' => '3x |2x ',
            ]),
        ];

        $this->position = 1;
    }

    public function testTransformFlatToNested(): void
    {
        $containerId = Uuid::randomHex();
        $productId = Uuid::randomHex();
        $optionOneId = Uuid::randomHex();
        $optionTwoId = Uuid::randomHex();

        $orderLineItemCollection = new OrderLineItemCollection(
            [
                $this->buildOrderLineItemEntity($containerId, LineItem::CUSTOM_LINE_ITEM_TYPE, null, 3),
                $this->buildOrderLineItemEntity($productId, LineItem::PRODUCT_LINE_ITEM_TYPE, $containerId, 3),
                $this->buildOrderLineItemEntity($optionOneId, LineItem::CUSTOM_LINE_ITEM_TYPE, $containerId, 3),
                $this->buildOrderLineItemEntity($optionTwoId, LineItem::CUSTOM_LINE_ITEM_TYPE, $containerId, 3),
            ]
        );

        $nestedCollection = LineItemTransformer::transformFlatToNested($orderLineItemCollection);
        static::assertCount(1, $nestedCollection);

        
if ($toManipulate === 'order-add-line-item-download') {
            $orderLineItemDownload = new OrderLineItemDownloadEntity();
            $orderLineItemDownload->setId(Uuid::randomHex());
            $orderLineItemDownload->setMediaId(Uuid::randomHex());

            $orderLineItemDownloadCollection = new OrderLineItemDownloadCollection();
            $orderLineItemDownloadCollection->add($orderLineItemDownload);
            $orderLineItem->setDownloads($orderLineItemDownloadCollection);
        }

        $orderLineItemCollection = new OrderLineItemCollection();
        $orderLineItemCollection->add($orderLineItem);

        // Order delivery position         $orderDeliveryPositionCollection = new OrderDeliveryPositionCollection();
        $orderDeliveryPosition = new OrderDeliveryPositionEntity();
        $orderDeliveryPosition->setId('order-delivery-position-id-1');
        $orderDeliveryPosition->setOrderLineItem($orderLineItem);
        $orderDeliveryPosition->setPrice(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()));
        $orderDeliveryPositionCollection->add($orderDeliveryPosition);

        // Order delivery

        return $this->fmap(fn (OrderDeliveryPositionEntity $orderDeliveryPosition) => $orderDeliveryPosition->getOrderLineItemId());
    }

    public function filterByOrderLineItemId(string $id): self
    {
        return $this->filter(fn (OrderDeliveryPositionEntity $orderDeliveryPosition) => $orderDeliveryPosition->getOrderLineItemId() === $id);
    }

    public function getOrderLineItems(): OrderLineItemCollection
    {
        return new OrderLineItemCollection(
            $this->fmap(fn (OrderDeliveryPositionEntity $orderDeliveryPosition) => $orderDeliveryPosition->getOrderLineItem())
        );
    }

    public function getApiAlias(): string
    {
        return 'order_delivery_position_collection';
    }

    protected function getExpectedClass(): string
    {
        
Home | Imprint | This part of the site doesn't use cookies.