Skip to content

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

Ability to configure if taxes are included in prices

Anna Siwikf105d68af5f0
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 54272f3b8b..a2eafe41f1 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -137,7 +137,8 @@ def apply_tax_to_price(taxes, rate_name, base):
else:
tax_to_apply = taxes['standard']['tax']
- return tax_to_apply(base, keep_gross=settings.INCLUDE_TAXES_IN_PRICES)
+ keep_gross = Site.objects.get_current().settings.include_taxes_in_prices
+ return tax_to_apply(base, keep_gross=keep_gross)
def to_local_currency(price, currency):
diff --git a/saleor/dashboard/sites/forms.py b/saleor/dashboard/sites/forms.py
index d3ecc35f92..578d83f6f9 100644
--- a/saleor/dashboard/sites/forms.py
+++ b/saleor/dashboard/sites/forms.py
@@ -27,6 +27,16 @@ class SiteSettingsForm(forms.ModelForm):
'Description', 'Description')}
+class SiteSettingsTaxesForm(forms.ModelForm):
+ class Meta:
+ model = SiteSettings
+ fields = ['include_taxes_in_prices']
+ labels = {
+ 'include_taxes_in_prices': pgettext_lazy(
+ 'Include taxes in prices',
+ 'All taxes are included in my prices')}
+
+
class AuthorizationKeyForm(forms.ModelForm):
class Meta:
model = AuthorizationKey
diff --git a/saleor/dashboard/sites/urls.py b/saleor/dashboard/sites/urls.py
index 717f4e14f7..33fa97e182 100644
--- a/saleor/dashboard/sites/urls.py
+++ b/saleor/dashboard/sites/urls.py
@@ -10,6 +10,8 @@ urlpatterns = [
name='site-details'),
url(r'^(?P<pk>\d+)/delete/$', views.site_settings_edit,
name='site-delete'),
+ url(r'^(?P<pk>\d+)/configure-taxes/$', views.site_configure_taxes,
+ name='site-configure-taxes'),
url(r'^(?P<site_settings_pk>\d+)/authorization_key/add/$',
views.authorization_key_add, name='authorization-key-add'),
diff --git a/saleor/dashboard/sites/views.py b/saleor/dashboard/sites/views.py
index 3e69cd5baa..1ee94facdc 100644
--- a/saleor/dashboard/sites/views.py
+++ b/saleor/dashboard/sites/views.py
@@ -7,7 +7,8 @@ from django.utils.translation import pgettext_lazy
from ...site.models import AuthorizationKey, SiteSettings
from ..views import staff_member_required
-from .forms import AuthorizationKeyForm, SiteForm, SiteSettingsForm
+from .forms import (
+ AuthorizationKeyForm, SiteForm, SiteSettingsForm, SiteSettingsTaxesForm)
@staff_member_required
@@ -39,6 +40,22 @@ def site_settings_edit(request, pk):
return TemplateResponse(request, 'dashboard/sites/form.html', ctx)
+@staff_member_required
+@permission_required('site.edit_settings')
+def site_configure_taxes(request, pk):
+ site_settings = get_object_or_404(SiteSettings, pk=pk)
+ taxes_form = SiteSettingsTaxesForm(
+ request.POST or None, instance=site_settings)
+
+ if taxes_form.is_valid():
+ taxes_form.save()
+ msg = pgettext_lazy('Dashboard message', 'Updated taxes settings')
+ messages.success(request, msg)
+ return redirect('dashboard:site-details', pk=site_settings.id)
+ ctx = {'site': site_settings, 'taxes_form': taxes_form}
+ return TemplateResponse(request, 'dashboard/sites/taxes_form.html', ctx)
+
+
@staff_member_required
@permission_required('site.edit_settings')
def site_settings_details(request, pk):
diff --git a/saleor/product/migrations/0060_product_taxes.py b/saleor/product/migrations/0061_product_taxes.py
similarity index 92%
rename from saleor/product/migrations/0060_product_taxes.py
rename to saleor/product/migrations/0061_product_taxes.py
index 50252647d8..5a1de9b70f 100644
--- a/saleor/product/migrations/0060_product_taxes.py
+++ b/saleor/product/migrations/0061_product_taxes.py
@@ -6,7 +6,7 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
- ('product', '0059_generate_variant_name_from_attrs'),
+ ('product', '0060_collection_is_published'),
]
operations = [
diff --git a/saleor/settings.py b/saleor/settings.py
index 6d34709e7a..be529129b7 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -280,11 +280,8 @@ AVAILABLE_CURRENCIES = [DEFAULT_CURRENCY]
OPENEXCHANGERATES_API_KEY = os.environ.get('OPENEXCHANGERATES_API_KEY')
# VAT configuration
-# Enabling vat requires valid vatlayer access key. By default prices entered in
-# dashboard are treated as gross amounts. If you wish to reverse that and use
-# them as net amounts change the INCLUDE_TAXES_IN_PRICES to False
+# Enabling vat requires valid vatlayer access key.
VATLAYER_ACCESS_KEY = os.environ.get('VATLAYER_ACCESS_KEY')
-INCLUDE_TAXES_IN_PRICES = True
ACCOUNT_ACTIVATION_DAYS = 3
diff --git a/saleor/site/migrations/0014_sitesettings_include_taxes_in_prices.py b/saleor/site/migrations/0014_sitesettings_include_taxes_in_prices.py
new file mode 100644
index 0000000000..2189c96110
--- /dev/null
+++ b/saleor/site/migrations/0014_sitesettings_include_taxes_in_prices.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.0.3 on 2018-04-16 10:46
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('site', '0013_assign_default_menus'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='sitesettings',
+ name='include_taxes_in_prices',
+ field=models.BooleanField(default=True),
+ ),
+ ]
diff --git a/saleor/site/models.py b/saleor/site/models.py
index f5b71a432e..c28f0794cb 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -19,6 +19,7 @@ class SiteSettings(models.Model):
bottom_menu = models.ForeignKey(
'menu.Menu', on_delete=models.SET_NULL, related_name='+', blank=True,
null=True)
+ include_taxes_in_prices = models.BooleanField(default=True)
class Meta:
permissions = (
diff --git a/templates/dashboard/sites/detail.html b/templates/dashboard/sites/detail.html
index 6bbf951950..6981871420 100644
--- a/templates/dashboard/sites/detail.html
+++ b/templates/dashboard/sites/detail.html
@@ -54,18 +54,42 @@
{% trans "Description" context "Site settings field" %}
</h4>
<p>
- {% if site.description %}
- {{ site.description }}
+ {{ site.description|default:"-" }}
+ </p>
+ </div>
+ </div>
+ </div>
+ <div class="card-action">
+ <a class="btn-flat waves-effect" href="{% url 'dashboard:site-update' pk=site.pk %}">
+ {% trans "Edit site settings" context "Site settings action" %}
+ </a>
+ </div>
+ </div>
+ </div>
+ <div class="col s12 l9">
+ <div class="card">
+ <div class="card-content">
+ <span class="card-title">
+ {% trans "Taxes" context "Site settings title" %}
+ </span>
+ <div class="row">
+ <div class="col s12 m4 l12">
+ <h4>
+ {% trans "All taxes are included in my prices" context "Site settings field" %}
+ </h4>
+ <p>
+ {% if site.include_taxes_in_prices %}
+ {% trans "Yes" context "Include taxes in prices setting description" %}
{% else %}
- -
+ {% trans "No" context "Include taxes in prices setting description" %}
{% endif %}
</p>
</div>
</div>
</div>
<div class="card-action">
- <a class="btn-flat waves-effect" href="{% url 'dashboard:site-update' pk=site.pk %}">
- {% trans "Edit site settings" context "Shipping method action" %}
+ <a class="btn-flat waves-effect" href="{% url 'dashboard:site-configure-taxes' pk=site.pk %}">
+ {% trans "Configure taxes" context "Site settings configure taxes action" %}
</a>
</div>
</div>
diff --git a/templates/dashboard/sites/taxes_form.html b/templates/dashboard/sites/taxes_form.html
new file mode 100644
index 0000000000..c728c318e9
--- /dev/null
+++ b/templates/dashboard/sites/taxes_form.html
@@ -0,0 +1,60 @@
+{% extends 'dashboard/base.html' %}
+{% load i18n %}
+{% load materializecss %}
+
+{% block title %}
+ {% if site.pk %}
+ {{ site }}
+ {% else %}
+ {% trans "Add new site settings" context "Site settings page title" %}
+ {% endif %}
+ - {% trans "Site settings" context "Dashboard site settings" %} - {{ block.super }}
+{% endblock %}
+
+{% block body_class %}body-settings{% endblock %}
+
+{% block menu_settings_class %}active{% endblock %}
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--history">
+ <li>
+ <a href="{% url 'dashboard:site-index' %}" class="breadcrumbs--ellipsed-item">
+ {% trans "Site settings" context "Dashboard attributes list" %}
+ </a>
+ </li>
+ <li>
+ <span>
+ {% trans "Edit taxes" context "Dashboard site settings" %}
+ </span>
+ </li>
+ </ul>
+{% endblock %}
+
+{% block content %}
+ <div class="row">
+ <div class="col s12 l9">
+ <div class="card">
+ <form method="post" id="form-category" enctype="multipart/form-data" novalidate>
+ {% csrf_token %}
+ <div class="card-content card-content-form">
+ <div class="card-title">
+ {% trans "Taxes" context "Dashboard site taxes settings form title" %}
+ </div>
+ <div class="row">
+ {{ taxes_form.include_taxes_in_prices|materializecss }}
+ </div>
+ </div>
+ <div class="card-action right-align">
+ <a class="btn-flat waves-effect" href="{% url 'dashboard:site-details' pk=site.pk %}" target="_blank">
+ {% trans "Cancel" context "Dashboard cancel action" %}
+ </a>
+ <button type="submit" class="btn waves-effect">
+ {% trans "Update" context "Dashboard update action" %}
+ </button>
+ </div>
+ </form>
+ </div>
+ </div>
+ <div class="col m12 l3 help-box"></div>
+ </div>
+{% endblock %}
diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.py
index fbb23034e0..329ec84195 100644
--- a/tests/dashboard/test_permissions.py
+++ b/tests/dashboard/test_permissions.py
@@ -898,3 +898,19 @@ def test_staff_group_member_can_view_and_edit_site_settings(
response = staff_client.get(reverse('dashboard:site-update',
args=[site_settings.pk]))
assert response.status_code == 200
+
+
+def test_staff_group_member_can_view_and_edit_site_taxes_settings(
+ staff_client, staff_user, staff_group, site_settings,
+ permission_edit_settings):
+ assert not staff_user.has_perm('site.edit_settings')
+ url = reverse(
+ 'dashboard:site-configure-taxes', kwargs={'pk': site_settings.pk})
+ response = staff_client.get(url)
+ assert response.status_code == 302
+ staff_group.permissions.add(permission_edit_settings)
+ staff_user.groups.add(staff_group)
+ staff_user = User.objects.get(pk=staff_user.pk)
+ assert staff_user.has_perm('site.edit_settings')
+ response = staff_client.get(url)
+ assert response.status_code == 200
diff --git a/tests/dashboard/test_site_settings.py b/tests/dashboard/test_site_settings.py
index 13ed113fcc..b64a78c0aa 100644
--- a/tests/dashboard/test_site_settings.py
+++ b/tests/dashboard/test_site_settings.py
@@ -59,6 +59,18 @@ def test_site_update_view(admin_client, site_settings):
assert site_settings.site.name == 'Mirumee Labs'
+def test_site_configure_taxes(admin_client, site_settings):
+ url = reverse(
+ 'dashboard:site-configure-taxes', kwargs={'pk': site_settings.pk})
+ data = {'include_taxes_in_prices': False}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ site_settings.refresh_from_db()
+ assert not site_settings.include_taxes_in_prices
+
+
def test_get_authorization_key_for_backend(site_settings, authorization_key):
key_for_backend = utils.get_authorization_key_for_backend('Backend')
assert key_for_backend == authorization_key
diff --git a/tests/test_core.py b/tests/test_core.py
index f5b38edc72..c87a2cfed3 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -32,8 +32,9 @@ type_schema = {
'is_shipping_required': True}}
-def test_apply_tax_to_price_include_tax(settings, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+def test_apply_tax_to_price_include_tax(site_settings, taxes):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
money = Money(100, 'USD')
assert apply_tax_to_price(taxes, 'standard', money) == TaxedMoney(
@@ -49,8 +50,9 @@ def test_apply_tax_to_price_include_tax(settings, taxes):
def test_apply_tax_to_price_include_tax_fallback_to_standard_rate(
- settings, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+ site_settings, taxes):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
money = Money(100, 'USD')
taxed_money = TaxedMoney(net=Money(100, 'USD'), gross=Money(123, 'USD'))
diff --git a/tests/test_product.py b/tests/test_product.py
index 099948c483..af5e332803 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -439,10 +439,11 @@ def test_create_product_thumbnails(
('15.00', True, False, True, '10.00', '10.00'),
('15.00', True, True, True, '8.13', '10.00')])
def test_get_price_per_item(
- settings, product_type, default_category, taxes, sale, product_price,
+ product_type, default_category, taxes, sale, product_price,
include_taxes_in_prices, include_taxes, include_discounts,
- product_net, product_gross):
- settings.INCLUDE_TAXES_IN_PRICES = include_taxes_in_prices
+ product_net, product_gross, site_settings):
+ site_settings.include_taxes_in_prices = include_taxes_in_prices
+ site_settings.save()
product = models.Product.objects.create(
product_type=product_type,
category=default_category,
@@ -458,8 +459,9 @@ def test_get_price_per_item(
def test_product_get_price_per_item_variant_has_no_price(
- settings, product_type, default_category, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+ product_type, default_category, taxes, site_settings):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
product = models.Product.objects.create(
product_type=product_type,
category=default_category,
@@ -473,8 +475,9 @@ def test_product_get_price_per_item_variant_has_no_price(
def test_product_get_price_per_item_variant_with_price(
- settings, product_type, default_category, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+ product_type, default_category, taxes, site_settings):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
product = models.Product.objects.create(
product_type=product_type,
category=default_category,
@@ -488,8 +491,9 @@ def test_product_get_price_per_item_variant_with_price(
def test_product_get_price_range_with_variants(
- settings, product_type, default_category, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+ product_type, default_category, taxes, site_settings):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
product = models.Product.objects.create(
product_type=product_type,
category=default_category,
@@ -508,8 +512,9 @@ def test_product_get_price_range_with_variants(
def test_product_get_price_range_no_variants(
- settings, product_type, default_category, taxes):
- settings.INCLUDE_TAXES_IN_PRICES = False
+ product_type, default_category, taxes, site_settings):
+ site_settings.include_taxes_in_prices = False
+ site_settings.save()
product = models.Product.objects.create(
product_type=product_type,
category=default_category,