diff --git a/parking_permits/admin.py b/parking_permits/admin.py index 9a7aa3b8..02d81d4f 100644 --- a/parking_permits/admin.py +++ b/parking_permits/admin.py @@ -164,6 +164,13 @@ class RefundAdmin(admin.ModelAdmin): ) list_select_related = ("order",) ordering = ("-created_at",) + raw_id_fields = ( + "accepted_by", + "created_by", + "modified_by", + "order", + "permits", + ) @admin.register(Product) diff --git a/parking_permits/admin_resolvers.py b/parking_permits/admin_resolvers.py index 7cda3139..87c8bff1 100644 --- a/parking_permits/admin_resolvers.py +++ b/parking_permits/admin_resolvers.py @@ -804,6 +804,7 @@ def resolve_update_resident_permit( iban=iban, description=f"Refund for updating permit: {permit.id}", ) + refund.permits.add(permit) logger.info(f"Refund for lowered permit price created: {refund}") ParkingPermitEventFactory.make_create_refund_event( permit, refund, created_by=request.user @@ -939,6 +940,7 @@ def resolve_end_permit( iban=iban, description=description, ) + refund.permits.add(permit) send_refund_email(RefundEmailType.CREATED, permit.customer, refund) ParkingPermitEventFactory.make_create_refund_event( permit, refund, created_by=request.user diff --git a/parking_permits/customer_permit.py b/parking_permits/customer_permit.py index 764c9134..985e5fbf 100644 --- a/parking_permits/customer_permit.py +++ b/parking_permits/customer_permit.py @@ -390,6 +390,7 @@ def end( iban=iban, description=f"Refund for ending permits {','.join([str(permit.id) for permit in permits])}", ) + refund.permits.set(permits) send_refund_email(RefundEmailType.CREATED, self.customer, refund) for permit in permits: diff --git a/parking_permits/migrations/0042_add_permits_and_order_to_refund.py b/parking_permits/migrations/0042_add_permits_and_order_to_refund.py new file mode 100644 index 00000000..21c47ff2 --- /dev/null +++ b/parking_permits/migrations/0042_add_permits_and_order_to_refund.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.1 on 2023-11-01 13:14 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("parking_permits", "0041_alter_order_type"), + ] + + operations = [ + migrations.AddField( + model_name="refund", + name="permits", + field=models.ManyToManyField( + blank=True, + related_name="refunds", + to="parking_permits.parkingpermit", + verbose_name="Permits", + ), + ), + migrations.AlterField( + model_name="refund", + name="order", + field=models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + related_name="refunds", + to="parking_permits.order", + verbose_name="Order", + ), + ), + ] diff --git a/parking_permits/models/order.py b/parking_permits/models/order.py index de9ca85d..17e819e8 100644 --- a/parking_permits/models/order.py +++ b/parking_permits/models/order.py @@ -604,6 +604,7 @@ def cancel(self, cancel_reason, cancel_from_talpa=True): amount=order_item.product.unit_price, description=f"Refund for ending permit {str(permit.id)}", ) + refund.permits.add(permit) send_refund_email(RefundEmailType.CREATED, permit.customer, refund) ParkingPermitEventFactory.make_create_refund_event( permit, refund, created_by=permit.customer.user diff --git a/parking_permits/models/refund.py b/parking_permits/models/refund.py index e48af16b..836aa76d 100644 --- a/parking_permits/models/refund.py +++ b/parking_permits/models/refund.py @@ -18,11 +18,17 @@ class RefundStatus(models.TextChoices): class Refund(TimestampedModelMixin, UserStampedModelMixin): name = models.CharField(_("Name"), max_length=200, blank=True) - order = models.OneToOneField( + order = models.ForeignKey( "Order", verbose_name=_("Order"), on_delete=models.PROTECT, - related_name="refund", + related_name="refunds", + ) + permits = models.ManyToManyField( + "ParkingPermit", + verbose_name=_("Permits"), + related_name="refunds", + blank=True, ) amount = models.DecimalField( _("Amount"), default=0.00, max_digits=6, decimal_places=2 diff --git a/parking_permits/resolvers.py b/parking_permits/resolvers.py index 18cb6ed4..0ee42602 100644 --- a/parking_permits/resolvers.py +++ b/parking_permits/resolvers.py @@ -435,6 +435,7 @@ def resolve_update_permit_vehicle( iban=iban if iban else "", description=f"Refund for updating permits, customer switched vehicle to: {new_vehicle}", ) + refund.permits.add(permit) logger.info(f"Refund for updating permits created: {refund}") send_refund_email(RefundEmailType.CREATED, customer, refund) ParkingPermitEventFactory.make_create_refund_event( @@ -653,6 +654,7 @@ def resolve_change_address( iban=iban if iban else "", description=f"Refund for updating permits zone (customer switch address to: {address})", ) + refund.permits.set(order.permits.all()) logger.info(f"Refund for updating permits zone created: {refund}") send_refund_email(RefundEmailType.CREATED, customer, refund) for permit in order.permits.all():