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

@@ -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)