Compare commits

...

1 Commits

Author SHA1 Message Date
Raphael Michel
8a3a6a471e draft 2021-02-05 15:31:46 +01:00
4 changed files with 25 additions and 8 deletions

View File

@@ -327,7 +327,7 @@ def deny_order(order, comment='', user=None, send_mail: bool=True, auth=None):
def _cancel_order(order, user=None, send_mail: bool=True, api_token=None, device=None, oauth_application=None, def _cancel_order(order, user=None, send_mail: bool=True, api_token=None, device=None, oauth_application=None,
cancellation_fee=None, keep_fees=None): cancellation_fee=None, keep_fees=None, cancel_invoice=True):
""" """
Mark this order as canceled Mark this order as canceled
:param order: The order to change :param order: The order to change
@@ -351,9 +351,10 @@ def _cancel_order(order, user=None, send_mail: bool=True, api_token=None, device
if not order.cancel_allowed(): if not order.cancel_allowed():
raise OrderError(_('You cannot cancel this order.')) raise OrderError(_('You cannot cancel this order.'))
invoices = [] invoices = []
i = order.invoices.filter(is_cancellation=False).last() if cancel_invoice:
if i and not i.refered.exists(): i = order.invoices.filter(is_cancellation=False).last()
invoices.append(generate_cancellation(i)) if i and not i.refered.exists():
invoices.append(generate_cancellation(i))
for position in order.positions.all(): for position in order.positions.all():
for gc in position.issued_gift_cards.all(): for gc in position.issued_gift_cards.all():
@@ -403,7 +404,7 @@ def _cancel_order(order, user=None, send_mail: bool=True, api_token=None, device
order.cancellation_date = now() order.cancellation_date = now()
order.save(update_fields=['status', 'cancellation_date', 'total']) order.save(update_fields=['status', 'cancellation_date', 'total'])
if i: if cancel_invoice and i:
invoices.append(generate_invoice(order)) invoices.append(generate_invoice(order))
else: else:
with order.event.lock(): with order.event.lock():
@@ -2152,11 +2153,12 @@ def _try_auto_refund(order, manual_refund=False, allow_partial=False, source=Ord
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(OrderError,)) @app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(OrderError,))
@scopes_disabled() @scopes_disabled()
def cancel_order(self, order: int, user: int=None, send_mail: bool=True, api_token=None, oauth_application=None, def cancel_order(self, order: int, user: int=None, send_mail: bool=True, api_token=None, oauth_application=None,
device=None, cancellation_fee=None, try_auto_refund=False, refund_as_giftcard=False, comment=None): device=None, cancellation_fee=None, try_auto_refund=False, refund_as_giftcard=False, comment=None,
cancel_invoice=True):
try: try:
try: try:
ret = _cancel_order(order, user, send_mail, api_token, device, oauth_application, ret = _cancel_order(order, user, send_mail, api_token, device, oauth_application,
cancellation_fee) cancellation_fee, cancel_invoice=cancel_invoice)
if try_auto_refund: if try_auto_refund:
_try_auto_refund(order, refund_as_giftcard=refund_as_giftcard, _try_auto_refund(order, refund_as_giftcard=refund_as_giftcard,
comment=comment) comment=comment)

View File

@@ -117,6 +117,11 @@ class CancelForm(ConfirmPaymentForm):
'in your cancellation fee if you want to keep them. Please always enter a gross value, ' 'in your cancellation fee if you want to keep them. Please always enter a gross value, '
'tax will be calculated automatically.'), 'tax will be calculated automatically.'),
) )
cancel_invoice = forms.BooleanField(
label=_('Generate cancellation for invoice'),
initial=True,
required=False
)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@@ -130,6 +135,8 @@ class CancelForm(ConfirmPaymentForm):
self.fields['cancellation_fee'].max_value = prs self.fields['cancellation_fee'].max_value = prs
else: else:
del self.fields['cancellation_fee'] del self.fields['cancellation_fee']
if not self.instance.invoices.exists():
del self.fields['cancel_invoice']
def clean_cancellation_fee(self): def clean_cancellation_fee(self):
val = self.cleaned_data['cancellation_fee'] or Decimal('0.00') val = self.cleaned_data['cancellation_fee'] or Decimal('0.00')

