Skip to content

Commit

Permalink
Fixed #120 -- Explicitly sotre tax values
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Feb 11, 2016
1 parent 0741c00 commit febf352
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/pretix/base/migrations/0006_auto_20160211_1630.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-02-11 16:30
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pretixbase', '0005_auto_20160211_1459'),
]

operations = [
migrations.AddField(
model_name='orderposition',
name='tax_rate',
field=models.DecimalField(decimal_places=2, default=0, max_digits=7, verbose_name='Tax rate'),
preserve_default=False,
),
migrations.AddField(
model_name='orderposition',
name='tax_value',
field=models.DecimalField(decimal_places=2, default=0, max_digits=10, verbose_name='Tax value'),
preserve_default=False,
),
]
34 changes: 33 additions & 1 deletion src/pretix/base/models/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from datetime import datetime
from decimal import Decimal

from django.db import models, transaction
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from typing import List, Union

from ..decimal import round_decimal
from .base import CachedFile, LoggedModel
from .event import Event
from .items import Item, ItemVariation, Question, Quota
Expand Down Expand Up @@ -361,6 +362,14 @@ class OrderPosition(AbstractPosition):
related_name='positions',
on_delete=models.PROTECT
)
tax_rate = models.DecimalField(
max_digits=7, decimal_places=2,
verbose_name=_('Tax rate')
)
tax_value = models.DecimalField(
max_digits=10, decimal_places=2,
verbose_name=_('Tax value')
)

class Meta:
verbose_name = _("Order position")
Expand All @@ -381,6 +390,7 @@ def transform_cart_positions(cls, cp: List, order) -> list:
cartpos.voucher.redeemed = True
cartpos.voucher.save()
cartpos.delete()
op._calculate_tax()
ops.append(op)
OrderPosition.objects.bulk_create(ops)
return ops
Expand All @@ -390,6 +400,18 @@ def __repr__(self):
self.item.id, self.variation.id if self.variation else 0, self.order_id
)

def _calculate_tax(self):
self.tax_rate = self.item.tax_rate
if self.tax_rate:
self.tax_value = round_decimal(self.price * (1 - 100 / (100 + self.item.tax_rate)))
else:
self.tax_value = Decimal('0.00')

def save(self, *args, **kwargs):
if self.tax_rate is None:
self._calculate_tax()
return super().save(*args, **kwargs)


class CartPosition(AbstractPosition):
"""
Expand Down Expand Up @@ -429,3 +451,13 @@ def __repr__(self):
return '<CartPosition: item %d, variation %d for cart %s>' % (
self.item.id, self.variation.id if self.variation else 0, self.cart_id
)

@property
def tax_rate(self):
return self.item.tax_rate

@property
def tax_value(self):
if not self.tax_rate:
return Decimal('0.00')
return round_decimal(self.price * (1 - 100 / (100 + self.item.tax_rate)))
4 changes: 2 additions & 2 deletions src/pretix/control/templates/pretixcontrol/order/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ <h3 class="panel-title">
</div>
<div class="col-md-3 col-xs-6 price">
<strong>{{ event.currency }} {{ line.total|floatformat:2 }}</strong>
{% if line.item.tax_rate %}
{% if line.tax_rate %}
<br/>
<small>{% blocktrans trimmed with rate=line.item.tax_rate %}
<small>{% blocktrans trimmed with rate=line.tax_rate %}
incl. {{ rate }}% taxes
{% endblocktrans %}</small>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
</div>
<div class="col-md-3 col-xs-6 price">
<strong>{{ event.currency }} {{ line.total|floatformat:2 }}</strong>
{% if line.item.tax_rate %}
<br /><small>{% blocktrans trimmed with rate=line.item.tax_rate %}
{% if line.tax_rate %}
<br /><small>{% blocktrans trimmed with rate=line.tax_rate %}
incl. {{ rate }}% taxes
{% endblocktrans %}</small>
{% endif %}
Expand Down

0 comments on commit febf352

Please sign in to comment.