Skip to content

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

Save tax rates in order lines

Anna Siwike1ee18d5933e
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 283aa8700f..54272f3b8b 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -106,10 +106,14 @@ def get_taxes_for_country(country):
if tax_rates is None:
return None
- taxes = {'standard': get_tax_for_rate(tax_rates)}
+ taxes = {'standard': {
+ 'value': tax_rates['standard_rate'],
+ 'tax': get_tax_for_rate(tax_rates)}}
if tax_rates['reduced_rates']:
taxes.update({
- rate: get_tax_for_rate(tax_rates, rate)
+ rate: {
+ 'value': tax_rates['reduced_rates'][rate],
+ 'tax': get_tax_for_rate(tax_rates, rate)}
for rate in tax_rates['reduced_rates']})
return taxes
@@ -129,9 +133,9 @@ def apply_tax_to_price(taxes, rate_name, base):
raise TypeError('Unknown base for flat_tax: %r' % (base,))
if rate_name in taxes:
- tax_to_apply = taxes[rate_name]
+ tax_to_apply = taxes[rate_name]['tax']
else:
- tax_to_apply = taxes['standard']
+ tax_to_apply = taxes['standard']['tax']
return tax_to_apply(base, keep_gross=settings.INCLUDE_TAXES_IN_PRICES)
diff --git a/saleor/order/migrations/0046_orderline_tax_rate.py b/saleor/order/migrations/0046_orderline_tax_rate.py
new file mode 100644
index 0000000000..89543030ef
--- /dev/null
+++ b/saleor/order/migrations/0046_orderline_tax_rate.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.0.3 on 2018-04-16 07:58
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('order', '0045_auto_20180329_0142'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='orderline',
+ name='tax_rate',
+ field=models.DecimalField(decimal_places=2, default='0.0', max_digits=5),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index d796bab472..afc002eb99 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -197,6 +197,8 @@ class OrderLine(models.Model):
decimal_places=settings.DEFAULT_DECIMAL_PLACES)
unit_price = TaxedMoneyField(
net_field='unit_price_net', gross_field='unit_price_gross')
+ tax_rate = models.DecimalField(
+ max_digits=5, decimal_places=2, default='0.0')
def __str__(self):
return self.product_name
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index d50c3667de..46e057878e 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -128,6 +128,20 @@ def attach_order_to_user(order, user):
order.save(update_fields=['user'])
+def get_variant_tax_rate(variant, taxes=None):
+ """Return value of variant tax rate for current taxes."""
+ if not taxes:
+ return 0
+
+ rate_name = variant.product.tax_rate
+ if rate_name in taxes:
+ tax = taxes[rate_name]
+ else:
+ tax = taxes['standard']
+
+ return tax['value']
+
+
def add_variant_to_order(
order, variant, total_quantity, discounts=None, taxes=None,
add_to_existing=True):
@@ -155,7 +169,8 @@ def add_variant_to_order(
variant.product.product_type.is_shipping_required),
quantity=quantity_not_fulfilled,
unit_price=price,
- variant=variant)
+ variant=variant,
+ tax_rate=get_variant_tax_rate(variant, taxes))
allocate_stock(variant, quantity_not_fulfilled)
diff --git a/templates/dashboard/order/pdf/invoice.html b/templates/dashboard/order/pdf/invoice.html
index 9dedc83f96..1486690c47 100644
--- a/templates/dashboard/order/pdf/invoice.html
+++ b/templates/dashboard/order/pdf/invoice.html
@@ -64,7 +64,7 @@
{% trans "SKU" context "Shipment order table header" %}
</th>
<th class="right-align">
- {% trans "Price net" context "Shipment order table header" %}
+ {% trans "Tax rate" context "Shipment order table header" %}
</th>
<th class="right-align">
{% trans "Price gross" context "Shipment order table header" %}
@@ -87,7 +87,9 @@
{{ line.product_sku }}
</td>
<td class="right-align">
- {{ line.unit_price.net|amount:'html' }}
+ {% blocktrans with tax_rate=line.tax_rate context "Order line tax rate value" %}
+ {{ tax_rate }} %
+ {% endblocktrans %}
</td>
<td class="right-align">
{{ line.unit_price.gross|amount:'html' }}
@@ -133,7 +135,7 @@
{% if order.discount_amount %}
<tr>
<td>
- {% trans "Voucher" context "Order voucher header" %} {{ order.discount_name }}
+ {% trans "Discount" context "Order voucher header" %} {% if order.discount_name %}({{ order.discount_name }}){% endif %}
</td>
<td class="right-align">
{% discount_as_negative order.discount_amount html=True %}
diff --git a/tests/conftest.py b/tests/conftest.py
index 3c9a80a732..a5c87a524d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -399,7 +399,7 @@ def order_with_lines(order, product_type, default_category, shipping_method):
unit_price_net=Decimal('20.00'),
unit_price_gross=Decimal('20.00'),
variant=variant)
- product = Product.objects.create(
+ Product.objects.create(
name='Test product 3', price=Money('30.00', 'USD'),
product_type=product_type, category=default_category)
@@ -711,10 +711,14 @@ def tax_rates():
@pytest.fixture
def taxes(tax_rates):
- taxes = {'standard': get_tax_for_rate(tax_rates)}
+ taxes = {'standard': {
+ 'value': tax_rates['standard_rate'],
+ 'tax': get_tax_for_rate(tax_rates)}}
if tax_rates['reduced_rates']:
taxes.update({
- rate: get_tax_for_rate(tax_rates, rate)
+ rate: {
+ 'value': tax_rates['reduced_rates'][rate],
+ 'tax': get_tax_for_rate(tax_rates, rate)}
for rate in tax_rates['reduced_rates']})
return taxes
diff --git a/tests/test_order.py b/tests/test_order.py
index ef7589a79f..698d0f0441 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -10,8 +10,9 @@ from saleor.order import FulfillmentStatus, OrderStatus, models
from saleor.order.forms import OrderNoteForm
from saleor.order.models import Order
from saleor.order.utils import (
- add_variant_to_order, cancel_fulfillment, cancel_order, recalculate_order,
- restock_fulfillment_lines, restock_order_lines, update_order_status)
+ add_variant_to_order, cancel_fulfillment, cancel_order,
+ get_variant_tax_rate, recalculate_order, restock_fulfillment_lines,
+ restock_order_lines, update_order_status)
def test_total_setter():
@@ -36,18 +37,44 @@ def test_order_get_subtotal(order_with_lines):
assert order_with_lines.get_subtotal() == target_subtotal
+def test_get_variant_tax_rate(product, taxes):
+ variant = product.variants.get()
+
+ tax_rate = get_variant_tax_rate(variant, taxes)
+
+ assert tax_rate == taxes[product.tax_rate]['value']
+
+
+def test_get_variant_tax_rate_fallback_to_standard(product, taxes):
+ product.tax_rate = 'unexisting tax rate'
+ variant = product.variants.get()
+
+ tax_rate = get_variant_tax_rate(variant, taxes)
+
+ assert tax_rate == taxes['standard']['value']
+
+
+def test_get_variant_tax_rate_empty_taxes(product):
+ variant = product.variants.get()
+
+ tax_rate = get_variant_tax_rate(variant)
+
+ assert tax_rate == 0
+
+
def test_add_variant_to_order_adds_line_for_new_variant(
- order_with_lines, product):
+ order_with_lines, product, taxes):
order = order_with_lines
variant = product.variants.get()
lines_before = order.lines.count()
- add_variant_to_order(order, variant, 1)
+ add_variant_to_order(order, variant, 1, taxes=taxes)
line = order.lines.last()
assert order.lines.count() == lines_before + 1
assert line.product_sku == variant.sku
assert line.quantity == 1
+ assert line.tax_rate == taxes[product.tax_rate]['value']
def test_add_variant_to_order_allocates_stock_for_new_variant(