#961 Delete payments when Payment.NONE in pizzas

parent cb15e55c
......@@ -60,11 +60,10 @@ class AdminOrderSerializer(serializers.ModelSerializer):
return super().validate(attrs)
def update(self, instance: Model, validated_data: Any) -> Any:
if validated_data.get(
'payment', {}
).get('type', instance.payment.type) != instance.payment.type:
instance.payment.type = validated_data['payment']['type']
instance.payment.save()
instance.update_payment(
validated_data['payment']['type'],
self.context['request'].user
)
if 'payment' in validated_data:
del validated_data['payment']
return super().update(instance, validated_data)
# Generated by Django 2.2.6 on 2019-11-16 22:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('pizzas', '0010_payment_registration'),
]
operations = [
migrations.AlterField(
model_name='order',
name='payment',
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='pizzas_order', to='payments.Payment', verbose_name='payment'),
),
]
......@@ -182,6 +182,8 @@ class Order(models.Model):
to='payments.Payment',
related_name='pizzas_order',
on_delete=models.CASCADE,
blank=True,
null=True
)
product = models.ForeignKey(
......@@ -204,26 +206,49 @@ class Order(models.Model):
'name': _('Either specify a member or a name'),
})
def save(self, *args, **kwargs):
if not self.id and self.pizza_event.end_reminder:
self.pizza_event.end_reminder.users.remove(self.member)
def update_payment(self, payment_type=None, processed_by=None):
notes = (f'Pizza order by {self.member_name} '
f'for {self.pizza_event.event.title_en}')
try:
if payment_type == Payment.TPAY and not self.member.tpay_enabled:
payment_type = None
if self.payment and payment_type == Payment.NONE:
payment = self.payment
self.payment = None
self.save()
payment.delete()
elif self.payment:
if payment_type is not None:
self.payment.type = payment_type
self.payment.notes = notes
self.payment.paid_by = self.member
self.payment.amount = self.product.price
self.payment.processed_by = processed_by
self.payment.save()
except ObjectDoesNotExist:
else:
self.payment = Payment.objects.create(
type=payment_type,
amount=self.product.price,
notes=notes,
paid_by=self.member
paid_by=self.member,
processed_by=processed_by
)
self.save()
def save(self, *args, **kwargs):
if not self.id and self.pizza_event.end_reminder:
self.pizza_event.end_reminder.users.remove(self.member)
super().save(*args, **kwargs)
if self.payment:
notes = (f'Pizza order by {self.member_name} '
f'for {self.pizza_event.event.title_en}')
self.payment.notes = notes
self.payment.paid_by = self.member
self.payment.amount = self.product.price
self.payment.save()
def delete(self, using=None, keep_parents=False):
if not self.id and self.pizza_event.end_reminder:
self.pizza_event.end_reminder.users.add(self.member)
......@@ -253,7 +278,7 @@ class Order(models.Model):
@property
def can_be_changed(self):
try:
return (self.payment and not (self.payment.processed and not
return (not (self.payment and not
self.payment.type == Payment.TPAY) and not
self.pizza_event.has_ended)
except ObjectDoesNotExist:
......
from events.services import is_organiser
from . models import Product, Order, PizzaEvent
from .models import Product, Order, PizzaEvent
def gen_stats_pizza_orders():
......
......@@ -106,16 +106,16 @@
<td>
<select name="payment"
data-none="{{ payment.NONE }}"
{% if order.payment.type != payment.NONE %}class="paid"{% endif %}>
{% if order.payment is not None %}class="paid"{% endif %}>
<option value="{{ payment.NONE }}"
{% if order.payment.type == payment.NONE %}selected{% endif %}>{% trans 'None' %}</option>
{% if order.payment is None %}selected{% endif %}>{% trans 'None' %}</option>
<option value="{{ payment.CARD }}"
{% if order.payment.type == payment.CARD %}selected{% endif %}>{% trans 'Card' %}</option>
<option value="{{ payment.CASH }}"
{% if order.payment.type == payment.CASH %}selected{% endif %}>{% trans 'Cash' %}</option>
<option value="{{ payment.TPAY }}"
{% if order.payment.type == payment.TPAY %}selected{% endif %}
{% if not order.payment.paid_by.tpay_enabled %} disabled {% endif %}>{% trans 'Thalia Pay' %}</option>
{% if not order.member.tpay_enabled %} disabled {% endif %}>{% trans 'Thalia Pay' %}</option>
</select>
</td>
<td>
......
......@@ -33,8 +33,6 @@ def cancel_order(request):
messages.error(request,
_('You can no longer cancel.'))
elif order.member == request.member:
if order.payment.type == Payment.TPAY:
order.payment.type = Payment.NONE
order.delete()
messages.success(request, _("Your order has been cancelled."))
except Http404:
......@@ -48,7 +46,7 @@ def pay_order(request):
try:
order = get_object_or_404(Order, pk=int(request.POST['order']))
if order.member == request.member:
order.payment.type = Payment.TPAY
order.update_payment(Payment.TPAY)
order.save()
messages.success(request, _("Your order has been paid with "
"Thalia Pay."))
......@@ -82,5 +80,7 @@ def order(request):
if not order:
order = Order(pizza_event=event, member=request.member)
order.product = product
order.update_payment()
order.save()
return redirect('pizzas:index')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment