NetPriceCalculator example


class NetPriceCalculatorTest extends TestCase
{
    /** * @dataProvider referencePriceCalculationProvider */
    public function testReferencePriceCalculation(?ReferencePriceDefinition $reference, ?ReferencePrice $expected): void
    {
        $definition = new QuantityPriceDefinition(100, new TaxRuleCollection(), 1);
        $definition->setReferencePriceDefinition($reference);

        $calculator = new NetPriceCalculator(new TaxCalculator()new CashRounding());
        $price = $calculator->calculate($definitionnew CashRoundingConfig(2, 0.01, true));

        static::assertEquals($expected$price->getReferencePrice());
    }

    public static function referencePriceCalculationProvider(): \Generator
    {
        yield 'test calculation without reference price' => [
            null,
            null,
        ];

        

    private Calculator $calculator;

    private SalesChannelContext $context;

    protected function setUp(): void
    {
        $cashRounding = new CashRounding();
        $percentageTaxRuleBuilder = new PercentageTaxRuleBuilder();
        $quantityPriceCalculator = new QuantityPriceCalculator(
            new GrossPriceCalculator(new TaxCalculator()$cashRounding),
            new NetPriceCalculator(new TaxCalculator()$cashRounding),
        );
        $this->calculator = new Calculator(
            $quantityPriceCalculator,
            new PercentagePriceCalculator($cashRounding$quantityPriceCalculator$percentageTaxRuleBuilder),
            new AbsolutePriceCalculator($quantityPriceCalculator$percentageTaxRuleBuilder)
        );

        $this->context = $this->createMock(SalesChannelContext::class);
        $this->context->method('getItemRounding')->willReturn(new CashRoundingConfig(2, 0.01, true));
    }

    
yield 'Test net default currency' => ['default', CartPrice::TAX_STATE_NET, 8.0, 0.8];
        yield 'Test net usd currency' => ['usd', CartPrice::TAX_STATE_NET, 9.0, 0.9];
    }

    private function rampUpPriceFacade(IdsCollection $ids, string $currencyKey, string $taxState): PriceFacade
    {
        $entity = new Entity();

        $quantityCalculator = new QuantityPriceCalculator(
            new GrossPriceCalculator(new TaxCalculator()new CashRounding()),
            new NetPriceCalculator(new TaxCalculator()new CashRounding())
        );

        $stubs = new ScriptPriceStubs(
            // not necessary for this test             $this->createMock(Connection::class),
            $quantityCalculator,
            new PercentagePriceCalculator(new CashRounding()$quantityCalculatornew PercentageTaxRuleBuilder()),
        );

        $original = new CalculatedPrice(10, 10, new CalculatedTaxCollection()new TaxRuleCollection(new TaxRuleCollection([new TaxRule(10)])));

        
private ProductPriceCalculator $calculator;

    protected function setUp(): void
    {
        $this->calculator = new ProductPriceCalculator(
            new StaticEntityRepository([
                new UnitCollection([(
                new UnitEntity())->assign(['id' => Defaults::CURRENCY, 'translated' => ['name' => 'test']])]),
            ]),
            new QuantityPriceCalculator(
                new GrossPriceCalculator(new TaxCalculator()new CashRounding()),
                new NetPriceCalculator(new TaxCalculator()new CashRounding())
            )
        );
    }

    /** * @dataProvider priceWillBeCalculated */
    public function testPriceWillBeCalculated(Entity $entity, ?PriceAssertion $expected): void
    {
        $context = $this->createMock(SalesChannelContext::class);
        $context->method('getCurrencyId')->willReturn(Defaults::CURRENCY);
        

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

        static::assertEquals($expected$lineItemPrice);
    }

    /** * @dataProvider netPrices */

    public function testCalculateAbsolutePriceOfGrossPrices(AbsoluteCalculation $calculation): void
    {
        $rounding = new CashRounding();

        $taxCalculator = new TaxCalculator();

        $calculator = new AbsolutePriceCalculator(
            new QuantityPriceCalculator(
                new GrossPriceCalculator($taxCalculator$rounding),
                new NetPriceCalculator($taxCalculator$rounding),
            ),
            new PercentageTaxRuleBuilder()
        );

        $calculatedPrice = $calculator->calculate(
            $calculation->getDiscount(),
            $calculation->getPrices(),
            Generator::createSalesChannelContext()
        );

        static::assertEquals($calculation->getExpected()->getCalculatedTaxes()$calculatedPrice->getCalculatedTaxes());
        
public function __construct(
        float $price,
        TaxRuleCollection $taxes,
        SalesChannelContext $context,
        bool $good = true,
        int $quantity = 1
    ) {
        parent::__construct($price$taxes$good$quantity);

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

        \assert($this->getPriceDefinition() instanceof QuantityPriceDefinition);
        $this->price = $calculator->calculate($this->getPriceDefinition()$context);
    }
}
return $cart;
    }

