From df70521dcda794ac09242933738edb48532ffff7 Mon Sep 17 00:00:00 2001 From: Ishaan Mittal Date: Sun, 17 Nov 2024 15:23:47 +0530 Subject: [PATCH] fix: left short rebate (#103) --- home/admin.py | 47 ++++++++++++++++++++++------------------------- home/views.py | 3 ++- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/home/admin.py b/home/admin.py index 08a85b3..39c7f1b 100644 --- a/home/admin.py +++ b/home/admin.py @@ -5,6 +5,7 @@ """ import csv +from datetime import timedelta from django.contrib import admin from django.http import HttpRequest, HttpResponse @@ -34,6 +35,7 @@ UnregisteredStudent, Update, ) +from home.utils.rebate_checker import max_days_rebate from .resources import ( AllocationResource, @@ -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" @@ -1028,7 +1025,7 @@ 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 """ @@ -1036,35 +1033,35 @@ def Add(self, request, 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) diff --git a/home/views.py b/home/views.py index 0a88830..e4becc7 100644 --- a/home/views.py +++ b/home/views.py @@ -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(