Skip to content

Commit

Permalink
Add permits to Refund
Browse files Browse the repository at this point in the history
1. Adds "permits" M:M relationship to Refund
2. Make Order a 1:M field instead of 1:1

Permits should be added to refund as appropriate, e.g. a single permit
when ending a permit, or all order permits in other cases.

Refs: PV-684
  • Loading branch information
danjacob-anders committed Nov 1, 2023
1 parent 785ba69 commit a9fbd91
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
7 changes: 7 additions & 0 deletions parking_permits/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions parking_permits/admin_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions parking_permits/customer_permit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions parking_permits/migrations/0042_add_permits_and_order_to_refund.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
1 change: 1 addition & 0 deletions parking_permits/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions parking_permits/models/refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions parking_permits/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit a9fbd91

Please sign in to comment.