getGross example

public function testDecodeIsBackwardCompatible(): void
    {
        $json = '{"cb7d2554b0ce847cd82f3ac9bd1c0dfca":{"net":5.0,"gross":5.0,"currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true,"listPrice":{"net":"10","gross":"10","currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true},"regulationPrice":{"net":"10","gross":"10","currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true}}}';

        $field = new PriceField('test', 'test');

        $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');
        
if ($currency->getCurrencyId() !== $context->getCurrency()->getId()) {
            $value *= $context->getContext()->getCurrencyFactor();
        }

        return $value;
    }

    private function getPriceForTaxState(Price $price, SalesChannelContext $context): float
    {
        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $price->getGross();
        }

        return $price->getNet();
    }

    private function getListPrice(PriceCollection $prices, SalesChannelContext $context): ?float
    {
        $price = $prices->getCurrencyPrice($context->getCurrency()->getId());
        if ($price === null || $price->getListPrice() === null) {
            return null;
        }

        
/** @var ProductEntity $product */
        $product = $this->productRepository->search((new Criteria([$productId]))->addAssociation('prices')$context)->first();

        static::assertInstanceOf(ProductPriceCollection::class$product->getPrices());
        static::assertEquals(2, $product->getPrices()->count());
        $firstPrice = $product->getPrices()->first();

        static::assertInstanceOf(ProductPriceEntity::class$firstPrice);
        static::assertEquals($ruleId$firstPrice->getRuleId());
        static::assertInstanceOf(Price::class$firstPrice->getPrice()->first());
        static::assertEquals(7.89, $firstPrice->getPrice()->first()->getNet());
        static::assertEquals(9.39, $firstPrice->getPrice()->first()->getGross());
        static::assertEquals(1, $firstPrice->getQuantityStart());
        static::assertEquals(10, $firstPrice->getQuantityEnd());
        $lastPrice = $product->getPrices()->last();
        static::assertInstanceOf(ProductPriceEntity::class$lastPrice);
        static::assertEquals($ruleId$lastPrice->getRuleId());
        static::assertInstanceOf(Price::class$lastPrice->getPrice()->first());
        static::assertEquals(5.67, $lastPrice->getPrice()->first()->getNet());
        static::assertEquals(6.75, $lastPrice->getPrice()->first()->getGross());
        static::assertEquals(11, $lastPrice->getQuantityStart());
        static::assertNull($lastPrice->getQuantityEnd());

        

        $this->sort(fn (ProductPriceEntity $a, ProductPriceEntity $b) => $a->getQuantityStart() <=> $b->getQuantityStart());
    }

    public function sortByPrice(Context $context): void
    {
        $this->sort(function DProductPriceEntity $a, ProductPriceEntity $b) use ($context) {
            $a = $a->getPrice()->first();
            $b = $b->getPrice()->first();

            if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
                return ($a ? $a->getGross() : 0) <=> ($b ? $b->getGross() : 0);
            }

            return ($a ? $a->getNet() : 0) <=> ($b ? $b->getNet() : 0);
        });
    }

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

    public function calculate(PriceCollection $price, CalculatedPriceCollection $prices, SalesChannelContext $context, int $quantity = 1): CalculatedPrice
    {
        $currency = $price->getCurrencyPrice($context->getCurrencyId());

        if (!$currency) {
            throw CartException::invalidPriceDefinition();
        }

        $value = $context->getTaxState() === CartPrice::TAX_STATE_GROSS ? $currency->getGross() : $currency->getNet();

        if ($currency->getCurrencyId() !== $context->getCurrencyId()) {
            $value *= $context->getCurrency()->getFactor();
        }

        $taxRules = $this->percentageTaxRuleBuilder->buildRules($prices->sum());

        $definition = new QuantityPriceDefinition($value$taxRules$quantity);

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


    private function getPriceForTaxState(PriceCollection $price, SalesChannelContext $context): float
    {
        $currency = $price->getCurrencyPrice($this->context->getCurrencyId());

        if (!$currency instanceof Price) {
            throw ProductException::invalidPriceDefinition();
        }

        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $currency->getGross();
        }

        return $currency->getNet();
    }
}

        return $this->linked;
    }

    public function setLinked(bool $linked): void
    {
        $this->linked = $linked;
    }

    public function add(self $price): void
    {
        $this->gross += $price->getGross();
        $this->net += $price->getNet();
    }

    public function getCurrencyId(): string
    {
        return $this->currencyId;
    }

    public function setCurrencyId(string $currencyId): void
    {
        $this->currencyId = $currencyId;
    }


        $stubs = new ScriptPriceStubs($connection$this->createMock(QuantityPriceCalculator::class)$this->createMock(PercentagePriceCalculator::class));

        $actual = $stubs->build($prices);

        foreach ($expected as $expectedPrice) {
            $actualPrice = $actual->getCurrencyPrice($expectedPrice->getCurrencyId());

            static::assertInstanceOf(Price::class$actualPrice);
            static::assertEquals($expectedPrice->getNet()$actualPrice->getNet());
            static::assertEquals($expectedPrice->getGross()$actualPrice->getGross());
            static::assertEquals($expectedPrice->getLinked()$actualPrice->getLinked());
        }
    }

    public static function priceCases(): \Generator
    {
        yield 'manual price definition' => [
            [
                'default' => ['gross' => 100, 'net' => 90],
                'USD' => ['gross' => 90, 'net' => 80],
            ],
            


    protected function getPriceForTaxState(PriceCollection $price, SalesChannelContext $context): float
    {
        $currency = $price->getCurrencyPrice($this->context->getCurrencyId());

        if (!$currency instanceof Price) {
            throw CartException::invalidPriceDefinition();
        }

        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $currency->getGross();
        }

        return $currency->getNet();
    }

    private function overwrite(QuantityPriceDefinition $definition): void
    {
        if ($this->item instanceof LineItem) {
            $this->item->markModifiedByApp();

            $this->item->setPriceDefinition($definition);
        }
if ($price->getCurrencyId() === Defaults::CURRENCY) {
            $value *= $context->getContext()->getCurrencyFactor();
        }

        return $value;
    }

    private function getPriceForTaxState(Price $price, SalesChannelContext $context): float
    {
        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $price->getGross();
        }

        return $price->getNet();
    }

    private function getMatchingPriceOfRule(Delivery $delivery, SalesChannelContext $context, ShippingMethodPriceCollection $shippingPrices): ?CalculatedPrice
    {
        $shippingPrices->sort(
            function DShippingMethodPriceEntity $priceEntityA, ShippingMethodPriceEntity $priceEntityB) use ($context) {
                /** @var PriceCollection $priceCollectionA */
                $priceCollectionA = $priceEntityA->getCurrencyPrice();
                
if ($type !== AbsolutePriceDefinition::TYPE) {
            throw CartException::surchargeTypeNotSupported($key$type);
        }
        if (!$value instanceof PriceCollection) {
            throw CartException::absoluteSurchargeMissingPriceCollection($key);
        }
        if (!$value->has(Defaults::CURRENCY)) {
            throw CartException::missingDefaultPriceCollectionForSurcharge($key);
        }

        foreach ($value as $price) {
            $price->setGross(\abs($price->getGross()));
            $price->setNet(\abs($price->getNet()));

            if (!$price->getListPrice()) {
                continue;
            }
            $price->getListPrice()->setGross(\abs($price->getListPrice()->getGross()));
            $price->getListPrice()->setNet(\abs($price->getListPrice()->getNet()));
        }

        return new CurrencyPriceDefinition($value);
    }
}
$this->productRepository->update([
            [
                'id' => $id,
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 1000, 'net' => 1000, 'linked' => false]],
            ],
        ]$versionContext);

        $product = $this->productRepository->search(new Criteria([$id])$context)->first();
        static::assertInstanceOf(ProductEntity::class$product);
        $price = $product->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$price);
        static::assertEquals(100, $price->getGross());
        static::assertEquals(10, $price->getNet());

        $product = $this->productRepository->search(new Criteria([$id])$versionContext)->first();
        static::assertInstanceOf(ProductEntity::class$product);
        $price = $product->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$price);
        static::assertEquals(1000, $price->getGross());
        static::assertEquals(1000, $price->getNet());

        $this->productRepository->merge($versionId$context);

        
