getPercentage example


        }

        static::assertNotNull($customLineItem);
        static::assertNotNull($customLineItem->getPrice());
        static::assertSame($customLineItem->getPrice()->getUnitPrice(), 33.31);
        static::assertSame($customLineItem->getPrice()->getQuantity(), 10);
        static::assertSame($customLineItem->getPrice()->getTotalPrice(), 333.1);
        /** @var TaxRule $taxRule */
        $taxRule = $customLineItem->getPrice()->getTaxRules()->first();
        static::assertSame($taxRule->getTaxRate(), 19.0);
        static::assertSame($taxRule->getPercentage(), 100.0);
        /** @var CalculatedTax $calculatedTaxes */
        $calculatedTaxes = $customLineItem->getPrice()->getCalculatedTaxes()->first();
        static::assertSame($calculatedTaxes->getPrice(), 333.1);
        static::assertSame($calculatedTaxes->getTaxRate(), 19.0);
        static::assertSame($calculatedTaxes->getTax(), 53.18);

        static::assertEquals($order->getOrderDateTime()$orderDateTime);
        static::assertSame($customLineItem->getPrice()->getTotalPrice() + $oldTotal$order->getAmountTotal());
        static::assertSame($stateId$order->getStateId());
    }

    
$item->setPrice(
                $item->getChildren()->getPrices()->sum()
            );

            return;
        }

        $definition = $item->getPriceDefinition();

        if ($definition instanceof PercentagePriceDefinition) {
            $price = $this->percentageCalculator->calculate($definition->getPercentage()$scope->filterGoods()->getPrices()$context);
        } elseif ($definition instanceof CurrencyPriceDefinition) {
            $price = $this->currencyCalculator->calculate($definition->getPrice()$scope->filterGoods()->getPrices()$context);
        } elseif ($definition instanceof QuantityPriceDefinition) {
            $price = $this->quantityCalculator->calculate($definition$context);
        } else {
            throw CartException::missingLineItemPrice($item->getId());
        }

        $item->setPrice($price);
    }

    
->get($id);

            static::assertInstanceOf(SalesChannelProductEntity::class$product);

            $price = $product->getCalculatedPrice();

            static::assertInstanceOf(ListPrice::class$price->getListPrice());

            static::assertEquals($case->expectedPrice, $price->getUnitPrice());
            static::assertEquals($case->expectedWas, $price->getListPrice()->getPrice());

            static::assertEquals($case->percentage, $price->getListPrice()->getPercentage());
            static::assertEquals($case->discount, $price->getListPrice()->getDiscount());

            $partialCriteria = new Criteria([$id]);
            $partialCriteria->addFields(['price', 'taxId']);
            $product = $this->getContainer()->get('sales_channel.product.repository')
                ->search($partialCriteria$context)
                ->get($id);

            static::assertInstanceOf(PartialEntity::class$product);

            $price = $product->get('calculatedPrice');

            

        $calculatedPrice = $lineItem->getPrice();

        if (!$calculatedPrice instanceof CalculatedPrice) {
            return RuleComparison::isNegativeOperator($this->operator);
        }

        $listPrice = $calculatedPrice->getListPrice();

        $listPriceRatioAmount = null;
        if ($listPrice instanceof ListPrice) {
            $listPriceRatioAmount = $listPrice->getPercentage();
        }

        return RuleComparison::numeric($listPriceRatioAmount(float) $this->amount, $this->operator);
    }
}
$decoded = $this->serializer->decode($field$json);

        $price = $decoded->get(Defaults::CURRENCY);

        static::assertSame(5.0, $price->getNet());
        static::assertSame(5.0, $price->getGross());
        static::assertSame(10.0, $price->getListPrice()->getNet());
        static::assertSame(10.0, $price->getListPrice()->getGross());
        static::assertSame(10.0, $price->getRegulationPrice()->getNet());
        static::assertSame(10.0, $price->getRegulationPrice()->getGross());

        static::assertNull($price->getPercentage());
    }

    private function encode(array $data): string
    {
        $field = new PriceField('test', 'test');
        $existence = new EntityExistence('test', ['someId' => true], true, false, false, []);
        $keyPair = new KeyValuePair('someId', $data, false);
        $bag = new WriteParameterBag(
            $this->getContainer()->get(ProductDefinition::class),
            WriteContext::createFromContext(Context::createDefaultContext()),
            '',
            


        // should not be possible to get negative carts         $total = $price->getTotalPrice() + $cart->getLineItems()->getPrices()->sum()->getTotalPrice();

        return $total >= 0;
    }

    private function calculate(?PriceDefinitionInterface $definition, LineItemCollection $goods, SalesChannelContext $context): CalculatedPrice
    {
        if ($definition instanceof PercentagePriceDefinition) {
            return $this->percentageCalculator->calculate($definition->getPercentage()$goods->getPrices()$context);
        }

        if ($definition instanceof CurrencyPriceDefinition) {
            return $this->currencyCalculator->calculate($definition->getPrice()$goods->getPrices()$context);
        }

        throw CartException::invalidPriceDefinition();
    }
}
public function calculate(
        DiscountLineItem $discount,
        DiscountPackageCollection $packages,
        SalesChannelContext $context
    ): DiscountCalculatorResult {
        $definition = $discount->getPriceDefinition();

        if (!$definition instanceof PercentagePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $definedPercentage = -abs($definition->getPercentage());

        // now simply calculate the price object         // with that sum for the corresponding line items.         // we dont need to check on the actual item count in there,         // because our calculation does always go for the original cart items         // without considering any previously applied discounts.         $affectedPrices = $packages->getAffectedPrices();

        $calculatedPrice = $this->percentagePriceCalculator->calculate(
            $definedPercentage,
            $affectedPrices,
            
            $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->absolutePriceCalculator->calculate($definition->getPrice()$prices$context);
        }

        if ($definition instanceof PercentagePriceDefinition) {
            // reduce line items for provided filter             $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->percentagePriceCalculator->calculate($definition->getPercentage()$prices$context);
        }

        if ($definition instanceof QuantityPriceDefinition) {
            $definition->setQuantity($lineItem->getQuantity());

            return $this->quantityPriceCalculator->calculate($definition$context);
        }

        throw CartException::missingLineItemPrice($lineItem->getId());
    }
}
$taxes = [];
        foreach ($rules as $rule) {
            $taxes[] = $this->calculateTaxFromNetPrice($price$rule);
        }

        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();

        
