CalculatedTax example


    private function createProductItem(float $price, float $taxRate): LineItem
    {
        $product = new LineItem(Uuid::randomBytes(), LineItem::PRODUCT_LINE_ITEM_TYPE);

        // allow quantity change         $product->setStackable(true);

        $taxValue = $price * ($taxRate / 100.0);

        $calculatedTaxes = new CalculatedTaxCollection();
        $calculatedTaxes->add(new CalculatedTax($taxValue$taxRate$taxValue));

        $product->setPrice(new CalculatedPrice($price$price$calculatedTaxesnew TaxRuleCollection()));

        return $product;
    }
}

#[Package('checkout')] class CalculatedTaxes extends CalculatedTaxCollection
{
    /** * @param array<int, float> $taxes */
    public function __construct(array $taxes = [])
    {
        parent::__construct();
        foreach ($taxes as $rate => $value) {
            $this->add(new CalculatedTax($value$rate, 0));
        }
    }
}

    public static function structDataProvider(): iterable
    {
        yield [null, null, null, false];
        yield [[], null, null, false];
        yield [null, [], null, false];
        yield [null, null, new CalculatedTaxCollection(), false];

        yield [['foo' => new CalculatedTaxCollection()], null, null, true];
        yield [null, ['foo' => new CalculatedTaxCollection()], null, true];
        yield [null, null, new CalculatedTaxCollection([new CalculatedTax(0, 0, 0)]), true];
    }
}
new TaxAdjustmentCalculator()
            ),
            new CashRounding()
        );
    }

    public function testItThrowsOnEmptyLineItemPrice(): void
    {
        $struct = new TaxProviderResult(
            [
                $this->ids->get('line-item-1') => new CalculatedTaxCollection([
                    new CalculatedTax(
                        7,
                        7,
                        100
                    ),
                ]),
            ],
            [
                $this->ids->get('delivery-position-1') => new CalculatedTaxCollection([
                    new CalculatedTax(
                        7,
                        7,
                        

        $product = new LineItem(Uuid::randomBytes(), LineItem::PRODUCT_LINE_ITEM_TYPE);

        // allow quantity change         $product->setStackable(true);

        $taxValue = $netPrice * ($taxRate / 100.0);

        $grossPrice = $netPrice + $taxValue;

        $calculatedTaxes = new CalculatedTaxCollection();
        $calculatedTaxes->add(new CalculatedTax($taxValue$taxRate$taxValue));

        $listPrice = null;
        if ($listPriceNet !== null) {
            $listPrice = ListPrice::createFromUnitPrice($netPrice$listPriceNet);
        }

        $product->setPrice(new CalculatedPrice(
            $grossPrice,
            $grossPrice,
            $calculatedTaxes,
            new TaxRuleCollection(),
            
$priceDefinition = new QuantityPriceDefinition(104.00, new TaxRuleCollection([new TaxRule(21, 100)]), 1);
        $price = $calculator->calculate($priceDefinition, Generator::createSalesChannelContext());
        static::assertSame(104.00, $price->getTotalPrice());
        static::assertSame(18.05, $price->getCalculatedTaxes()->getAmount());

        return new PercentageCalculation(
            -10,
            new CalculatedPrice(
                -10.4,
                -10.4,
                new CalculatedTaxCollection([
                    new CalculatedTax(-1.80, 21, -10.4),
                ]),
                new TaxRuleCollection([new TaxRule(21)])
            ),
            new PriceCollection([$price])
        );
    }

    private static function getFiftyPercentageCalculation(): PercentageCalculation
    {
        $calculator = self::createQuantityPriceCalculator();

        

    public static function create(array $data): self
    {
        $response = new self();

        if (isset($data['lineItemTaxes'])) {
            foreach ($data['lineItemTaxes'] as $lineItemId => $taxes) {
                $lineItemTax = new CalculatedTaxCollection();

                foreach ($taxes as $tax) {
                    $lineItemTax->add(new CalculatedTax($tax['tax']$tax['taxRate']$tax['price']));
                }

                $response->lineItemTaxes[$lineItemId] = $lineItemTax;
            }
        }

        if (isset($data['deliveryTaxes'])) {
            foreach ($data['deliveryTaxes'] as $deliveryId => $taxes) {
                $deliveryTax = new CalculatedTaxCollection();

                foreach ($taxes as $tax) {
                    
$this->connection->rollBack();

        $this->connection->executeStatement(CalculatedPriceFieldTestDefinition::getCreateTable());

        $this->connection->beginTransaction();

        $price = new CalculatedPrice(
            100.20,
            100.30,
            new CalculatedTaxCollection([
                new CalculatedTax(0.19, 10, 10),
                new CalculatedTax(0.19, 5, 10),
            ]),
            new TaxRuleCollection([
                new TaxRule(10, 50),
                new TaxRule(5, 50),
            ])
        );

        /** @var CalculatedPriceFieldTestDefinition $definition */
        $definition = $this->getContainer()->get(CalculatedPriceFieldTestDefinition::class);
        $repository = new EntityRepository(
            

    public static function calculationProvider()
    {
        return [
            [
                [
                    new ItemBlueprint(1.43, 1, 19),
                    new ItemBlueprint(1.43, 1, 19),
                    new ItemBlueprint(1.43, 1, 19),
                ],
                new CalculatedTaxCollection([new CalculatedTax(0.69, 19, 4.29)]),
                new CalculatedTaxCollection([new CalculatedTax(0.68, 19, 4.29)]),
            ],
            [
                [
                    new ItemBlueprint(19.99, 1, 19),
                    new ItemBlueprint(19.99, 1, 19),
                    new ItemBlueprint(19.99, 1, 19),
                ],
                new CalculatedTaxCollection([new CalculatedTax(9.57, 19, 59.97)]),
                new CalculatedTaxCollection([new CalculatedTax(9.58, 19, 59.97)]),
            ],
            [


    /** * @return list<array{0: CalculatedPrice, 1: QuantityPriceDefinition}> */
    public static function netPrices(): array
    {
        $highTaxRules = new TaxRuleCollection([new TaxRule(19)]);

        return [
            [
                new CalculatedPrice(13.44, 13.44, new CalculatedTaxCollection([new CalculatedTax(2.55, 19, 13.44)])$highTaxRules),
                new QuantityPriceDefinition(13.436974789916, $highTaxRules),
            ],
        ];
    }

    /** * @return list<array{0: CalculatedPrice, 1: QuantityPriceDefinition}> */
    public static function netDeliveryPrices(): array
    {
        $highTaxRules = new TaxRuleCollection([new TaxRule(19)]);

        
return new CalculatedTaxCollection($taxes);
    }

    public function calculateTaxFromNetPrice(float $net, TaxRule $rule): CalculatedTax
    {
        // calculate percentage value of net price         $net = $net / 100 * $rule->getPercentage();

        $calculatedTax = $net * ($rule->getTaxRate() / 100);

        return new CalculatedTax($calculatedTax$rule->getTaxRate()$net);
    }

    private function calculateTaxFromGrossPrice(float $gross, TaxRule $rule): CalculatedTax
    {
        // calculate percentage value of gross price         $gross = $gross / 100 * $rule->getPercentage();

        $calculatedTax = $gross / ((100 + $rule->getTaxRate()) / 100) * ($rule->getTaxRate() / 100);

        return new CalculatedTax($calculatedTax$rule->getTaxRate()$gross);
    }
}


        $taxRules = array_map(
            fn (array $tax) => new TaxRule(
                (float) $tax['taxRate'],
                (float) $tax['percentage']
            ),
            $decoded['taxRules']
        );

        $calculatedTaxes = array_map(
            fn (array $tax) => new CalculatedTax(
                (float) $tax['tax'],
                (float) $tax['taxRate'],
                (float) $tax['price']
            ),
            $decoded['calculatedTaxes']
        );

        return new CartPrice(
            (float) $decoded['netPrice'],
            (float) $decoded['totalPrice'],
            (float) $decoded['positionPrice'],
            
final public const DUMMY_TAX_NAME = 'dummy-tax';

    public function testCollectionIsCountable(): void
    {
        $collection = new CalculatedTaxCollection();
        static::assertCount(0, $collection);
    }

    public function testCountReturnsCorrectValue(): void
    {
        $collection = new CalculatedTaxCollection([
            new CalculatedTax(10.99, 19, 1),
            new CalculatedTax(5.99, 14, 1),
            new CalculatedTax(1.99, 2, 1),
        ]);
        static::assertCount(3, $collection);
    }

    public function testAddFunctionAddsATax(): void
    {
        $collection = new CalculatedTaxCollection();
        $collection->add(
            new CalculatedTax(10.99, 19, 1)
        );
$price1 = $calculator->calculate($definition, Generator::createSalesChannelContext());

        $definition = new QuantityPriceDefinition(30, new TaxRuleCollection([new TaxRule(7)]));
        $price2 = $calculator->calculate($definition, Generator::createSalesChannelContext());

        return new AbsoluteCalculation(
            -6,
            new CalculatedPrice(
                -6,
                -6,
                new CalculatedTaxCollection([
                    new CalculatedTax(-0.48, 19, -3),
                    new CalculatedTax(-0.20, 7, -3),
                ]),
                new TaxRuleCollection([
                    new TaxRule(19, 50),
                    new TaxRule(7, 50),
                ]),
                1
            ),
            new PriceCollection([$price1$price2])
        );
    }

    
$baseContext = Context::createDefaultContext();

        $context->expects(static::atLeastOnce())->method('getContext')->willReturn($baseContext);
        $context->expects(static::atLeastOnce())->method('getRuleIds')->willReturn([]);
        $context->expects(static::atLeastOnce())->method('getShippingMethod')->willReturn($shippingMethod);

        $firstLineItem = $this->createLineItem(
            new DeliveryInformation(10, 12.0, false, null, $this->deliveryTime),
            new CalculatedPrice(
                10,
                10,
                new CalculatedTaxCollection([new CalculatedTax(5, 19, 5)]),
                new TaxRuleCollection([new TaxRule(19)])
            )
        );

        $secondLineItem = $this->createLineItem(
            new DeliveryInformation(10, 12.0, false, null, $this->deliveryTime),
            new CalculatedPrice(
                10,
                10,
                new CalculatedTaxCollection([new CalculatedTax(5, 7, 5)]),
                new TaxRuleCollection([new TaxRule(7)])
            )
Home | Imprint | This part of the site doesn't use cookies.