Tax rules and reverse charge (#559)

Tax rules and reverse charge
This commit is contained in:
Raphael Michel
2017-08-23 13:13:16 +03:00
committed by GitHub
parent b9ec5ea83c
commit 56338be13e
82 changed files with 2934 additions and 428 deletions

View File

@@ -11,7 +11,7 @@ from django.views.generic.base import TemplateResponseMixin
from pretix.base.models import Order
from pretix.base.models.orders import InvoiceAddress
from pretix.base.services.cart import set_cart_addons
from pretix.base.services.cart import set_cart_addons, update_tax_rates
from pretix.base.services.orders import perform_order
from pretix.multidomain.urlreverse import eventreverse
from pretix.presale.forms.checkout import (
@@ -80,6 +80,17 @@ class BaseCheckoutFlowStep:
if n:
return n.get_step_url()
@cached_property
def invoice_address(self):
iapk = self.request.session.get('invoice_address_{}'.format(self.request.event.pk))
if not iapk:
return InvoiceAddress()
try:
return InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
return InvoiceAddress()
def get_checkout_flow(event):
flow = list([step(event) for step in DEFAULT_FLOW])
@@ -163,7 +174,7 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
quota_cache = {}
item_cache = {}
for cartpos in get_cart(self.request).filter(addon_to__isnull=True).prefetch_related(
'item__addons', 'item__addons__addon_category', 'addons', 'addons__variation'
'item__addons', 'item__addons__addon_category', 'addons', 'addons__variation',
):
current_addon_products = {
a.item_id: a.variation_id for a in cartpos.addons.all()
@@ -244,7 +255,8 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
if not is_valid:
return self.get(request, *args, **kwargs)
return self.do(self.request.event.id, data, self.request.session.session_key)
return self.do(self.request.event.id, data, self.request.session.session_key,
invoice_address=self.invoice_address.pk)
class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
@@ -266,21 +278,17 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
initial=initial)
@cached_property
def invoice_address(self):
iapk = self.request.session.get('invoice_address')
if not iapk:
return InvoiceAddress()
try:
return InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
return InvoiceAddress()
def eu_reverse_charge_relevant(self):
return any([p.item.tax_rule and p.item.tax_rule.eu_reverse_charge
for p in self.positions])
@cached_property
def invoice_form(self):
return InvoiceAddressForm(data=self.request.POST if self.request.method == "POST" else None,
event=self.request.event,
instance=self.invoice_address)
request=self.request,
instance=self.invoice_address,
validate_vat_id=self.eu_reverse_charge_relevant)
def post(self, request):
self.request = request
@@ -294,9 +302,15 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
request.session['email'] = self.contact_form.cleaned_data['email']
if request.event.settings.invoice_address_asked:
addr = self.invoice_form.save()
request.session['invoice_address'] = addr.pk
request.session['invoice_address_{}'.format(request.event.pk)] = addr.pk
request.session['contact_form_data'] = self.contact_form.cleaned_data
update_tax_rates(
event=request.event,
cart_id=request.session.session_key,
invoice_address=self.invoice_form.instance
)
return redirect(self.get_next_url(request))
def is_completed(self, request, warn=False):
@@ -354,6 +368,7 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
ctx['formgroups'] = self.formdict.items()
ctx['contact_form'] = self.contact_form
ctx['invoice_form'] = self.invoice_form
ctx['reverse_charge_relevant'] = self.eu_reverse_charge_relevant
return ctx
@@ -387,7 +402,9 @@ class PaymentStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
if p['provider'].identifier == request.POST.get('payment', ''):
request.session['payment'] = p['provider'].identifier
resp = p['provider'].checkout_prepare(
request, self.get_cart(payment_fee=p['provider'].calculate_fee(self._total_order_value)))
request,
self.get_cart()
)
if isinstance(resp, str):
return redirect(resp)
elif resp is True:
@@ -476,16 +493,6 @@ class ConfirmStep(CartMixin, AsyncAction, TemplateFlowStep):
def payment_provider(self):
return self.request.event.get_payment_providers().get(self.request.session['payment'])
@cached_property
def invoice_address(self):
try:
return InvoiceAddress.objects.get(
pk=self.request.session.get('invoice_address'),
order__isnull=True
)
except InvoiceAddress.DoesNotExist:
return InvoiceAddress()
def get(self, request):
self.request = request
if 'async_id' in request.GET and settings.HAS_CELERY:

