Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getLineItemTaxes example
[
'tax' => 8.0,
'taxRate' => 8.0,
'price' => 8.0,
]
,
]
,
]
;
$response
= TaxProviderResponse::
create
(
$arrayStruct
)
;
static
::
assertNotNull
(
$response
->
getLineItemTaxes
(
)
)
;
static
::
assertCount
(
2,
$response
->
getLineItemTaxes
(
)
)
;
static
::
assertArrayHasKey
(
$this
->ids->
get
(
'line-item-1'
)
,
$response
->
getLineItemTaxes
(
)
)
;
static
::
assertArrayHasKey
(
$this
->ids->
get
(
'line-item-2'
)
,
$response
->
getLineItemTaxes
(
)
)
;
static
::
assertEquals
(
new
CalculatedTax
(
1.0, 1.0, 1.0
)
,
$response
->
getLineItemTaxes
(
)
[
$this
->ids->
get
(
'line-item-1'
)
]
->
getAt
(
0
)
)
;
static
::
assertEquals
(
new
CalculatedTax
(
2.0, 2.0, 2.0
)
,
$response
->
getLineItemTaxes
(
)
[
$this
->ids->
get
(
'line-item-2'
)
]
->
getAt
(
0
)
)
;
static
::
assertEquals
(
new
CalculatedTax
(
3.0, 3.0, 3.0
)
,
$response
->
getLineItemTaxes
(
)
[
$this
->ids->
get
(
'line-item-2'
)
]
->
getAt
(
1
)
)
;
static
::
assertNotNull
(
$response
->
getDeliveryTaxes
(
)
)
;
static
::
assertCount
(
2,
$response
->
getDeliveryTaxes
(
)
)
;
static
::
assertArrayHasKey
(
$this
->ids->
get
(
'delivery-1'
)
,
$response
->
getDeliveryTaxes
(
)
)
;
class
TaxProviderStructTest
extends
TestCase
{
public
function
testEmpty
(
)
: void
{
$struct
=
new
TaxProviderResult
(
)
;
static
::
assertNull
(
$struct
->
getLineItemTaxes
(
)
)
;
static
::
assertNull
(
$struct
->
getDeliveryTaxes
(
)
)
;
static
::
assertNull
(
$struct
->
getCartPriceTaxes
(
)
)
;
static
::
assertFalse
(
$struct
->
declaresTaxes
(
)
)
;
}
/** * @param array<string, CalculatedTaxCollection>|null $lineItemTaxes * @param array<string, CalculatedTaxCollection>|null $deliveryTaxes * * @dataProvider structDataProvider */
->
willReturn
(
'very-secret'
)
;
$taxResponse
=
$taxProviderPayloadService
->
request
(
$url
,
$payload
,
$app
,
$context
)
;
static
::
assertInstanceOf
(
TaxProviderResponse::
class
,
$taxResponse
)
;
$lineItemTaxes
=
$taxResponse
->
getLineItemTaxes
(
)
;
static
::
assertNotNull
(
$lineItemTaxes
)
;
static
::
assertArrayHasKey
(
$this
->ids->
get
(
'line-item-1'
)
,
$lineItemTaxes
)
;
$taxes
=
$lineItemTaxes
[
$this
->ids->
get
(
'line-item-1'
)
]
;
$tax
=
$taxes
->
first
(
)
;
static
::
assertInstanceOf
(
CalculatedTax::
class
,
$tax
)
;
static
::
assertCount
(
1,
$taxes
)
;
static
::
assertSame
(
19.0,
$tax
->
getTax
(
)
)
;
static
::
assertSame
(
19.0,
$tax
->
getTaxRate
(
)
)
;
static
::
assertSame
(
100.0,
$tax
->
getPrice
(
)
)
;
$deliveryTaxes
=
$taxResponse
->
getDeliveryTaxes
(
)
;
public
function
__construct
(
private
readonly AmountCalculator
$amountCalculator
,
private
readonly CashRounding
$rounding
)
{
}
public
function
adjust
(
Cart
$cart
, TaxProviderResult
$result
, SalesChannelContext
$context
)
: void
{
$lineItems
=
$cart
->
getLineItems
(
)
;
$deliveries
=
$cart
->
getDeliveries
(
)
;
$this
->
applyLineItemTaxes
(
$lineItems
,
$result
->
getLineItemTaxes
(
)
)
;
$this
->
applyDeliveryTaxes
(
$deliveries
,
$result
->
getDeliveryTaxes
(
)
)
;
$price
=
$this
->amountCalculator->
calculate
(
$cart
->
getLineItems
(
)
->
getPrices
(
)
,
$cart
->
getDeliveries
(
)
->
getShippingCosts
(
)
,
$context
)
;
// either take the price from the tax provider result or take the calculated taxes
$taxes
=
$price
->
getCalculatedTaxes
(
)
->
filter
(
fn
(
CalculatedTax
$tax
)
=>
$tax
->
getTax
(
)
> 0.0
)
;
$price
->
setCalculatedTaxes
(
$taxes
)
;