setIsCalculated example

/** * `discount()` allows a percentage discount calculation of the current price scope. The provided value will be ensured to be negative via `abs(value) * -1`. * The provided discount is interpreted as a percentage value and will be applied to the unit price and the total price as well. * * @example pricing-cases/product-pricing.twig 30 1 Adds a 10% discount to the existing calculated price * * @param float $value The percentage value of the discount. The value will be ensured to be negative via `abs(value) * -1`. */
    public function discount(float $value): void
    {
        $definition = new QuantityPriceDefinition($this->price->getUnitPrice()$this->price->getTaxRules());
        $definition->setIsCalculated(true);

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

        $discount = $this->priceStubs->calculatePercentage(\abs($value)new CalculatedPriceCollection([$unit])$this->context);

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

        

    private function calculatePrice(float $price, float $taxRate, int $quantity, string $output, bool $preCalculated): array
    {
        $calculator = $this->grossCalculator;
        if ($output === 'net') {
            $calculator = $this->netCalculator;
        }

        $taxRules = new TaxRuleCollection([new TaxRule($taxRate)]);

        $definition = new QuantityPriceDefinition($price$taxRules$quantity);
        $definition->setIsCalculated($preCalculated);

        $config = new CashRoundingConfig(50, 0.01, true);

        $calculated = $calculator->calculate($definition$config);

        return json_decode((string) json_encode($calculated, \JSON_PRESERVE_ZERO_FRACTION), true, 512, \JSON_THROW_ON_ERROR);
    }
}
(float) $tax['percentage']
            ),
            $data['taxRules']
        );

        $self = new self(
            (float) $data['price'],
            new TaxRuleCollection($taxRules),
            \array_key_exists('quantity', $data) ? $data['quantity'] : 1
        );

        $self->setIsCalculated(\array_key_exists('isCalculated', $data) ? $data['isCalculated'] : false);
        $self->setListPrice(isset($data['listPrice']) ? (float) $data['listPrice'] : null);
        $self->setRegulationPrice(isset($data['regulationPrice']) ? (float) $data['regulationPrice'] : null);

        return $self;
    }

    public function jsonSerialize(): array
    {
        $data = parent::jsonSerialize();
        $data['type'] = $this->getType();

        
class QuantityPriceCalculatorTest extends TestCase
{
    /** * @dataProvider priceCalculationWithGrossPricesProvider */
    public function testPriceCalculationWithGrossPrices(
        CashRoundingConfig $config,
        CalculatedPrice $expected,
        QuantityPriceDefinition $priceDefinition
    ): void {
        $taxCalculator = new TaxCalculator();
        $priceDefinition->setIsCalculated(false);

        $calculator = new QuantityPriceCalculator(
            new GrossPriceCalculator($taxCalculatornew CashRounding()),
            new NetPriceCalculator($taxCalculatornew CashRounding())
        );

        $context = Generator::createSalesChannelContext();
        $context->setItemRounding($config);

        $lineItemPrice = $calculator->calculate($priceDefinition$context);

        
Home | Imprint | This part of the site doesn't use cookies.