static::assertEquals(1, $result->count());

        $element = $result->first();
        static::assertNotNull($element);
        static::assertEquals(3, $element->getTotal());
        static::assertEquals('Test Cross Selling', $element->getCrossSelling()->getName());

        $lastPrice = 0;
        foreach ($element->getProducts() as $product) {
            $productPrice = $product->getCurrencyPrice(Defaults::CURRENCY);
            static::assertNotNull($productPrice);
            static::assertGreaterThanOrEqual($lastPrice$productPrice->getGross());
            $lastPrice = $productPrice->getGross();
        }
    }

    public function testLoadForProduct(): void
    {
        $productId = Uuid::randomHex();

        $productData = $this->getProductData($productId);
        $productData['crossSellings'] = [[
            'name' => 'Test Cross Selling',
            
static::assertSame($id$product->getId());

        static::assertEquals(new Price(Defaults::CURRENCY, 10, 15, false)$product->getCurrencyPrice(Defaults::CURRENCY));
        static::assertInstanceOf(ProductPriceCollection::class$product->getPrices());
        static::assertCount(2, $product->getPrices());

        $price = $product->getPrices()->get($ruleA);
        static::assertInstanceOf(ProductPriceEntity::class$price);
        $currencyPrice = $price->getPrice()->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$currencyPrice);
        static::assertSame(15.0, $currencyPrice->getGross());
        static::assertSame(10.0, $currencyPrice->getNet());

        $price = $product->getPrices()->get($ruleB);
        static::assertInstanceOf(ProductPriceEntity::class$price);
        $currencyPrice = $price->getPrice()->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$currencyPrice);
        static::assertSame(10.0, $currencyPrice->getGross());
        static::assertSame(8.0, $currencyPrice->getNet());
    }

    public function testProductPricesSortByGrossPrice(): void
    {
$criteria = new Criteria([$parentId$greenId$redId]);
        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(false);
        $products = $this->productRepository->search($criteria$context);

        /** @var ProductEntity $parent */
        $parent = $products->get($parentId);
        static::assertInstanceOf(ProductEntity::class$parent);
        static::assertInstanceOf(TaxEntity::class$parent->getTax());
        static::assertInstanceOf(Price::class$parent->getCurrencyPrice(Defaults::CURRENCY));
        static::assertEquals(50, $parent->getCurrencyPrice(Defaults::CURRENCY)->getGross());

        /** @var ProductEntity $red */
        $red = $products->get($redId);

        // check red product contains full inheritance of parent         static::assertInstanceOf(ProductEntity::class$red);

        // has no own tax         static::assertNull($red->getTax());
        static::assertNull($red->getTaxId());
        static::assertNull($red->getCurrencyPrice(Defaults::CURRENCY));

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