View File

@@ -23,13 +23,16 @@
<input type="hidden" name="status" value="c"/> <input type="hidden" name="status" value="c"/>
{% bootstrap_form_errors form %} {% bootstrap_form_errors form %}
{% bootstrap_field form.send_email layout='' %} {% bootstrap_field form.send_email layout='' %}
{% if form.cancel_invoice %}
{% bootstrap_field form.cancel_invoice layout='' %}
{% endif %}
{% if form.cancellation_fee %} {% if form.cancellation_fee %}
{% bootstrap_field form.cancellation_fee layout='' %} {% bootstrap_field form.cancellation_fee layout='' %}
{% endif %} {% endif %}
<div class="row checkout-button-row"> <div class="row checkout-button-row">
<div class="col-md-4"> <div class="col-md-4">
<a class="btn btn-block btn-default btn-lg" <a class="btn btn-block btn-default btn-lg"
href="{% url "control:event.order" event=request.event.slug organizer=request.event.organizer.slug code=order.code %}"> href="{% url "control:event.order" event=request.event.slug organizer=request.event.organizer.slug code=order.code %}">
{% trans "No, take me back" %} {% trans "No, take me back" %}
</a> </a>
</div> </div>

View File

@@ -151,6 +151,9 @@ class OrderList(OrderSearchMixin, EventPermissionRequiredMixin, PaginationMixin,
s = OrderPosition.objects.filter( s = OrderPosition.objects.filter(
order=OuterRef('pk') order=OuterRef('pk')
).order_by().values('order').annotate(k=Count('id')).values('k') ).order_by().values('order').annotate(k=Count('id')).values('k')
i = Invoice.objects.filter(
order=OuterRef('pk')
).order_by().values('order').annotate(k=Count('id')).values('k')
annotated = { annotated = {
o['pk']: o o['pk']: o
for o in for o in
@@ -158,6 +161,7 @@ class OrderList(OrderSearchMixin, EventPermissionRequiredMixin, PaginationMixin,
pk__in=[o.pk for o in ctx['orders']] pk__in=[o.pk for o in ctx['orders']]
).annotate( ).annotate(
pcnt=Subquery(s, output_field=IntegerField()), pcnt=Subquery(s, output_field=IntegerField()),
icnt=Subquery(i, output_field=IntegerField()),
has_cancellation_request=Exists(CancellationRequest.objects.filter(order=OuterRef('pk'))) has_cancellation_request=Exists(CancellationRequest.objects.filter(order=OuterRef('pk')))
).values( ).values(
'pk', 'pcnt', 'is_overpaid', 'is_underpaid', 'is_pending_with_full_payment', 'has_external_refund', 'pk', 'pcnt', 'is_overpaid', 'is_underpaid', 'is_pending_with_full_payment', 'has_external_refund',
@@ -1133,6 +1137,7 @@ class OrderTransition(OrderView):
try: try:
cancel_order(self.order.pk, user=self.request.user, cancel_order(self.order.pk, user=self.request.user,
send_mail=self.mark_canceled_form.cleaned_data['send_email'], send_mail=self.mark_canceled_form.cleaned_data['send_email'],
cancel_invoice=self.mark_canceled_form.cleaned_data.get('cancel_invoice', True),
cancellation_fee=self.mark_canceled_form.cleaned_data.get('cancellation_fee')) cancellation_fee=self.mark_canceled_form.cleaned_data.get('cancellation_fee'))
except OrderError as e: except OrderError as e:
messages.error(self.request, str(e)) messages.error(self.request, str(e))