Skip to content

Saleor Core: the high performance, composable, headless commerce API.

add vat tax rates to country

Michał Jelonekba63d23eacc5
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index 02442d4806..52ff71b7ea 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -8,6 +8,7 @@ from saleor.graphql.core.utils import str_to_enum
from ....core import weight
from ..connection import CountableConnection
+from .money import VAT
class ReportingPeriod(graphene.Enum):
@@ -34,7 +35,8 @@ class Decimal(graphene.Float):
class CountryDisplay(graphene.ObjectType):
code = graphene.String(description='Country code.', required=True)
- country = graphene.String(description='Country.', required=True)
+ country = graphene.String(description='Country name.', required=True)
+ vat = graphene.Field(VAT, description='Country tax.')
class CountableDjangoObjectType(DjangoObjectType):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e428f9a1f7..e8ceea0764 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -468,6 +468,7 @@ type CollectionUpdate {
type CountryDisplay {
code: String!
country: String!
+ vat: VAT
}
type CreateToken {
@@ -668,6 +669,7 @@ input FulfillmentUpdateTrackingInput {
enum GatewaysEnum {
DUMMY
BRAINTREE
+ RAZORPAY
}
scalar GenericScalar
@@ -1591,6 +1593,11 @@ type Query {
node(id: ID!): Node
}
+type ReducedRate {
+ rate: Float!
+ rateType: String!
+}
+
type Refresh {
token: String
payload: GenericScalar
@@ -1956,6 +1963,12 @@ input UserCreateInput {
sendPasswordEmail: Boolean
}
+type VAT {
+ countryCode: String!
+ standardRate: Float
+ reducedRates: [ReducedRate]
+}
+
type VariantImageAssign {
errors: [Error]
productVariant: ProductVariant
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index 7259074cd1..03865129ed 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -4,6 +4,7 @@ import graphene
import graphene_django_optimizer as gql_optimizer
from django.conf import settings
from django_countries import countries
+from django_prices_vatlayer.models import VAT
from graphql_jwt.decorators import permission_required
from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
@@ -117,8 +118,10 @@ class Shop(graphene.ObjectType):
return site_models.AuthorizationKey.objects.all()
def resolve_countries(self, info):
+ taxes = {vat.country_code: vat for vat in VAT.objects.all()}
return [
- CountryDisplay(code=country[0], country=country[1])
+ CountryDisplay(
+ code=country[0], country=country[1], vat=taxes.get(country[0]))
for country in countries]
def resolve_currencies(self, info):
@@ -195,8 +198,11 @@ class Shop(graphene.ObjectType):
default_country_code = settings.DEFAULT_COUNTRY
default_country_name = countries.countries.get(default_country_code)
if default_country_name:
+ vat = VAT.objects.filter(country_code=default_country_code).first()
default_country = CountryDisplay(
- code=default_country_code, country=default_country_name)
+ code=default_country_code,
+ country=default_country_name,
+ vat=vat)
else:
default_country = None
return default_country
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index 7ec0ac3a75..b51874e27f 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -1,5 +1,6 @@
import graphene
from django_countries import countries
+from django_prices_vatlayer.models import VAT
from saleor.core.permissions import MODELS_PERMISSIONS
from saleor.graphql.core.utils import str_to_enum
from saleor.site import AuthenticationBackends
@@ -44,6 +45,74 @@ def test_query_countries(user_api_client):
assert len(data['countries']) == len(countries)
+def test_query_countries_with_tax(user_api_client, vatlayer, tax_rates):
+ query = """
+ query {
+ shop {
+ countries {
+ code
+ vat {
+ standardRate
+ reducedRates {
+ rate
+ rateType
+ }
+ }
+ }
+ }
+ }
+ """
+ response = user_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ data = content['data']['shop']['countries']
+ vat = VAT.objects.first()
+ country = next(
+ country for country in data if country['code'] == vat.country_code)
+ assert country['vat']['standardRate'] == tax_rates['standard_rate']
+ rates = {
+ rate['rateType']: rate['rate']
+ for rate in country['vat']['reducedRates']}
+ assert rates == tax_rates['reduced_rates']
+
+
+def test_query_default_country(user_api_client, settings):
+ query = """
+ query {
+ shop {
+ defaultCountry {
+ country
+ }
+ }
+ }
+ """
+ response = user_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ data = content['data']['shop']['defaultCountry']
+ assert data['country'] == settings.DEFAULT_COUNTRY
+
+
+def test_query_default_country_with_tax(
+ user_api_client, settings, vatlayer, tax_rates):
+ settings.DEFAULT_COUNTRY = 'PL'
+ query = """
+ query {
+ shop {
+ defaultCountry {
+ code
+ vat {
+ standardRate
+ }
+ }
+ }
+ }
+ """
+ response = user_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ data = content['data']['shop']['defaultCountry']
+ assert data['code'] == settings.DEFAULT_COUNTRY
+ assert data['vat']['standardRate'] == tax_rates['standard_rate']
+
+
def test_query_currencies(user_api_client, settings):
query = """
query {