Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
mathRound example
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
(
)
)
;
}
$price
=
$this
->priceRounding->
cashRound
(
$unitPrice
*
$definition
->
getQuantity
(
)
,
$exists
->
increment
(
$calculatedTax
)
;
}
return
$new
;
}
public
function
round
(
CashRounding
$rounding
, CashRoundingConfig
$config
)
: void
{
foreach
(
$this
->elements
as
$tax
)
{
$tax
->
setTax
(
$rounding
->
mathRound
(
$tax
->
getTax
(
)
,
$config
)
)
;
}
}
public
function
getApiAlias
(
)
: string
{
return
'cart_tax_calculated_collection';
}
protected
function
getExpectedClass
(
)
: ?string
{
$rules
=
$this
->percentageTaxRuleBuilder->
buildRules
(
$price
)
;
$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
(
)
)
;
}
}
#[Package('checkout')]
class
CashRounding
{
public
function
mathRound
(
float
$price
, CashRoundingConfig
$config
)
: float
{
return
round
(
$price
,
$config
->
getDecimals
(
)
)
;
}
public
function
cashRound
(
float
$price
, CashRoundingConfig
$config
)
: float
{
$rounded
=
$this
->
mathRound
(
$price
,
$config
)
;
if
(
$config
->
getDecimals
(
)
> 2
)
{
return
$rounded
;
}
$multiplier
= 100 /
(
$config
->
getInterval
(
)
* 100
)
;
return
round
(
$rounded
*
$multiplier
, 0
)
/
$multiplier
;
}
}
$unitPrice
=
$this
->
round
(
$definition
->
getPrice
(
)
,
$config
)
;
$taxRules
=
$definition
->
getTaxRules
(
)
;
$calculatedTaxes
=
$this
->taxCalculator->
calculateNetTaxes
(
$unitPrice
,
$definition
->
getTaxRules
(
)
)
;
foreach
(
$calculatedTaxes
as
$tax
)
{
$total
=
$this
->priceRounding->
mathRound
(
$tax
->
getTax
(
)
*
$definition
->
getQuantity
(
)
,
$config
)
;
$tax
->
setTax
(
$total
)
;
$tax
->
setPrice
(
$tax
->
getPrice
(
)
*
$definition
->
getQuantity
(
)
)
;
}
$price
=
$this
->
round
(
$unitPrice
*
$definition
->
getQuantity
(
)
,
$config
)
;
$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
,
$price
,
$prices
->
sum
(
)
->
getTotalPrice
(
)
,
$taxes
,
$total
->
getTaxRules
(
)
,
CartPrice::TAX_STATE_GROSS,