Skip to content

Commit df70521

Browse files
fix: left short rebate (#103)
1 parent 9e20a03 commit df70521

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

home/admin.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import csv
8+
from datetime import timedelta
89

910
from django.contrib import admin
1011
from django.http import HttpRequest, HttpResponse
@@ -34,6 +35,7 @@
3435
UnregisteredStudent,
3536
Update,
3637
)
38+
from home.utils.rebate_checker import max_days_rebate
3739

3840
from .resources import (
3941
AllocationResource,
@@ -46,12 +48,7 @@
4648
)
4749
from .utils.django_email_server import long_rebate_query_mail
4850
from .utils.month import fill_periods, map_periods_to_long_rebate
49-
from .utils.rebate_bills_saver import (
50-
fix_all_bills,
51-
save_long_bill,
52-
save_short_bill,
53-
update_bills,
54-
)
51+
from .utils.rebate_bills_saver import fix_all_bills, save_long_bill, update_bills
5552

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

10301027
@admin.action(description="Add left short rebate to Bills")
1031-
def Add(self, request, queryset):
1028+
def Add(self, request, queryset: list[LeftShortRebate]):
10321029
"""
10331030
Export action available in the admin page
10341031
"""
10351032
for obj in queryset:
10361033
email = obj.email
10371034
student_obj = Student.objects.filter(email=email).last()
10381035
for period in Period.objects.all():
1039-
if (
1040-
period.start_date <= obj.start_date
1041-
and period.end_date >= obj.end_date
1042-
):
1043-
days = (obj.end_date - obj.start_date).days + 1
1036+
if period.start_date <= obj.start_date <= period.end_date:
1037+
start_date = obj.start_date
1038+
end_date = obj.end_date
1039+
date_applied = obj.date_applied
1040+
if period.end_date < obj.end_date:
1041+
obj.start_date = period.end_date + timedelta(days=1)
1042+
obj.save()
1043+
end_date = period.end_date
1044+
else:
1045+
obj.delete()
10441046
allocation = Allocation.objects.filter(
10451047
email=student_obj, period=period
10461048
).last()
10471049
if allocation:
1048-
save_short_bill(
1049-
student_obj,
1050-
period,
1051-
days,
1052-
allocation.high_tea,
1053-
allocation.caterer,
1050+
upper_cap_check = max_days_rebate(
1051+
student_obj, start_date, end_date, period
10541052
)
1055-
print("Saved")
1053+
if upper_cap_check == 0:
1054+
continue
1055+
end_date = start_date + timedelta(days=upper_cap_check - 1)
10561056
short_rebate = Rebate(
10571057
email=student_obj,
10581058
allocation_id=allocation,
1059-
start_date=obj.start_date,
1060-
end_date=obj.end_date,
1061-
date_applied=obj.date_applied,
1059+
start_date=start_date,
1060+
end_date=end_date,
1061+
date_applied=date_applied,
10621062
approved=True,
10631063
)
10641064
short_rebate.save()
1065-
LeftShortRebate.objects.filter(
1066-
email=email, date_applied=obj.date_applied
1067-
).delete()
10681065

10691066

10701067
@admin.register(AllocationForm)

home/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,12 @@ def rebate(request):
218218
end_date=end_date,
219219
date_applied=date.today(),
220220
)
221-
short_left_rebate.save()
222221
end_date = period_end
223222
upper_cap_check = max_days_rebate(
224223
student, start_date, period_end, period_obj
225224
)
225+
if upper_cap_check < 0:
226+
short_left_rebate.save()
226227
additional_text = " Note: The days after the current period end date will be added to your bills in the next period."
227228
else:
228229
upper_cap_check = max_days_rebate(

0 commit comments

Comments
 (0)