getTaxRules example



    private function getComparisonUrl(string $productId): string
    {
        $loc = $this->seoUrlPlaceholderHandler->generate('frontend.detail.page', ['productId' => $productId]);

        return $this->seoUrlPlaceholderHandler->replace($loc$this->getHost($this->salesChannelContext)$this->salesChannelContext);
    }

    private function getBasicProductData(): array
    {
        $taxId = $this->salesChannelContext->getTaxRules()->first()->getId();

        return [
            'stock' => 100,
            'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false]],
            'tax' => ['id' => $taxId],
            'manufacturer' => ['name' => 'test'],
            'visibilities' => [
                ['salesChannelId' => $this->salesChannelContext->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL],
            ],
        ];
    }

    
true,
        ];
    }

    /** * @param array<mixed> $data */
    private function createProduct(array $data = []): void
    {
        $ids = new IdsCollection();

        $tax = $this->context->getTaxRules()->first();

        static::assertInstanceOf(TaxEntity::class$tax);

        $product = array_merge(
            [
                'name' => 'test',
                'productNumber' => 'test',
                'stock' => 10,
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false]],
                'tax' => ['id' => $tax->getId(), 'name' => 'test', 'taxRate' => 15],
                'visibilities' => [[
                    
if ($this->tryTransition($order$paymentMethodId$context)) {
            return;
        }

        $initialState = $this->initialStateIdLoader->get(OrderTransactionStates::STATE_MACHINE);

        $transactionAmount = new CalculatedPrice(
            $order->getPrice()->getTotalPrice(),
            $order->getPrice()->getTotalPrice(),
            $order->getPrice()->getCalculatedTaxes(),
            $order->getPrice()->getTaxRules()
        );

        $transactionId = Uuid::randomHex();
        $payload = [
            'id' => $order->getId(),
            'transactions' => [
                [
                    'id' => $transactionId,
                    'paymentMethodId' => $paymentMethodId,
                    'stateId' => $initialState,
                    'amount' => $transactionAmount,
                ],
return isset($tag) && $request->getQuery('nocache') !== null;
    }

    private function resetCookies(Request $request, Response $response): void
    {
        $response->setCookie('x-cache-context-hash', null, strtotime('-1 Year')$request->getBasePath() . '/');
    }

    private function setContextCookie(Request $request, ShopContextInterface $context, Response $response): void
    {
        $hash = json_encode($context->getTaxRules()) . json_encode($context->getCurrentCustomerGroup());

        $hash = $this->eventManager->filter('Shopware_Plugins_HttpCache_ContextCookieValue', $hash[
            'shopContext' => $context,
            'session' => $this->session,
            'request' => $request,
            'response' => $response,
        ]);

        $response->setCookie('x-cache-context-hash', sha1($hash), 0, $request->getBasePath() . '/', null, $request->isSecure());
    }
}


    private function createProducts(): void
    {
        $products = $this->getProductTestData($this->salesChannelContext);

        $this->getContainer()->get('product.repository')->create($products$this->salesChannelContext->getContext());
    }

    private function getProductTestData(SalesChannelContext $salesChannelContext): array
    {
        $taxId = $salesChannelContext->getTaxRules()->first()->getId();

        $products = [
            [
                'id' => Uuid::randomHex(),
                'productNumber' => Uuid::randomHex(),
                'stock' => 100,
                'name' => 'test product 1',
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false]],
                'tax' => ['id' => $taxId],
                'manufacturer' => ['name' => 'test'],
                'visibilities' => [
                    [


        if ($price instanceof PriceFacade) {
            $price = $price->getInner();
        }

        if ($price instanceof PriceCollection) {
            $value = $this->getPriceForTaxState($price$this->context);

            $definition = new QuantityPriceDefinition(
                $value,
                $this->price->getTaxRules(),
                $this->getQuantity()
            );

            $price = $this->priceStubs->calculateQuantity($definition$this->context);
        }

        if (!$price instanceof CalculatedPrice) {
            throw ProductException::invalidCheapestPriceFacade($this->item->getUniqueIdentifier());
        }

        $this->price->overwrite(
            
'group' => [
                            'id' => $colorGroupId,
                            'name' => 'Color',
                        ],
                    ],
                ],
            ],
            'visibilities' => [[
                'salesChannelId' => $context->getSalesChannelId(),
                'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL,
            ]],
            'tax' => ['id' => $context->getTaxRules()->first()->getId(), 'name' => 'test', 'taxRate' => 15],
            'cmsPage' => [
                'id' => $expectedCmsPageId,
                'type' => 'product_detail',
                'sections' => [],
            ],
        ]);

        $childProduct = $this->createData([
            'id' => Uuid::randomHex(),
            'productNumber' => Uuid::randomHex(),
            'tax' => ['id' => $context->getTaxRules()->first()->getId(), 'name' => 'test', 'taxRate' => 15],
            

    public function __construct(
        private readonly TaxCalculator $taxCalculator,
        private readonly CashRounding $priceRounding
    ) {
    }

    public function calculate(QuantityPriceDefinition $definition, CashRoundingConfig $config): CalculatedPrice
    {
        $unitPrice = $this->getUnitPrice($definition$config);

        $unitTaxes = $this->taxCalculator->calculateGrossTaxes($unitPrice$definition->getTaxRules());

        foreach ($unitTaxes as $tax) {
            $total = $this->priceRounding->mathRound(
                $tax->getTax() * $definition->getQuantity(),
                $config
            );

            $tax->setTax($total);

            $tax->setPrice($tax->getPrice() * $definition->getQuantity());
        }

        

        return $this->price->getCalculatedTaxes();
    }

    /** * `getRules()` returns the tax rules that were used to calculate the price. * * @return TaxRuleCollection Returns the tax rules. */
    public function getRules(): TaxRuleCollection
    {
        return $this->price->getTaxRules();
    }

    /** * `change()` allows a price overwrite of the current price scope. The provided price will be recalculated * over the quantity price calculator to consider quantity, tax rule and cash rounding configurations. * * @example pricing-cases/product-pricing.twig 40 5 Overwrite prices with a static defined collection * * @param PriceCollection $price The provided price can be a fetched price from the database or generated over the `PriceFactory` statically */
    public function change(PriceCollection $price): void
    {
$netPrice = $this->rounding->cashRound(
                $grossPrice - $taxSum,
                $context->getTotalRounding()
            );
        }

        return new CartPrice(
            $netPrice,
            $grossPrice,
            $price->getPositionPrice(),
            $taxes,
            $price->getTaxRules(),
            $context->getTaxState(),
            $grossPrice
        );
    }

    /** * @param array<string, CalculatedTaxCollection>|null $taxes */
    private function applyLineItemTaxes(LineItemCollection $lineItems, ?array $taxes): void
    {
        if (!$taxes) {
            
return $context->getCurrentCustomerGroup()->getId() !== $context->getFallbackCustomerGroup()->getId();
    }

    /** * Builds the tax cases for the price selection query * * @return string */
    private function buildTaxCase(ShopContextInterface $context)
    {
        $cases = [];
        foreach ($context->getTaxRules() as $rule) {
            $cases[] = ' WHEN ' . $rule->getId() . ' THEN ' . $rule->getTax();
        }

        return '(CASE tax.id ' . implode(' ', $cases) . ' END)';
    }
}
new DeliveryDate(new \DateTime()new \DateTime()),
            $shippingMethod,
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(5, 5, new CalculatedTaxCollection()new TaxRuleCollection())
        );

        $this->deliveryCalculator->calculate(new CartDataCollection()new Cart('test')new DeliveryCollection([$delivery])$context);

        $newCosts = $delivery->getShippingCosts();
        static::assertEquals(5, $newCosts->getUnitPrice());
        static::assertEquals(5, $newCosts->getTotalPrice());
        static::assertCount(0, $newCosts->getTaxRules());
        static::assertCount(0, $newCosts->getCalculatedTaxes());
    }

    public function testCalculateWithoutShippingMethodPrices(): void
    {
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId(Uuid::randomHex());
        $shippingMethod->setDeliveryTime($this->deliveryTimeEntity);
        $shippingMethod->setPrices(new ShippingMethodPriceCollection());
        $shippingMethod->setName(Uuid::randomHex());

        

    public function process(Cart $cart, SalesChannelContext $context): TransactionCollection
    {
        $price = $cart->getPrice()->getTotalPrice();

        return new TransactionCollection([
            new Transaction(
                new CalculatedPrice(
                    $price,
                    $price,
                    $cart->getPrice()->getCalculatedTaxes(),
                    $cart->getPrice()->getTaxRules()
                ),
                $context->getPaymentMethod()->getId()
            ),
        ]);
    }
}
$net = $this->rounding->mathRound(
            $total->getTotalPrice() - $taxes->getAmount(),
            $context->getItemRounding()
        );

        return new CartPrice(
            $net,
            $price,
            $prices->sum()->getTotalPrice(),
            $taxes,
            $total->getTaxRules(),
            CartPrice::TAX_STATE_GROSS,
            $total->getTotalPrice()
        );
    }

    /** * Calculates the amount for a net based delivery, but gross prices has be be payed * `CalculatedPrice::netPrice` contains the summed net prices. * `CalculatedPrice::price` contains the summed net prices plus amount of calculated taxes * Calculated taxes are based on the net prices */
    
return $this->elements[$key];
        }

        return null;
    }

    public function getTaxRules(): TaxRuleCollection
    {
        $rules = new TaxRuleCollection([]);

        foreach ($this->getIterator() as $price) {
            $rules = $rules->merge($price->getTaxRules());
        }

        return $rules;
    }

    public function sum(): CalculatedPrice
    {
        return new CalculatedPrice(
            $this->getUnitPriceAmount(),
            $this->getAmount(),
            $this->getCalculatedTaxes(),
            
Home | Imprint | This part of the site doesn't use cookies.