getDeliveryTaxes example


class TaxProviderStructTest extends TestCase
{
    public function testEmpty(): void
    {
        $struct = new TaxProviderResult();

        static::assertNull($struct->getLineItemTaxes());
        static::assertNull($struct->getDeliveryTaxes());
        static::assertNull($struct->getCartPriceTaxes());
        static::assertFalse($struct->declaresTaxes());
    }

    /** * @param array<string, CalculatedTaxCollection>|null $lineItemTaxes * @param array<string, CalculatedTaxCollection>|null $deliveryTaxes * * @dataProvider structDataProvider */
    public function testDirty(
        ?
private readonly AmountCalculator $amountCalculator,
        private readonly CashRounding $rounding
    ) {
    }

    public function adjust(Cart $cart, TaxProviderResult $result, SalesChannelContext $context): void
    {
        $lineItems = $cart->getLineItems();
        $deliveries = $cart->getDeliveries();

        $this->applyLineItemTaxes($lineItems$result->getLineItemTaxes());
        $this->applyDeliveryTaxes($deliveries$result->getDeliveryTaxes());

        $price = $this->amountCalculator->calculate(
            $cart->getLineItems()->getPrices(),
            $cart->getDeliveries()->getShippingCosts(),
            $context
        );

        // either take the price from the tax provider result or take the calculated taxes         $taxes = $price->getCalculatedTaxes()->filter(fn (CalculatedTax $tax) => $tax->getTax() > 0.0);
        $price->setCalculatedTaxes($taxes);

        
$lineItemTaxes = $taxResponse->getLineItemTaxes();
        static::assertNotNull($lineItemTaxes);
        static::assertArrayHasKey($this->ids->get('line-item-1')$lineItemTaxes);
        $taxes = $lineItemTaxes[$this->ids->get('line-item-1')];
        $tax = $taxes->first();
        static::assertInstanceOf(CalculatedTax::class$tax);
        static::assertCount(1, $taxes);
        static::assertSame(19.0, $tax->getTax());
        static::assertSame(19.0, $tax->getTaxRate());
        static::assertSame(100.0, $tax->getPrice());

        $deliveryTaxes = $taxResponse->getDeliveryTaxes();
        static::assertNotNull($deliveryTaxes);
        static::assertArrayHasKey($this->ids->get('delivery-1')$deliveryTaxes);
        $taxes = $deliveryTaxes[$this->ids->get('delivery-1')];
        $tax = $taxes->first();
        static::assertInstanceOf(CalculatedTax::class$tax);
        static::assertCount(1, $taxes);
        static::assertSame(7.0, $tax->getTax());
        static::assertSame(7.0, $tax->getTaxRate());
        static::assertSame(100.0, $tax->getPrice());

        $cartPriceTaxes = $taxResponse->getCartPriceTaxes();
        
$response = TaxProviderResponse::create($arrayStruct);

        static::assertNotNull($response->getLineItemTaxes());
        static::assertCount(2, $response->getLineItemTaxes());
        static::assertArrayHasKey($this->ids->get('line-item-1')$response->getLineItemTaxes());
        static::assertArrayHasKey($this->ids->get('line-item-2')$response->getLineItemTaxes());

        static::assertEquals(new CalculatedTax(1.0, 1.0, 1.0)$response->getLineItemTaxes()[$this->ids->get('line-item-1')]->getAt(0));
        static::assertEquals(new CalculatedTax(2.0, 2.0, 2.0)$response->getLineItemTaxes()[$this->ids->get('line-item-2')]->getAt(0));
        static::assertEquals(new CalculatedTax(3.0, 3.0, 3.0)$response->getLineItemTaxes()[$this->ids->get('line-item-2')]->getAt(1));

        static::assertNotNull($response->getDeliveryTaxes());
        static::assertCount(2, $response->getDeliveryTaxes());
        static::assertArrayHasKey($this->ids->get('delivery-1')$response->getDeliveryTaxes());
        static::assertArrayHasKey($this->ids->get('delivery-2')$response->getDeliveryTaxes());

        static::assertEquals(new CalculatedTax(4.0, 4.0, 4.0)$response->getDeliveryTaxes()[$this->ids->get('delivery-1')]->getAt(0));
        static::assertEquals(new CalculatedTax(5.0, 5.0, 5.0)$response->getDeliveryTaxes()[$this->ids->get('delivery-2')]->getAt(0));
        static::assertEquals(new CalculatedTax(6.0, 6.0, 6.0)$response->getDeliveryTaxes()[$this->ids->get('delivery-2')]->getAt(1));

        static::assertNotNull($response->getCartPriceTaxes());
        static::assertCount(2, $response->getCartPriceTaxes());
        static::assertEquals(new CalculatedTax(7.0, 7.0, 7.0)$response->getCartPriceTaxes()->getAt(0));
        
Home | Imprint | This part of the site doesn't use cookies.