Commit b3b0c03f authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg

Issue #961 Delete payments when Payment.NONE in pizzas

parent 567ebaaf
......@@ -67,12 +67,9 @@ 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', '0011_auto_20191129_1634'),
]
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'),
),
]
......@@ -189,6 +189,8 @@ class Order(models.Model):
to="payments.Payment",
related_name="pizzas_order",
on_delete=models.CASCADE,
blank=True,
null=True,
)
product = models.ForeignKey(
......@@ -208,26 +210,53 @@ class Order(models.Model):
}
)
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(
amount=self.product.price, notes=notes, paid_by=self.member
type=payment_type,
amount=self.product.price,
notes=notes,
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)
......@@ -258,12 +287,8 @@ class Order(models.Model):
def can_be_changed(self):
try:
return (
self.payment
and not (
self.payment.processed and not self.payment.type == Payment.TPAY
)
and not self.pizza_event.has_ended
)
not self.payment or self.payment.type == Payment.TPAY
) and not self.pizza_event.has_ended
except ObjectDoesNotExist:
return False
......
......@@ -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>
......
......@@ -34,8 +34,6 @@ def cancel_order(request):
if not order.can_be_changed:
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:
......@@ -50,7 +48,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.")
......@@ -85,5 +83,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