View File

@@ -1,8 +1,12 @@
import logging
import os
from decimal import Decimal
from itertools import chain
import vat_moss.errors
import vat_moss.id
from django import forms
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.db.models import Count, Prefetch, Q
from django.utils.encoding import force_text
@@ -10,13 +14,15 @@ from django.utils.formats import number_format
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from pretix.base.decimal import round_decimal
from pretix.base.models import ItemVariation, Question
from pretix.base.models.orders import InvoiceAddress, OrderPosition
from pretix.base.models.tax import EU_COUNTRIES, TAXED_ZERO
from pretix.base.templatetags.rich_text import rich_text
from pretix.multidomain.urlreverse import eventreverse
from pretix.presale.signals import contact_form_fields, question_form_fields
logger = logging.getLogger(__name__)
class ContactForm(forms.Form):
required_css_class = 'required'
@@ -94,6 +100,8 @@ class InvoiceAddressForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.event = event = kwargs.pop('event')
self.request = kwargs.pop('request', None)
self.validate_vat_id = kwargs.pop('validate_vat_id')
super().__init__(*args, **kwargs)
if not event.settings.invoice_address_vatid:
del self.fields['vat_id']
@@ -115,9 +123,35 @@ class InvoiceAddressForm(forms.ModelForm):
if not data.get('name') and not data.get('company') and self.event.settings.invoice_address_required:
raise ValidationError(_('You need to provide either a company name or your name.'))
if 'vat_id' in self.changed_data or not data.get('vat_id'):
self.instance.vat_id_validated = False
if self.validate_vat_id and self.instance.vat_id_validated and 'vat_id' not in self.changed_data:
pass
elif self.validate_vat_id and data.get('is_business') and data.get('country') in EU_COUNTRIES and data.get('vat_id'):
if data.get('vat_id')[:2] != str(data.get('country')):
raise ValidationError(_('Your VAT ID does not match the selected country.'))
try:
result = vat_moss.id.validate(data.get('vat_id'))
if result:
country_code, normalized_id, company_name = result
self.instance.vat_id_validated = True
self.instance.vat_id = normalized_id
except vat_moss.errors.InvalidError:
raise ValidationError(_('This VAT ID is not valid. Please re-check your input.'))
except vat_moss.errors.WebServiceUnavailableError:
logger.exception('VAT ID checking failed for country {}'.format(data.get('country')))
self.instance.vat_id_validated = False
if self.request:
messages.warning(self.request, _('Your VAT ID could not be checked, as the VAT checking service of '
'your country is currently not available. We will therefore '
'need to charge VAT on your invoice. You can get the tax amount '
'back via the VAT reimbursement process.'))
else:
self.instance.vat_id_validated = False
class UploadedFileWidget(forms.ClearableFileInput):
def __init__(self, *args, **kwargs):
self.position = kwargs.pop('position')
self.event = kwargs.pop('event')
@@ -309,7 +343,6 @@ class AddOnRadioSelect(forms.RadioSelect):
class AddOnVariationField(forms.ChoiceField):
def valid_value(self, value):
text_value = force_text(value)
for k, v, d in self.choices:
@@ -328,39 +361,37 @@ class AddOnsForm(forms.Form):
variation = item_or_variation
item = item_or_variation.item
price = variation.price
price_net = variation.net_price
label = variation.value
else:
item = item_or_variation
price = item.default_price
price_net = item.default_price_net
label = item.name
if override_price:
price = override_price
tax_value = round_decimal(price * (1 - 100 / (100 + item.tax_rate)))
price_net = price - tax_value
if self.price_included:
price = Decimal('0.00')
price = TAXED_ZERO
else:
price = item.tax(price)
if not price:
if not price.gross:
n = '{name}'.format(
name=label
)
elif not item.tax_rate:
elif not price.rate:
n = _('{name} (+ {currency} {price})').format(
name=label, currency=event.currency, price=number_format(price)
name=label, currency=event.currency, price=number_format(price.gross)
)
elif event.settings.display_net_prices:
n = _('{name} (+ {currency} {price} plus {taxes}% taxes)').format(
name=label, currency=event.currency, price=number_format(price_net),
taxes=number_format(item.tax_rate)
n = _('{name} (+ {currency} {price} plus {taxes}% {taxname})').format(
name=label, currency=event.currency, price=number_format(price.net),
taxes=number_format(price.rate), taxname=price.name
)
else:
n = _('{name} (+ {currency} {price} incl. {taxes}% taxes)').format(
name=label, currency=event.currency, price=number_format(price),
taxes=number_format(item.tax_rate)
n = _('{name} (+ {currency} {price} incl. {taxes}% {taxname})').format(
name=label, currency=event.currency, price=number_format(price.gross),
taxes=number_format(price.rate), taxname=price.name
)
if avail[0] < 20:
@@ -406,7 +437,7 @@ class AddOnsForm(forms.Form):
& Q(Q(available_from__isnull=True) | Q(available_from__lte=now()))
& Q(Q(available_until__isnull=True) | Q(available_until__gte=now()))
& Q(hide_without_voucher=False)
).prefetch_related(
).select_related('tax_rule').prefetch_related(
Prefetch('quotas',
to_attr='_subevent_quotas',
queryset=event.quotas.filter(subevent=subevent)),

View File

@@ -1,6 +1,7 @@
{% load i18n %}
{% load eventurl %}
{% load safelink %}
{% blocktrans asvar s_taxes %}taxes{% endblocktrans %}
{% for line in cart.positions %}
<div class="row cart-row {% if download and line.item.admission|default:event.settings.ticket_download_nonadm %}has-downloads{% endif %}">
<div class="product">
@@ -121,16 +122,22 @@
{% if event.settings.display_net_prices %}
<strong>{{ event.currency }} {{ line.net_total|floatformat:2 }}</strong>
{% if line.tax_rate %}
<br /><small>{% blocktrans trimmed with rate=line.tax_rate %}
<strong>plus</strong> {{ rate }}% taxes
{% endblocktrans %}</small>
<br />
<small>
{% blocktrans trimmed with rate=line.tax_rate taxname=line.tax_rule.name|default:s_taxes %}
<strong>plus</strong> {{ rate }}% {{ taxname }}
{% endblocktrans %}
</small>
{% endif %}
{% else %}
<strong>{{ event.currency }} {{ line.total|floatformat:2 }}</strong>
{% if line.tax_rate %}
<br /><small>{% blocktrans trimmed with rate=line.tax_rate %}
incl. {{ rate }}% taxes
{% endblocktrans %}</small>
<br />
<small>
{% blocktrans trimmed with rate=line.tax_rate taxname=line.tax_rule.name|default:s_taxes %}
incl. {{ rate }}% {{ taxname }}
{% endblocktrans %}
</small>
{% endif %}
{% endif %}
</div>
@@ -158,18 +165,22 @@
{% if event.settings.display_net_prices %}
<strong>{{ event.currency }} {{ cart.payment_fee_net|floatformat:2 }}</strong>
{% if cart.payment_fee_tax_rate %}
<br/>
<small>{% blocktrans trimmed with rate=cart.payment_fee_tax_rate %}
<strong>plus</strong> {{ rate }}% taxes
{% endblocktrans %}</small>
<br />
<small>
{% blocktrans trimmed with rate=cart.payment_fee_tax_rate taxname=cart.payment_fee_tax_rule.name|default:s_taxes %}
<strong>plus</strong> {{ rate }}% {{ taxname }}
{% endblocktrans %}
</small>
{% endif %}
{% else %}
<strong>{{ event.currency }} {{ cart.payment_fee|floatformat:2 }}</strong>
{% if cart.payment_fee_tax_rate %}
<br/>
<small>{% blocktrans trimmed with rate=cart.payment_fee_tax_rate %}
incl. {{ rate }}% taxes
{% endblocktrans %}</small>
<br />
<small>
{% blocktrans trimmed with rate=cart.payment_fee_tax_rate taxname=cart.payment_fee_tax_rule.name|default:s_taxes %}
incl. {{ rate }}% {{ taxname }}
{% endblocktrans %}
</small>
{% endif %}
{% endif %}
</div>

View File

@@ -233,20 +233,24 @@
<span class="input-group-addon">{{ event.currency }}</span>
<input type="number" class="form-control input-item-price"
placeholder="0"
min="{{ var.display_price|stringformat:"0.2f" }}"
min="{% if event.settings.display_net_prices %}{{ var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
name="price_{{ item.id }}_{{ var.id }}"
step="any" value="{{ var.display_price|stringformat:"0.2f" }}">
step="any"
value="{% if event.settings.display_net_prices %}{{var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
>
</div>
{% elif event.settings.display_net_prices %}
{{ event.currency }} {{ var.display_price.net|floatformat:2 }}
{% else %}
{{ event.currency }} {{ var.display_price|floatformat:2 }}
{{ event.currency }} {{ var.display_price.gross|floatformat:2 }}
{% endif %}
{% if item.tax_rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
<strong>plus</strong> {{ rate }}% taxes
{% if var.display_price.rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
<strong>plus</strong> {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% elif item.tax_rate %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
incl. {{ rate }}% taxes
{% elif var.display_price.rate %}
<small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
incl. {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% endif %}
</div>
@@ -313,20 +317,23 @@
<div class="input-group input-group-price">
<span class="input-group-addon">{{ event.currency }}</span>
<input type="number" class="form-control input-item-price" placeholder="0"
min="{{ item.display_price|stringformat:"0.2f" }}"
min="{% if event.settings.display_net_prices %}{{ item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
name="price_{{ item.id }}"
step="any" value="{{ item.display_price|stringformat:"0.2f" }}">
value="{% if event.settings.display_net_prices %}{{item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
step="any">
</div>
{% elif event.settings.display_net_prices %}
{{ event.currency }} {{ item.display_price.net|floatformat:2 }}
{% else %}
{{ event.currency }} {{ item.display_price|floatformat:2 }}
{{ event.currency }} {{ item.display_price.gross|floatformat:2 }}
{% endif %}
{% if item.tax_rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
<strong>plus</strong> {{ rate }}% taxes
{% if item.display_price.rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
<strong>plus</strong> {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% elif item.tax_rate %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
incl. {{ rate }}% taxes
{% elif item.display_price.rate %}
<small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
incl. {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% endif %}
</div>

View File

@@ -82,20 +82,22 @@
<span class="input-group-addon">{{ event.currency }}</span>
<input type="number" class="form-control input-item-price"
placeholder="0"
min="{{ var.price|stringformat:"0.2f" }}"
min="{% if event.settings.display_net_prices %}{{ var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
name="price_{{ item.id }}_{{ var.id }}"
step="any"
value="{{ var.display_price|stringformat:"0.2f" }}">
value="{% if event.settings.display_net_prices %}{{var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
step="any">
</div>
{% elif event.settings.display_net_prices %}
{{ event.currency }} {{ var.display_price.net|floatformat:2 }}
{% else %}
{{ event.currency }} {{ var.display_price|floatformat:2 }}
{{ event.currency }} {{ var.display_price.gross|floatformat:2 }}
{% endif %}
{% if item.tax_rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
{% if var.display_price.rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
<strong>plus</strong> {{ rate }}% taxes
{% endblocktrans %}</small>
{% elif item.tax_rate %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
{% elif var.display_price.rate %}
<small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
incl. {{ rate }}% taxes
{% endblocktrans %}</small>
{% endif %}
@@ -145,20 +147,23 @@
<div class="input-group input-group-price">
<span class="input-group-addon">{{ event.currency }}</span>
<input type="number" class="form-control input-item-price" placeholder="0"
min="{{ item.price|stringformat:"0.2f" }}"
min="{% if event.settings.display_net_prices %}{{ item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
name="price_{{ item.id }}"
step="any" value="{{ item.price|stringformat:"0.2f" }}">
value="{% if event.settings.display_net_prices %}{{ item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
step="any">
</div>
{% elif event.settings.display_net_prices %}
{{ event.currency }} {{ item.display_price.net|floatformat:2 }}
{% else %}
{{ event.currency }} {{ item.price|floatformat:2 }}
{{ event.currency }} {{ item.display_price.gross|floatformat:2 }}
{% endif %}
{% if item.tax_rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
<strong>plus</strong> {{ rate }}% taxes
{% if item.display_price.rate and event.settings.display_net_prices %}
<small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
<strong>plus</strong> {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% elif item.tax_rate %}
<small>{% blocktrans trimmed with rate=item.tax_rate %}
incl. {{ rate }}% taxes
{% elif item.display_price.rate %}
<small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
incl. {{ rate }}% {{ name }}
{% endblocktrans %}</small>
{% endif %}
</div>

View File

@@ -7,8 +7,8 @@ from django.db.models import Sum
from django.utils.functional import cached_property
from django.utils.timezone import now
from pretix.base.decimal import round_decimal
from pretix.base.models import CartPosition, OrderPosition
from pretix.base.models import CartPosition, InvoiceAddress, OrderPosition
from pretix.base.models.tax import TaxRule
from pretix.presale.signals import question_form_fields
@@ -20,7 +20,7 @@ class CartMixin:
"""
return list(get_cart(self.request))
def get_cart(self, answers=False, queryset=None, payment_fee=None, payment_fee_tax_rate=None, downloads=False):
def get_cart(self, answers=False, queryset=None, order=None, downloads=False):
if queryset:
prefetch = []
if answers:
@@ -90,6 +90,7 @@ class CartMixin:
group.total = group.count * group.price
group.net_total = group.count * group.net_price
group.has_questions = answers and k[0] != ""
group.tax_rule = group.item.tax_rule
if answers:
group.cache_answers()
group.additional_answers = pos_additional_fields.get(group.pk)
@@ -99,14 +100,35 @@ class CartMixin:
net_total = sum(p.net_total for p in positions)
tax_total = sum(p.total - p.net_total for p in positions)
payment_fee = payment_fee if payment_fee is not None else self.get_payment_fee(total)
payment_fee_tax_rate = round_decimal(payment_fee_tax_rate
if payment_fee_tax_rate is not None
else self.request.event.settings.tax_rate_default)
payment_fee_tax_value = round_decimal(payment_fee * (1 - 100 / (100 + payment_fee_tax_rate)))
payment_fee_net = payment_fee - payment_fee_tax_value
tax_total += payment_fee_tax_value
net_total += payment_fee_net
if order:
payment_fee = order.payment_fee
tax_total += order.payment_fee_tax_value
payment_fee_net = order.payment_fee - order.payment_fee_tax_value
net_total += payment_fee_net
payment_fee_tax_rule = order.payment_fee_tax_rule
payment_fee_tax_rate = order.payment_fee_tax_rate
else:
payment_fee = self.get_payment_fee(total)
payment_fee_tax_rule = self.request.event.settings.tax_rate_default or TaxRule.zero()
iapk = self.request.session.get('invoice_address_{}'.format(self.request.event.pk))
ia = None
if payment_fee_tax_rule.eu_reverse_charge and iapk:
try:
ia = InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
pass
if payment_fee_tax_rule.tax_applicable(ia):
payment_fee_tax = payment_fee_tax_rule.tax(payment_fee, base_price_is='gross')
tax_total += payment_fee_tax.tax
net_total += payment_fee_tax.net
payment_fee_net = payment_fee_tax.net
payment_fee_tax_rate = payment_fee_tax.rate
else:
net_total += payment_fee
payment_fee_net = payment_fee
payment_fee_tax_rate = Decimal('0.00')
try:
first_expiry = min(p.expires for p in positions) if positions else now()
@@ -124,6 +146,7 @@ class CartMixin:
'payment_fee': payment_fee,
'payment_fee_net': payment_fee_net,
'payment_fee_tax_rate': payment_fee_tax_rate,
'payment_fee_tax_rule': payment_fee_tax_rule,
'answers': answers,
'minutes_left': minutes_left,
'first_expiry': first_expiry,
@@ -147,7 +170,8 @@ def get_cart(request):
).order_by(
'item', 'variation'
).select_related(
'item', 'variation', 'subevent', 'subevent__event', 'subevent__event__organizer'
'item', 'variation', 'subevent', 'subevent__event', 'subevent__event__organizer',
'item__tax_rule'
).prefetch_related(
'item__questions', 'answers'
)

View File

@@ -6,13 +6,14 @@ from django.db.models import Count, Prefetch, Q
from django.http import FileResponse, Http404, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils import translation
from django.utils.functional import cached_property
from django.utils.timezone import now
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView, View
from pretix.base.decimal import round_decimal
from pretix.base.models import (
CartPosition, ItemVariation, QuestionAnswer, Quota, SubEvent, Voucher,
CartPosition, InvoiceAddress, ItemVariation, QuestionAnswer, Quota,
SubEvent, Voucher,
)
from pretix.base.services.cart import (
CartError, add_items_to_cart, clear_cart, remove_cart_position,
@@ -37,6 +38,17 @@ class CartActionMixin:
def get_error_url(self):
return self.get_next_url()
@cached_property
def invoice_address(self):
iapk = self.request.session.get('invoice_address_{}'.format(self.request.event.pk))
if not iapk:
return InvoiceAddress()
try:
return InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
return InvoiceAddress()
def _item_from_post_value(self, key, value, voucher=None):
if value.strip() == '' or '_' not in key:
return
@@ -154,7 +166,8 @@ class CartAdd(EventViewMixin, CartActionMixin, AsyncAction, View):
def post(self, request, *args, **kwargs):
items = self._items_from_post_data()
if items:
return self.do(self.request.event.id, items, self.request.session.session_key, translation.get_language())
return self.do(self.request.event.id, items, self.request.session.session_key, translation.get_language(),
self.invoice_address.pk)
else:
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
return JsonResponse({
@@ -190,12 +203,12 @@ class RedeemView(EventViewMixin, TemplateView):
items = items.filter(quotas__in=[self.voucher.quota_id])
items = items.filter(vouchq).select_related(
'category', # for re-grouping
'category', 'tax_rule', # for re-grouping
).prefetch_related(
Prefetch('quotas',
to_attr='_subevent_quotas',
queryset=self.request.event.quotas.filter(subevent=self.subevent)),
Prefetch('variations', to_attr='avail_variations',
Prefetch('variations', to_attr='available_variations',
queryset=ItemVariation.objects.filter(active=True, quotas__isnull=False).prefetch_related(
Prefetch('quotas',
to_attr='_subevent_quotas',
@@ -217,13 +230,11 @@ class RedeemView(EventViewMixin, TemplateView):
var_price_override = {}
for item in items:
item.available_variations = list(item.variations.filter(active=True, quotas__isnull=False).distinct())
if self.voucher.item_id and self.voucher.variation_id:
item.available_variations = [v for v in item.available_variations if v.pk == self.voucher.variation_id]
item.order_max = item.max_per_order or int(self.request.event.settings.max_items_per_order)
item.has_variations = item.variations.exists()
if not item.has_variations:
item._remove = not bool(item._subevent_quotas)
if self.voucher.allow_ignore_quota or self.voucher.block_quota:
@@ -231,32 +242,32 @@ class RedeemView(EventViewMixin, TemplateView):
else:
item.cached_availability = item.check_quotas(subevent=self.subevent, _cache=quota_cache)
item.price = item_price_override.get(item.pk, item.default_price)
item.price = self.voucher.calculate_price(item.price)
if self.request.event.settings.display_net_prices:
item.price -= round_decimal(item.price * (1 - 100 / (100 + item.tax_rate)))
price = item_price_override.get(item.pk, item.default_price)
price = self.voucher.calculate_price(price)
item.display_price = item.tax(price)
else:
item._remove = False
for var in item.avail_variations:
for var in item.available_variations:
if self.voucher.allow_ignore_quota or self.voucher.block_quota:
var.cached_availability = (Quota.AVAILABILITY_OK, 1)
else:
var.cached_availability = list(var.check_quotas(subevent=self.subevent, _cache=quota_cache))
var.display_price = var_price_override.get(var.pk, var.price)
var.display_price = self.voucher.calculate_price(var.display_price)
if self.request.event.settings.display_net_prices:
var.display_price -= round_decimal(var.display_price * (1 - 100 / (100 + item.tax_rate)))
price = var_price_override.get(var.pk, var.price)
price = self.voucher.calculate_price(price)
var.display_price = item.tax(price)
item.available_variations = [
v for v in item.avail_variations if v._subevent_quotas
v for v in item.available_variations if v._subevent_quotas
]
if self.voucher.variation_id:
item.available_variations = [v for v in item.available_variations
if v.pk == self.voucher.variation_id]
if len(item.available_variations) > 0:
item.min_price = min([v.display_price for v in item.avail_variations])
item.max_price = max([v.display_price for v in item.avail_variations])
item.min_price = min([v.display_price.net if self.request.event.settings.display_net_prices else
v.display_price.gross for v in item.available_variations])
item.max_price = max([v.display_price.net if self.request.event.settings.display_net_prices else
v.display_price.gross for v in item.available_variations])
items = [item for item in items
if (len(item.available_variations) > 0 or not item.has_variations) and not item._remove]

View File

@@ -17,7 +17,6 @@ from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from pretix.base.decimal import round_decimal
from pretix.base.models import ItemVariation
from pretix.base.models.event import SubEvent
from pretix.multidomain.urlreverse import eventreverse
@@ -53,7 +52,7 @@ def get_grouped_items(event, subevent=None):
& Q(hide_without_voucher=False)
& ~Q(category__is_addon=True)
).select_related(
'category', # for re-grouping
'category', 'tax_rule', # for re-grouping
).prefetch_related(
Prefetch('quotas',
to_attr='_subevent_quotas',
@@ -88,17 +87,9 @@ def get_grouped_items(event, subevent=None):
item.order_max = min(item.cached_availability[1]
if item.cached_availability[1] is not None else sys.maxsize,
max_per_order)
item.price = item.default_price
price = item.default_price
item.display_price = item.tax(item_price_override.get(item.pk, price))
if event.settings.display_net_prices:
if item_price_override.get(item.pk):
_p = item_price_override.get(item.pk)
tax_value = round_decimal(_p * (1 - 100 / (100 + item.tax_rate)))
item.display_price = _p - tax_value
else:
item.display_price = item.default_price_net
else:
item.display_price = item_price_override.get(item.pk, item.price)
display_add_to_cart = display_add_to_cart or item.order_max > 0
else:
for var in item.available_variations:
@@ -107,15 +98,7 @@ def get_grouped_items(event, subevent=None):
if var.cached_availability[1] is not None else sys.maxsize,
max_per_order)
if event.settings.display_net_prices:
if var_price_override.get(var.pk):
_p = var_price_override.get(var.pk)
tax_value = round_decimal(_p * (1 - 100 / (100 + item.tax_rate)))
var.display_price = _p - tax_value
else:
var.display_price = var.net_price
else:
var.display_price = var_price_override.get(var.pk, var.price)
var.display_price = var.tax(var_price_override.get(var.pk, var.price))
display_add_to_cart = display_add_to_cart or var.order_max > 0
@@ -123,8 +106,10 @@ def get_grouped_items(event, subevent=None):
v for v in item.available_variations if v._subevent_quotas
]
if len(item.available_variations) > 0:
item.min_price = min([v.display_price for v in item.available_variations])
item.max_price = max([v.display_price for v in item.available_variations])
item.min_price = min([v.display_price.net if event.settings.display_net_prices else
v.display_price.gross for v in item.available_variations])
item.max_price = max([v.display_price.net if event.settings.display_net_prices else
v.display_price.gross for v in item.available_variations])
item._remove = not bool(item.available_variations)
items = [item for item in items

View File

@@ -97,8 +97,8 @@ class OrderDetails(EventViewMixin, OrderDetailMixin, CartMixin, TemplateView):
ctx['download_buttons'] = self.download_buttons
ctx['cart'] = self.get_cart(
answers=True, downloads=ctx['can_download'],
queryset=self.order.positions.all(),
payment_fee=self.order.payment_fee, payment_fee_tax_rate=self.order.payment_fee_tax_rate
queryset=self.order.positions.select_related('tax_rule'),
order=self.order
)
ctx['can_download_multi'] = any([b['multi'] for b in self.download_buttons]) and (
self.request.event.settings.ticket_download_nonadm or
@@ -412,7 +412,7 @@ class OrderModify(EventViewMixin, OrderDetailMixin, QuestionsViewMixin, Template
def invoice_form(self):
return InvoiceAddressForm(data=self.request.POST if self.request.method == "POST" else None,
event=self.request.event,
instance=self.invoice_address)
instance=self.invoice_address, validate_vat_id=False)
def post(self, request, *args, **kwargs):
failed = not self.save() or not self.invoice_form.is_valid()