getTaxRate example

$deliveryTaxes = $delivery->getShippingCosts()->getCalculatedTaxes()->getElements();

        static::assertArrayHasKey('7', $lineItemTaxes);
        static::assertArrayHasKey('7', $deliveryTaxes);

        static::assertInstanceOf(CalculatedTax::class$lineItemTaxes['7']);
        static::assertInstanceOf(CalculatedTax::class$deliveryTaxes['7']);

        $lineItemTax = $lineItemTaxes['7'];
        $deliveryTax = $deliveryTaxes['7'];

        static::assertEquals(7, $lineItemTax->getTaxRate());
        static::assertEquals(7, $deliveryTax->getTaxRate());
    }

    public function testNoTaxResultsGivenThrowsException(): void
    {
        // empty data set should result in exception to prevent invalid taxes         $taxProviderStruct = new TaxProviderResult();

        $cart = new Cart('foo');
        $salesChannelContext = static::createMock(SalesChannelContext::class);
        $salesChannelContext
            

    public function getEncodeValues(string $shopwareVersion): array
    {
        return [
            'object' => [
                'tax' => [
                    'id' => $this->tax->getId(),
                    '_uniqueIdentifier' => $this->tax->getId(),
                    'versionId' => null,
                    'name' => $this->tax->getName(),
                    'taxRate' => $this->tax->getTaxRate(),
                    'position' => $this->tax->getPosition(),
                    'customFields' => null,
                    'translated' => [],
                    'createdAt' => $this->tax->getCreatedAt() ? $this->tax->getCreatedAt()->format(\DATE_RFC3339_EXTENDED) : null,
                    'updatedAt' => null,
                    'extensions' => [],
                    'apiAlias' => 'tax',
                ],
            ],
        ];
    }

    
foreach ($voucherPrices as $voucherPrice) {
                $sql = ' INSERT INTO s_order_basket ( sessionID, articlename, articleID, ordernumber, shippingfree, quantity, price, netprice,tax_rate, datum, modus, currencyFactor ) VALUES (?,?,?,?,?,1,?,?,?,?,2,?) ';

                $params = [
                    $this->session->get('sessionId'),
                    $voucherName . ($hasMultipleTaxes ? ' (' . $voucherPrice->getTaxRate() . '%)' : ''),
                    $voucherDetails['id'],
                    $voucherDetails['ordercode'],
                    $freeShipping,
                    $voucherPrice->getPrice(),
                    $voucherPrice->getNetPrice(),
                    $voucherPrice->getTaxRate(),
                    $timeInsert,
                    $this->sSYSTEM->sCurrency['factor'],
                ];

                $sql = $this->eventManager->filter(
                    

        $tax = $this->taxRules->get($taxId);

        if ($tax === null || $tax->getRules() === null) {
            throw new TaxNotFoundException($taxId);
        }

        if ($tax->getRules()->first() !== null) {
            // NEXT-21735 - This is covered randomly             // @codeCoverageIgnoreStart             return new TaxRuleCollection([
                new TaxRule($tax->getRules()->first()->getTaxRate(), 100),
            ]);
            // @codeCoverageIgnoreEnd         }

        return new TaxRuleCollection([
            new TaxRule($tax->getTaxRate(), 100),
        ]);
    }

    public function getCustomer(): ?CustomerEntity
    {
        
private function buildVariants(array $combinations, array $prices, TaxCollection $taxes): array
    {
        $configurator = [];

        $variants = [];
        foreach ($combinations as $options) {
            $price = $this->faker->randomFloat(2, 1, 1000);
            $tax = $taxes->get(array_rand($taxes->getIds()));
            if (!$tax instanceof TaxEntity) {
                continue;
            }
            $taxRate = 1 + ($tax->getTaxRate() / 100);

            $id = Uuid::randomHex();
            $variants[] = [
                'id' => $id,
                'productNumber' => $id,
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => $price, 'net' => $price / $taxRate, 'linked' => true]],
                'active' => true,
                'stock' => $this->faker->numberBetween(1, 50),
                'prices' => $this->faker->randomElement($prices),
                'options' => array_map(fn ($id) => ['id' => $id]$options),
            ];

            
if (!$new->exists($rule)) {
                    $new->add($rule);
                }
            }
        );

        return $new;
    }

    public function highestRate(): ?TaxRule
    {
        return $this->reduce(fn ($result$item) => $result === null || $item->getTaxRate() > $result->getTaxRate() ? $item : $result);
    }

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

    protected function getExpectedClass(): ?string
    {
        return TaxRule::class;
    }

    
