cashRound example


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

    /** * @return array<array{float, int, TaxRule, float}> */
    public static function netPricesToGross(): array
    {
        
foreach ($unitTaxes as $tax) {
            $total = $this->priceRounding->mathRound(
                $tax->getTax() * $definition->getQuantity(),
                $config
            );

            $tax->setTax($total);

            $tax->setPrice($tax->getPrice() * $definition->getQuantity());
        }

        $price = $this->priceRounding->cashRound(
            $unitPrice * $definition->getQuantity(),
            $config
        );

        $reference = $this->calculateReferencePrice($unitPrice$definition->getReferencePriceDefinition()$config);

        return new CalculatedPrice(
            $unitPrice,
            $price,
            $unitTaxes,
            $definition->getTaxRules(),
            
$cart->setPrice($price);
    }

    private function applyCartPriceTaxes(CartPrice $price, CalculatedTaxCollection $taxes, SalesChannelContext $context): CartPrice
    {
        $netPrice = $price->getNetPrice();
        $grossPrice = $price->getTotalPrice();
        $taxSum = $taxes->getAmount();

        if ($context->getTaxState() === CartPrice::TAX_STATE_NET) {
            $grossPrice = $this->rounding->cashRound(
                $netPrice + $taxSum,
                $context->getTotalRounding()
            );
        }

        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            $netPrice = $this->rounding->cashRound(
                $grossPrice - $taxSum,
                $context->getTotalRounding()
            );
        }

        
class CashRoundingTest extends TestCase
{
    /** * @dataProvider provider_german */
    public function testGerman(float $price, float $expected): void
    {
        $service = new CashRounding();

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

        $actual = $service->cashRound($price$config);
        static::assertEquals($expected$actual);
    }

    /** * @dataProvider provider_hong_kong */
    public function testHongKong(float $price, float $expected): void
    {
        $service = new CashRounding();

        $config = new CashRoundingConfig(2, 0.10, true);

        

    private function calculateGrossAmount(PriceCollection $prices, PriceCollection $shippingCosts, SalesChannelContext $context): CartPrice
    {
        $all = $prices->merge($shippingCosts);

        $total = $all->sum();
        $taxes = $this->calculateTaxes($all$context);

        $price = $this->rounding->cashRound(
            $total->getTotalPrice(),
            $context->getTotalRounding()
        );

        $net = $this->rounding->mathRound(
            $total->getTotalPrice() - $taxes->getAmount(),
            $context->getItemRounding()
        );

        return new CartPrice(
            $net,
            
return new ReferencePrice(
            $price,
            $definition->getPurchaseUnit(),
            $definition->getReferenceUnit(),
            $definition->getUnitName()
        );
    }

    private function round(float $price, CashRoundingConfig $config): float
    {
        if ($config->roundForNet()) {
            return $this->priceRounding->cashRound($price$config);
        }

        return $this->priceRounding->mathRound($price$config);
    }
}
$definition = new QuantityPriceDefinition($discount$rules, 1);

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

    private function round(float $price, SalesChannelContext $context): float
    {
        if ($context->getTaxState() !== CartPrice::TAX_STATE_GROSS && !$context->getItemRounding()->roundForNet()) {
            return $this->rounding->mathRound($price$context->getItemRounding());
        }

        return $this->rounding->cashRound($price$context->getItemRounding());
    }
}
Home | Imprint | This part of the site doesn't use cookies.