Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
calculateGrossTaxes example
$context
->
getItemRounding
(
)
)
;
return
$taxes
;
}
$price
=
$prices
->
sum
(
)
;
$rules
=
$this
->taxRuleBuilder->
buildRules
(
$price
)
;
if
(
$context
->
getTaxState
(
)
=== CartPrice::TAX_STATE_GROSS
)
{
$taxes
=
$this
->taxCalculator->
calculateGrossTaxes
(
$price
->
getTotalPrice
(
)
,
$rules
)
;
}
else
{
$taxes
=
$this
->taxCalculator->
calculateNetTaxes
(
$price
->
getTotalPrice
(
)
,
$rules
)
;
}
$taxes
->
round
(
$this
->rounding,
$context
->
getItemRounding
(
)
)
;
return
$taxes
;
}
}
public
function
__construct
(
private
readonly TaxCalculator
$taxCalculator
,
private
readonly CashRounding
$priceRounding
)
{
}
public
function
calculate
(
QuantityPriceDefinition
$definition
, CashRoundingConfig
$config
)
: CalculatedPrice
{
$unitPrice
=
$this
->
getUnitPrice
(
$definition
,
$config
)
;
$unitTaxes
=
$this
->taxCalculator->
calculateGrossTaxes
(
$unitPrice
,
$definition
->
getTaxRules
(
)
)
;
foreach
(
$unitTaxes
as
$tax
)
{
$total
=
$this
->priceRounding->
mathRound
(
$tax
->
getTax
(
)
*
$definition
->
getQuantity
(
)
,
$config
)
;
$tax
->
setTax
(
$total
)
;
$tax
->
setPrice
(
$tax
->
getPrice
(
)
*
$definition
->
getQuantity
(
)
)
;
}
class
TaxAdjustmentCalculatorTest
extends
TestCase
{
public
function
testCalculateGrossTaxesActuallyCalculatesNetTaxes
(
)
: void
{
$calculator
=
new
TaxAdjustmentCalculator
(
)
;
$taxes
=
$calculator
->
calculateGrossTaxes
(
100,
new
TaxRuleCollection
(
[
new
TaxRule
(
20, 50
)
,
new
TaxRule
(
10, 50
)
,
]
)
)
;
$taxes
=
$taxes
->
getElements
(
)
;
static
::
assertCount
(
2,
$taxes
)
;
static
::
assertArrayHasKey
(
20,
$taxes
)
;
static
::
assertArrayHasKey
(
10,
$taxes
)
;
static
::
assertEquals
(
20,
$taxes
[
20
]
->
getTaxRate
(
)
)
;