Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: left short rebate #103

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions home/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import csv
from datetime import timedelta

from django.contrib import admin
from django.http import HttpRequest, HttpResponse
Expand Down Expand Up @@ -34,6 +35,7 @@
UnregisteredStudent,
Update,
)
from home.utils.rebate_checker import max_days_rebate

from .resources import (
AllocationResource,
Expand All @@ -46,12 +48,7 @@
)
from .utils.django_email_server import long_rebate_query_mail
from .utils.month import fill_periods, map_periods_to_long_rebate
from .utils.rebate_bills_saver import (
fix_all_bills,
save_long_bill,
save_short_bill,
update_bills,
)
from .utils.rebate_bills_saver import fix_all_bills, save_long_bill, update_bills

# Customising the heading and title of the admin page
admin.site.site_header = "Dining Website Admin Page"
Expand Down Expand Up @@ -1028,43 +1025,43 @@ class about_Admin(admin.ModelAdmin):
actions = ["Add"]

@admin.action(description="Add left short rebate to Bills")
def Add(self, request, queryset):
def Add(self, request, queryset: list[LeftShortRebate]):
"""
Export action available in the admin page
"""
for obj in queryset:
email = obj.email
student_obj = Student.objects.filter(email=email).last()
for period in Period.objects.all():
if (
period.start_date <= obj.start_date
and period.end_date >= obj.end_date
):
days = (obj.end_date - obj.start_date).days + 1
if period.start_date <= obj.start_date <= period.end_date:
start_date = obj.start_date
end_date = obj.end_date
date_applied = obj.date_applied
if period.end_date < obj.end_date:
obj.start_date = period.end_date + timedelta(days=1)
obj.save()
end_date = period.end_date
else:
obj.delete()
allocation = Allocation.objects.filter(
email=student_obj, period=period
).last()
if allocation:
save_short_bill(
student_obj,
period,
days,
allocation.high_tea,
allocation.caterer,
upper_cap_check = max_days_rebate(
student_obj, start_date, end_date, period
)
print("Saved")
if upper_cap_check == 0:
continue
end_date = start_date + timedelta(days=upper_cap_check - 1)
short_rebate = Rebate(
email=student_obj,
allocation_id=allocation,
start_date=obj.start_date,
end_date=obj.end_date,
date_applied=obj.date_applied,
start_date=start_date,
end_date=end_date,
date_applied=date_applied,
approved=True,
)
short_rebate.save()
LeftShortRebate.objects.filter(
email=email, date_applied=obj.date_applied
).delete()


@admin.register(AllocationForm)
Expand Down
3 changes: 2 additions & 1 deletion home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ def rebate(request):
end_date=end_date,
date_applied=date.today(),
)
short_left_rebate.save()
end_date = period_end
upper_cap_check = max_days_rebate(
student, start_date, period_end, period_obj
)
if upper_cap_check < 0:
short_left_rebate.save()
additional_text = " Note: The days after the current period end date will be added to your bills in the next period."
else:
upper_cap_check = max_days_rebate(
Expand Down
Loading