$taxId = Uuid::randomHex();
        $taxData = [
            'id' => $taxId,
            'taxRate' => 15,
            'name' => Uuid::randomHex(),
        ];
        $salesChannelContext = $this->createSalesChannelContext([$taxData][]);
        $taxRuleCollection = $salesChannelContext->buildTaxRules($taxId);

        static::assertCount(1, $taxRuleCollection);
        static::assertSame(15.0, $taxRuleCollection->first()?->getTaxRate());
        static::assertSame(100.0, $taxRuleCollection->first()->getPercentage());
    }

    public function testGetTaxRuleCollectionCustomerShippingCountry(): void
    {
        $customerId = Uuid::randomHex();
        $shippingCountryId = Uuid::randomHex();
        $this->createCustomer($customerId$shippingCountryId, Uuid::randomHex());
        $taxId = Uuid::randomHex();
        static::assertInstanceOf(TaxRuleTypeEntity::class$this->taxRuleTypes->getByTechnicalName(EntireCountryRuleTypeFilter::TECHNICAL_NAME));
        $taxData = [
            'id' => $taxId,
            


        return $deliveryAdded;
    }

    /** * calculate the discount on all deliveries for a discount of type percentage */
    private function calculatePercentage(DeliveryCollection $deliveries, PercentagePriceDefinition $definition, SalesChannelContext $context, string $maxValue): bool
    {
        $deliveryAdded = false;
        $reduceValue = abs($definition->getPercentage());

        // we may only discount the available shipping costs (these may be reduced by another discount before)         $maxReducedPrice = $deliveries->getShippingCosts()->sum()->getTotalPrice();

        if ($maxValue !== '') {
            $castedMaxValue = (float) $maxValue;

            if ($castedMaxValue < $maxReducedPrice) {
                $maxReducedPrice = $castedMaxValue;
            }
        }

        
/** @var ArrayEntity $entity */
        static::assertInstanceOf(ArrayEntity::class$entity);

        $price = $entity->get('price');

        /** @var CalculatedPrice $price */
        static::assertInstanceOf(CalculatedPrice::class$price);

        static::assertInstanceOf(ListPrice::class$price->getListPrice());
        static::assertEquals(200, $price->getListPrice()->getPrice());
        static::assertEquals(-100, $price->getListPrice()->getDiscount());
        static::assertEquals(50, $price->getListPrice()->getPercentage());
    }
}
Home | Imprint | This part of the site doesn't use cookies.