Allow customers to change to a different product variation (#1719)

This commit is contained in:
Raphael Michel
2020-07-20 16:36:24 +02:00
committed by GitHub
parent c8ef825de5
commit e7b9c49620
17 changed files with 952 additions and 132 deletions

View File

@@ -623,6 +623,9 @@ class EventSettingsSerializer(serializers.Serializer):
'cancel_allow_user_paid_adjust_fees_explanation',
'cancel_allow_user_paid_refund_as_giftcard',
'cancel_allow_user_paid_require_approval',
'change_allow_user_variation',
'change_allow_user_until',
'change_allow_user_price',
]
def __init__(self, *args, **kwargs):

View File

@@ -315,6 +315,51 @@ def base_placeholders(sender, **kwargs):
}
),
),
SimpleFunctionalMailTextPlaceholder(
'url_info_change', ['order', 'event'], lambda order, event: build_absolute_uri(
event,
'presale:event.order.modify', kwargs={
'order': order.code,
'secret': order.secret,
}
), lambda event: build_absolute_uri(
event,
'presale:event.order.modify', kwargs={
'order': 'F8VVL',
'secret': '6zzjnumtsx136ddy',
}
),
),
SimpleFunctionalMailTextPlaceholder(
'url_products_change', ['order', 'event'], lambda order, event: build_absolute_uri(
event,
'presale:event.order.change', kwargs={
'order': order.code,
'secret': order.secret,
}
), lambda event: build_absolute_uri(
event,
'presale:event.order.change', kwargs={
'order': 'F8VVL',
'secret': '6zzjnumtsx136ddy',
}
),
),
SimpleFunctionalMailTextPlaceholder(
'url_cancel', ['order', 'event'], lambda order, event: build_absolute_uri(
event,
'presale:event.order.cancel', kwargs={
'order': order.code,
'secret': order.secret,
}
), lambda event: build_absolute_uri(
event,
'presale:event.order.cancel', kwargs={
'order': 'F8VVL',
'secret': '6zzjnumtsx136ddy',
}
),
),
SimpleFunctionalMailTextPlaceholder(
'url', ['event', 'position'], lambda event, position: build_absolute_uri(
event,

View File

@@ -378,9 +378,9 @@ class Item(LoggedModel):
'but only for fixed bundles!')
)
allow_cancel = models.BooleanField(
verbose_name=_('Allow product to be canceled'),
verbose_name=_('Allow product to be canceled or changed'),
default=True,
help_text=_('If this is checked, the usual cancellation settings of this event apply. If this is unchecked, '
help_text=_('If this is checked, the usual cancellation and order change settings of this event apply. If this is unchecked, '
'orders containing this product can not be canceled by users but only by you.')
)
min_per_order = models.IntegerField(

View File

@@ -434,6 +434,19 @@ class Order(LockModel, LoggedModel):
self.status in (Order.STATUS_PENDING, Order.STATUS_PAID, Order.STATUS_EXPIRED) and self.count_positions
)
@cached_property
def user_change_deadline(self):
until = self.event.settings.get('change_allow_user_until', as_type=RelativeDateWrapper)
if until:
if self.event.has_subevents:
terms = [
until.datetime(se)
for se in self.event.subevents.filter(id__in=self.positions.values_list('subevent', flat=True))
]
return min(terms) if terms else None
else:
return until.datetime(self.event)
@cached_property
def user_cancel_deadline(self):
if self.status == Order.STATUS_PAID and self.total != Decimal('0.00'):
@@ -466,6 +479,36 @@ class Order(LockModel, LoggedModel):
fee += self.event.settings.cancel_allow_user_paid_keep
return round_decimal(fee, self.event.currency)
@property
@scopes_disabled()
def user_change_allowed(self) -> bool:
"""
Returns whether or not this order can be canceled by the user.
"""
from .checkin import Checkin
if self.status not in (Order.STATUS_PENDING, Order.STATUS_PAID) or not self.count_positions:
return False
if self.cancellation_requests.exists():
return False
positions = list(
self.positions.all().annotate(
has_variations=Exists(ItemVariation.objects.filter(item_id=OuterRef('item_id'))),
has_checkin=Exists(Checkin.objects.filter(position_id=OuterRef('pk')))
).select_related('item').prefetch_related('issued_gift_cards')
)
cancelable = all([op.item.allow_cancel and not op.has_checkin for op in positions])
if not cancelable or not positions:
return False
for op in positions:
if op.issued_gift_cards.all():
return False
if self.user_change_deadline and now() > self.user_change_deadline:
return False
return self.event.settings.change_allow_user_variation and any([op.has_variations for op in positions])
@property
@scopes_disabled()
def user_cancel_allowed(self) -> bool:
@@ -474,7 +517,7 @@ class Order(LockModel, LoggedModel):
"""
from .checkin import Checkin
if self.cancellation_requests.exists():
if self.cancellation_requests.exists() or not self.cancel_allowed():
return False
positions = list(
self.positions.all().annotate(

View File

@@ -1131,7 +1131,7 @@ def notify_user_changed_order(order, user=None, auth=None, invoices=[]):
try:
order.send_mail(
email_subject, email_template, email_context,
'pretix.event.order.email.order_changed', user, auth=auth, invoices=invoices,
'pretix.event.order.email.order_changed', user, auth=auth, invoices=invoices, attach_tickets=True,
)
except SendMailException:
logger.exception('Order changed email could not be sent')
@@ -1869,9 +1869,10 @@ class OrderChangeManager:
def _reissue_invoice(self):
i = self.order.invoices.filter(is_cancellation=False).last()
if self.reissue_invoice and i and self._invoice_dirty:
self._invoices.append(generate_cancellation(i))
if invoice_qualified(self.order):
if self.reissue_invoice and self._invoice_dirty:
if i:
self._invoices.append(generate_cancellation(i))
if (i or self.event.settings.invoice_generate == 'True') and invoice_qualified(self.order):
self._invoices.append(generate_invoice(self.order))
def _check_complete_cancel(self):

View File

@@ -89,7 +89,7 @@ class QuotaAvailability:
def compute(self, now_dt=None):
now_dt = now_dt or now()
quotas = list(self._queue)
quotas = list(set(self._queue))
quotas_original = list(self._queue)
self._queue.clear()
if not quotas:

View File

@@ -925,6 +925,46 @@ DEFAULTS = {
"multiple event dates, the earliest date will be used."),
)
},
'change_allow_user_variation': {
'default': 'False',
'type': bool,
'form_class': forms.BooleanField,
'serializer_class': serializers.BooleanField,
'form_kwargs': dict(
label=_("Customers can change the variation of the products they purchased"),
)
},
'change_allow_user_price': {
'default': 'gt',
'type': str,
'form_class': forms.ChoiceField,
'serializer_class': serializers.ChoiceField,
'serializer_kwargs': dict(
choices=(
('gt', _('Only allow changes if the resulting price is higher or equal than the previous price.')),
('eq', _('Only allow changes if the resulting price is equal to the previous price.')),
('any', _('Allow changes regardless of price, even if this results in a refund.')),
)
),
'form_kwargs': dict(
label=_("Requirement for changed prices"),
choices=(
('gt', _('Only allow changes if the resulting price is higher or equal than the previous price.')),
('eq', _('Only allow changes if the resulting price is equal to the previous price.')),
('any', _('Allow changes regardless of price, even if this results in a refund.')),
),
widget=forms.RadioSelect,
),
},
'change_allow_user_until': {
'default': None,
'type': RelativeDateWrapper,
'form_class': RelativeDateTimeField,
'serializer_class': SerializerRelativeDateTimeField,
'form_kwargs': dict(
label=_("Do not allow changes after"),
)
},
'cancel_allow_user': {
'default': 'True',
'type': bool,

View File

@@ -37,7 +37,7 @@ def money_filter(value: Decimal, arg='', hide_currency=False):
arg,
floatformat(value, 2)
)
return format_currency(value, arg, locale=translation.get_language())
return format_currency(value, arg, locale=translation.get_language()[:2])
except:
return '{} {}'.format(
arg,

View File

@@ -575,6 +575,9 @@ class CancelSettingsForm(SettingsForm):
'cancel_allow_user_paid_adjust_fees_explanation',
'cancel_allow_user_paid_refund_as_giftcard',
'cancel_allow_user_paid_require_approval',
'change_allow_user_variation',
'change_allow_user_price',
'change_allow_user_until',
]
def __init__(self, *args, **kwargs):

View File

@@ -38,6 +38,17 @@
</div>
{% endif %}
</fieldset>
<fieldset>
<legend>{% trans "Order changes" %}</legend>
<div class="alert alert-info">
{% blocktrans trimmed %}
Allowing users to change their order is a feature under development. Therefore, currently only specific changes (such as changing the variation of a product) are possible. More options might be added later.
{% endblocktrans %}
</div>
{% bootstrap_field form.change_allow_user_variation layout="control" %}
{% bootstrap_field form.change_allow_user_price layout="control" %}
{% bootstrap_field form.change_allow_user_until layout="control" %}
</fieldset>
</div>
<div class="form-group submit-group">
<button type="submit" class="btn btn-primary btn-save">

View File

@@ -0,0 +1,96 @@
from decimal import Decimal
from django import forms
from django.utils.translation import ugettext_lazy as _
from pretix.base.models import Quota
from pretix.base.models.tax import TaxedPrice
from pretix.base.services.pricing import get_price
from pretix.base.services.quotas import QuotaAvailability
from pretix.base.templatetags.money import money_filter
class OrderPositionChangeForm(forms.Form):
itemvar = forms.ChoiceField(
label=_('Product'),
required=False,
)
def __init__(self, *args, **kwargs):
instance = kwargs.pop('instance')
invoice_address = kwargs.pop('invoice_address')
initial = kwargs.get('initial', {})
event = kwargs.pop('event')
kwargs['initial'] = initial
if instance.variation_id:
initial['itemvar'] = f'{instance.item_id}-{instance.variation_id}'
else:
initial['itemvar'] = f'{instance.item_id}'
super().__init__(*args, **kwargs)
choices = []
i = instance.item
pname = str(i)
variations = list(i.variations.all())
if variations:
current_quotas = instance.variation.quotas.all() if instance.variation else instance.item.quotas.all()
qa = QuotaAvailability()
for v in variations:
qa.queue(*v.quotas.all())
qa.compute()
for v in variations:
label = f'{i.name} {v.value}'
if instance.variation_id == v.id:
choices.append((f'{i.pk}-{v.pk}', label))
continue
if not v.active:
continue
q_res = [qa.results[q][0] != Quota.AVAILABILITY_OK for q in v.quotas.all() if q not in current_quotas]
if not v.quotas.all() or (q_res and any(q_res)):
continue
new_price = get_price(i, v, voucher=instance.voucher, subevent=instance.subevent,
invoice_address=invoice_address)
current_price = TaxedPrice(tax=instance.tax_value, gross=instance.price, net=instance.price - instance.tax_value,
name=instance.tax_rule.name if instance.tax_rule else '', rate=instance.tax_rate)
if new_price.gross < current_price.gross and event.settings.change_allow_user_price == 'gt':
continue
if new_price.gross != current_price.gross and event.settings.change_allow_user_price == 'eq':
continue
if new_price.gross < current_price.gross:
if event.settings.display_net_prices:
label += ' (- {} {})'.format(money_filter(current_price.gross - new_price.gross, event.currency), _('plus taxes'))
else:
label += ' (- {})'.format(money_filter(current_price.gross - new_price.gross, event.currency))
elif current_price.gross < new_price.gross:
if event.settings.display_net_prices:
label += ' ({}{} {})'.format(
'+ ' if current_price.gross != Decimal('0.00') else '',
money_filter(new_price.gross - current_price.gross, event.currency),
_('plus taxes')
)
else:
label += ' ({}{})'.format(
'+ ' if current_price.gross != Decimal('0.00') else '',
money_filter(new_price.gross - current_price.gross, event.currency)
)
choices.append((f'{i.pk}-{v.pk}', label))
if not choices:
self.fields['itemvar'].widget.attrs['disabled'] = True
self.fields['itemvar'].help_text = _('No other variation of this product is currently available for you.')
else:
choices.append((str(i.pk), '%s' % pname))
self.fields['itemvar'].widget.attrs['disabled'] = True
self.fields['itemvar'].help_text = _('No other variations of this product exist.')
self.fields['itemvar'].choices = choices

View File

@@ -312,92 +312,110 @@
{% endif %}
<div class="clearfix"></div>
</div>
{% if order.cancel_allowed and order.user_cancel_allowed %}
{% if order.user_change_allowed or order.user_cancel_allowed %}
<div class="panel panel-primary panel-cancellation">
<div class="panel-heading">
<h3 class="panel-title">
{% trans "Cancellation" context "action" %}
{% trans "Change or cancel your order" context "action" %}
</h3>
</div>
<div class="panel-body">
{% if order.status == "p" and order.total != 0 %}
{% if order.user_cancel_fee >= order.total %}
<ul class="list-group">
{% if order.user_change_allowed %}
<li class="list-group-item">
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed %}
You can request to cancel this order, but you will not receive a refund.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
You can cancel this order, but you will not receive a refund.
{% endblocktrans %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
{% blocktrans trimmed %}
If you want to make changes to the products you bought, you can click on the button to change your order.
{% endblocktrans %}
</p>
{% elif order.user_cancel_fee %}
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed with fee=order.user_cancel_fee|money:request.event.currency %}
You can request to cancel this order. If your request is approved, a cancellation
fee of <strong>{{ fee }}</strong> will be kept and you will receive a refund of
the remainder.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with fee=order.user_cancel_fee|money:request.event.currency %}
You can cancel this order. In this case, a cancellation fee of <strong>{{ fee }}</strong>
will be kept and you will receive a refund of the remainder.
{% endblocktrans %}
{% endif %}
{% if request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "force" %}
{% trans "The refund will be issued in form of a gift card that you can use for further purchases." %}
{% elif request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "option" %}
{% trans "The refund can be issued to your original payment method or as a gift card." %}
{% else %}
{% trans "The refund will be issued to your original payment method." %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
</p>
{% else %}
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed %}
You can request to cancel this order. If your request is approved, you get a full
refund.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
You can cancel this order and receive a full refund.
{% endblocktrans %}
{% endif %}
{% if request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "force" %}
{% trans "The refund will be issued in form of a gift card that you can use for further purchases." %}
{% elif request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "option" %}
{% trans "The refund can be issued to your original payment method or as a gift card." %}
{% else %}
{% trans "The refund will be issued to your original payment method." %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
</p>
{% endif %}
<a href="{% eventurl event 'presale:event.order.cancel' secret=order.secret order=order.code %}"
class="btn btn-danger">
<span class="fa fa-remove"></span>
{% trans "Cancel order" %}
</a>
{% else %}
<p>
{% blocktrans trimmed %}
You can cancel this order using the following button.
{% endblocktrans %}
{% trans "This will invalidate all tickets in this order." %}
</p>
<a href="{% eventurl event 'presale:event.order.cancel' secret=order.secret order=order.code %}"
class="btn btn-danger">
<span class="fa fa-remove"></span>
{% trans "Cancel order" %}
</a>
<a href="{% eventurl event 'presale:event.order.change' secret=order.secret order=order.code %}"
class="btn btn-default">
<span class="fa fa-edit"></span>
{% trans "Change order" %}
</a>
</li>
{% endif %}
</div>
{% if order.user_cancel_allowed %}
<li class="list-group-item">
{% if order.status == "p" and order.total != 0 %}
{% if order.user_cancel_fee >= order.total %}
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed %}
You can request to cancel this order, but you will not receive a refund.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
You can cancel this order, but you will not receive a refund.
{% endblocktrans %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
</p>
{% elif order.user_cancel_fee %}
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed with fee=order.user_cancel_fee|money:request.event.currency %}
You can request to cancel this order. If your request is approved, a cancellation
fee of <strong>{{ fee }}</strong> will be kept and you will receive a refund of
the remainder.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with fee=order.user_cancel_fee|money:request.event.currency %}
You can cancel this order. In this case, a cancellation fee of <strong>{{ fee }}</strong>
will be kept and you will receive a refund of the remainder.
{% endblocktrans %}
{% endif %}
{% if request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "force" %}
{% trans "The refund will be issued in form of a gift card that you can use for further purchases." %}
{% elif request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "option" %}
{% trans "The refund can be issued to your original payment method or as a gift card." %}
{% else %}
{% trans "The refund will be issued to your original payment method." %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
</p>
{% else %}
<p>
{% if request.event.settings.cancel_allow_user_paid_require_approval %}
{% blocktrans trimmed %}
You can request to cancel this order. If your request is approved, you get a full
refund.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
You can cancel this order and receive a full refund.
{% endblocktrans %}
{% endif %}
{% if request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "force" %}
{% trans "The refund will be issued in form of a gift card that you can use for further purchases." %}
{% elif request.event.settings.cancel_allow_user_paid_refund_as_giftcard == "option" %}
{% trans "The refund can be issued to your original payment method or as a gift card." %}
{% else %}
{% trans "The refund will be issued to your original payment method." %}
{% endif %}
{% trans "This will invalidate all tickets in this order." %}
</p>
{% endif %}
<a href="{% eventurl event 'presale:event.order.cancel' secret=order.secret order=order.code %}"
class="btn btn-danger">
<span class="fa fa-remove"></span>
{% trans "Cancel order" %}
</a>
{% else %}
<p>
{% blocktrans trimmed %}
You can cancel this order using the following button.
{% endblocktrans %}
{% trans "This will invalidate all tickets in this order." %}
</p>
<a href="{% eventurl event 'presale:event.order.cancel' secret=order.secret order=order.code %}"
class="btn btn-danger">
<span class="fa fa-remove"></span>
{% trans "Cancel order" %}
</a>
{% endif %}
</li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}

View File

@@ -0,0 +1,80 @@
{% extends "pretixpresale/event/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load rich_text %}
{% block title %}{% trans "Modify order" %}{% endblock %}
{% block content %}
<h2>
{% blocktrans trimmed with code=order.code %}
Change order: {{ code }}
{% endblocktrans %}
</h2>
<form method="post" href="">
{% csrf_token %}
{% for position, positions in formgroups.items %}
<div class="panel panel-default items">
<div class="panel-heading">
<h3 class="panel-title">
<strong>{{ position.item }}</strong>
{% if position.variation %}
{{ position.variation }}
{% endif %}
</h3>
</div>
<div class="panel-body">
<div class="form-order-change form-horizontal">
{% if position.subevent %}
<div class="form-group">
<label class="col-md-3 control-label">
{% trans "Date" context "subevent" %}
</label>
<div class="col-md-9 form-control-text">
<ul class="addon-list">
{{ pos.subevent.name }} &middot; {{ pos.subevent.get_date_range_display }}
{% if pos.event.settings.show_times %}
<span class="fa fa-clock-o"></span>
{{ pos.subevent.date_from|date:"TIME_FORMAT" }}
{% endif %}
</ul>
</div>
</div>
{% endif %}
{% for p in positions %}
{% if p.pk != position.pk %}
{# Add-Ons #}
<legend>+ {{ p.item.name }}{% if p.variation %} {{ p.variation.value }}{% endif %}</legend>
{% endif %}
{% if p.attendee_name %}
<div class="form-group">
<label class="col-md-3 control-label">
{% trans "Attendee name" %}
</label>
<div class="col-md-9 form-control-text">
{{ p.attendee_name }}
</div>
</div>
{% endif %}
{% bootstrap_form p.form layout="checkout" %}
{% endfor %}
</div>
</div>
</div>
{% endfor %}
<div class="row checkout-button-row">
<div class="col-md-4">
<a class="btn btn-block btn-default btn-lg"
href="{{ view.get_order_url }}">
{% trans "Cancel" %}
</a>
</div>
<div class="col-md-4 col-md-offset-4">
<button class="btn btn-block btn-primary btn-lg" type="submit">
{% trans "Save changes" %}
</button>
</div>
<div class="clearfix"></div>
</div>
</form>
{% endblock %}

View File

@@ -64,6 +64,9 @@ event_patterns = [
url(r'^order/(?P<order>[^/]+)/(?P<secret>[A-Za-z0-9]+)/invoice$',
pretix.presale.views.order.OrderInvoiceCreate.as_view(),
name='event.order.geninvoice'),
url(r'^order/(?P<order>[^/]+)/(?P<secret>[A-Za-z0-9]+)/change$',
pretix.presale.views.order.OrderChange.as_view(),
name='event.order.change'),
url(r'^order/(?P<order>[^/]+)/(?P<secret>[A-Za-z0-9]+)/cancel$',
pretix.presale.views.order.OrderCancel.as_view(),
name='event.order.cancel'),

View File

@@ -2,6 +2,7 @@ import inspect
import mimetypes
import os
import re
from collections import OrderedDict
from decimal import Decimal
from django import forms
@@ -25,7 +26,8 @@ from pretix.base.models import (
CachedTicket, GiftCard, Invoice, Order, OrderPosition, Quota,
)
from pretix.base.models.orders import (
CachedCombinedTicket, OrderFee, OrderPayment, OrderRefund, QuestionAnswer,
CachedCombinedTicket, InvoiceAddress, OrderFee, OrderPayment, OrderRefund,
QuestionAnswer,
)
from pretix.base.payment import PaymentException
from pretix.base.services.invoices import (
@@ -34,17 +36,20 @@ from pretix.base.services.invoices import (
)
from pretix.base.services.mail import SendMailException
from pretix.base.services.orders import (
OrderError, cancel_order, change_payment_provider,
OrderChangeManager, OrderError, cancel_order, change_payment_provider,
)
from pretix.base.services.pricing import get_price
from pretix.base.services.tickets import generate, invalidate_cache
from pretix.base.signals import (
allow_ticket_download, order_modified, register_ticket_outputs,
)
from pretix.base.templatetags.money import money_filter
from pretix.base.views.mixins import OrderQuestionsViewMixin
from pretix.base.views.tasks import AsyncAction
from pretix.helpers.safedownload import check_token
from pretix.multidomain.urlreverse import build_absolute_uri, eventreverse
from pretix.presale.forms.checkout import InvoiceAddressForm, QuestionsForm
from pretix.presale.forms.order import OrderPositionChangeForm
from pretix.presale.views import (
CartMixin, EventViewMixin, iframe_entry_view_wrapper,
)
@@ -1006,3 +1011,131 @@ class InvoiceDownload(EventViewMixin, OrderDetailMixin, View):
resp['Content-Disposition'] = 'inline; filename="{}.pdf"'.format(invoice.number)
resp._csp_ignore = True # Some browser's PDF readers do not work with CSP
return resp
@method_decorator(xframe_options_exempt, 'dispatch')
class OrderChange(EventViewMixin, OrderDetailMixin, TemplateView):
template_name = "pretixpresale/event/order_change.html"
def dispatch(self, request, *args, **kwargs):
self.request = request
self.kwargs = kwargs
if not self.order:
raise Http404(_('Unknown order code or not authorized to access this order.'))
if not self.order.user_change_allowed:
messages.error(request, _('You cannot change this order.'))
return redirect(self.get_order_url())
return super().dispatch(request, *args, **kwargs)
@cached_property
def formdict(self):
storage = OrderedDict()
for pos in self.positions:
if pos.addon_to_id:
if pos.addon_to not in storage:
storage[pos.addon_to] = []
storage[pos.addon_to].append(pos)
else:
if pos not in storage:
storage[pos] = []
storage[pos].append(pos)
return storage
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['order'] = self.order
ctx['positions'] = self.positions
ctx['formgroups'] = self.formdict
return ctx
@cached_property
def positions(self):
positions = list(
self.order.positions.select_related('item', 'item__tax_rule').prefetch_related(
'item__quotas', 'item__variations', 'item__variations__quotas'
)
)
try:
ia = self.order.invoice_address
except InvoiceAddress.DoesNotExist:
ia = None
for p in positions:
p.form = OrderPositionChangeForm(prefix='op-{}'.format(p.pk), instance=p,
invoice_address=ia, event=self.request.event,
data=self.request.POST if self.request.method == "POST" else None)
return positions
def _process_change(self, ocm):
try:
ia = self.order.invoice_address
except InvoiceAddress.DoesNotExist:
ia = None
for p in self.positions:
if not p.form.is_valid():
return False
try:
change_item = None
if p.form.cleaned_data['itemvar']:
if '-' in p.form.cleaned_data['itemvar']:
itemid, varid = p.form.cleaned_data['itemvar'].split('-')
else:
itemid, varid = p.form.cleaned_data['itemvar'], None
item = self.request.event.items.get(pk=itemid)
if varid:
variation = item.variations.get(pk=varid)
else:
variation = None
if item != p.item or variation != p.variation:
change_item = (item, variation)
if change_item is not None:
ocm.change_item(p, *change_item)
new_price = get_price(change_item[0], change_item[1], voucher=p.voucher, subevent=p.subevent,
invoice_address=ia)
if new_price.gross != p.price or new_price.rate != p.tax_rate:
ocm.change_price(p, new_price.gross)
if change_item[0].tax_rule != p.tax_rule or new_price.rate != p.tax_rate:
ocm.change_tax_rule(p, change_item[0].tax_rule)
except OrderError as e:
p.custom_error = str(e)
return False
return True
def post(self, *args, **kwargs):
was_paid = self.order.status == Order.STATUS_PAID
ocm = OrderChangeManager(
self.order,
user=self.request.user,
notify=True,
reissue_invoice=True,
)
form_valid = self._process_change(ocm)
if not form_valid:
messages.error(self.request, _('An error occurred. Please see the details below.'))
else:
try:
ocm.commit(check_quotas=True)
except OrderError as e:
messages.error(self.request, str(e))
else:
if self.order.status != Order.STATUS_PAID and was_paid:
messages.success(self.request, _('The order has been changed. You can now proceed by paying the open amount of {amount}.').format(
amount=money_filter(self.order.pending_sum, self.request.event.currency)
))
return redirect(eventreverse(self.request.event, 'presale:event.order.pay.change', kwargs={
'order': self.order.code,
'secret': self.order.secret
}))
else:
messages.success(self.request, _('The order has been changed.'))
return redirect(self.get_order_url())
return self.get(*args, **kwargs)