TaxCalculator example


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

        

class AbsolutePriceCalculatorTest extends TestCase
{
    /** * @dataProvider provider */
    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(),
            
class DiscountAbsoluteCalculatorTest extends TestCase
{
    /** * @dataProvider priceProvider */
    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);

        

class TaxCalculatorTest extends TestCase
{
    /** * @dataProvider netPricesToGross */
    public function testCalculateGrossPriceOfNetPrice(float $expected, int $precision, TaxRule $taxRule, float $net): void
    {
        $calculator = new TaxCalculator();

        $rules = new TaxRuleCollection([$taxRule]);

        $rounding = new CashRounding();
        static::assertEquals(
            $expected,
            $rounding->cashRound(
                $calculator->calculateGross($net$rules),
                new CashRoundingConfig($precision, 0.01, true)
            )
        );
    }

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

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

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

    public static function referencePriceCalculationProvider(): \Generator
    {
        yield 'test calculation without reference price' => [
            null,
            100,
            null,
        ];
class CalculatorTest extends TestCase
{
    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));
    }

    
$salesChannelContext->method('getSalesChannel')->willReturn(new SalesChannelEntity());

        $salesChannelContext->method('getContext')->willReturn(Context::createDefaultContext());
        $salesChannelContext->method('getTaxCalculationType')->willReturn(SalesChannelDefinition::CALCULATION_TYPE_HORIZONTAL);
        $salesChannelContext->method('getItemRounding')->willReturn(new CashRoundingConfig(2, 0.01, true));
        $salesChannelContext->method('getTotalRounding')->willReturn(new CashRoundingConfig(2, 0.01, true));
        $salesChannelContext->method('getTaxState')->willReturn(CartPrice::TAX_STATE_GROSS);

        $calculator = new AmountCalculator(
            new CashRounding(),
            new PercentageTaxRuleBuilder(),
            new TaxCalculator()
        );

        $cartPrice = $calculator->calculate($pricesnew PriceCollection()$salesChannelContext);
        static::assertEquals($expected$cartPrice);
    }

    /** * @dataProvider calculateAmountWithNetPricesProvider */
    public function testCalculateAmountWithNetPrices(CartPrice $expected, PriceCollection $prices): void
    {
        
$cart = new Cart('token');
        $cart->addLineItems(new LineItemCollection($products));

        return $cart;
    }

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

        $taxCalculator = new TaxCalculator();

        return new QuantityPriceCalculator(
            new GrossPriceCalculator($taxCalculator$priceRounding),
            new NetPriceCalculator($taxCalculator$priceRounding),
        );
    }
}

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

    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);
    }
}
yield 'Test net default currency' => ['default', CartPrice::TAX_STATE_NET, 2.0, 0.2];
        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())]);

        

class PercentagePriceCalculatorTest extends TestCase
{
    /** * @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(
            
yield 'Test usd currency' => ['usd', CartPrice::TAX_STATE_GROSS, 6.0, 0.55];

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

        
new ShippingMethodEntity(),
                new ShippingLocation(new CountryEntity()new CountryStateEntity(), null),
                clone $calculatedPrice
            ),
        ]);

        $cart->addDeliveries($deliveries);

        $calculator = new AmountCalculator(
            new CashRounding(),
            new PercentageTaxRuleBuilder(),
            new TaxCalculator()
        );

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

        // let the cart calculate with the real tax calculator to simulate previous cart calculation         $cartPrice = $calculator->calculate(
            $cart->getLineItems()->getPrices(),
            $cart->getDeliveries()->getShippingCosts(),
            
Home | Imprint | This part of the site doesn't use cookies.