$this->repository->create($data$this->context);
        $ids = array_column($data, 'id');
        $criteria = new Criteria($ids);
        $criteria->addAssociation('tax');
        $products = $this->repository->search($criteria$this->context);

        $product = $products->get($ids[0]);

        static::assertInstanceOf(ProductEntity::class$product);
        static::assertInstanceOf(TaxEntity::class$product->getTax());
        static::assertSame('without id', $product->getTax()->getName());
        static::assertSame(19.0, $product->getTax()->getTaxRate());

        $product = $products->get($ids[1]);
        static::assertInstanceOf(ProductEntity::class$product);
        static::assertInstanceOf(TaxEntity::class$product->getTax());
        static::assertSame($tax$product->getTaxId());
        static::assertSame($tax$product->getTax()->getId());
        static::assertSame('with id', $product->getTax()->getName());
        static::assertSame(18.0, $product->getTax()->getTaxRate());

        $product = $products->get($ids[2]);
        static::assertInstanceOf(ProductEntity::class$product);
        
$cart = new Cart('test');

        $this->deliveryCalculator->calculate($data$cart$deliveries$context);

        $delivery = $deliveries->first();
        static::assertNotNull($delivery);
        $shippingCosts = $delivery->getShippingCosts();

        static::assertCount(1, $shippingCosts->getTaxRules());
        $taxRule = $shippingCosts->getTaxRules()->first();
        static::assertNotNull($taxRule);
        static::assertEquals(20, $taxRule->getTaxRate());
    }

    public function testCalculateByFixedTaxRate(): void
    {
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId(Uuid::randomHex());
        $shippingMethod->setDeliveryTime($this->deliveryTimeEntity);
        $shippingMethod->setName(Uuid::randomHex());
        $shippingMethod->setTaxType(ShippingMethodEntity::TAX_TYPE_FIXED);

        $taxRate = 10;

        
$cart->addLineItems(new LineItemCollection([$productLineItem$customLineItem$creditLineItem]));

        $calculated = $this->processor->process($cart$this->context, new CartBehavior());

        static::assertCount(3, $calculated->getLineItems());

        $creditLineItem = $calculated->getLineItems()->filterType(LineItem::CREDIT_LINE_ITEM_TYPE)->first();
        static::assertInstanceOf(LineItem::class$creditLineItem);
        static::assertInstanceOf(CalculatedPrice::class$creditLineItem->getPrice());
        static::assertCount(2, $creditCalculatedTaxes = $creditLineItem->getPrice()->getCalculatedTaxes()->getElements());

        $calculatedTaxForCustomItem = array_filter($creditCalculatedTaxesfn (CalculatedTax $tax) => (int) $tax->getTaxRate() === $taxForCustomItem);

        static::assertNotEmpty($calculatedTaxForCustomItem);
        static::assertCount(1, $calculatedTaxForCustomItem);

        $calculatedTaxForProductItem = array_filter($creditCalculatedTaxesfn (CalculatedTax $tax) => (int) $tax->getTaxRate() === $taxForProductItem);

        static::assertNotEmpty($calculatedTaxForProductItem);
        static::assertCount(1, $calculatedTaxForProductItem);
    }

    public function testShippingCostIsCalculatedWithCustomItemOnly(): void
    {

        $rules = new TaxRuleCollection([]);

        foreach ($price->getCalculatedTaxes() as $tax) {
            $percentage = 0;
            if ($price->getTotalPrice() > 0) {
                $percentage = $tax->getPrice() / $price->getTotalPrice() * 100;
            }

            $rules->add(
                new TaxRule(
                    $tax->getTaxRate(),
                    $percentage
                )
            );
        }

        return $rules;
    }
}
return $taxes;
    }

    public function getHighestTaxRule(): TaxRuleCollection
    {
        $rules = new TaxRuleCollection();

        $highestRate = $this->getTaxRules()->highestRate();

        if ($highestRate !== null) {
            $rules->add(new TaxRule($highestRate->getTaxRate(), 100));
        }

        return $rules;
    }

    public function merge(self $prices): self
    {
        return new self(array_merge($this->elements, $prices->getElements()));
    }

    public function getApiAlias(): string
    {
$delivery = $deliveries->getEntities()->first();
        static::assertNotNull($delivery);
        $newShippingCosts = $delivery->getShippingCosts();

        $firstTax = $newShippingCosts->getCalculatedTaxes()->first();
        $lastTax = $newShippingCosts->getCalculatedTaxes()->last();

        // tax is now mixed         static::assertEquals(2, $newShippingCosts->getCalculatedTaxes()->count());
        static::assertNotNull($firstTax);
        static::assertSame(19.0, $firstTax->getTaxRate());
        static::assertNotNull($lastTax);
        static::assertSame(5.0, $lastTax->getTaxRate());
    }

    public function testAddProductToOrderWithCustomerComment(): void
    {
        // create order         $cart = $this->generateDemoCart();
        $cart->setCustomerComment('test comment');
        $cart->setAffiliateCode('test_affiliate_code');
        $cart->setCampaignCode('test_campaign_code');
        

    public function getEncodeValues(string $shopwareVersion): array
    {
        $taxes = [];

        foreach ($this->taxes->getElements() as $tax) {
            $taxes[] = [
                'id' => $tax->getId(),
                '_uniqueIdentifier' => $tax->getId(),
                'versionId' => null,
                'name' => $tax->getName(),
                'taxRate' => $tax->getTaxRate(),
                'position' => $tax->getPosition(),
                'customFields' => null,
                'translated' => [],
                'createdAt' => $tax->getCreatedAt() ? $tax->getCreatedAt()->format(\DATE_RFC3339_EXTENDED) : null,
                'updatedAt' => null,
                'extensions' => [],
                'apiAlias' => 'tax',
            ];
        }

        return [
            
$taxes = [];

            if ($this->_order['taxfree']) {
                $this->_amountNetto += $this->_order['invoice_shipping'];
            } else {
                if ($this->_order['is_proportional_calculation']) {
                    $taxes = Shopware()->Container()->get('shopware.cart.proportional_tax_calculator')->calculate($this->_order['invoice_shipping']$this->getPricePositions(), false);
                    $taxNet = 0;

                    foreach ($taxes as $tax) {
                        $taxNet += $tax->getNetPrice();
                        $this->_tax[number_format($tax->getTaxRate(), 2)] += $tax->getTax();
                    }

                    $this->_amountNetto += $taxNet;
                } else {
                    $this->_amountNetto += ($this->_order['invoice_shipping'] / (100 + $taxShipping) * 100);

                    if (!empty($taxShipping) && !empty($this->_order['invoice_shipping'])) {
                        $shippingTax = Shopware()->Container()->get(TaxAggregatorInterface::class)->shippingCostsTaxSum([
                            CheckoutKey::SHIPPING_COSTS_TAX => (float) $this->_order['invoice_shipping_tax_rate'],
                            CheckoutKey::SHIPPING_COSTS_NET => (float) $this->_order['invoice_shipping_net'],
                            CheckoutKey::SHIPPING_COSTS_WITH_TAX => (float) $this->_order['invoice_shipping'],
                        ]);
$price = (float) $request->request->get('price');
        $quantity = $request->request->getInt('quantity', 1);
        $output = (string) $request->request->get('output', 'gross');
        $preCalculated = $request->request->getBoolean('calculated', true);

        $taxes = $this->taxRepository->search(new Criteria([$taxId])$context);
        $tax = $taxes->get($taxId);
        if (!$tax instanceof TaxEntity) {
            throw CartException::taxRuleNotFound($taxId);
        }

        $data = $this->calculatePrice($price$tax->getTaxRate()$quantity$output$preCalculated);

        return new JsonResponse(
            ['data' => $data]
        );
    }

    #[Route(path: 'api/_action/calculate-prices', name: 'api.action.calculate-prices', methods: ['POST'])]     public function calculatePrices(Request $request, Context $context): JsonResponse
    {
        if (!$request->request->has('taxId')) {
            throw CartException::taxIdParameterIsMissing();
        }
Home | Imprint | This part of the site doesn't use cookies.