    private function createQuantityPriceCalculator(): QuantityPriceCalculator
    {
        $priceRounding = new CashRounding();

        $taxCalculator = new TaxCalculator();

        return new QuantityPriceCalculator(
            new GrossPriceCalculator($taxCalculator$priceRounding),
            new NetPriceCalculator($taxCalculator$priceRounding),
        );
    }
}
/** * @dataProvider grossPriceDataProvider */
    public function testCalculatePercentagePriceOfGrossPrices(PercentageCalculation $calculation): void
    {
        $taxCalculator = new TaxCalculator();
        $rounding = new CashRounding();
        $calculator = new PercentagePriceCalculator(
            $rounding,
            new QuantityPriceCalculator(
                new GrossPriceCalculator($taxCalculator$rounding),
                new NetPriceCalculator($taxCalculator$rounding),
            ),
            new PercentageTaxRuleBuilder()
        );

        $price = $calculator->calculate(
            $calculation->getPercentageDiscount(),
            $calculation->getPrices(),
            Generator::createSalesChannelContext()
        );
        $expected = $calculation->getExpected();

        
yield 'Test net usd currency' => ['usd', CartPrice::TAX_STATE_NET, 1.0, 0.1];
    }

    private function rampUpPriceFacade(IdsCollection $ids, string $currencyKey, string $taxState): CheapestPriceFacade
    {
        $entity = new class() extends Entity {
            protected CalculatedPrice $calculatedPrice;
        };

        $quantityCalculator = new QuantityPriceCalculator(
            new GrossPriceCalculator(new TaxCalculator()new CashRounding()),
            new NetPriceCalculator(new TaxCalculator()new CashRounding())
        );

        $stubs = new ScriptPriceStubs(
            // not necessary for this test             $this->createMock(Connection::class),
            $quantityCalculator,
            new PercentagePriceCalculator(new CashRounding()$quantityCalculatornew PercentageTaxRuleBuilder()),
        );

        $entity->assign(['calculatedPrice' => new CalculatedPrice(10, 10, new CalculatedTaxCollection()new TaxRuleCollection())]);

        
public function testCalculate(float $discountIn, float $packageSum, float $discountOut): void
    {
        $context = Generator::createSalesChannelContext();

        $rounding = new CashRounding();

        $taxCalculator = new TaxCalculator();

        $calculator = new AbsolutePriceCalculator(
            new QuantityPriceCalculator(
                new GrossPriceCalculator($taxCalculator$rounding),
                new NetPriceCalculator($taxCalculator$rounding),
            ),
            new PercentageTaxRuleBuilder()
        );

        $discountCalculator = new DiscountAbsoluteCalculator($calculator);

        $priceDefinition = new AbsolutePriceDefinition($discountIn);
        $discount = new DiscountLineItem('foo', $priceDefinition['discountScope' => 'foo', 'discountType' => 'bar'], null);

        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 2);
        $lineItem->setPrice(new CalculatedPrice($packageSum / 2, $packageSumnew CalculatedTaxCollection()new TaxRuleCollection()));

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